Fixe problem when calling from different path
[snom-frontend] / snom.pl
1 #!/bin/sh
2
3 PATH=/opt/csw/bin:/opt/local/bin:/usr/bin:/bin exec perl -w -x "$0" "$@"
4
5 #!perl
6
7 use strict;
8 use warnings FATAL => 'all';
9
10 use Glib qw/TRUE FALSE/;
11 use Gtk2 '-init';
12 use Gtk2::Gdk::Keysyms;
13 use LWP::UserAgent;
14 use FindBin qw($Bin);
15
16
17 my %g_key = ();
18 my %g_arrows;
19 my $g_host = Gtk2::ComboBox->new_text();
20 my $g_identity = Gtk2::ComboBox->new_text();
21 my $ua = LWP::UserAgent->new;
22
23 open(INIFILE,"<$ENV{HOME}/.snomrc") || die("can't open config: $ENV{HOME}/.snomrc: $!");
24 my %Config = ();
25 my @sections = ();
26 while(<INIFILE>) {
27 chomp;
28
29 next if (m/^#/);
30
31 if (m/^\s*\[(.*)\]\s*$/) {
32 push @sections, $1;
33 next;
34 }
35
36 if (@sections) {
37 if (m/^\s*(.+)\s*=\s*(.*)\s*$/) {
38 ${$Config{$sections[$#sections]}}{$1} = $2;
39 }
40 }
41 }
42 close(INIFILE);
43
44 for my $section (@sections) {
45 if (defined(${$Config{$section}}{host})) {
46 $g_host->append_text($section);
47 if (defined(${$Config{$section}}{login}) &&
48 defined(${$Config{$section}}{password})) {
49 $ua->credentials(${$Config{$section}}{host}.":80",
50 "snom",
51 ${$Config{$section}}{login},
52 ${$Config{$section}}{password});
53 }
54 }
55 }
56 $g_host->set_active(0);
57 UpdatePhoneInfo();
58 $g_host->signal_connect(changed => \&UpdatePhoneInfo);
59 $g_host->show() if($#sections);
60 $g_identity->signal_connect(changed => \&SwitchIdentity);
61
62 set_locale Gtk2;
63
64 sub snom_key {
65 my $key = shift;
66 $key='%23' if ($key eq '#');
67 my $req = HTTP::Request->new(GET => "http://".${$Config{$sections[$g_host->get_active]}}{host}."/command.htm?key=${key}");
68 $ua->request($req);
69 }
70
71 sub snom_number {
72 my $number = shift;
73 $number =~ s/[^\d]//g;
74 my $req = HTTP::Request->new(GET => "http://".${$Config{$sections[$g_host->get_active]}}{host}."/command.htm?number=${number}");
75 $ua->request($req);
76 }
77
78 sub gen_table {
79 my $rows = shift;
80 my $cols = shift;
81 my $homogeneous = shift;
82 my $row_spacing = shift;
83 my $col_spacing = shift;
84 my $keys = shift;
85
86 my $table = Gtk2::Table->new($rows, $cols, $homogeneous);
87 $table->set_row_spacings($row_spacing);
88 $table->set_col_spacings($col_spacing);
89
90 my $n = 0;
91 foreach my $key (@$keys) {
92 if ($key ne '_') {
93 if($key=~m/^ARROWS(.*)$/) {
94 $g_key{$key} = Gtk2::Button->new;
95 $g_key{$key}->add($g_arrows{"${1}"});
96 } else {
97 $g_key{$key} = Gtk2::Button->new("${key}");
98 }
99 $g_key{$key}->signal_connect(clicked => \&KeyPressed, $key);
100 $table->attach_defaults($g_key{$key}, (($n)%$cols), (($n)%$cols)+1, int(($n)/$cols), int((($n)/$cols)+1));
101 $g_key{$key}->show();
102 }
103 $n++;
104 }
105
106 $table->show();
107
108 $table;
109 }
110
111 my $window = Gtk2::Window->new('toplevel');
112 $window->set_title("snom");
113 $window->signal_connect(delete_event => \&CloseAppWindow);
114 $window->signal_connect(destroy => sub { Gtk2->main_quit; });
115 my $kphandler = $window->signal_connect(key_press_event => \&KBDInput);
116 $window->set_border_width(15);
117 $window->set_resizable(FALSE);
118
119 my $keypad = gen_table(4, 3, TRUE, 2, 2, [1, 2, 3, 4, 5, 6, 7, 8, 9, '*', 0, '#']);
120
121 my $fkeys = gen_table(6, 2, TRUE, 2, 2,
122 ['_Redial', 'S_ettings', 'Director_y', 'Hel_p', 'Men_u', 'snom', '_', '_',
123 '_Conference', '_Transfer', 'H_old', '_DND']);
124
125 my $pkeys = gen_table(6, 2, TRUE, 2, 2, ['P1', 'P7', 'P2', 'P8', 'P3', 'P9', 'P4',
126 'P10', 'P5', 'P11', 'P6', 'P12']);
127
128 my $softkeys = gen_table(1, 4, FALSE, 0, 6, ['F1', 'F2', 'F3', 'F4']);
129
130 $g_arrows{up} = Gtk2::Arrow->new('up', 'none');
131 $g_arrows{up}->show();
132 $g_arrows{down} = Gtk2::Arrow->new('down', 'none');
133 $g_arrows{down}->show();
134 $g_arrows{left} = Gtk2::Arrow->new('left', 'none');
135 $g_arrows{left}->show();
136 $g_arrows{right} = Gtk2::Arrow->new('right', 'none');
137 $g_arrows{right}->show();
138
139 my $navi = gen_table(3, 5, FALSE, 0, 0,
140 ['_', '_', 'ARROWSup', '_', '_', 'X', 'ARROWSleft', '_', 'ARROWSright', 'OK', '_', '_', 'ARROWSdown', '_', '_']);
141
142 my $output = gen_table(2, 3, FALSE, 2, 2, ['-', '+', '_', '_Mute', '_Speaker', '_Headset']);
143
144 my $special = gen_table(1, 2, FALSE, 0, 2, ['Record', 'Retrieve']);
145
146 my $display = Gtk2::Entry->new();
147 $display->signal_connect(activate => \&DialNumber);
148 my $kph_blocked = FALSE;
149 $display->signal_connect(enter_notify_event => \&DisplayMouseOver, TRUE);
150 $display->signal_connect(leave_notify_event => \&DisplayMouseOver, FALSE);
151 $display->show();
152
153 my $cbextern = Gtk2::CheckButton->new("extern");
154 $cbextern->show();
155
156 my $dialbox = Gtk2::HBox->new(0, 30);
157 $dialbox->pack_start($display, 0, 0, 0);
158 $dialbox->pack_start($cbextern, 0, 0, 0);
159 $dialbox->show();
160
161 my $dispbox = Gtk2::VBox->new(0, 5);
162 $dispbox->pack_start($dialbox, 0, 0, 0);
163 $dispbox->pack_start($softkeys, 0, 0, 0);
164 $dispbox->pack_start($navi, 0, 0, 0);
165 $dispbox->show();
166
167 my $outbox = Gtk2::VBox->new(0, 0);
168 $outbox->pack_start($g_host, 0, 0, 0);
169 $outbox->pack_end($output, 0, 0, 0);
170 $outbox->show();
171
172 my $specialbox = Gtk2::VBox->new(0, 0);
173 $specialbox->pack_start($g_identity, 0, 0, 0);
174 $specialbox->pack_end($special, 0, 0, 0);
175 $specialbox->show();
176
177 my $ubox = Gtk2::HBox->new(0, 30);
178 $ubox->pack_start($outbox, 0, 0, 0);
179 $ubox->pack_start($dispbox, 0, 0, 0);
180 $ubox->pack_start($specialbox, 0, 0, 0);
181 $ubox->show();
182
183
184 my $kbbox = Gtk2::HBox->new(0, 30);
185 $kbbox->pack_start($keypad, 1, 1, 0);
186 $kbbox->pack_start($fkeys, 0, 0, 0);
187 $kbbox->show();
188
189 my $kvbox = Gtk2::VBox->new(0, 5);
190 $kvbox->pack_end($kbbox, 0, 0, 0);
191 $kvbox->show();
192
193 my $lbox = Gtk2::HBox->new(0, 30);
194 $lbox->pack_start($kvbox, 1, 1, 0);
195 $lbox->pack_start($pkeys, 0, 0, 0);
196 $lbox->show();
197
198 my $mainbox = Gtk2::VBox->new(0,5);
199 $mainbox->pack_start($ubox, 0, 0, 0);
200 $mainbox->pack_start($lbox, 0, 0, 0);
201 $mainbox->show();
202
203
204
205 #########################################################
206 #
207 # Tray Icon
208 #
209 #########################################################
210 sub quit_cb {
211 my ($widget, $status_icon) = @_;
212
213 $status_icon->set_visible(0) if $status_icon;
214 Gtk2->main_quit();
215 }
216
217 sub popup_menu_cb {
218 my ($widget, $button, $time, $menu) = @_;
219
220 if ($button == 3) {
221 my ($x, $y, $push_in)
222 = Gtk2::StatusIcon::position_menu($menu, $widget);
223
224 $menu->show_all();
225 $menu->popup( undef, undef,
226 sub{return ($x,$y,0)} ,
227 undef, 0, $time );
228 }
229 }
230
231 sub activate_icon_cb {
232 if ($window->visible){
233 $window->hide();
234 } else {
235 $window->show();
236 }
237 }
238
239 my $status_icon = Gtk2::StatusIcon->new_from_file($Bin.'/snom.ico');
240 my $menu = Gtk2::Menu->new();
241
242 my $menuItem = Gtk2::ImageMenuItem->new_from_stock('gtk-quit');
243 $menuItem->signal_connect('activate', \&quit_cb, $status_icon);
244 $menu->append($menuItem);
245
246 $status_icon->set_tooltip('Snom Tray');
247 $status_icon->signal_connect('activate', \&activate_icon_cb);
248 $status_icon->signal_connect('popup-menu', \&popup_menu_cb, $menu);
249 $status_icon->set_visible(1);
250 ########################################################################333
251
252 $window->add($mainbox);
253 $window->set_icon_from_file($Bin.'/snom.ico');
254 $window->show();
255
256 $g_key{OK}->grab_focus();
257
258 # Gtk2 event loop
259 Gtk2->main;
260
261 # Should never get here
262 exit( 0 );
263
264
265 sub KeyPressed
266 {
267 my ($button, $text) = @_;
268 $text=~ s/_//g;
269 foreach my $i (1, 2, 3, 4, 5, 6, 7, 8, 9, '*', 0, '#',
270 'F1', 'F2', 'F3', 'F4',
271 'P1', 'P2', 'P3', 'P4', 'P5', 'P6',
272 'P7', 'P8', 'P9', 'P10', 'P11', 'P12') {
273 if ($i eq $text) {
274 snom_key($i);
275 return TRUE;
276 }
277 }
278
279 snom_key("REDIAL") if ( $text eq "Redial" );
280 snom_key("SETTINGS") if ( $text eq "Settings" );
281 snom_key("F_ADR_BOOK") if ( $text eq "Directory" );
282 snom_key("HELP") if ( $text eq "Help" );
283 snom_key("MENU") if ( $text eq "Menu" );
284 snom_key("SNOM") if ( $text eq "snom" );
285 snom_key("CONFERENCE") if ( $text eq "Conference" );
286 snom_key("TRANSFER") if ( $text eq "Transfer" );
287 snom_key("F_R") if ( $text eq "Hold" );
288 snom_key("DND") if ( $text eq "DND" );
289
290 snom_key("MUTE") if ( $text eq "Mute" );
291 snom_key("SPEAKER") if ( $text eq "Speaker" );
292 snom_key("HEADSET") if ( $text eq "Headset" );
293
294 snom_key("CANCEL") if ( $text eq "X" );
295 snom_key("ENTER") if ( $text eq "OK" );
296 snom_key("UP") if ( $text eq "ARROWSup" );
297 snom_key("DOWN") if ( $text eq "ARROWSdown" );
298 snom_key("LEFT") if ( $text eq "ARROWSleft" );
299 snom_key("RIGHT") if ( $text eq "ARROWSright" );
300
301 snom_key("F_REC") if ( $text eq "Record" );
302 snom_key("F_RETRIEVE") if ( $text eq "Retrieve" );
303
304 snom_key("VOLUME_UP") if ( $text eq "+" );
305 snom_key("VOLUME_DOWN") if ( $text eq "-" );
306 return TRUE;
307 }
308
309 sub DialNumber
310 {
311 my ($entry) = @_;
312 my ($num) = $entry->get_text();
313 if ($cbextern->get_active){
314 snom_number("09".$num)
315 } else {
316 snom_number($num);
317 }
318 return TRUE;
319 }
320
321 sub DisplayMouseOver
322 {
323 my ($widget, $event, $active) = @_;
324
325 if ($active) {
326 $widget->grab_focus();
327 if (!$kph_blocked) {
328 $window->signal_handler_block($kphandler);
329 $kph_blocked=TRUE;
330 }
331 } else {
332 $g_key{OK}->grab_focus();
333 if ($kph_blocked) {
334 $window->signal_handler_unblock($kphandler);
335 $kph_blocked=FALSE;
336 }
337 }
338
339 return TRUE;
340 }
341
342 sub KBDInput
343 {
344 my ($widget, $event) = @_;
345
346 my $keyval = $event->keyval;
347
348 for (my $i=0; $i<10; $i++) {
349 if ($keyval == $Gtk2::Gdk::Keysyms{$i} || $keyval == $Gtk2::Gdk::Keysyms{"KP_${i}"}) {
350 snom_key($i);
351 return TRUE;
352 }
353 }
354
355 if (!$event->state || !($event->state & "control-mask" || $event->state & "mod1-mask")) {
356 if ($keyval == $Gtk2::Gdk::Keysyms{asterisk}) {snom_key('*'); return TRUE;}
357 if ($keyval == $Gtk2::Gdk::Keysyms{KP_Multiply}) {snom_key('*'); return TRUE;}
358 if ($keyval == $Gtk2::Gdk::Keysyms{numbersign}) {snom_key('#'); return TRUE;}
359 if ($keyval == $Gtk2::Gdk::Keysyms{KP_Add}) {snom_key('VOLUME_UP'); return TRUE;}
360 if ($keyval == $Gtk2::Gdk::Keysyms{plus}) {snom_key('VOLUME_UP'); return TRUE;}
361 if ($keyval == $Gtk2::Gdk::Keysyms{KP_Subtract}) {snom_key('VOLUME_DOWN'); return TRUE;}
362 if ($keyval == $Gtk2::Gdk::Keysyms{minus}) {snom_key('VOLUME_DOWN'); return TRUE;}
363 if ($keyval == $Gtk2::Gdk::Keysyms{Return}) {snom_key("ENTER"); return TRUE;}
364 if ($keyval == $Gtk2::Gdk::Keysyms{KP_Enter}) {snom_key("ENTER"); return TRUE;}
365 if ($keyval == $Gtk2::Gdk::Keysyms{Escape}) {snom_key("CANCEL"); return TRUE;}
366 if ($keyval == $Gtk2::Gdk::Keysyms{Left}) {snom_key("LEFT"); return TRUE;}
367 if ($keyval == $Gtk2::Gdk::Keysyms{KP_Left}) {snom_key("LEFT"); return TRUE;}
368 if ($keyval == $Gtk2::Gdk::Keysyms{Right}) {snom_key("RIGHT"); return TRUE;}
369 if ($keyval == $Gtk2::Gdk::Keysyms{KP_Right}) {snom_key("RIGHT"); return TRUE;}
370 if ($keyval == $Gtk2::Gdk::Keysyms{Up}) {snom_key("UP"); return TRUE;}
371 if ($keyval == $Gtk2::Gdk::Keysyms{KP_Up}) {snom_key("UP"); return TRUE;}
372 if ($keyval == $Gtk2::Gdk::Keysyms{Down}) {snom_key("DOWN"); return TRUE;}
373 if ($keyval == $Gtk2::Gdk::Keysyms{KP_Down}) {snom_key("DOWN"); return TRUE;}
374 if ($keyval == $Gtk2::Gdk::Keysyms{M}) {snom_key('MUTE'); return TRUE;}
375 if ($keyval == $Gtk2::Gdk::Keysyms{m}) {snom_key('MUTE'); return TRUE;}
376 if ($keyval == $Gtk2::Gdk::Keysyms{S}) {snom_key('SPEAKER'); return TRUE;}
377 if ($keyval == $Gtk2::Gdk::Keysyms{s}) {snom_key('SPEAKER'); return TRUE;}
378 if ($keyval == $Gtk2::Gdk::Keysyms{H}) {snom_key('HEADSET'); return TRUE;}
379 if ($keyval == $Gtk2::Gdk::Keysyms{h}) {snom_key('HEADSET'); return TRUE;}
380 if ($keyval == $Gtk2::Gdk::Keysyms{C}) {snom_key('CONFERENCE'); return TRUE;}
381 if ($keyval == $Gtk2::Gdk::Keysyms{c}) {snom_key('CONFERENCE'); return TRUE;}
382 if ($keyval == $Gtk2::Gdk::Keysyms{T}) {snom_key('TRANSFER'); return TRUE;}
383 if ($keyval == $Gtk2::Gdk::Keysyms{t}) {snom_key('TRANSFER'); return TRUE;}
384 if ($keyval == $Gtk2::Gdk::Keysyms{O}) {snom_key('F_R'); return TRUE;}
385 if ($keyval == $Gtk2::Gdk::Keysyms{o}) {snom_key('F_R'); return TRUE;}
386 if ($keyval == $Gtk2::Gdk::Keysyms{D}) {snom_key('DND'); return TRUE;}
387 if ($keyval == $Gtk2::Gdk::Keysyms{d}) {snom_key('DND'); return TRUE;}
388 if ($keyval == $Gtk2::Gdk::Keysyms{R}) {snom_key('REDIAL'); return TRUE;}
389 if ($keyval == $Gtk2::Gdk::Keysyms{r}) {snom_key('REDIAL'); return TRUE;}
390 if ($keyval == $Gtk2::Gdk::Keysyms{E}) {snom_key('SETTINGS'); return TRUE;}
391 if ($keyval == $Gtk2::Gdk::Keysyms{e}) {snom_key('SETTINGS'); return TRUE;}
392 if ($keyval == $Gtk2::Gdk::Keysyms{Y}) {snom_key('F_ADR_BOOK'); return TRUE;}
393 if ($keyval == $Gtk2::Gdk::Keysyms{y}) {snom_key('F_ADR_BOOK'); return TRUE;}
394 if ($keyval == $Gtk2::Gdk::Keysyms{P}) {snom_key('HELP'); return TRUE;}
395 if ($keyval == $Gtk2::Gdk::Keysyms{p}) {snom_key('HELP'); return TRUE;}
396 if ($keyval == $Gtk2::Gdk::Keysyms{U}) {snom_key('MENU'); return TRUE;}
397 if ($keyval == $Gtk2::Gdk::Keysyms{u}) {snom_key('MENU'); return TRUE;}
398 }
399
400 if (!$event->state) {
401 for (my $i=1; $i<5; $i++) {
402 if ($keyval == $Gtk2::Gdk::Keysyms{"F${i}"}) {
403 snom_key("F${i}");
404 return TRUE;
405 }
406 }
407 } else {
408 if ($event->state & "mod1-mask") {
409 for (my $i=1; $i<13; $i++) {
410 if ($keyval == $Gtk2::Gdk::Keysyms{"F${i}"}) {
411 snom_key("P${i}");
412 return TRUE;
413 }
414 }
415 }
416 }
417
418 print "Unhandled: Modifier: ".$event->state.", Key: ";
419 foreach my $key (keys(%Gtk2::Gdk::Keysyms)) {
420 if ($keyval == $Gtk2::Gdk::Keysyms{$key}) {
421 print "${key}\n";
422 return TRUE;
423 }
424 }
425 print "ERR\n";
426 return FALSE;
427 }
428
429 sub UpdatePhoneInfo
430 {
431 my $req = HTTP::Request->new(GET => "http://".${$Config{$sections[$g_host->get_active]}}{host}."/");
432 my $response = $ua->request($req);
433
434 $g_identity->set_active(0);
435 while($g_identity->get_active() == 0) {
436 $g_identity->remove_text(0);
437 $g_identity->set_active(0);
438 }
439 my @lines=split("\n", $response->content());
440 my $num = 0;
441 my $activated = 0;
442
443 foreach (@lines) {
444 chomp;
445 #<option value="1" selected>51@stargate.gernoth.loc</option>
446 #<option value="2">89@asterix.ear-projekt.de</option>
447 #<option value="3">41@grumpy.gernoth.loc</option>
448 if (m/^\<option value=\"(.+)\"( selected)?\>([^<\@]*)\@([^<]*)\<\/option\>\s*$/) {
449 my $line = $3;
450 $g_identity->append_text("${3}");
451 if (defined($2)) {
452 $g_identity->set_active(${1}-1);
453 $activated=1;
454 }
455 $num++;
456 }
457 }
458 $g_identity->set_active(0) if (!$activated);
459 if ($num > 1) {
460 $g_identity->show;
461 } else {
462 $g_identity->hide;
463 }
464 }
465
466 sub SwitchIdentity
467 {
468 my $req = HTTP::Request->new(GET => "http://".${$Config{$sections[$g_host->get_active]}}{host}."/dummy.htm?SETTINGS=Set&active_line=".($g_identity->get_active+1));
469 $ua->request($req);
470 }
471
472 sub CloseAppWindow
473 {
474 $window->destroy;
475 return TRUE;
476 }
Impressum, Datenschutz