Add support for a configure script
[dhcpcd-ui] / src / dhcpcd-gtk / main.c
1 /*
2  * dhcpcd-gtk
3  * Copyright 2009 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 static void
180 notify_closed(void)
181 {
182 #ifdef NOTIFY
183         nn = NULL;
184 #endif
185 }
186
187 #ifdef NOTIFY
188 static void
189 notify(const char *title, const char *msg, const char *icon)
190 {
191         char **msgs, **m;
192
193         msgs = g_strsplit(msg, "\n", 0);
194         for (m = msgs; *m; m++)
195                 g_message("%s", *m);
196         g_strfreev(msgs);
197         if (nn != NULL)
198                 notify_notification_close(nn, NULL);
199         if (gtk_status_icon_get_visible(status_icon))
200                 nn = notify_notification_new_with_status_icon(title,
201                     msg, icon, status_icon);
202         else
203                 nn = notify_notification_new(title, msg, icon, NULL);
204         notify_notification_set_timeout(nn, 5000);
205         g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
206         notify_notification_show(nn, NULL);
207 }
208 #else
209 #  define notify(a, b, c)
210 #endif
211
212 static void
213 event_cb(DHCPCD_CONNECTION *con, DHCPCD_IF *i, _unused void *data)
214 {
215         char *msg;
216         const char *icon;
217
218         g_message("%s: %s", i->ifname, i->reason);
219         update_online(con, false);
220         
221         /* We should ignore renew and stop so we don't annoy the user */
222         if (g_strcmp0(i->reason, "RENEW") == 0 ||
223             g_strcmp0(i->reason, "STOP") == 0)
224                 return;
225
226         msg = dhcpcd_if_message(i);
227         if (dhcpcd_if_up(i))
228                 icon = "network-transmit-receive";
229         else
230                 icon = "network-transmit";
231         if (dhcpcd_if_down(i))
232                 icon = "network-offline";
233         notify(_("Network event"), msg, icon);
234         g_free(msg);
235 }
236
237 static void
238 status_cb(DHCPCD_CONNECTION *con, const char *status, _unused void *data)
239 {
240         static char *last = NULL;
241         char *version;
242         const char *msg;
243         bool refresh;
244         WI_SCAN *w;
245
246         g_message("Status changed to %s", status);
247         if (g_strcmp0(status, "down") == 0) {
248                 msg = N_(last ?
249                     "Connection to dhcpcd lost" : "dhcpcd not running");
250                 gtk_status_icon_set_tooltip(status_icon, msg);
251                 notify(_("No network"), msg, "network-offline");
252                 dhcpcd_prefs_abort();
253                 while (wi_scans) {
254                         w = wi_scans->next;
255                         dhcpcd_wi_scans_free(wi_scans->scans);
256                         g_free(wi_scans);
257                         wi_scans = w;
258                 }
259         } else {
260                 if ((last == NULL || g_strcmp0(last, "down") == 0) &&
261                     dhcpcd_command(con, "GetDhcpcdVersion", NULL, &version))
262                 {
263                         g_message(_("Connected to %s-%s"), "dhcpcd", version);
264                         g_free(version);
265                         refresh = true;
266                 } else
267                         refresh = false;
268                 update_online(con, refresh);
269         }
270         last = g_strdup(status);
271 }
272
273 static void
274 scan_cb(DHCPCD_CONNECTION *con, DHCPCD_IF *i, _unused void *data)
275 {
276         WI_SCAN *w;
277         DHCPCD_WI_SCAN *scans, *s1, *s2;
278         char *txt, *t;
279         const char *msg;
280
281         g_message(_("%s: Received scan results"), i->ifname);
282         scans = dhcpcd_wi_scans(con, i);
283         if (scans == NULL) {
284                 g_warning("%s: %s", i->ifname, dhcpcd_error(con));
285                 dhcpcd_error_clear(con);
286         }
287         for (w = wi_scans; w; w = w->next)
288                 if (w->connection == con && w->interface == i)
289                         break;
290         if (w == NULL) {
291                 w = g_malloc(sizeof(*w));
292                 w->connection = con;
293                 w->interface = i;
294                 w->next = wi_scans;
295                 wi_scans = w;
296         } else {
297                 txt = NULL;
298                 msg = N_("New Access Point");
299                 for (s1 = scans; s1; s1 = s1->next) {
300                         for (s2 = w->scans; s2; s2 = s2->next)
301                                 if (g_strcmp0(s1->ssid, s2->ssid) == 0)
302                                         break;
303                         if (s2 == NULL) {
304                                 if (txt == NULL)
305                                         txt = g_strdup(s1->ssid);
306                                 else {
307                                         msg = N_("New Access Points");
308                                         t = g_strconcat(txt, "\n",
309                                             s1->ssid, NULL);
310                                         g_free(txt);
311                                         txt = t;
312                                 }
313                         }
314                 }
315                 if (txt) {
316                         notify(msg, txt, "network-wireless");
317                         g_free(txt);
318                 }
319                 dhcpcd_wi_scans_free(w->scans);
320         }
321         w->scans = scans;
322 }
323
324 static gboolean
325 gio_callback(GIOChannel *gio, _unused GIOCondition c, _unused gpointer d)
326 {
327         int fd;
328
329         fd = g_io_channel_unix_get_fd(gio);
330         dhcpcd_dispatch(fd);
331         return true;
332 }
333
334 static void
335 delete_watch_cb(_unused DHCPCD_CONNECTION *con, const struct pollfd *fd,
336     _unused void *data)
337 {
338         struct watch *w, *l;
339
340         l = NULL;
341         for (w = watches; w; w = w->next) {
342                 if (w->pollfd.fd == fd->fd) {
343                         if (l == NULL)
344                                 watches = w->next;
345                         else
346                                 l->next = w->next;
347                         g_source_remove(w->eventid);
348                         g_io_channel_unref(w->gio);
349                         g_free(w);
350                         break;
351                 }
352         }
353 }
354
355 static void
356 add_watch_cb(DHCPCD_CONNECTION *con, const struct pollfd *fd,
357     _unused void *data)
358 {
359         struct watch *w;
360         GIOChannel *gio;
361         int flags, eventid;
362
363         /* Remove any existing watch */
364         delete_watch_cb(con, fd, data);
365         
366         gio = g_io_channel_unix_new(fd->fd);
367         if (gio == NULL) {
368                 g_error(_("Error creating new GIO Channel\n"));
369                 return;
370         }
371         flags = 0;
372         if (fd->events & POLLIN)
373                 flags |= G_IO_IN;
374         if (fd->events & POLLOUT)
375                 flags |= G_IO_OUT;
376         if (fd->events & POLLERR)
377                 flags |= G_IO_ERR;
378         if (fd->events & POLLHUP)
379                 flags |= G_IO_HUP;
380         if ((eventid = g_io_add_watch(gio, flags, gio_callback, con)) == 0) {
381                 g_io_channel_unref(gio);
382                 g_error(_("Error creating watch\n"));
383                 return;
384         }
385         w = g_malloc(sizeof(*w));
386         memcpy(&w->pollfd, fd, sizeof(w->pollfd));
387         w->eventid = eventid;
388         w->gio = gio;
389         w->next = watches;
390         watches = w;
391 }
392
393 int
394 main(int argc, char *argv[])
395 {
396         char *error = NULL;
397         char *version = NULL;
398         DHCPCD_CONNECTION *con;
399                 
400         setlocale(LC_ALL, "");
401         bindtextdomain(PACKAGE, NULL);
402         bind_textdomain_codeset(PACKAGE, "UTF-8");
403         textdomain(PACKAGE); 
404
405         gtk_init(&argc, &argv);
406         g_set_application_name("Network Configurator");
407         gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(),
408             ICONDIR);
409         status_icon = gtk_status_icon_new_from_icon_name("network-offline");
410         
411         gtk_status_icon_set_tooltip(status_icon,
412             _("Connecting to dhcpcd ..."));
413         gtk_status_icon_set_visible(status_icon, true);
414
415         notify_init(PACKAGE);
416
417         g_message(_("Connecting ..."));
418         con = dhcpcd_open(&error);
419         if (con ==  NULL) {
420                 g_critical("libdhcpcd: %s", error);
421                 exit(EXIT_FAILURE);
422         }
423
424         gtk_status_icon_set_tooltip(status_icon, _("Triggering dhcpcd ..."));
425         online = false;
426
427         if (!dhcpcd_command(con, "GetVersion", NULL, &version)) {
428                 g_critical("libdhcpcd: GetVersion: %s", dhcpcd_error(con));
429                 exit(EXIT_FAILURE);
430         }
431         g_message(_("Connected to %s-%s"), "dhcpcd-dbus", version);
432         g_free(version);
433
434         dhcpcd_set_watch_functions(con, add_watch_cb, delete_watch_cb, NULL);
435         dhcpcd_set_signal_functions(con, event_cb, status_cb, scan_cb, NULL);
436         if (dhcpcd_error(con))
437                 g_error("libdhcpcd: %s", dhcpcd_error(con));
438
439         menu_init(status_icon, con);
440
441         gtk_main();
442         dhcpcd_close(con);
443         return 0;
444 }