Adding support for "urgent" voicemail messages. Messages which are
[asterisk/asterisk.git] / pbx / pbx_gtkconsole.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief GTK Console monitor -- very kludgy right now
22  * 
23  */
24
25 /*** MODULEINFO
26         <depend>gtk</depend>
27         <defaultenabled>no</defaultenabled>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include <fcntl.h>
35 #include <signal.h>
36 #include <sys/time.h>
37
38 #include <gtk/gtk.h>
39 #include <glib.h>
40
41 #include "asterisk/pbx.h"
42 #include "asterisk/config.h"
43 #include "asterisk/module.h"
44 #include "asterisk/logger.h"
45 #include "asterisk/cli.h"
46 #include "asterisk/utils.h"
47 #include "asterisk/paths.h"
48 #include "asterisk/term.h"
49
50 AST_MUTEX_DEFINE_STATIC(verb_lock);
51
52 static pthread_t console_thread;
53
54 static int inuse=0;
55 static int clipipe[2];
56 static int cleanupid = -1;
57
58 static GtkWidget *window;
59 static GtkWidget *quit;
60 static GtkWidget *closew;
61 static GtkWidget *verb;
62 static GtkWidget *modules;
63 static GtkWidget *statusbar;
64 static GtkWidget *cli;
65
66 static struct timeval last;
67
68 static void update_statusbar(char *msg)
69 {
70         gtk_statusbar_pop(GTK_STATUSBAR(statusbar), 1);
71         gtk_statusbar_push(GTK_STATUSBAR(statusbar), 1, msg);
72 }
73
74 static int unload_module(void)
75 {
76         if (inuse) {
77                 /* Kill off the main thread */
78                 pthread_cancel(console_thread);
79                 gdk_threads_enter();
80                 gtk_widget_destroy(window);
81                 gdk_threads_leave();
82                 close(clipipe[0]);
83                 close(clipipe[1]);
84         }
85         return 0;
86 }
87
88 static int cleanup(void *useless)
89 {
90         gdk_threads_enter();
91         gtk_clist_thaw(GTK_CLIST(verb));
92         gtk_widget_queue_resize(verb->parent);
93         gtk_clist_moveto(GTK_CLIST(verb), GTK_CLIST(verb)->rows - 1, 0, 0, 0);
94         cleanupid = -1;
95         gdk_threads_leave();
96         return 0;
97 }
98
99
100 static void __verboser(const char *_stuff)
101 {
102         char *s2[2];
103         struct timeval tv;
104         int ms;
105         char *stuff;
106
107         stuff = ast_strdupa(_stuff);
108         term_strip(stuff, stuff, strlen(stuff) + 1);
109
110         s2[0] = (char *)stuff;
111         s2[1] = NULL;
112         gtk_clist_freeze(GTK_CLIST(verb));
113         gtk_clist_append(GTK_CLIST(verb), s2);
114         if (!ast_tvzero(last)) {
115                 gdk_threads_leave();
116                 gettimeofday(&tv, NULL);
117                 if (cleanupid > -1)
118                         gtk_timeout_remove(cleanupid);
119                 ms = ast_tvdiff_ms(tv, last);
120                 if (ms < 100) {
121                         /* We just got a message within 100ms, so just schedule an update
122                            in the near future */
123                         cleanupid = gtk_timeout_add(200, cleanup, NULL);
124                 } else {
125                         cleanup(&cleanupid);
126                 }
127                 last = tv;
128         } else {
129                 gettimeofday(&last, NULL);
130         }
131 }
132
133 static void verboser(const char *stuff) 
134 {
135         ast_mutex_lock(&verb_lock);
136         /* Lock appropriately if we're really being called in verbose mode */
137         __verboser(stuff);
138         ast_mutex_unlock(&verb_lock);
139 }
140
141 static void cliinput(void *data, int source, GdkInputCondition ic)
142 {
143         static char buf[256];
144         static int offset = 0;
145         int res;
146         char *c;
147         char *l;
148         char n;
149         /* Read as much stuff is there */
150         res = read(source, buf + offset, sizeof(buf) - 1 - offset);
151         if (res > -1)
152                 buf[res + offset] = '\0';
153         /* make sure we've null terminated whatever we have so far */
154         c = buf;
155         l = buf;
156         while(*c) {
157                 if (*c == '\n') {
158                         /* Keep the trailing \n */
159                         c++;
160                         n = *c;
161                         *c = '\0';
162                         __verboser(l);
163                         *(c - 1) = '\0';
164                         *c = n;
165                         l = c;
166                 } else
167                 c++;
168         }
169         if (strlen(l)) {
170                 /* We have some left over */
171                 memmove(buf, l, strlen(l) + 1);
172                 offset = strlen(buf);
173         } else {
174                 offset = 0;
175         }
176
177 }
178
179 static void remove_module(void)
180 {
181         int res;
182         char *module;
183         char buf[256];
184         if (GTK_CLIST(modules)->selection) {
185                 module = (char *) gtk_clist_get_row_data(GTK_CLIST(modules), (long) GTK_CLIST(modules)->selection->data);
186                 gdk_threads_leave();
187                 res = ast_unload_resource(module, 0);
188                 gdk_threads_enter();
189                 if (res) {
190                         snprintf(buf, sizeof(buf), "Module '%s' is in use", module);
191                         update_statusbar(buf);
192                 } else {
193                         snprintf(buf, sizeof(buf), "Module '%s' removed", module);
194                         update_statusbar(buf);
195                 }
196         }
197 }
198
199 static int reload(void)
200 {
201         int res, x;
202         char *module;
203         char buf[256];
204         if (GTK_CLIST(modules)->selection) {
205                 module= (char *)gtk_clist_get_row_data(GTK_CLIST(modules), (long) GTK_CLIST(modules)->selection->data);
206                 module = strdup(module);
207                 if (module) {
208                         gdk_threads_leave();
209                         res = ast_unload_resource(module, 0);
210                         gdk_threads_enter();
211                         if (res) {
212                                 snprintf(buf, sizeof(buf), "Module '%s' is in use", module);
213                                 update_statusbar(buf);
214                         } else {
215                                 gdk_threads_leave();
216                                 res = ast_load_resource(module);
217                                 gdk_threads_enter();
218                                 if (res) {
219                                         snprintf(buf, sizeof(buf), "Error reloading module '%s'", module);
220                                 } else {
221                                         snprintf(buf, sizeof(buf), "Module '%s' reloaded", module);
222                                 }
223                                 for (x=0; x < GTK_CLIST(modules)->rows; x++) {
224                                         if (!strcmp((char *)gtk_clist_get_row_data(GTK_CLIST(modules), x), module)) {
225                                                 gtk_clist_select_row(GTK_CLIST(modules), x, -1);
226                                                 break;
227                                         }
228                                 }
229                                 update_statusbar(buf);
230                                 
231                         }
232                         free(module);
233                 }
234         }
235
236         return AST_MODULE_LOAD_SUCCESS;
237 }
238
239 static void file_ok_sel(GtkWidget *w, GtkFileSelection *fs)
240 {
241         char tmp[PATH_MAX];
242         char *module = gtk_file_selection_get_filename(fs);
243         char buf[256];
244         snprintf(tmp, sizeof(tmp), "%s/", ast_config_AST_MODULE_DIR);
245         if (!strncmp(module, (char *)tmp, strlen(tmp))) 
246                 module += strlen(tmp);
247         gdk_threads_leave();
248         if (ast_load_resource(module)) {
249                 snprintf(buf, sizeof(buf), "Error loading module '%s'.", module);
250                 update_statusbar(buf);
251         } else {
252                 snprintf(buf, sizeof(buf), "Module '%s' loaded", module);
253                 update_statusbar(buf);
254         }
255         gdk_threads_enter();
256         gtk_widget_destroy(GTK_WIDGET(fs));
257 }
258
259 static void add_module(void)
260 {
261         char tmp[PATH_MAX];
262         GtkWidget *filew;
263         snprintf(tmp, sizeof(tmp), "%s/*.so", ast_config_AST_MODULE_DIR);
264         filew = gtk_file_selection_new("Load Module");
265         gtk_signal_connect(GTK_OBJECT (GTK_FILE_SELECTION(filew)->ok_button),
266                                         "clicked", GTK_SIGNAL_FUNC(file_ok_sel), filew);
267         gtk_signal_connect_object(GTK_OBJECT (GTK_FILE_SELECTION(filew)->cancel_button),
268                                         "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(filew));
269         gtk_file_selection_set_filename(GTK_FILE_SELECTION(filew), (char *)tmp);
270         gtk_widget_show(filew);
271 }
272
273 static int add_mod(const char *module, const char *description, int usecount, const char *like)
274 {
275         char use[10];
276         const char *pass[4];
277         int row;
278         snprintf(use, sizeof(use), "%d", usecount);
279         pass[0] = module;
280         pass[1] = description;
281         pass[2] = use;
282         pass[3] = NULL;
283         row = gtk_clist_append(GTK_CLIST(modules), (char **) pass);
284         gtk_clist_set_row_data(GTK_CLIST(modules), row, (char *) module);
285         return 0;       
286 }
287
288 static int mod_update(void)
289 {
290         char *module= NULL;
291         /* Update the mod stuff */
292         if (GTK_CLIST(modules)->selection) {
293                 module= (char *)gtk_clist_get_row_data(GTK_CLIST(modules), (long) GTK_CLIST(modules)->selection->data);
294         }
295         gtk_clist_freeze(GTK_CLIST(modules));
296         gtk_clist_clear(GTK_CLIST(modules));
297         ast_update_module_list(add_mod, NULL);
298         if (module)
299                 gtk_clist_select_row(GTK_CLIST(modules), gtk_clist_find_row_from_data(GTK_CLIST(modules), module), -1);
300         gtk_clist_thaw(GTK_CLIST(modules));
301         return 1;
302 }
303
304 static void exit_now(GtkWidget *widget, gpointer data)
305 {
306         ast_loader_unregister(mod_update);
307         gtk_main_quit();
308         inuse--;
309         ast_update_use_count();
310         ast_unregister_verbose(verboser);
311         ast_unload_resource("pbx_gtkconsole", 0);
312         ast_verb(2, "GTK Console Monitor Exiting\n");
313         /* XXX Trying to quit after calling this makes asterisk segfault XXX */
314 }
315
316 static void exit_completely(GtkWidget *widget, gpointer data)
317 {
318 #if 0
319         /* Clever... */
320         ast_cli_command(clipipe[1], "quit");
321 #else
322         kill(getpid(), SIGTERM);
323 #endif
324 }
325
326 static void exit_nicely(GtkWidget *widget, gpointer data)
327 {
328         fflush(stdout);
329         gtk_widget_destroy(window);
330 }
331
332 static void *consolethread(void *data)
333 {
334         gtk_widget_show(window);
335         gdk_threads_enter();
336         gtk_main();
337         gdk_threads_leave();
338         return NULL;
339 }
340
341 static int cli_activate(void)
342 {
343         char buf[256] = "";
344         strncpy(buf, gtk_entry_get_text(GTK_ENTRY(cli)), sizeof(buf) - 1);
345         gtk_entry_set_text(GTK_ENTRY(cli), "");
346         if (strlen(buf)) {
347                 ast_cli_command(clipipe[1], buf);
348         }
349         return TRUE;
350 }
351
352 static int show_console(void)
353 {
354         GtkWidget *hbox;
355         GtkWidget *wbox;
356         GtkWidget *notebook;
357         GtkWidget *sw;
358         GtkWidget *bbox, *hbbox, *add, *removew, *reloadw;
359         char *modtitles[3] = { "Module", "Description", "Use Count" };
360         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
361         
362         statusbar = gtk_statusbar_new();
363         gtk_widget_show(statusbar);
364         
365         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
366                         GTK_SIGNAL_FUNC (exit_nicely), window);
367         gtk_signal_connect(GTK_OBJECT(window), "destroy",
368                         GTK_SIGNAL_FUNC (exit_now), window);
369         gtk_container_set_border_width(GTK_CONTAINER(window), 10);
370
371         quit = gtk_button_new_with_label("Quit Asterisk");
372         gtk_signal_connect(GTK_OBJECT(quit), "clicked",
373                         GTK_SIGNAL_FUNC (exit_completely), window);
374         gtk_widget_show(quit);
375
376         closew = gtk_button_new_with_label("Close Window");
377         gtk_signal_connect(GTK_OBJECT(closew), "clicked",
378                         GTK_SIGNAL_FUNC (exit_nicely), window);
379         gtk_widget_show(closew);
380
381         notebook = gtk_notebook_new();
382         verb = gtk_clist_new(1);
383         gtk_clist_columns_autosize(GTK_CLIST(verb));
384         sw = gtk_scrolled_window_new(NULL, NULL);
385         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
386         gtk_container_add(GTK_CONTAINER(sw), verb);
387         gtk_widget_show(verb);
388         gtk_widget_show(sw);
389         gtk_widget_set_usize(verb, 640, 400);
390         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), sw, gtk_label_new("Verbose Status"));
391
392         
393         modules = gtk_clist_new_with_titles(3, modtitles);
394         gtk_clist_columns_autosize(GTK_CLIST(modules));
395         gtk_clist_set_column_auto_resize(GTK_CLIST(modules), 0, TRUE);
396         gtk_clist_set_column_auto_resize(GTK_CLIST(modules), 1, TRUE);
397         gtk_clist_set_column_auto_resize(GTK_CLIST(modules), 2, TRUE);
398         gtk_clist_set_sort_column(GTK_CLIST(modules), 0);
399         gtk_clist_set_auto_sort(GTK_CLIST(modules), TRUE);
400         gtk_clist_column_titles_passive(GTK_CLIST(modules));
401         sw = gtk_scrolled_window_new(NULL, NULL);
402         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
403         gtk_container_add(GTK_CONTAINER(sw), modules);
404         gtk_clist_set_selection_mode(GTK_CLIST(modules), GTK_SELECTION_BROWSE);
405         gtk_widget_show(modules);
406         gtk_widget_show(sw);
407
408         add = gtk_button_new_with_label("Load...");
409         gtk_widget_show(add);
410         removew = gtk_button_new_with_label("Unload");
411         gtk_widget_show(removew);
412         reloadw = gtk_button_new_with_label("Reload");
413         gtk_widget_show(reloadw);
414         gtk_signal_connect(GTK_OBJECT(removew), "clicked",
415                         GTK_SIGNAL_FUNC (remove_module), window);
416         gtk_signal_connect(GTK_OBJECT(add), "clicked",
417                         GTK_SIGNAL_FUNC (add_module), window);
418         gtk_signal_connect(GTK_OBJECT(reloadw), "clicked",
419                         GTK_SIGNAL_FUNC (reload), window);
420                 
421         bbox = gtk_vbox_new(FALSE, 5);
422         gtk_widget_show(bbox);
423
424         gtk_widget_set_usize(bbox, 100, -1);
425         gtk_box_pack_start(GTK_BOX(bbox), add, FALSE, FALSE, 5);
426         gtk_box_pack_start(GTK_BOX(bbox), removew, FALSE, FALSE, 5);
427         gtk_box_pack_start(GTK_BOX(bbox), reloadw, FALSE, FALSE, 5);
428
429         hbbox = gtk_hbox_new(FALSE, 5);
430         gtk_widget_show(hbbox);
431         
432         gtk_box_pack_start(GTK_BOX(hbbox), sw, TRUE, TRUE, 5);
433         gtk_box_pack_start(GTK_BOX(hbbox), bbox, FALSE, FALSE, 5);
434
435         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), hbbox, gtk_label_new("Module Information"));
436
437         gtk_widget_show(notebook);
438
439         wbox = gtk_hbox_new(FALSE, 5);
440         gtk_widget_show(wbox);
441         gtk_box_pack_end(GTK_BOX(wbox), quit, FALSE, FALSE, 5);
442         gtk_box_pack_end(GTK_BOX(wbox), closew, FALSE, FALSE, 5);
443
444         hbox = gtk_vbox_new(FALSE, 0);
445         gtk_widget_show(hbox);
446         
447         /* Command line */
448         cli = gtk_entry_new();
449         gtk_widget_show(cli);
450
451         gtk_signal_connect(GTK_OBJECT(cli), "activate",
452                         GTK_SIGNAL_FUNC (cli_activate), NULL);
453
454         gtk_box_pack_start(GTK_BOX(hbox), notebook, TRUE, TRUE, 5);
455         gtk_box_pack_start(GTK_BOX(hbox), wbox, FALSE, FALSE, 5);
456         gtk_box_pack_start(GTK_BOX(hbox), cli, FALSE, FALSE, 0);
457         gtk_box_pack_start(GTK_BOX(hbox), statusbar, FALSE, FALSE, 0);
458         gtk_container_add(GTK_CONTAINER(window), hbox);
459         gtk_window_set_title(GTK_WINDOW(window), "Asterisk Console");
460         gtk_widget_grab_focus(cli);
461         ast_pthread_create(&console_thread, NULL, consolethread, NULL);
462         /* XXX Okay, seriously fix me! XXX */
463         usleep(100000);
464         ast_register_verbose(verboser);
465         gtk_clist_freeze(GTK_CLIST(verb));
466         ast_loader_register(mod_update);
467         gtk_clist_thaw(GTK_CLIST(verb));
468         gdk_input_add(clipipe[0], GDK_INPUT_READ, cliinput, NULL);
469         mod_update();
470         update_statusbar("Asterisk Console Ready");
471         return 0;
472 }
473
474
475 static int load_module(void)
476 {
477         if (pipe(clipipe)) {
478                 ast_log(LOG_WARNING, "Unable to create CLI pipe\n");
479                 return AST_MODULE_LOAD_FAILURE;
480         }
481         g_thread_init(NULL);
482         if (gtk_init_check(NULL, NULL))  {
483                 if (!show_console()) {
484                         inuse++;
485                         ast_update_use_count();
486                         ast_verb(2, "Launched GTK Console monitor\n");
487                 } else
488                         ast_log(LOG_WARNING, "Unable to start GTK console\n");
489         } else {
490                 if (option_debug)
491                         ast_log(LOG_DEBUG, "Unable to start GTK console monitor -- ignoring\n");
492                 else
493                         ast_verb(2, "GTK is not available -- skipping monitor\n");
494         }
495         return AST_MODULE_LOAD_SUCCESS;
496 }
497
498 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "GTK Console",
499                 .load = load_module,
500                 .unload = unload_module,
501                 .reload = reload,
502                );