]> Pileus Git - aweather/blob - src/main.c
Fix memory leaks
[aweather] / src / main.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 #define _XOPEN_SOURCE
19 #include <sys/time.h>
20 #include <config.h>
21 #include <gtk/gtk.h>
22 #include <glib/gstdio.h>
23
24 #include <grits.h>
25
26 #include "aweather-gui.h"
27 #include "aweather-location.h"
28
29 static gint log_levels = 0;
30
31 static int int2log(int level) {
32         level = G_LOG_LEVEL_ERROR << level;
33         level = (level<<1) - 1;
34         level = level & G_LOG_LEVEL_MASK;
35         return level;
36 }
37
38 static void log_func(const gchar *log_domain, GLogLevelFlags log_level,
39               const gchar *message, gpointer udata)
40 {
41         if (log_level & log_levels) {
42                 if (log_level == G_LOG_LEVEL_DEBUG)
43                         g_fprintf(stderr, "DEBUG: (%p) %s\n",
44                                         g_thread_self(), message);
45                 else
46                         g_log_default_handler(log_domain, log_level, message, udata);
47         }
48 }
49
50 static void on_log_level_changed(GtkSpinButton *spinner, AWeatherGui *self)
51 {
52         g_message("main: log_level_changed");
53         log_levels = int2log(gtk_spin_button_get_value_as_int(spinner));
54 }
55
56 static void set_location_time(AWeatherGui *gui, char *site, char *time)
57 {
58         /* Set time
59          *   Do this before setting setting location
60          *   so that it doesn't refresh twice */
61         struct tm tm = {};
62         strptime(time, "%Y-%m-%d %H:%M", &tm);
63         time_t sec = mktime(&tm);
64         if (sec > 0)
65                 grits_viewer_set_time(gui->viewer, sec);
66         g_debug("date = [%s] == %lu\n", time, sec);
67
68         /* Set location */
69         for (city_t *city = cities; city->type; city++) {
70                 if (city->type == LOCATION_CITY && g_str_equal(city->code, site)) {
71                         grits_viewer_set_location(gui->viewer,
72                                 city->pos.lat, city->pos.lon, EARTH_R/35);
73                         break;
74                 }
75         }
76 }
77
78 static void set_toggle_action(AWeatherGui *gui, const char *action, gboolean enabled)
79 {
80         GObject *object = aweather_gui_get_object(gui, action);
81         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(object), enabled);
82 }
83
84 /********
85  * Main *
86  ********/
87 int main(int argc, char *argv[])
88 {
89         /* Defaults */
90         gint     debug      = 2; // G_LOG_LEVEL_WARNING
91         gchar   *site       = "";
92         gchar   *time       = "";
93         gboolean autoupdate = FALSE;
94         gboolean offline    = FALSE;
95         gboolean fullscreen = FALSE;
96
97         /* Arguments */
98         gint     opt_debug      = -1;
99         gchar   *opt_site       = NULL;
100         gchar   *opt_time       = NULL;
101         gboolean opt_offline    = FALSE;
102         gboolean opt_autoupdate = FALSE;
103         gboolean opt_fullscreen = FALSE;
104         GOptionEntry entries[] = {
105                 //long         short flg type                 location         description                 arg desc
106                 {"debug",      'd',  0,  G_OPTION_ARG_INT,    &opt_debug,      "Change default log level", "[0-5]"},
107                 {"site",       's',  0,  G_OPTION_ARG_STRING, &opt_site,       "Set initial site",         "SITE"},
108                 {"time",       't',  0,  G_OPTION_ARG_STRING, &opt_time,       "Set initial date/time",    "DATE"},
109                 {"offline",    'o',  0,  G_OPTION_ARG_NONE,   &opt_offline,    "Run in offline mode",      NULL},
110                 {"autoupdate", 'a',  0,  G_OPTION_ARG_NONE,   &opt_autoupdate, "Auto update radar",        NULL},
111                 {"fullscreen", 'f',  0,  G_OPTION_ARG_NONE,   &opt_fullscreen, "Open in fullscreen mode",  NULL},
112                 {NULL}
113         };
114
115         /* Init */
116         GError *error = NULL;
117         g_thread_init(NULL);
118         gdk_threads_init();
119         if (!gtk_init_with_args(&argc, &argv, "aweather", entries, NULL, &error)) {
120                 g_print("%s\n", error->message);
121                 g_error_free(error);
122                 return -1;
123         }
124
125         /* Setup debug level for aweather_gui_new */
126         g_log_set_handler(NULL, G_LOG_LEVEL_MASK, log_func, NULL);
127         log_levels = int2log(opt_debug >= 0 ? opt_debug : debug);
128
129         /* Set up AWeather */
130         gdk_threads_enter();
131         /* Pre-load some types for gtkbuilder */
132         GRITS_TYPE_OPENGL;
133         AWEATHER_TYPE_GUI;
134         GtkBuilder *builder = gtk_builder_new();
135         if (!gtk_builder_add_from_file(builder, PKGDATADIR "/main.ui", &error))
136                 g_error("Failed to create gtk builder: %s", error->message);
137         AWeatherGui *gui = AWEATHER_GUI(gtk_builder_get_object(builder, "main_window"));
138         g_signal_connect(gui, "destroy", gtk_main_quit, NULL);
139         GObject *action = aweather_gui_get_object(gui, "prefs_general_log");
140         g_signal_connect(action, "changed", G_CALLBACK(on_log_level_changed), NULL);
141
142         /* Finish setting up options */
143         GError *err = NULL;
144         gint     prefs_debug      = grits_prefs_get_integer(gui->prefs, "aweather/log_level",    &err);
145         gchar   *prefs_site       = grits_prefs_get_string(gui->prefs,  "aweather/initial_site", NULL);
146         gboolean prefs_offline    = grits_prefs_get_boolean(gui->prefs, "grits/offline",         NULL);
147         gint     prefs_autoupdate = grits_prefs_get_boolean(gui->prefs, "aweather/update_enab",  NULL);
148
149         debug      = (opt_debug >= 0 ? opt_debug   :
150                       err == NULL    ? prefs_debug : debug);
151         site       = (opt_site       ?: prefs_site       ?: site);
152         time       = (opt_time       ?:                     time);
153         offline    = (opt_offline    ?: prefs_offline    ?: offline);
154         autoupdate = (opt_autoupdate ?: prefs_autoupdate ?: autoupdate);
155         fullscreen = (opt_fullscreen ?:                     fullscreen);
156
157         log_levels = int2log(debug);
158         set_location_time(gui, site, time);
159         grits_viewer_set_offline(gui->viewer, offline);
160         set_toggle_action(gui, "update",     autoupdate);
161         set_toggle_action(gui, "fullscreen", fullscreen);
162         g_free(prefs_site);
163
164         /* Done with init, show gui */
165         gtk_widget_show_all(GTK_WIDGET(gui));
166         set_toggle_action(gui, "fullscreen", fullscreen); // Resest widget hiding
167         gtk_main();
168         gdk_threads_leave();
169         gdk_display_close(gdk_display_get_default());
170         return 0;
171 }