Don't spam the last message if the same.
[dhcpcd-ui] / src / dhcpcd-gtk / main.c
1 /*
2  * dhcpcd-gtk
3  * Copyright 2009-2010 Roy Marples <roy@marples.name>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <locale.h>
28 #include <poll.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #ifdef NOTIFY
33 #  include <libnotify/notify.h>
34 static NotifyNotification *nn;
35 #endif
36
37 #include "config.h"
38 #include "dhcpcd-gtk.h"
39
40 static GtkStatusIcon *status_icon;
41 static int ani_timer;
42 static int ani_counter;
43 static bool online;
44 static bool carrier;
45
46 struct watch {
47         struct pollfd pollfd;
48         int eventid;
49         GIOChannel *gio;
50         struct watch *next;
51 };
52 static struct watch *watches;
53
54 WI_SCAN *wi_scans;
55
56 WI_SCAN *
57 wi_scan_find(DHCPCD_WI_SCAN *scan)
58 {
59         WI_SCAN *w;
60         DHCPCD_WI_SCAN *dw;
61
62         for (w = wi_scans; w; w = w->next) {
63                 for (dw = w->scans; dw; dw = dw->next)
64                         if (dw == scan)
65                                 break;
66                 if (dw)
67                         return w;
68         }
69         return NULL;
70 }
71
72 static gboolean
73 animate_carrier(_unused gpointer data)
74 {
75         const char *icon;
76         
77         if (ani_timer == 0)
78                 return false;
79
80         switch(ani_counter++) {
81         case 0:
82                 icon = "network-transmit";
83                 break;
84         case 1:
85                 icon = "network-receive";
86                 break;
87         default:
88                 icon = "network-idle";
89                 ani_counter = 0;
90                 break;
91         }
92         gtk_status_icon_set_from_icon_name(status_icon, icon);
93         return true;
94 }
95
96 static gboolean
97 animate_online(_unused gpointer data)
98 {
99         const char *icon;
100         
101         if (ani_timer == 0)
102                 return false;
103
104         if (ani_counter++ > 6) {
105                 ani_timer = 0;
106                 ani_counter = 0;
107                 return false;
108         }
109
110         if (ani_counter % 2 == 0)
111                 icon = "network-idle";
112         else
113                 icon = "network-transmit-receive";
114         gtk_status_icon_set_from_icon_name(status_icon, icon);
115         return true;
116 }
117
118 static void
119 update_online(DHCPCD_CONNECTION *con, bool showif)
120 {
121         bool ison, iscarrier;
122         char *msg, *msgs, *tmp;
123         DHCPCD_IF *ifs, *i;
124
125         ison = iscarrier = false;
126         msgs = NULL;
127         ifs = dhcpcd_interfaces(con);
128         for (i = ifs; i; i = i->next) {
129                 if (showif)
130                         g_message("%s: %s", i->ifname, i->reason);
131                 if (strcmp(i->reason, "RELEASE") == 0 ||
132                     strcmp(i->reason, "STOP") == 0)
133                         continue;
134                 if (dhcpcd_if_up(i))
135                         ison = iscarrier = true;
136                 if (!iscarrier && g_strcmp0(i->reason, "CARRIER") == 0)
137                         iscarrier = true;
138                 msg = dhcpcd_if_message(i);
139                 if (msgs) {
140                         tmp = g_strconcat(msgs, "\n", msg, NULL);
141                         g_free(msgs);
142                         g_free(msg);
143                         msgs = tmp;
144                 } else
145                         msgs = msg;
146         }
147
148         if (online != ison || carrier != iscarrier) {
149                 online = ison;
150                 if (ani_timer != 0) {
151                         g_source_remove(ani_timer);
152                         ani_timer = 0;
153                         ani_counter = 0;
154                 }
155                 if (ison) {
156                         animate_online(NULL);
157                         ani_timer = g_timeout_add(300, animate_online, NULL);
158                 } else if (iscarrier) {
159                         animate_carrier(NULL);
160                         ani_timer = g_timeout_add(500, animate_carrier, NULL);
161                 } else {
162                         gtk_status_icon_set_from_icon_name(status_icon,
163                             "network-offline");
164                 }
165         }
166         gtk_status_icon_set_tooltip(status_icon, msgs);
167         g_free(msgs);
168 }
169
170 void
171 notify_close(void)
172 {
173 #ifdef NOTIFY
174         if (nn != NULL)
175                 notify_notification_close(nn, NULL);
176 #endif
177 }
178
179 #ifdef NOTIFY
180 static char *notify_last_msg;
181
182 static void
183 notify_closed(void)
184 {
185         nn = NULL;
186 }
187
188 static void
189 notify(const char *title, const char *msg, const char *icon)
190 {
191         char **msgs, **m;
192
193         /* Don't spam the same message */
194         if (notify_last_msg) {
195                 if (strcmp(msg, notify_last_msg) == 0)
196                         return;
197                 g_free(notify_last_msg);
198         }
199         notify_last_msg = g_strdup(msg);
200
201         msgs = g_strsplit(msg, "\n", 0);
202         for (m = msgs; *m; m++)
203                 g_message("%s", *m);
204         g_strfreev(msgs);
205         if (nn != NULL)
206                 notify_notification_close(nn, NULL);
207         if (gtk_status_icon_get_visible(status_icon))
208                 nn = notify_notification_new_with_status_icon(title,
209                     msg, icon, status_icon);
210         else
211                 nn = notify_notification_new(title, msg, icon, NULL);
212         notify_notification_set_timeout(nn, 5000);
213         g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
214         notify_notification_show(nn, NULL);
215 }
216 #else
217 #  define notify(a, b, c)
218 #endif
219
220 static void
221 event_cb(DHCPCD_CONNECTION *con, DHCPCD_IF *i, _unused void *data)
222 {
223         char *msg;
224         const char *icon;
225
226         g_message("%s: %s", i->ifname, i->reason);
227         update_online(con, false);
228         
229         /* We should ignore renew and stop so we don't annoy the user */
230         if (g_strcmp0(i->reason, "RENEW") == 0 ||
231             g_strcmp0(i->reason, "STOP") == 0)
232                 return;
233
234         msg = dhcpcd_if_message(i);
235         if (dhcpcd_if_up(i))
236                 icon = "network-transmit-receive";
237         else
238                 icon = "network-transmit";
239         if (dhcpcd_if_down(i))
240                 icon = "network-offline";
241         notify(_("Network event"), msg, icon);
242         g_free(msg);
243 }
244
245 static void
246 status_cb(DHCPCD_CONNECTION *con, const char *status, _unused void *data)
247 {
248         static char *last = NULL;
249         char *version;
250         const char *msg;
251         bool refresh;
252         WI_SCAN *w;
253
254         g_message("Status changed to %s", status);
255         if (g_strcmp0(status, "down") == 0) {
256                 msg = N_(last ?
257                     "Connection to dhcpcd lost" : "dhcpcd not running");
258                 gtk_status_icon_set_tooltip(status_icon, msg);
259                 notify(_("No network"), msg, "network-offline");
260                 dhcpcd_prefs_abort();
261                 while (wi_scans) {
262                         w = wi_scans->next;
263                         dhcpcd_wi_scans_free(wi_scans->scans);
264                         g_free(wi_scans);
265                         wi_scans = w;
266                 }
267         } else {
268                 if ((last == NULL || g_strcmp0(last, "down") == 0) &&
269                     dhcpcd_command(con, "GetDhcpcdVersion", NULL, &version))
270                 {
271                         g_message(_("Connected to %s-%s"), "dhcpcd", version);
272                         g_free(version);
273                         refresh = true;
274                 } else
275                         refresh = false;
276                 update_online(con, refresh);
277         }
278         last = g_strdup(status);
279 }
280
281 static void
282 scan_cb(DHCPCD_CONNECTION *con, DHCPCD_IF *i, _unused void *data)
283 {
284         WI_SCAN *w;
285         DHCPCD_WI_SCAN *scans, *s1, *s2;
286         char *txt, *t;
287         const char *msg;
288
289         g_message(_("%s: Received scan results"), i->ifname);
290         scans = dhcpcd_wi_scans(con, i);
291         if (scans == NULL && dhcpcd_error(con) != NULL) {
292                 g_warning("%s: %s", i->ifname, dhcpcd_error(con));
293                 dhcpcd_error_clear(con);
294         }
295         for (w = wi_scans; w; w = w->next)
296                 if (w->connection == con && w->interface == i)
297                         break;
298         if (w == NULL) {
299                 w = g_malloc(sizeof(*w));
300                 w->connection = con;
301                 w->interface = i;
302                 w->next = wi_scans;
303                 wi_scans = w;
304         } else {
305                 txt = NULL;
306                 msg = N_("New Access Point");
307                 for (s1 = scans; s1; s1 = s1->next) {
308                         for (s2 = w->scans; s2; s2 = s2->next)
309                                 if (g_strcmp0(s1->ssid, s2->ssid) == 0)
310                                         break;
311                         if (s2 == NULL) {
312                                 if (txt == NULL)
313                                         txt = g_strdup(s1->ssid);
314                                 else {
315                                         msg = N_("New Access Points");
316                                         t = g_strconcat(txt, "\n",
317                                             s1->ssid, NULL);
318                                         g_free(txt);
319                                         txt = t;
320                                 }
321                         }
322                 }
323                 if (txt) {
324                         notify(msg, txt, "network-wireless");
325                         g_free(txt);
326                 }
327                 dhcpcd_wi_scans_free(w->scans);
328         }
329         w->scans = scans;
330 }
331
332 static gboolean
333 gio_callback(GIOChannel *gio, _unused GIOCondition c, _unused gpointer d)
334 {
335         int fd;
336
337         fd = g_io_channel_unix_get_fd(gio);
338         dhcpcd_dispatch(fd);
339         return true;
340 }
341
342 static void
343 delete_watch_cb(_unused DHCPCD_CONNECTION *con, const struct pollfd *fd,
344     _unused void *data)
345 {
346         struct watch *w, *l;
347
348         l = NULL;
349         for (w = watches; w; w = w->next) {
350                 if (w->pollfd.fd == fd->fd) {
351                         if (l == NULL)
352                                 watches = w->next;
353                         else
354                                 l->next = w->next;
355                         g_source_remove(w->eventid);
356                         g_io_channel_unref(w->gio);
357                         g_free(w);
358                         break;
359                 }
360         }
361 }
362
363 static void
364 add_watch_cb(DHCPCD_CONNECTION *con, const struct pollfd *fd,
365     _unused void *data)
366 {
367         struct watch *w;
368         GIOChannel *gio;
369         int flags, eventid;
370
371         /* Remove any existing watch */
372         delete_watch_cb(con, fd, data);
373         
374         gio = g_io_channel_unix_new(fd->fd);
375         if (gio == NULL) {
376                 g_error(_("Error creating new GIO Channel\n"));
377                 return;
378         }
379         flags = 0;
380         if (fd->events & POLLIN)
381                 flags |= G_IO_IN;
382         if (fd->events & POLLOUT)
383                 flags |= G_IO_OUT;
384         if (fd->events & POLLERR)
385                 flags |= G_IO_ERR;
386         if (fd->events & POLLHUP)
387                 flags |= G_IO_HUP;
388         if ((eventid = g_io_add_watch(gio, flags, gio_callback, con)) == 0) {
389                 g_io_channel_unref(gio);
390                 g_error(_("Error creating watch\n"));
391                 return;
392         }
393         w = g_malloc(sizeof(*w));
394         memcpy(&w->pollfd, fd, sizeof(w->pollfd));
395         w->eventid = eventid;
396         w->gio = gio;
397         w->next = watches;
398         watches = w;
399 }
400
401 int
402 main(int argc, char *argv[])
403 {
404         char *error = NULL;
405         char *version = NULL;
406         DHCPCD_CONNECTION *con;
407                 
408         setlocale(LC_ALL, "");
409         bindtextdomain(PACKAGE, NULL);
410         bind_textdomain_codeset(PACKAGE, "UTF-8");
411         textdomain(PACKAGE); 
412
413         gtk_init(&argc, &argv);
414         g_set_application_name("Network Configurator");
415         gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(),
416             ICONDIR);
417         status_icon = gtk_status_icon_new_from_icon_name("network-offline");
418         
419         gtk_status_icon_set_tooltip(status_icon,
420             _("Connecting to dhcpcd ..."));
421         gtk_status_icon_set_visible(status_icon, true);
422
423 #ifdef NOTIFY
424         notify_init(PACKAGE);
425 #endif
426
427         g_message(_("Connecting ..."));
428         con = dhcpcd_open(&error);
429         if (con ==  NULL) {
430                 g_critical("libdhcpcd: %s", error);
431                 exit(EXIT_FAILURE);
432         }
433
434         gtk_status_icon_set_tooltip(status_icon, _("Triggering dhcpcd ..."));
435         online = false;
436
437         if (!dhcpcd_command(con, "GetVersion", NULL, &version)) {
438                 g_critical("libdhcpcd: GetVersion: %s", dhcpcd_error(con));
439                 exit(EXIT_FAILURE);
440         }
441         g_message(_("Connected to %s-%s"), "dhcpcd-dbus", version);
442         g_free(version);
443
444         dhcpcd_set_watch_functions(con, add_watch_cb, delete_watch_cb, NULL);
445         dhcpcd_set_signal_functions(con, event_cb, status_cb, scan_cb, NULL);
446         if (dhcpcd_error(con))
447                 g_error("libdhcpcd: %s", dhcpcd_error(con));
448
449         menu_init(status_icon, con);
450
451         gtk_main();
452         dhcpcd_close(con);
453         return 0;
454 }