e3ef2ab62afb3cdaebeb9b6f399ade4239dcf3c6
[aweather] / src / aweather-gui.c
1 /*
2  * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <config.h>
19 #include <gtk/gtk.h>
20 #include <gdk/gdkkeysyms.h>
21 #include <glib/gstdio.h>
22 #include <math.h>
23
24 #include <grits.h>
25
26 #include "aweather-gui.h"
27 #include "aweather-location.h"
28
29 /* Autohide */
30 typedef struct {
31         GtkWidget      *widget;
32         GtkWidget      *window;
33         GtkPositionType position;
34         gulong          handler;
35 } GtkAutohideData;
36 static gboolean _gtk_widget_autohide_cb(GtkWidget *win,
37                 GdkEventMotion *event, GtkAutohideData *data)
38 {
39         GtkAllocation alloc;
40         gtk_widget_get_allocation(data->window, &alloc);
41         if (data->position == GTK_POS_BOTTOM)
42                 g_debug("GtkWidget: autohide_cb - y=%lf, h=%d", event->y, alloc.height);
43         gint position = -1;
44         if (event->x < 20)                position = GTK_POS_LEFT;
45         if (event->y < 20)                position = GTK_POS_TOP;
46         if (event->x > alloc.width  - 20) position = GTK_POS_RIGHT;
47         if (event->y > alloc.height - 20) position = GTK_POS_BOTTOM;
48         if (position == data->position)
49                 gtk_widget_show(data->widget);
50         else
51                 gtk_widget_hide(data->widget);
52         return FALSE;
53 }
54 static void _gtk_widget_autohide(GtkWidget *widget, GtkWidget *viewer,
55                 GtkPositionType position)
56 {
57         GtkAutohideData *data = g_new0(GtkAutohideData, 1);
58         data->widget   = widget;
59         data->position = position;
60         data->window   = viewer;
61         data->handler  = g_signal_connect(data->window, "motion-notify-event",
62                         G_CALLBACK(_gtk_widget_autohide_cb), data);
63         g_object_set_data(G_OBJECT(widget), "autohidedata", data);
64         gtk_widget_hide(widget);
65 }
66 static void _gtk_widget_autoshow(GtkWidget *widget)
67 {
68         GtkAutohideData *data = g_object_get_data(G_OBJECT(widget), "autohidedata");
69         g_signal_handler_disconnect(data->window, data->handler);
70         g_free(data);
71         gtk_widget_show(widget);
72 }
73
74 /*************
75  * Callbacks *
76  *************/
77 G_MODULE_EXPORT gboolean on_gui_key_press(GtkWidget *widget, GdkEventKey *event, AWeatherGui *self)
78 {
79         g_debug("AWeatherGui: on_gui_key_press - key=%x, state=%x",
80                         event->keyval, event->state);
81         GObject *action = aweather_gui_get_object(self, "fullscreen");
82         gboolean full   = gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action));
83         if (event->keyval == GDK_q)
84                 gtk_widget_destroy(GTK_WIDGET(self));
85         else if (event->keyval == GDK_F11 ||
86                 (event->keyval == GDK_Escape && full))
87                 gtk_action_activate(GTK_ACTION(action));
88         else if (event->keyval == GDK_r && event->state & GDK_CONTROL_MASK)
89                 grits_viewer_refresh(self->viewer);
90         else if (event->keyval == GDK_Tab || event->keyval == GDK_ISO_Left_Tab) {
91                 GtkNotebook *tabs = GTK_NOTEBOOK(aweather_gui_get_widget(self, "main_tabs"));
92                 gint num_tabs = gtk_notebook_get_n_pages(tabs);
93                 gint cur_tab  = gtk_notebook_get_current_page(tabs);
94                 if (event->state & GDK_SHIFT_MASK)
95                         gtk_notebook_set_current_page(tabs, (cur_tab-1)%num_tabs);
96                 else
97                         gtk_notebook_set_current_page(tabs, (cur_tab+1)%num_tabs);
98         };
99         return FALSE;
100 }
101
102 static void cleancache_r(gchar *path)
103 {
104         GDir *dir = g_dir_open(path, 0, NULL);
105         if (!dir)
106                 return;
107         const gchar *child;
108         while ((child = g_dir_read_name(dir))) {
109                 gchar *child_path = g_build_filename(path, child, NULL);
110                 if (g_file_test(child_path, G_FILE_TEST_IS_DIR)) {
111                         cleancache_r(child_path);
112                 } else {
113                         struct stat st;
114                         g_stat(child_path, &st);
115                         if (st.st_atime < time(NULL)-60*60*24)
116                                 g_remove(child_path);
117                 }
118                 g_free(child_path);
119         }
120         g_dir_close(dir);
121         g_rmdir(path);
122 }
123 G_MODULE_EXPORT void on_cleancache(GtkMenuItem *menu, AWeatherGui *self)
124 {
125         g_debug("AWeatherGui: on_cleancache");
126         /* Todo: move this to grits */
127         gchar *cache = g_build_filename(g_get_user_cache_dir(), "grits", NULL);
128         cleancache_r(cache);
129 }
130
131 G_MODULE_EXPORT void on_contents(GtkMenuItem *menu, AWeatherGui *self)
132 {
133         GError *err = NULL;
134         gchar *path = g_strdup(HTMLDIR "/aweather.html");
135         g_strdelimit(path, "/", G_DIR_SEPARATOR);
136         gchar *argv[] = {"xdg-open", path, NULL};
137         g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &err);
138         if (err) {
139                 g_warning("Unable to open help page: %s - %s",
140                                 argv[1], err->message);
141                 g_error_free(err);
142         }
143         g_free(path);
144 }
145
146 G_MODULE_EXPORT void on_quit(GtkMenuItem *menu, AWeatherGui *self)
147 {
148         gtk_widget_destroy(GTK_WIDGET(self));
149 }
150
151 G_MODULE_EXPORT void on_zoomin(GtkAction *action, AWeatherGui *self)
152 {
153         grits_viewer_zoom(self->viewer, 3./4);
154 }
155
156 G_MODULE_EXPORT void on_zoomout(GtkAction *action, AWeatherGui *self)
157 {
158         grits_viewer_zoom(self->viewer, 4./3);
159 }
160
161 G_MODULE_EXPORT void on_fullscreen(GtkToggleAction *action, AWeatherGui *self)
162 {
163         GtkWidget *menu    = aweather_gui_get_widget(self, "main_menu");
164         GtkWidget *toolbar = aweather_gui_get_widget(self, "main_toolbar");
165         GtkWidget *sidebar = aweather_gui_get_widget(self, "main_sidebar");
166         GtkWidget *tabs    = aweather_gui_get_widget(self, "main_tabs");
167         if (gtk_toggle_action_get_active(action)) {
168                 gtk_window_fullscreen(GTK_WINDOW(self));
169                 gtk_widget_hide(menu);
170                 _gtk_widget_autohide(toolbar, GTK_WIDGET(self->viewer), GTK_POS_TOP);
171                 _gtk_widget_autohide(sidebar, GTK_WIDGET(self->viewer), GTK_POS_RIGHT);
172                 _gtk_widget_autohide(tabs,    GTK_WIDGET(self->viewer), GTK_POS_BOTTOM);
173         } else {
174                 gtk_window_unfullscreen(GTK_WINDOW(self));
175                 gtk_widget_show(menu);
176                 _gtk_widget_autoshow(toolbar);
177                 _gtk_widget_autoshow(sidebar);
178                 _gtk_widget_autoshow(tabs);
179         }
180 }
181
182 G_MODULE_EXPORT void on_refresh(GtkAction *action, AWeatherGui *self)
183 {
184         grits_viewer_refresh(self->viewer);
185 }
186
187 static gboolean on_update_timeout(AWeatherGui *self)
188 {
189         g_debug("AWeatherGui: on_update_timeout");
190         grits_viewer_set_time(self->viewer, time(NULL));
191         grits_viewer_refresh(self->viewer);
192         return FALSE;
193 }
194 static void set_update_timeout(AWeatherGui *self)
195 {
196         GObject *action = aweather_gui_get_object(self, "update");
197         gint freq = grits_prefs_get_integer(self->prefs, "aweather/update_freq", NULL);
198         if (self->update_source) {
199                 g_debug("AWeatherGui: set_update_timeout - clear");
200                 g_source_remove(self->update_source);
201         }
202         self->update_source = 0;
203         if (gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action)) && freq > 0) {
204                 g_debug("AWeatherGui: set_update_timeout - %d min", freq);
205                 self->update_source = g_timeout_add_seconds(freq*60,
206                                 (GSourceFunc)on_update_timeout, self);
207         }
208 }
209 G_MODULE_EXPORT void on_update(GtkToggleAction *action, AWeatherGui *self)
210 {
211         grits_prefs_set_boolean(self->prefs, "aweather/update_enab",
212                         gtk_toggle_action_get_active(action));
213         set_update_timeout(self);
214 }
215
216 G_MODULE_EXPORT void on_plugin_toggled(GtkCellRendererToggle *cell,
217                 gchar *path_str, AWeatherGui *self)
218 {
219         GtkWidget    *tview = aweather_gui_get_widget(self, "prefs_plugins_view");
220         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(tview));
221         gboolean state;
222         gchar *name;
223         GtkTreeIter iter;
224         gtk_tree_model_get_iter_from_string(model, &iter, path_str);
225         gtk_tree_model_get(model, &iter, 0, &name, 1, &state, -1);
226         state = !state;
227         gtk_list_store_set(GTK_LIST_STORE(model), &iter, 1, state, -1);
228         if (state)
229                 aweather_gui_attach_plugin(self, name);
230         else
231                 aweather_gui_deattach_plugin(self, name);
232         g_free(name);
233 }
234
235 G_MODULE_EXPORT void on_time_changed(AWeatherGui *self)
236 {
237         g_debug("AWeatherGui: on_time_changed");
238         struct tm tm = {.tm_isdst = -1};
239
240         GtkWidget *cal = aweather_gui_get_widget(self, "main_date_cal");
241         gtk_calendar_get_date(GTK_CALENDAR(cal), (guint*)&tm.tm_year,
242                         (guint*)&tm.tm_mon, (guint*)&tm.tm_mday);
243         tm.tm_year -= 1900;
244
245         GtkTreeIter   iter;
246         GtkTreePath  *path  = NULL;
247         GtkWidget    *view  = aweather_gui_get_widget(self, "main_time");
248         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
249         gtk_tree_view_get_cursor(GTK_TREE_VIEW(view), &path, NULL);
250         gtk_tree_model_get_iter(model, &iter, path);
251         gtk_tree_model_get(model, &iter, 1, &tm.tm_hour, 2, &tm.tm_min, -1);
252
253         grits_viewer_set_time(self->viewer, mktime(&tm));
254 }
255
256 static gboolean gtk_tree_model_find_string(GtkTreeModel *model,
257                 GtkTreeIter *rval, GtkTreeIter *parent, gint field, const gchar *key)
258 {
259         g_debug("AWeatherGui: gtk_tree_model_find - field=%d key=%s", field, key);
260         char *text;
261         GtkTreeIter cur;
262         gint num_children = gtk_tree_model_iter_n_children(model, parent);
263         for (int i = 0; i < num_children; i++) {
264                 gtk_tree_model_iter_nth_child(model, &cur, parent, i);
265                 gtk_tree_model_get(model, &cur, field, &text, -1);
266                 if (text != NULL && g_str_equal(text, key)) {
267                         *rval = cur;
268                         g_free(text);
269                         return TRUE;
270                 }
271                 g_free(text);
272                 if (gtk_tree_model_iter_has_child(model, &cur))
273                         if (gtk_tree_model_find_string(model, rval, &cur, field, key))
274                                 return TRUE;
275         }
276         return FALSE;
277 }
278
279 static void update_time_widget(GritsViewer *viewer, time_t time, AWeatherGui *self)
280 {
281         g_debug("AWeatherGui: update_time_widget - time=%u", (guint)time);
282         // FIXME
283         //GtkTreeView  *tview = GTK_TREE_VIEW(aweather_gui_get_widget(self, "main_time"));
284         //GtkTreeModel *model = GTK_TREE_MODEL(gtk_tree_view_get_model(tview));
285         //GtkTreeIter iter;
286         //if (gtk_tree_model_find_string(model, &iter, NULL, 0, time)) {
287         //      GtkTreePath *path = gtk_tree_model_get_path(model, &iter);
288         //      gtk_tree_view_set_cursor(tview, path, NULL, FALSE);
289         //      gtk_tree_path_free(path);
290         //}
291 }
292
293 /* Prefs callbacks */
294 G_MODULE_EXPORT void on_offline(GtkToggleAction *action, AWeatherGui *self)
295 {
296         gboolean value = gtk_toggle_action_get_active(action);
297         g_debug("AWeatherGui: on_offline - offline=%d", value);
298         grits_viewer_set_offline(self->viewer, value);
299 }
300
301 G_MODULE_EXPORT int on_update_freq_changed(GtkSpinButton *spinner, AWeatherGui *self)
302 {
303         gint value = gtk_spin_button_get_value_as_int(spinner);
304         g_debug("AWeatherGui: on_update_freq_changed - %p, freq=%d", self, value);
305         grits_prefs_set_integer(self->prefs, "aweather/update_freq", value);
306         set_update_timeout(self);
307         return TRUE;
308 }
309
310 G_MODULE_EXPORT void on_initial_site_changed(GtkComboBox *combo, AWeatherGui *self)
311 {
312         gchar *code;
313         GtkTreeIter iter;
314         GtkTreeModel *model = gtk_combo_box_get_model(combo);
315         gtk_combo_box_get_active_iter(combo, &iter);
316         gtk_tree_model_get(model, &iter, 0, &code, -1);
317         g_debug("AWeatherGui: on_initial_site_changed - code=%s", code);
318         grits_prefs_set_string(self->prefs, "aweather/initial_site", code);
319         g_free(code);
320 }
321
322 G_MODULE_EXPORT void on_nexrad_url_changed(GtkEntry *entry, AWeatherGui *self)
323 {
324         const gchar *text = gtk_entry_get_text(entry);
325         g_debug("AWeatherGui: on_nexrad_url_changed - url=%s", text);
326         grits_prefs_set_string(self->prefs, "aweather/nexrad_url", text);
327 }
328
329 G_MODULE_EXPORT int on_log_level_changed(GtkSpinButton *spinner, AWeatherGui *self)
330 {
331         gint value = gtk_spin_button_get_value_as_int(spinner);
332         g_debug("AWeatherGui: on_log_level_changed - %p, level=%d", self, value);
333         grits_prefs_set_integer(self->prefs, "aweather/log_level", value);
334         return TRUE;
335 }
336
337
338 /*****************
339  * Setup helpers *
340  *****************/
341 static void combo_sensitive(GtkCellLayout *cell_layout, GtkCellRenderer *cell,
342                 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
343 {
344         gboolean sensitive = !gtk_tree_model_iter_has_child(tree_model, iter);
345         g_object_set(cell, "sensitive", sensitive, NULL);
346 }
347 static void site_setup(AWeatherGui *self)
348 {
349         GtkTreeIter state, city;
350         GtkTreeStore *store = GTK_TREE_STORE(aweather_gui_get_object(self, "sites"));
351         gtk_tree_store_append(store, &state, NULL);
352         gtk_tree_store_set   (store, &state, 0, "", 1, "None", -1);
353         for (int i = 0; cities[i].type; i++) {
354                 if (cities[i].type == LOCATION_STATE) {
355                         gtk_tree_store_append(store, &state, NULL);
356                         gtk_tree_store_set   (store, &state, 0, cities[i].code,
357                                                              1, cities[i].name, -1);
358                 } else {
359                         gtk_tree_store_append(store, &city, &state);
360                         gtk_tree_store_set   (store, &city, 0, cities[i].code,
361                                                             1, cities[i].name, -1);
362                 }
363         }
364
365         GtkWidget *combo    = aweather_gui_get_widget(self, "prefs_general_site");
366         GObject   *renderer = aweather_gui_get_object(self, "prefs_general_site_rend");
367         gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(combo),
368                         GTK_CELL_RENDERER(renderer), combo_sensitive, NULL, NULL);
369 }
370
371 static void prefs_setup(AWeatherGui *self)
372 {
373         /* Set values */
374         gint   uf = grits_prefs_get_integer(self->prefs, "aweather/update_freq",  NULL);
375         gchar *nu = grits_prefs_get_string (self->prefs, "aweather/nexrad_url",   NULL);
376         gint   ll = grits_prefs_get_integer(self->prefs, "aweather/log_level",    NULL);
377         gchar *is = grits_prefs_get_string (self->prefs, "aweather/initial_site", NULL);
378         GtkWidget *ufw = aweather_gui_get_widget(self, "prefs_general_freq");
379         GtkWidget *nuw = aweather_gui_get_widget(self, "prefs_general_url");
380         GtkWidget *llw = aweather_gui_get_widget(self, "prefs_general_log");
381         GtkWidget *isw = aweather_gui_get_widget(self, "prefs_general_site");
382         if (uf) gtk_spin_button_set_value(GTK_SPIN_BUTTON(ufw), uf);
383         if (nu) gtk_entry_set_text(GTK_ENTRY(nuw), nu), g_free(nu);
384         if (ll) gtk_spin_button_set_value(GTK_SPIN_BUTTON(llw), ll);
385         if (is) {
386                 GtkTreeModel *model = gtk_combo_box_get_model(GTK_COMBO_BOX(isw));
387                 GtkTreeIter iter;
388                 if (gtk_tree_model_find_string(model, &iter, NULL, 0, is))
389                         gtk_combo_box_set_active_iter(GTK_COMBO_BOX(isw), &iter);
390         }
391
392         /* Load Plugins */
393         GtkTreeView       *tview = GTK_TREE_VIEW(aweather_gui_get_widget(self, "prefs_plugins_view"));
394         GtkCellRenderer   *rend1 = gtk_cell_renderer_text_new();
395         GtkCellRenderer   *rend2 = gtk_cell_renderer_toggle_new();
396         GtkTreeViewColumn *col1  = gtk_tree_view_column_new_with_attributes(
397                         "Plugin",  rend1, "text",   0, NULL);
398         GtkTreeViewColumn *col2  = gtk_tree_view_column_new_with_attributes(
399                         "Enabled", rend2, "active", 1, NULL);
400         g_object_set(rend2, "xalign", 0.0, NULL);
401         gtk_tree_view_append_column(tview, col1);
402         gtk_tree_view_append_column(tview, col2);
403         g_signal_connect(rend2, "toggled", G_CALLBACK(on_plugin_toggled), self);
404         gtk_tree_view_set_model(GTK_TREE_VIEW(tview), GTK_TREE_MODEL(self->gtk_plugins));
405 }
406
407 static void time_setup(AWeatherGui *self)
408 {
409         /* Add times */
410         GtkTreeStore *store = GTK_TREE_STORE(aweather_gui_get_object(self, "times"));
411         for (int hour = 0; hour < 24; hour++) {
412                 GtkTreeIter hour_iter;
413                 gchar *str = g_strdup_printf("%02d:00Z", hour);
414                 gtk_tree_store_append(store, &hour_iter, NULL);
415                 gtk_tree_store_set(store, &hour_iter, 0, str, 1, hour, 2, 0, -1);
416                 g_free(str);
417                 for (int min = 5; min < 60; min += 5) {
418                         GtkTreeIter min_iter;
419                         gchar *str = g_strdup_printf("%02d:%02dZ", hour, min);
420                         gtk_tree_store_append(store, &min_iter, &hour_iter);
421                         gtk_tree_store_set(store, &min_iter, 0, str, 1, hour, 2, min, -1);
422                         g_free(str);
423                 }
424         }
425
426         /* Connect signals */
427         GtkWidget *cal  = aweather_gui_get_widget(self, "main_date_cal");
428         GtkWidget *view = aweather_gui_get_widget(self, "main_time");
429         g_signal_connect_swapped(cal,  "day-selected-double-click",
430                         G_CALLBACK(on_time_changed), self);
431         g_signal_connect_swapped(view, "row-activated",
432                         G_CALLBACK(on_time_changed), self);
433         g_signal_connect(self->viewer, "time-changed",
434                         G_CALLBACK(update_time_widget), self);
435 }
436
437 static void icons_setup(AWeatherGui *self)
438 {
439         gchar *icons[] = {
440                 ICONDIR "/hicolor/16x16/apps/aweather.png",
441                 ICONDIR "/hicolor/22x22/apps/aweather.png",
442                 ICONDIR "/hicolor/24x24/apps/aweather.png",
443                 ICONDIR "/hicolor/32x32/apps/aweather.png",
444                 ICONDIR "/hicolor/48x48/apps/aweather.png",
445                 ICONDIR "/hicolor/scalable/apps/aweather.svg",
446         };
447         GList *list = NULL;
448         for (int i = 0; i < G_N_ELEMENTS(icons); i++) {
449                 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(icons[i], NULL);
450                 if (!pixbuf)
451                         g_warning("AWeatherGui: icons_setup - %s failed", icons[i]);
452                 list = g_list_prepend(list, pixbuf);
453         }
454         gtk_window_set_default_icon_list(list);
455         g_list_free(list);
456 }
457
458
459 /***********
460  * Methods *
461  ***********/
462 AWeatherGui *aweather_gui_new()
463 {
464         g_debug("AWeatherGui: new");
465         return g_object_new(AWEATHER_TYPE_GUI, NULL);
466 }
467 GritsViewer *aweather_gui_get_viewer(AWeatherGui *self)
468 {
469         g_assert(AWEATHER_IS_GUI(self));
470         return self->viewer;
471 }
472 GtkBuilder *aweather_gui_get_builder(AWeatherGui *self)
473 {
474         g_debug("AWeatherGui: get_builder");
475         g_assert(AWEATHER_IS_GUI(self));
476         return self->builder;
477 }
478 GtkWidget *aweather_gui_get_widget(AWeatherGui *self, const gchar *name)
479 {
480         g_debug("AWeatherGui: get_widget - name=%s", name);
481         g_assert(AWEATHER_IS_GUI(self));
482         GObject *widget = gtk_builder_get_object(self->builder, name);
483         if (!GTK_IS_WIDGET(widget))
484                 g_error("Failed to get widget `%s'", name);
485         return GTK_WIDGET(widget);
486 }
487 GObject *aweather_gui_get_object(AWeatherGui *self, const gchar *name)
488 {
489         g_debug("AWeatherGui: get_widget - name=%s", name);
490         g_assert(AWEATHER_IS_GUI(self));
491         return gtk_builder_get_object(self->builder, name);
492 }
493 void aweather_gui_attach_plugin(AWeatherGui *self, const gchar *name)
494 {
495         GritsPlugin *plugin = grits_plugins_enable(self->plugins, name,
496                         self->viewer, self->prefs);
497         GtkWidget *body = grits_plugin_get_config(plugin);
498         if (body) {
499                 GtkWidget *config = aweather_gui_get_widget(self, "main_tabs");
500                 GtkWidget *tab    = gtk_label_new(name);
501                 gtk_notebook_append_page(GTK_NOTEBOOK(config), body, tab);
502                 gtk_widget_show_all(config);
503         }
504         gtk_widget_queue_draw(GTK_WIDGET(self->viewer));
505 }
506 void aweather_gui_deattach_plugin(AWeatherGui *self, const gchar *name)
507 {
508         GtkWidget *config = aweather_gui_get_widget(self, "main_tabs");
509         guint n_pages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(config));
510         for (int i = 0; i < n_pages; i++) {
511                 g_debug("testing tab %d", i);
512                 GtkWidget *body = gtk_notebook_get_nth_page(GTK_NOTEBOOK(config), i);
513                 if (!body) continue;
514                 GtkWidget *tab = gtk_notebook_get_tab_label(GTK_NOTEBOOK(config), body);
515                 if (!tab) continue;
516                 const gchar *tab_name = gtk_label_get_text(GTK_LABEL(tab));
517                 if (tab_name && g_str_equal(name, tab_name))
518                         gtk_notebook_remove_page(GTK_NOTEBOOK(config), i);
519         }
520         grits_plugins_disable(self->plugins, name);
521         gtk_widget_queue_draw(GTK_WIDGET(self->viewer));
522 }
523 void aweather_gui_load_plugins(AWeatherGui *self)
524 {
525         g_debug("AWeatherGui: load_plugins");
526         GtkTreeIter iter;
527         for (GList *cur = grits_plugins_available(self->plugins); cur; cur = cur->next) {
528                 gchar *name = cur->data;
529                 GError *error = NULL;
530                 gboolean enabled = grits_prefs_get_boolean_v(self->prefs, "plugins", cur->data, &error);
531                 if (error && error->code == G_KEY_FILE_ERROR_GROUP_NOT_FOUND)
532                         enabled = TRUE;
533                 gtk_list_store_append(self->gtk_plugins, &iter);
534                 gtk_list_store_set(self->gtk_plugins, &iter, 0, name, 1, enabled, -1);
535                 if (enabled)
536                         aweather_gui_attach_plugin(self, name);
537         }
538 }
539
540
541
542 /****************
543  * GObject code *
544  ****************/
545 static void aweather_gui_parser_finished(GtkBuildable *_self, GtkBuilder *builder)
546 {
547         g_debug("AWeatherGui: parser finished");
548         AWeatherGui *self = AWEATHER_GUI(_self);
549         self->builder = builder;
550
551         /* Simple things */
552         gchar *config   = g_build_filename(g_get_user_config_dir(), PACKAGE, "config.ini", NULL);
553         gchar *defaults = g_build_filename(PKGDATADIR, "defaults.ini", NULL);
554         self->prefs   = grits_prefs_new(config, defaults);
555         self->plugins = grits_plugins_new(PLUGINSDIR, self->prefs);
556         self->viewer  = GRITS_VIEWER(aweather_gui_get_widget(self, "main_viewer"));
557         self->gtk_plugins = GTK_LIST_STORE(aweather_gui_get_object(self, "plugins"));
558         grits_viewer_setup(self->viewer, self->plugins, self->prefs);
559         g_free(config);
560         g_free(defaults);
561
562         /* Misc, helpers */
563         site_setup(self);
564         time_setup(self);
565         prefs_setup(self);
566         icons_setup(self);
567
568         /* Connect signals */
569         gtk_builder_connect_signals(self->builder, self);
570         g_signal_connect(self, "key-press-event",
571                         G_CALLBACK(on_gui_key_press), self);
572         g_signal_connect_swapped(self->viewer, "offline",
573                         G_CALLBACK(gtk_toggle_action_set_active),
574                         aweather_gui_get_object(self, "offline"));
575         g_signal_connect_swapped(self->viewer, "refresh",
576                         G_CALLBACK(set_update_timeout), self);
577         g_signal_connect_swapped(self->viewer, "realize",
578                         G_CALLBACK(aweather_gui_load_plugins), self);
579 }
580 static void aweather_gui_buildable_init(GtkBuildableIface *iface)
581 {
582         iface->parser_finished = aweather_gui_parser_finished;
583 }
584 G_DEFINE_TYPE_WITH_CODE(AWeatherGui, aweather_gui, GTK_TYPE_WINDOW,
585                 G_IMPLEMENT_INTERFACE(GTK_TYPE_BUILDABLE,
586                         aweather_gui_buildable_init));
587 static void aweather_gui_init(AWeatherGui *self)
588 {
589         g_debug("AWeatherGui: init");
590         /* Do all the real work in parser_finished */
591 }
592 static GObject *aweather_gui_constructor(GType gtype, guint n_properties,
593                 GObjectConstructParam *properties)
594 {
595         g_debug("AWeatherGui: constructor");
596         GObjectClass *parent_class = G_OBJECT_CLASS(aweather_gui_parent_class);
597         return parent_class->constructor(gtype, n_properties, properties);
598 }
599 static void aweather_gui_dispose(GObject *_self)
600 {
601         g_debug("AWeatherGui: dispose");
602         AWeatherGui *self = AWEATHER_GUI(_self);
603         if (self->plugins) {
604                 grits_plugins_free(self->plugins);
605                 self->plugins = NULL;
606         }
607         if (self->builder) {
608                 g_object_unref(self->builder);
609                 self->builder = NULL;
610         }
611         if (self->prefs) {
612                 g_object_unref(self->prefs);
613                 self->prefs = NULL;
614         }
615         G_OBJECT_CLASS(aweather_gui_parent_class)->dispose(_self);
616 }
617 static void aweather_gui_finalize(GObject *_self)
618 {
619         g_debug("AWeatherGui: finalize");
620         G_OBJECT_CLASS(aweather_gui_parent_class)->finalize(_self);
621
622 }
623 static void aweather_gui_class_init(AWeatherGuiClass *klass)
624 {
625         g_debug("AWeatherGui: class_init");
626         GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
627         gobject_class->constructor  = aweather_gui_constructor;
628         gobject_class->dispose      = aweather_gui_dispose;
629         gobject_class->finalize     = aweather_gui_finalize;
630 }