X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=src%2Faweather-gui.c;h=c0cbf0d12a2d133e84b6ea66dee6b2c774b86b3c;hb=c66778bac36f8b1d827bd907307a084533427b33;hp=f93a2ea8c6f0ee9d864c24e312681f3ab4c08985;hpb=5761f0ec012080b2f12c5e29a93a97b9a7ede4b4;p=aweather diff --git a/src/aweather-gui.c b/src/aweather-gui.c index f93a2ea..c0cbf0d 100644 --- a/src/aweather-gui.c +++ b/src/aweather-gui.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -49,6 +50,50 @@ G_MODULE_EXPORT gboolean on_gui_key_press(GtkWidget *widget, GdkEventKey *event, return FALSE; } +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 libgis */ + gchar *cache = g_build_filename(g_get_user_cache_dir(), "libgis", NULL); + cleancache_r(cache); +} + +G_MODULE_EXPORT void on_contents(GtkMenuItem *menu, AWeatherGui *self) +{ + GError *err = NULL; + gchar *path = g_strdup(HTMLDIR "/aweather.html"); + 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); +} + G_MODULE_EXPORT void on_quit(GtkMenuItem *menu, AWeatherGui *self) { gtk_widget_destroy(GTK_WIDGET(self)); @@ -90,7 +135,7 @@ G_MODULE_EXPORT void on_plugin_toggled(GtkCellRendererToggle *cell, gchar *path_ G_MODULE_EXPORT void on_time_changed(AWeatherGui *self) { g_debug("AWeatherGui: on_time_changed"); - struct tm tm = {}; + struct tm tm = {.tm_isdst = -1}; GtkWidget *cal = aweather_gui_get_widget(self, "main_date_cal"); gtk_calendar_get_date(GTK_CALENDAR(cal), (guint*)&tm.tm_year, @@ -194,6 +239,8 @@ 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); @@ -247,28 +294,26 @@ static void prefs_setup(AWeatherGui *self) static void time_setup(AWeatherGui *self) { - /* Setup UI */ - GtkCalendar *cal = GTK_CALENDAR(aweather_gui_get_widget(self, "main_date_cal")); - GtkTreeView *view = 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(view, col); - g_object_set(rend, "size-points", 8.0, NULL); - /* Add times */ - GtkListStore *store = GTK_LIST_STORE(aweather_gui_get_object(self, "times")); + GtkTreeStore *store = GTK_TREE_STORE(aweather_gui_get_object(self, "times")); for (int hour = 0; hour < 24; hour++) { - for (int min = 0; min < 60; min += 10) { - GtkTreeIter iter; - gchar *str = g_strdup_printf("%02d:%02d", hour, min); - gtk_list_store_append(store, &iter); - gtk_list_store_set(store, &iter, 0, str, 1, hour, 2, min, -1); + 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); } } /* 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", @@ -347,32 +392,22 @@ void aweather_gui_deattach_plugin(AWeatherGui *self, const gchar *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->prefs); - self->viewer = gis_opengl_new(self->plugins, self->prefs); + self->viewer = GIS_VIEWER(aweather_gui_get_widget(self, "main_viewer")); + gis_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_set_size_request(GTK_WIDGET(self->viewer), 600, 400); - gtk_widget_show_all(GTK_WIDGET(self)); - /* Plugins */ GtkTreeIter iter; self->gtk_plugins = GTK_LIST_STORE(aweather_gui_get_object(self, "plugins")); @@ -401,10 +436,22 @@ static void aweather_gui_init(AWeatherGui *self) G_CALLBACK(gtk_toggle_action_set_active), aweather_gui_get_object(self, "offline")); } +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); } @@ -430,7 +477,6 @@ static void aweather_gui_finalize(GObject *_self) { g_debug("AWeatherGui: finalize"); G_OBJECT_CLASS(aweather_gui_parent_class)->finalize(_self); - gtk_main_quit(); } static void aweather_gui_class_init(AWeatherGuiClass *klass)