3 * Copyright 2009 Roy Marples <roy@marples.name>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 # include <libnotify/notify.h>
34 static NotifyNotification *nn;
38 #include "dhcpcd-gtk.h"
40 static GtkStatusIcon *status_icon;
42 static int ani_counter;
52 static struct watch *watches;
57 wi_scan_find(DHCPCD_WI_SCAN *scan)
62 for (w = wi_scans; w; w = w->next) {
63 for (dw = w->scans; dw; dw = dw->next)
73 animate_carrier(_unused gpointer data)
80 switch(ani_counter++) {
82 icon = "network-transmit";
85 icon = "network-receive";
88 icon = "network-idle";
92 gtk_status_icon_set_from_icon_name(status_icon, icon);
97 animate_online(_unused gpointer data)
104 if (ani_counter++ > 6) {
110 if (ani_counter % 2 == 0)
111 icon = "network-idle";
113 icon = "network-transmit-receive";
114 gtk_status_icon_set_from_icon_name(status_icon, icon);
119 update_online(DHCPCD_CONNECTION *con, bool showif)
121 bool ison, iscarrier;
122 char *msg, *msgs, *tmp;
125 ison = iscarrier = false;
127 ifs = dhcpcd_interfaces(con);
128 for (i = ifs; i; i = i->next) {
130 g_message("%s: %s", i->ifname, i->reason);
131 if (strcmp(i->reason, "RELEASE") == 0 ||
132 strcmp(i->reason, "STOP") == 0)
135 ison = iscarrier = true;
136 if (!iscarrier && g_strcmp0(i->reason, "CARRIER") == 0)
138 msg = dhcpcd_if_message(i);
140 tmp = g_strconcat(msgs, "\n", msg, NULL);
148 if (online != ison || carrier != iscarrier) {
150 if (ani_timer != 0) {
151 g_source_remove(ani_timer);
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);
162 gtk_status_icon_set_from_icon_name(status_icon,
166 gtk_status_icon_set_tooltip(status_icon, msgs);
175 notify_notification_close(nn, NULL);
187 notify(const char *title, const char *msg, const char *icon)
191 msgs = g_strsplit(msg, "\n", 0);
192 for (m = msgs; *m; m++)
196 notify_notification_close(nn, NULL);
197 if (gtk_status_icon_get_visible(status_icon))
198 nn = notify_notification_new_with_status_icon(title,
199 msg, icon, status_icon);
201 nn = notify_notification_new(title, msg, icon, NULL);
202 notify_notification_set_timeout(nn, 5000);
203 g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
204 notify_notification_show(nn, NULL);
207 # define notify(a, b, c)
211 event_cb(DHCPCD_CONNECTION *con, DHCPCD_IF *i, _unused void *data)
216 g_message("%s: %s", i->ifname, i->reason);
217 update_online(con, false);
219 /* We should ignore renew and stop so we don't annoy the user */
220 if (g_strcmp0(i->reason, "RENEW") == 0 ||
221 g_strcmp0(i->reason, "STOP") == 0)
224 msg = dhcpcd_if_message(i);
226 icon = "network-transmit-receive";
228 icon = "network-transmit";
229 if (dhcpcd_if_down(i))
230 icon = "network-offline";
231 notify(_("Network event"), msg, icon);
236 status_cb(DHCPCD_CONNECTION *con, const char *status, _unused void *data)
238 static char *last = NULL;
244 g_message("Status changed to %s", status);
245 if (g_strcmp0(status, "down") == 0) {
247 "Connection to dhcpcd lost" : "dhcpcd not running");
248 gtk_status_icon_set_tooltip(status_icon, msg);
249 notify(_("No network"), msg, "network-offline");
250 dhcpcd_prefs_abort();
253 dhcpcd_wi_scans_free(wi_scans->scans);
258 if ((last == NULL || g_strcmp0(last, "down") == 0) &&
259 dhcpcd_command(con, "GetDhcpcdVersion", NULL, &version))
261 g_message(_("Connected to %s-%s"), "dhcpcd", version);
266 update_online(con, refresh);
268 last = g_strdup(status);
272 scan_cb(DHCPCD_CONNECTION *con, DHCPCD_IF *i, _unused void *data)
275 DHCPCD_WI_SCAN *scans, *s1, *s2;
279 g_message(_("%s: Received scan results"), i->ifname);
280 scans = dhcpcd_wi_scans(con, i);
282 g_warning("%s: %s", i->ifname, dhcpcd_error(con));
283 dhcpcd_error_clear(con);
285 for (w = wi_scans; w; w = w->next)
286 if (w->connection == con && w->interface == i)
289 w = g_malloc(sizeof(*w));
296 msg = N_("New Access Point");
297 for (s1 = scans; s1; s1 = s1->next) {
298 for (s2 = w->scans; s2; s2 = s2->next)
299 if (g_strcmp0(s1->ssid, s2->ssid) == 0)
303 txt = g_strdup(s1->ssid);
305 msg = N_("New Access Points");
306 t = g_strconcat(txt, "\n",
314 notify(msg, txt, "network-wireless");
317 dhcpcd_wi_scans_free(w->scans);
323 gio_callback(GIOChannel *gio, _unused GIOCondition c, _unused gpointer d)
327 fd = g_io_channel_unix_get_fd(gio);
333 delete_watch_cb(_unused DHCPCD_CONNECTION *con, const struct pollfd *fd,
339 for (w = watches; w; w = w->next) {
340 if (w->pollfd.fd == fd->fd) {
345 g_source_remove(w->eventid);
346 g_io_channel_unref(w->gio);
354 add_watch_cb(DHCPCD_CONNECTION *con, const struct pollfd *fd,
361 /* Remove any existing watch */
362 delete_watch_cb(con, fd, data);
364 gio = g_io_channel_unix_new(fd->fd);
366 g_error(_("Error creating new GIO Channel\n"));
370 if (fd->events & POLLIN)
372 if (fd->events & POLLOUT)
374 if (fd->events & POLLERR)
376 if (fd->events & POLLHUP)
378 if ((eventid = g_io_add_watch(gio, flags, gio_callback, con)) == 0) {
379 g_io_channel_unref(gio);
380 g_error(_("Error creating watch\n"));
383 w = g_malloc(sizeof(*w));
384 memcpy(&w->pollfd, fd, sizeof(w->pollfd));
385 w->eventid = eventid;
392 main(int argc, char *argv[])
395 char *version = NULL;
396 DHCPCD_CONNECTION *con;
398 setlocale(LC_ALL, "");
399 bindtextdomain(PACKAGE, NULL);
400 bind_textdomain_codeset(PACKAGE, "UTF-8");
403 gtk_init(&argc, &argv);
404 g_set_application_name("Network Configurator");
405 gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(),
407 status_icon = gtk_status_icon_new_from_icon_name("network-offline");
409 gtk_status_icon_set_tooltip(status_icon,
410 _("Connecting to dhcpcd ..."));
411 gtk_status_icon_set_visible(status_icon, true);
414 notify_init(PACKAGE);
417 g_message(_("Connecting ..."));
418 con = dhcpcd_open(&error);
420 g_critical("libdhcpcd: %s", error);
424 gtk_status_icon_set_tooltip(status_icon, _("Triggering dhcpcd ..."));
427 if (!dhcpcd_command(con, "GetVersion", NULL, &version)) {
428 g_critical("libdhcpcd: GetVersion: %s", dhcpcd_error(con));
431 g_message(_("Connected to %s-%s"), "dhcpcd-dbus", version);
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));
439 menu_init(status_icon, con);