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
27 #include "dhcpcd-gtk.h"
31 find_network(const char *ifname, const char *ssid)
43 otype = dbus_g_type_get_struct("GValueArray",
49 otype = dbus_g_type_get_collection("GPtrArray", otype);
51 if (!dbus_g_proxy_call(dbus, "GetNetworks", &error,
52 G_TYPE_STRING, ifname, G_TYPE_INVALID,
53 otype, &array, G_TYPE_INVALID))
55 g_warning("GetNetworks: %s", error->message);
60 for (i = 0; i < array->len; i++) {
61 varray = g_ptr_array_index(array, i);
62 val = g_value_array_get_nth(varray, 1);
63 str = g_value_get_string(val);
64 if (g_strcmp0(str, ssid) == 0) {
65 val = g_value_array_get_nth(varray, 0);
66 return g_value_get_int(val);
70 if (!dbus_g_proxy_call(dbus, "AddNetwork", &error,
71 G_TYPE_STRING, ifname, G_TYPE_INVALID,
72 G_TYPE_INT, &id, G_TYPE_INVALID))
74 g_warning("AddNetwork: %s", error->message);
79 nssid = g_strconcat("\"", ssid, "\"", NULL);
80 if (!dbus_g_proxy_call(dbus, "SetNetworkParameter", &error,
81 G_TYPE_STRING, ifname,
83 G_TYPE_STRING, "ssid",
88 g_warning("SetNetworkParameter: %s", error->message);
99 configure_network(const char *ifname, int id, const char *var, const char *val)
105 str = g_strconcat("\"", val, "\"", NULL);
106 if (!dbus_g_proxy_call(dbus, "SetNetworkParameter", &error,
107 G_TYPE_STRING, ifname,
114 g_warning("SetNetworkParameter: %s", error->message);
121 if (!dbus_g_proxy_call(dbus, "SelectNetwork", &error,
122 G_TYPE_STRING, ifname,
127 g_warning("SelectNetwork: %s", error->message);
136 wpa_configure(const char *ssid)
138 GtkWidget *dialog, *label, *psk, *vbox, *hbox;
142 const char *ifname = "iwi0";
144 title = g_strdup_printf(_("Configuration for %s"), ssid);
145 dialog = gtk_dialog_new_with_buttons(title,
148 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
149 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
152 vbox = GTK_DIALOG(dialog)->vbox;
154 hbox = gtk_hbox_new(FALSE, 2);
155 label = gtk_label_new("Pre Shared Key:");
156 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
157 psk = gtk_entry_new();
158 gtk_entry_set_max_length(GTK_ENTRY(psk), 130);
159 gtk_box_pack_start(GTK_BOX(hbox), psk, TRUE, TRUE, 0);
160 gtk_container_add(GTK_CONTAINER(vbox), hbox);
162 gtk_widget_show_all(dialog);
163 result = gtk_dialog_run(GTK_DIALOG(dialog));
166 if (result == GTK_RESPONSE_ACCEPT) {
167 id = find_network(ifname, ssid);
169 id = configure_network(ifname, id, "psk",
170 gtk_entry_get_text(GTK_ENTRY(psk)));
172 gtk_widget_destroy(dialog);
173 return id == -1 ? FALSE : TRUE;