/*
- * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
+ * Copyright (C) 2009-2011 Andy Spencer <andy753421@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
#include <config.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
+#include <glib/gstdio.h>
#include <math.h>
-#include <gis/gis.h>
+#include <grits.h>
#include "aweather-gui.h"
#include "aweather-location.h"
+/* Autohide */
+typedef struct {
+ GtkWidget *widget;
+ GtkWidget *window;
+ GtkPositionType position;
+ gulong handler;
+} GtkAutohideData;
+static gboolean _gtk_widget_autohide_cb(GtkWidget *win,
+ GdkEventMotion *event, GtkAutohideData *data)
+{
+ GtkAllocation alloc;
+ gtk_widget_get_allocation(data->window, &alloc);
+ if (data->position == GTK_POS_BOTTOM)
+ g_debug("GtkWidget: autohide_cb - y=%lf, h=%d", event->y, alloc.height);
+ gint position = -1;
+ if (event->x < 20) position = GTK_POS_LEFT;
+ if (event->y < 20) position = GTK_POS_TOP;
+ if (event->x > alloc.width - 20) position = GTK_POS_RIGHT;
+ if (event->y > alloc.height - 20) position = GTK_POS_BOTTOM;
+ if (position == data->position)
+ gtk_widget_show(data->widget);
+ else
+ gtk_widget_hide(data->widget);
+ return FALSE;
+}
+static void _gtk_widget_autohide(GtkWidget *widget, GtkWidget *viewer,
+ GtkPositionType position)
+{
+ GtkAutohideData *data = g_new0(GtkAutohideData, 1);
+ data->widget = widget;
+ data->position = position;
+ data->window = viewer;
+ data->handler = g_signal_connect(data->window, "motion-notify-event",
+ G_CALLBACK(_gtk_widget_autohide_cb), data);
+ g_object_set_data(G_OBJECT(widget), "autohidedata", data);
+ gtk_widget_hide(widget);
+}
+static void _gtk_widget_autoshow(GtkWidget *widget)
+{
+ GtkAutohideData *data = g_object_get_data(G_OBJECT(widget), "autohidedata");
+ g_signal_handler_disconnect(data->window, data->handler);
+ g_free(data);
+ gtk_widget_show(widget);
+}
/*************
* Callbacks *
*************/
-gboolean on_gui_key_press(GtkWidget *widget, GdkEventKey *event, AWeatherGui *self)
+G_MODULE_EXPORT gboolean on_gui_key_press(GtkWidget *widget, GdkEventKey *event, AWeatherGui *self)
{
g_debug("AWeatherGui: on_gui_key_press - key=%x, state=%x",
event->keyval, event->state);
+ GObject *action = aweather_gui_get_object(self, "fullscreen");
+ gboolean full = gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action));
if (event->keyval == GDK_q)
gtk_widget_destroy(GTK_WIDGET(self));
+ else if (event->keyval == GDK_F11 ||
+ (event->keyval == GDK_Escape && full))
+ gtk_action_activate(GTK_ACTION(action));
else if (event->keyval == GDK_r && event->state & GDK_CONTROL_MASK)
- gis_viewer_refresh(self->viewer);
+ grits_viewer_refresh(self->viewer);
else if (event->keyval == GDK_Tab || event->keyval == GDK_ISO_Left_Tab) {
GtkNotebook *tabs = GTK_NOTEBOOK(aweather_gui_get_widget(self, "main_tabs"));
gint num_tabs = gtk_notebook_get_n_pages(tabs);
return FALSE;
}
-void on_quit(GtkMenuItem *menu, AWeatherGui *self)
+static void cleancache_r(gchar *path)
+{
+ GDir *dir = g_dir_open(path, 0, NULL);
+ if (!dir)
+ return;
+ const gchar *child;
+ while ((child = g_dir_read_name(dir))) {
+ gchar *child_path = g_build_filename(path, child, NULL);
+ if (g_file_test(child_path, G_FILE_TEST_IS_DIR)) {
+ cleancache_r(child_path);
+ } else {
+ struct stat st;
+ g_stat(child_path, &st);
+ if (st.st_atime < time(NULL)-60*60*24)
+ g_remove(child_path);
+ }
+ g_free(child_path);
+ }
+ g_dir_close(dir);
+ g_rmdir(path);
+}
+G_MODULE_EXPORT void on_cleancache(GtkMenuItem *menu, AWeatherGui *self)
+{
+ g_debug("AWeatherGui: on_cleancache");
+ /* Todo: move this to grits */
+ gchar *cache = g_build_filename(g_get_user_cache_dir(), "grits", NULL);
+ cleancache_r(cache);
+}
+
+G_MODULE_EXPORT void on_help(GtkMenuItem *menu, AWeatherGui *self)
+{
+ GError *err = NULL;
+ const gchar *name = gtk_buildable_get_name(GTK_BUILDABLE(menu));
+ gchar *page = g_str_has_suffix(name, "userguide") ? "userguide" :
+ g_str_has_suffix(name, "manpage") ? "aweather" : NULL;
+ if (page == NULL) {
+ g_warning("Unknown help page: %s", page);
+ return;
+ }
+ gchar *path = g_strdup_printf("%s/%s.html", HTMLDIR, page);
+ g_strdelimit(path, "/", G_DIR_SEPARATOR);
+ gchar *argv[] = {"xdg-open", path, NULL};
+ g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &err);
+ if (err) {
+ g_warning("Unable to open help page: %s - %s",
+ argv[1], err->message);
+ g_error_free(err);
+ }
+ g_free(path);
+}
+
+void on_radar_changed(GtkMenuItem *menu, AWeatherGui *self)
+{
+ city_t *city = g_object_get_data(G_OBJECT(menu), "city");
+ grits_viewer_set_location(self->viewer,
+ city->pos.lat, city->pos.lon, EARTH_R/35);
+ /* Focus radar tab */
+ GtkWidget *config = aweather_gui_get_widget(self, "main_tabs");
+ gint npages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(config));
+ for (int i = 0; i < npages; i++) {
+ GtkWidget *child = gtk_notebook_get_nth_page(GTK_NOTEBOOK(config), i);
+ const gchar *plugin = gtk_notebook_get_tab_label_text(GTK_NOTEBOOK(config), child);
+ if (g_str_equal(plugin, "radar"))
+ gtk_notebook_set_current_page(GTK_NOTEBOOK(config), i);
+ }
+}
+
+G_MODULE_EXPORT void on_quit(GtkMenuItem *menu, AWeatherGui *self)
{
gtk_widget_destroy(GTK_WIDGET(self));
}
-void on_zoomin(GtkAction *action, AWeatherGui *self)
+G_MODULE_EXPORT void on_zoomin(GtkAction *action, AWeatherGui *self)
+{
+ grits_viewer_zoom(self->viewer, 3./4);
+}
+
+G_MODULE_EXPORT void on_zoomout(GtkAction *action, AWeatherGui *self)
{
- gis_viewer_zoom(self->viewer, 3./4);
+ grits_viewer_zoom(self->viewer, 4./3);
}
-void on_zoomout(GtkAction *action, AWeatherGui *self)
+G_MODULE_EXPORT void on_fullscreen(GtkToggleAction *action, AWeatherGui *self)
{
- gis_viewer_zoom(self->viewer, 4./3);
+ GtkWidget *menu = aweather_gui_get_widget(self, "main_menu");
+ GtkWidget *toolbar = aweather_gui_get_widget(self, "main_toolbar");
+ GtkWidget *sidebar = aweather_gui_get_widget(self, "main_sidebar");
+ GtkWidget *tabs = aweather_gui_get_widget(self, "main_tabs");
+ static gboolean menushow = TRUE; // Toolbar can be always disabled
+ if (gtk_toggle_action_get_active(action)) {
+ menushow = gtk_widget_get_visible(menu);
+ gtk_window_fullscreen(GTK_WINDOW(self));
+ gtk_widget_hide(menu);
+ _gtk_widget_autohide(toolbar, GTK_WIDGET(self->viewer), GTK_POS_TOP);
+ _gtk_widget_autohide(sidebar, GTK_WIDGET(self->viewer), GTK_POS_RIGHT);
+ _gtk_widget_autohide(tabs, GTK_WIDGET(self->viewer), GTK_POS_BOTTOM);
+ } else {
+ gtk_window_unfullscreen(GTK_WINDOW(self));
+ gtk_widget_set_visible(menu, menushow);
+ _gtk_widget_autoshow(toolbar);
+ _gtk_widget_autoshow(sidebar);
+ _gtk_widget_autoshow(tabs);
+ }
}
-void on_refresh(GtkAction *action, AWeatherGui *self)
+G_MODULE_EXPORT void on_refresh(GtkAction *action, AWeatherGui *self)
{
- gis_viewer_refresh(self->viewer);
+ grits_viewer_refresh(self->viewer);
}
-void on_plugin_toggled(GtkCellRendererToggle *cell, gchar *path_str, AWeatherGui *self)
+static gboolean on_update_timeout(AWeatherGui *self)
+{
+ g_debug("AWeatherGui: on_update_timeout");
+ grits_viewer_set_time(self->viewer, time(NULL));
+ grits_viewer_refresh(self->viewer);
+ return FALSE;
+}
+static void set_update_timeout(AWeatherGui *self)
+{
+ GObject *action = aweather_gui_get_object(self, "update");
+ gint freq = grits_prefs_get_integer(self->prefs, "aweather/update_freq", NULL);
+ if (self->update_source) {
+ g_debug("AWeatherGui: set_update_timeout - clear");
+ g_source_remove(self->update_source);
+ }
+ self->update_source = 0;
+ if (gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action)) && freq > 0) {
+ g_debug("AWeatherGui: set_update_timeout - %d min", freq);
+ self->update_source = g_timeout_add_seconds(freq*60,
+ (GSourceFunc)on_update_timeout, self);
+ }
+}
+G_MODULE_EXPORT void on_update(GtkToggleAction *action, AWeatherGui *self)
+{
+ grits_prefs_set_boolean(self->prefs, "aweather/update_enab",
+ gtk_toggle_action_get_active(action));
+ set_update_timeout(self);
+}
+
+G_MODULE_EXPORT void on_plugin_toggled(GtkCellRendererToggle *cell,
+ gchar *path_str, AWeatherGui *self)
{
GtkWidget *tview = aweather_gui_get_widget(self, "prefs_plugins_view");
GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(tview));
aweather_gui_attach_plugin(self, name);
else
aweather_gui_deattach_plugin(self, name);
- gis_prefs_set_boolean_v(self->prefs, name, "enabled", state);
g_free(name);
}
-void on_time_changed(GtkTreeView *view, GtkTreePath *path,
- GtkTreeViewColumn *column, AWeatherGui *self)
+G_MODULE_EXPORT void on_time_changed(AWeatherGui *self)
{
- gchar *time;
- GtkTreeIter iter;
- GtkTreeModel *model = gtk_tree_view_get_model(view);
- gtk_tree_model_get_iter(model, &iter, path);
- gtk_tree_model_get(model, &iter, 0, &time, -1);
- gis_viewer_set_time(self->viewer, time);
- g_free(time);
-}
+ g_debug("AWeatherGui: on_time_changed");
+ struct tm tm = {.tm_isdst = -1};
-void on_site_changed(GtkComboBox *combo, AWeatherGui *self)
-{
- gchar *site;
- GtkTreeIter iter;
- GtkTreeModel *model = gtk_combo_box_get_model(combo);
- gtk_combo_box_get_active_iter(combo, &iter);
- gtk_tree_model_get(model, &iter, 1, &site, -1);
- city_t *city;
- for (city = cities; city->type; city++)
- if (city->code && g_str_equal(city->code, site)) {
- gis_viewer_set_location(self->viewer,
- city->lat, city->lon, EARTH_R/20);
- gis_viewer_set_rotation(self->viewer, 0, 0, 0);
- break;
- }
- g_free(site);
+ GtkWidget *cal = aweather_gui_get_widget(self, "main_date_cal");
+ gtk_calendar_get_date(GTK_CALENDAR(cal), (guint*)&tm.tm_year,
+ (guint*)&tm.tm_mon, (guint*)&tm.tm_mday);
+ tm.tm_year -= 1900;
+
+ GtkTreePath *path = NULL;
+ GtkWidget *view = aweather_gui_get_widget(self, "main_time");
+ gtk_tree_view_get_cursor(GTK_TREE_VIEW(view), &path, NULL);
+ if (path) {
+ GtkTreeIter iter;
+ GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
+ gtk_tree_model_get_iter(model, &iter, path);
+ gtk_tree_model_get(model, &iter, 1, &tm.tm_hour, 2, &tm.tm_min, -1);
+ }
+
+ grits_viewer_set_time(self->viewer, mktime(&tm));
}
static gboolean gtk_tree_model_find_string(GtkTreeModel *model,
return FALSE;
}
-static void update_time_widget(GisViewer *viewer, const char *time, AWeatherGui *self)
-{
- g_debug("AWeatherGui: update_time_widget - time=%s", time);
- GtkTreeView *tview = GTK_TREE_VIEW(aweather_gui_get_widget(self, "main_time"));
- GtkTreeModel *model = GTK_TREE_MODEL(gtk_tree_view_get_model(tview));
- GtkTreeIter iter;
- if (gtk_tree_model_find_string(model, &iter, NULL, 0, time)) {
- GtkTreePath *path = gtk_tree_model_get_path(model, &iter);
- g_signal_handlers_block_by_func(tview,
- G_CALLBACK(on_site_changed), self);
- gtk_tree_view_set_cursor(tview, path, NULL, FALSE);
- g_signal_handlers_unblock_by_func(tview,
- G_CALLBACK(on_site_changed), self);
- gtk_tree_path_free(path);
- }
-}
-
-static void update_site_widget(GisViewer *viewer, char *site, AWeatherGui *self)
+static void update_time_widget(GritsViewer *viewer, time_t time, AWeatherGui *self)
{
- g_debug("AWeatherGui: update_site_widget - site=%s", site);
- GtkComboBox *combo = GTK_COMBO_BOX(aweather_gui_get_widget(self, "main_site"));
- GtkTreeModel *model = GTK_TREE_MODEL(gtk_combo_box_get_model(combo));
- GtkTreeIter iter;
- if (gtk_tree_model_find_string(model, &iter, NULL, 1, site)) {
- g_signal_handlers_block_by_func(combo,
- G_CALLBACK(on_site_changed), self);
- gtk_combo_box_set_active_iter(combo, &iter);
- g_signal_handlers_unblock_by_func(combo,
- G_CALLBACK(on_site_changed), self);
- }
+ g_debug("AWeatherGui: update_time_widget - time=%u", (guint)time);
+ // FIXME
+ //GtkTreeView *tview = GTK_TREE_VIEW(aweather_gui_get_widget(self, "main_time"));
+ //GtkTreeModel *model = GTK_TREE_MODEL(gtk_tree_view_get_model(tview));
+ //GtkTreeIter iter;
+ //if (gtk_tree_model_find_string(model, &iter, NULL, 0, time)) {
+ // GtkTreePath *path = gtk_tree_model_get_path(model, &iter);
+ // gtk_tree_view_set_cursor(tview, path, NULL, FALSE);
+ // gtk_tree_path_free(path);
+ //}
}
/* Prefs callbacks */
-void on_offline(GtkToggleAction *action, AWeatherGui *self)
+G_MODULE_EXPORT void on_offline(GtkToggleAction *action, AWeatherGui *self)
{
gboolean value = gtk_toggle_action_get_active(action);
g_debug("AWeatherGui: on_offline - offline=%d", value);
- gis_prefs_set_boolean(self->prefs, "gis/offline", value);
- gis_viewer_set_offline(self->viewer, value);
+ grits_viewer_set_offline(self->viewer, value);
}
-void on_initial_site_changed(GtkComboBox *combo, AWeatherGui *self)
+G_MODULE_EXPORT int on_update_freq_changed(GtkSpinButton *spinner, AWeatherGui *self)
{
- gchar *site;
+ gint value = gtk_spin_button_get_value_as_int(spinner);
+ g_debug("AWeatherGui: on_update_freq_changed - %p, freq=%d", self, value);
+ grits_prefs_set_integer(self->prefs, "aweather/update_freq", value);
+ set_update_timeout(self);
+ return TRUE;
+}
+
+G_MODULE_EXPORT void on_initial_site_changed(GtkComboBox *combo, AWeatherGui *self)
+{
+ gchar *code;
GtkTreeIter iter;
GtkTreeModel *model = gtk_combo_box_get_model(combo);
gtk_combo_box_get_active_iter(combo, &iter);
- gtk_tree_model_get(model, &iter, 1, &site, -1);
- g_debug("AWeatherGui: on_initial_site_changed - site=%s", site);
- gis_prefs_set_string(self->prefs, "aweather/initial_site", site);
- g_free(site);
+ gtk_tree_model_get(model, &iter, 0, &code, -1);
+ g_debug("AWeatherGui: on_initial_site_changed - code=%s", code);
+ grits_prefs_set_string(self->prefs, "aweather/initial_site", code);
+ g_free(code);
}
-void on_nexrad_url_changed(GtkEntry *entry, AWeatherGui *self)
+G_MODULE_EXPORT void on_nexrad_url_changed(GtkEntry *entry, AWeatherGui *self)
{
const gchar *text = gtk_entry_get_text(entry);
g_debug("AWeatherGui: on_nexrad_url_changed - url=%s", text);
- gis_prefs_set_string(self->prefs, "aweather/nexrad_url", text);
+ grits_prefs_set_string(self->prefs, "aweather/nexrad_url", text);
}
-int on_log_level_changed(GtkSpinButton *spinner, AWeatherGui *self)
+G_MODULE_EXPORT int on_log_level_changed(GtkSpinButton *spinner, AWeatherGui *self)
{
gint value = gtk_spin_button_get_value_as_int(spinner);
g_debug("AWeatherGui: on_log_level_changed - %p, level=%d", self, value);
- gis_prefs_set_integer(self->prefs, "aweather/log_level", value);
+ grits_prefs_set_integer(self->prefs, "aweather/log_level", value);
return TRUE;
}
/*****************
* Setup helpers *
*****************/
+static void combo_sensitive(GtkCellLayout *cell_layout, GtkCellRenderer *cell,
+ GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
+{
+ gboolean sensitive = !gtk_tree_model_iter_has_child(tree_model, iter);
+ g_object_set(cell, "sensitive", sensitive, NULL);
+}
+static void site_setup(AWeatherGui *self)
+{
+ GtkTreeIter state, city;
+ GtkTreeStore *store = GTK_TREE_STORE(aweather_gui_get_object(self, "sites"));
+ gtk_tree_store_append(store, &state, NULL);
+ gtk_tree_store_set (store, &state, 0, "", 1, "None", -1);
+ for (int i = 0; cities[i].type; i++) {
+ if (cities[i].type == LOCATION_STATE) {
+ gtk_tree_store_append(store, &state, NULL);
+ gtk_tree_store_set (store, &state, 0, cities[i].code,
+ 1, cities[i].name, -1);
+ } else if (cities[i].type == LOCATION_CITY) {
+ gtk_tree_store_append(store, &city, &state);
+ gtk_tree_store_set (store, &city, 0, cities[i].code,
+ 1, cities[i].name, -1);
+ }
+ }
+
+ GtkWidget *combo = aweather_gui_get_widget(self, "prefs_general_site");
+ GObject *renderer = aweather_gui_get_object(self, "prefs_general_site_rend");
+ gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(combo),
+ GTK_CELL_RENDERER(renderer), combo_sensitive, NULL, NULL);
+}
+
+static void menu_setup(AWeatherGui *self)
+{
+ GtkWidget *menu = aweather_gui_get_widget(self, "main_menu_radar");
+ GtkWidget *states=NULL, *state=NULL, *sites=NULL, *site=NULL;
+
+ states = gtk_menu_new();
+ gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu), states);
+
+ for (int i = 0; cities[i].type; i++) {
+ if (cities[i].type == LOCATION_STATE) {
+ state = gtk_menu_item_new_with_label(cities[i].name);
+ sites = gtk_menu_new();
+ gtk_menu_shell_append(GTK_MENU_SHELL(states), state);
+ gtk_menu_item_set_submenu(GTK_MENU_ITEM(state), sites);
+ } else if (cities[i].type == LOCATION_CITY) {
+ site = gtk_menu_item_new_with_label(cities[i].name);
+ gtk_menu_shell_append(GTK_MENU_SHELL(sites), site);
+ g_object_set_data(G_OBJECT(site), "city", &cities[i]);
+ g_signal_connect(site, "activate", G_CALLBACK(on_radar_changed), self);
+ }
+ }
+}
+
static void prefs_setup(AWeatherGui *self)
{
/* Set values */
- gchar *nu = gis_prefs_get_string (self->prefs, "aweather/nexrad_url", NULL);
- gint ll = gis_prefs_get_integer(self->prefs, "aweather/log_level", NULL);
- gchar *is = gis_prefs_get_string (self->prefs, "aweather/initial_site", NULL);
+ gint uf = grits_prefs_get_integer(self->prefs, "aweather/update_freq", NULL);
+ gchar *nu = grits_prefs_get_string (self->prefs, "aweather/nexrad_url", NULL);
+ gint ll = grits_prefs_get_integer(self->prefs, "aweather/log_level", NULL);
+ gchar *is = grits_prefs_get_string (self->prefs, "aweather/initial_site", NULL);
+ GtkWidget *ufw = aweather_gui_get_widget(self, "prefs_general_freq");
GtkWidget *nuw = aweather_gui_get_widget(self, "prefs_general_url");
GtkWidget *llw = aweather_gui_get_widget(self, "prefs_general_log");
GtkWidget *isw = aweather_gui_get_widget(self, "prefs_general_site");
+ if (uf) gtk_spin_button_set_value(GTK_SPIN_BUTTON(ufw), uf);
if (nu) gtk_entry_set_text(GTK_ENTRY(nuw), nu), g_free(nu);
if (ll) gtk_spin_button_set_value(GTK_SPIN_BUTTON(llw), ll);
if (is) {
GtkTreeModel *model = gtk_combo_box_get_model(GTK_COMBO_BOX(isw));
GtkTreeIter iter;
- if (gtk_tree_model_find_string(model, &iter, NULL, 1, is))
+ if (gtk_tree_model_find_string(model, &iter, NULL, 0, is))
gtk_combo_box_set_active_iter(GTK_COMBO_BOX(isw), &iter);
+ g_free(is);
}
/* Load Plugins */
"Plugin", rend1, "text", 0, NULL);
GtkTreeViewColumn *col2 = gtk_tree_view_column_new_with_attributes(
"Enabled", rend2, "active", 1, NULL);
+ g_object_set(rend2, "xalign", 0.0, NULL);
gtk_tree_view_append_column(tview, col1);
gtk_tree_view_append_column(tview, col2);
g_signal_connect(rend2, "toggled", G_CALLBACK(on_plugin_toggled), self);
gtk_tree_view_set_model(GTK_TREE_VIEW(tview), GTK_TREE_MODEL(self->gtk_plugins));
}
-static void combo_sensitive(GtkCellLayout *cell_layout, GtkCellRenderer *cell,
- GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
-{
- gboolean sensitive = !gtk_tree_model_iter_has_child(tree_model, iter);
- g_object_set(cell, "sensitive", sensitive, NULL);
-}
-
-static void site_setup(AWeatherGui *self)
+static void time_setup(AWeatherGui *self)
{
- GtkTreeIter state, city;
- GtkTreeStore *store = GTK_TREE_STORE(aweather_gui_get_object(self, "sites"));
- for (int i = 0; cities[i].label; i++) {
- if (cities[i].type == LOCATION_STATE) {
- gtk_tree_store_append(store, &state, NULL);
- gtk_tree_store_set (store, &state, 0, cities[i].label,
- 1, cities[i].code, -1);
- } else {
- gtk_tree_store_append(store, &city, &state);
- gtk_tree_store_set (store, &city, 0, cities[i].label,
- 1, cities[i].code, -1);
+ /* Add times */
+ GtkTreeStore *store = GTK_TREE_STORE(aweather_gui_get_object(self, "times"));
+ for (int hour = 0; hour < 24; hour++) {
+ GtkTreeIter hour_iter;
+ gchar *str = g_strdup_printf("%02d:00Z", hour);
+ gtk_tree_store_append(store, &hour_iter, NULL);
+ gtk_tree_store_set(store, &hour_iter, 0, str, 1, hour, 2, 0, -1);
+ g_free(str);
+ for (int min = 5; min < 60; min += 5) {
+ GtkTreeIter min_iter;
+ gchar *str = g_strdup_printf("%02d:%02dZ", hour, min);
+ gtk_tree_store_append(store, &min_iter, &hour_iter);
+ gtk_tree_store_set(store, &min_iter, 0, str, 1, hour, 2, min, -1);
+ g_free(str);
}
}
- GtkWidget *combo = aweather_gui_get_widget(self, "main_site");
- GObject *renderer = aweather_gui_get_object(self, "main_site_rend");
- gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(combo),
- GTK_CELL_RENDERER(renderer), combo_sensitive, NULL, NULL);
-}
-
-static void time_setup(AWeatherGui *self)
-{
- GtkTreeView *tview = GTK_TREE_VIEW(aweather_gui_get_widget(self, "main_time"));
- GtkCellRenderer *rend = gtk_cell_renderer_text_new();
- GtkTreeViewColumn *col = gtk_tree_view_column_new_with_attributes(
- "Time", rend, "text", 0, NULL);
-
- gtk_tree_view_append_column(tview, col);
- g_object_set(rend, "size-points", 8.0, NULL);
-
+ /* Connect signals */
+ GtkWidget *cal = aweather_gui_get_widget(self, "main_date_cal");
+ GtkWidget *view = aweather_gui_get_widget(self, "main_time");
+ g_signal_connect_swapped(cal, "day-selected-double-click",
+ G_CALLBACK(on_time_changed), self);
+ g_signal_connect_swapped(view, "row-activated",
+ G_CALLBACK(on_time_changed), self);
g_signal_connect(self->viewer, "time-changed",
G_CALLBACK(update_time_widget), self);
}
-
-/*************************
- * Deprecated site stuff *
- *************************/
-/* TODO: These update times functions are getting ugly... */
-static void update_times_gtk(AWeatherGui *self, GList *times)
+static void icons_setup(AWeatherGui *self)
{
- gchar *last_time = NULL;
- GRegex *regex = g_regex_new("^[A-Z]{4}_([0-9]{8}_[0-9]{4})$", 0, 0, NULL); // KLSX_20090622_2113
- GMatchInfo *info;
-
- GtkTreeView *tview = GTK_TREE_VIEW(aweather_gui_get_widget(self, "main_time"));
- GtkListStore *lstore = GTK_LIST_STORE(gtk_tree_view_get_model(tview));
- gtk_list_store_clear(lstore);
- GtkTreeIter iter;
- times = g_list_reverse(times);
- for (GList *cur = times; cur; cur = cur->next) {
- if (g_regex_match(regex, cur->data, 0, &info)) {
- gchar *time = g_match_info_fetch(info, 1);
- gtk_list_store_insert(lstore, &iter, 0);
- gtk_list_store_set(lstore, &iter, 0, time, -1);
- last_time = time;
- }
+ gchar *icons[] = {
+ ICONDIR "/hicolor/16x16/apps/aweather.png",
+ ICONDIR "/hicolor/22x22/apps/aweather.png",
+ ICONDIR "/hicolor/24x24/apps/aweather.png",
+ ICONDIR "/hicolor/32x32/apps/aweather.png",
+ ICONDIR "/hicolor/48x48/apps/aweather.png",
+ ICONDIR "/hicolor/scalable/apps/aweather.svg",
+ };
+ GList *list = NULL;
+ for (int i = 0; i < G_N_ELEMENTS(icons); i++) {
+ GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(icons[i], NULL);
+ if (!pixbuf)
+ g_warning("AWeatherGui: icons_setup - %s failed", icons[i]);
+ list = g_list_prepend(list, pixbuf);
}
-
- gis_viewer_set_time(self->viewer, last_time);
-
- g_regex_unref(regex);
- g_list_foreach(times, (GFunc)g_free, NULL);
- g_list_free(times);
-}
-static void update_times_online_cb(char *path, gboolean updated, gpointer _self)
-{
- GList *times = NULL;
- gchar *data;
- gsize length;
- g_file_get_contents(path, &data, &length, NULL);
- gchar **lines = g_strsplit(data, "\n", -1);
- for (int i = 0; lines[i] && lines[i][0]; i++) {
- char **parts = g_strsplit(lines[i], " ", 2);
- times = g_list_prepend(times, g_strdup(parts[1]));
- g_strfreev(parts);
- }
- g_strfreev(lines);
- g_free(data);
-
- update_times_gtk(_self, times);
-}
-static void update_times(AWeatherGui *self, GisViewer *viewer, char *site)
-{
- g_debug("AWeatherGui: update_times - site=%s", site);
- if (gis_viewer_get_offline(self->viewer)) {
- GList *times = NULL;
- gchar *path = g_build_filename(g_get_user_cache_dir(),
- "libgis", "nexrd2", "raw", site, NULL);
- GDir *dir = g_dir_open(path, 0, NULL);
- if (dir) {
- const gchar *name;
- while ((name = g_dir_read_name(dir))) {
- times = g_list_prepend(times, g_strdup(name));
- }
- g_dir_close(dir);
- }
- g_free(path);
- update_times_gtk(self, times);
- } else {
- gchar *path = g_strdup_printf("nexrd2/raw/%s/dir.list", site);
- char *base = gis_prefs_get_string(self->prefs, "aweather/nexrad_url", NULL);
- cache_file(base, path, GIS_REFRESH, NULL, update_times_online_cb, self);
- /* update_times_gtk from update_times_online_cb */
- }
-}
-/* FIXME: This is redundent with the radar plugin
- * Make a shared wsr88d file? */
-static void on_gis_location_changed(GisViewer *viewer,
- gdouble lat, gdouble lon, gdouble elev,
- gpointer _self)
-{
- AWeatherGui *self = AWEATHER_GUI(_self);
- gdouble min_dist = EARTH_R / 5;
- city_t *city, *min_city = NULL;
- for (city = cities; city->type; city++) {
- if (city->type != LOCATION_CITY)
- continue;
- gdouble city_loc[3] = {};
- gdouble eye_loc[3] = {lat, lon, elev};
- lle2xyz(city->lat, city->lon, city->elev,
- &city_loc[0], &city_loc[1], &city_loc[2]);
- lle2xyz(lat, lon, elev,
- &eye_loc[0], &eye_loc[1], &eye_loc[2]);
- gdouble dist = distd(city_loc, eye_loc);
- if (dist < min_dist) {
- min_dist = dist;
- min_city = city;
- }
- }
- static city_t *last_city = NULL;
- if (min_city && min_city != last_city) {
- update_site_widget(self->viewer, min_city->code, self);
- update_times(self, viewer, min_city->code);
- }
- last_city = min_city;
-}
-static void on_gis_refresh(GisViewer *viewer, gpointer _self)
-{
- AWeatherGui *self = AWEATHER_GUI(_self);
- gdouble lat, lon, elev;
- gis_viewer_get_location(self->viewer, &lat, &lon, &elev);
- /* Hack to update times */
- on_gis_location_changed(self->viewer, lat, lon, elev, self);
+ gtk_window_set_default_icon_list(list);
+ g_list_free(list);
}
g_debug("AWeatherGui: new");
return g_object_new(AWEATHER_TYPE_GUI, NULL);
}
-GisViewer *aweather_gui_get_viewer(AWeatherGui *self)
+GritsViewer *aweather_gui_get_viewer(AWeatherGui *self)
{
g_assert(AWEATHER_IS_GUI(self));
return self->viewer;
}
void aweather_gui_attach_plugin(AWeatherGui *self, const gchar *name)
{
- GisPlugin *plugin = gis_plugins_load(self->plugins, name,
+ GritsPlugin *plugin = grits_plugins_enable(self->plugins, name,
self->viewer, self->prefs);
- GtkWidget *body = gis_plugin_get_config(plugin);
+ GtkWidget *body = grits_plugin_get_config(plugin);
if (body) {
GtkWidget *config = aweather_gui_get_widget(self, "main_tabs");
GtkWidget *tab = gtk_label_new(name);
if (tab_name && g_str_equal(name, tab_name))
gtk_notebook_remove_page(GTK_NOTEBOOK(config), i);
}
- gis_plugins_unload(self->plugins, name);
+ grits_plugins_disable(self->plugins, name);
gtk_widget_queue_draw(GTK_WIDGET(self->viewer));
}
+void aweather_gui_load_plugins(AWeatherGui *self)
+{
+ g_debug("AWeatherGui: load_plugins");
+ GtkTreeIter iter;
+ for (GList *cur = grits_plugins_available(self->plugins); cur; cur = cur->next) {
+ gchar *name = cur->data;
+ GError *error = NULL;
+ gboolean enabled = grits_prefs_get_boolean_v(self->prefs, "plugins", cur->data, &error);
+ if (error && error->code == G_KEY_FILE_ERROR_GROUP_NOT_FOUND)
+ enabled = TRUE;
+ gtk_list_store_append(self->gtk_plugins, &iter);
+ gtk_list_store_set(self->gtk_plugins, &iter, 0, name, 1, enabled, -1);
+ if (enabled)
+ aweather_gui_attach_plugin(self, name);
+ }
+}
+
/****************
* GObject code *
****************/
-G_DEFINE_TYPE(AWeatherGui, aweather_gui, GTK_TYPE_WINDOW);
-static void aweather_gui_init(AWeatherGui *self)
+static void aweather_gui_parser_finished(GtkBuildable *_self, GtkBuilder *builder)
{
- g_debug("AWeatherGui: init");
+ g_debug("AWeatherGui: parser finished");
+ AWeatherGui *self = AWEATHER_GUI(_self);
+ self->builder = builder;
/* Simple things */
gchar *config = g_build_filename(g_get_user_config_dir(), PACKAGE, "config.ini", NULL);
gchar *defaults = g_build_filename(PKGDATADIR, "defaults.ini", NULL);
- self->prefs = gis_prefs_new(config, defaults);
- self->plugins = gis_plugins_new(PLUGINSDIR);
- self->viewer = gis_opengl_new(self->plugins);
+ self->prefs = grits_prefs_new(config, defaults);
+ self->plugins = grits_plugins_new(PLUGINSDIR, self->prefs);
+ self->viewer = GRITS_VIEWER(aweather_gui_get_widget(self, "main_viewer"));
+ self->gtk_plugins = GTK_LIST_STORE(aweather_gui_get_object(self, "plugins"));
+ grits_viewer_setup(self->viewer, self->plugins, self->prefs);
g_free(config);
g_free(defaults);
- /* Setup window */
- self->builder = gtk_builder_new();
- GError *error = NULL;
- if (!gtk_builder_add_from_file(self->builder, PKGDATADIR "/main.ui", &error))
- g_error("Failed to create gtk builder: %s", error->message);
- gtk_widget_reparent(aweather_gui_get_widget(self, "main_body"), GTK_WIDGET(self));
- GtkWidget *paned = aweather_gui_get_widget(self, "main_paned");
- gtk_widget_destroy(gtk_paned_get_child1(GTK_PANED(paned)));
- gtk_paned_pack1(GTK_PANED(paned), GTK_WIDGET(self->viewer), TRUE, FALSE);
- gtk_widget_show_all(GTK_WIDGET(self));
-
- /* Plugins */
- GtkTreeIter iter;
- self->gtk_plugins = GTK_LIST_STORE(aweather_gui_get_object(self, "plugins"));
- for (GList *cur = gis_plugins_available(self->plugins); cur; cur = cur->next) {
- gchar *name = cur->data;
- GError *error = NULL;
- gboolean enabled = gis_prefs_get_boolean_v(self->prefs, cur->data, "enabled", &error);
- if (error && error->code == G_KEY_FILE_ERROR_GROUP_NOT_FOUND)
- enabled = TRUE;
- gtk_list_store_append(self->gtk_plugins, &iter);
- gtk_list_store_set(self->gtk_plugins, &iter, 0, name, 1, enabled, -1);
- if (enabled)
- aweather_gui_attach_plugin(self, name);
- }
-
/* Misc, helpers */
site_setup(self);
+ menu_setup(self);
time_setup(self);
prefs_setup(self);
+ icons_setup(self);
/* Connect signals */
gtk_builder_connect_signals(self->builder, self);
g_signal_connect_swapped(self->viewer, "offline",
G_CALLBACK(gtk_toggle_action_set_active),
aweather_gui_get_object(self, "offline"));
-
- /* deprecated site stuff */
- g_signal_connect(self->viewer, "location-changed",
- G_CALLBACK(on_gis_location_changed), self);
- g_signal_connect(self->viewer, "refresh",
- G_CALLBACK(on_gis_refresh), self);
+ g_signal_connect_swapped(self->viewer, "refresh",
+ G_CALLBACK(set_update_timeout), self);
+ g_signal_connect_swapped(self->viewer, "realize",
+ G_CALLBACK(aweather_gui_load_plugins), self);
+}
+static void aweather_gui_buildable_init(GtkBuildableIface *iface)
+{
+ iface->parser_finished = aweather_gui_parser_finished;
+}
+G_DEFINE_TYPE_WITH_CODE(AWeatherGui, aweather_gui, GTK_TYPE_WINDOW,
+ G_IMPLEMENT_INTERFACE(GTK_TYPE_BUILDABLE,
+ aweather_gui_buildable_init));
+static void aweather_gui_init(AWeatherGui *self)
+{
+ g_debug("AWeatherGui: init");
+ /* Do all the real work in parser_finished */
}
static GObject *aweather_gui_constructor(GType gtype, guint n_properties,
GObjectConstructParam *properties)
{
- g_debug("aweather_gui: constructor");
+ g_debug("AWeatherGui: constructor");
GObjectClass *parent_class = G_OBJECT_CLASS(aweather_gui_parent_class);
return parent_class->constructor(gtype, n_properties, properties);
}
{
g_debug("AWeatherGui: dispose");
AWeatherGui *self = AWEATHER_GUI(_self);
+ if (self->plugins) {
+ grits_plugins_free(self->plugins);
+ self->plugins = NULL;
+ }
if (self->builder) {
- /* Reparent to avoid double unrefs */
- GtkWidget *body = aweather_gui_get_widget(self, "main_body");
- GtkWidget *window = aweather_gui_get_widget(self, "main_window");
- gtk_widget_reparent(body, window);
g_object_unref(self->builder);
self->builder = NULL;
}
- if (self->viewer) {
- g_object_unref(self->viewer);
- self->viewer = NULL;
- }
- if (self->plugins) {
- gis_plugins_free(self->plugins);
- self->plugins = NULL;
- }
if (self->prefs) {
g_object_unref(self->prefs);
self->prefs = NULL;
{
g_debug("AWeatherGui: finalize");
G_OBJECT_CLASS(aweather_gui_parent_class)->finalize(_self);
- gtk_main_quit();
}
static void aweather_gui_class_init(AWeatherGuiClass *klass)