3 * Copyright 2009-2014 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"
30 wpa_dialog(const char *title, const char *txt)
34 dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL,
35 GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", txt);
36 gtk_window_set_title(GTK_WINDOW(dialog), title);
37 gtk_dialog_run(GTK_DIALOG(dialog));
38 gtk_widget_destroy(dialog);
42 configure_network(DHCPCD_CONNECTION *con, DHCPCD_IF *i,
43 int id, const char *mgmt, const char *var, const char *val, bool quote)
46 static bool warned = false;
48 if (!dhcpcd_wpa_set_network(con, i, id, "key_mgmt", mgmt))
51 str = g_strconcat("\"", val, "\"", NULL);
54 if (!dhcpcd_wpa_set_network(con, i, id, var, quote ? str : val)) {
55 g_warning("libdhcpcd: %s", dhcpcd_error(con));
56 dhcpcd_error_clear(con);
58 wpa_dialog(_("Error setting password"),
59 _("Failed to set password, probably too short."));
63 if (!dhcpcd_wpa_command(con, i, "EnableNetwork", id))
65 if (!dhcpcd_wpa_command(con, i, "SaveConfig", -1)) {
66 g_warning("libdhcpcd: %s", dhcpcd_error(con));
67 dhcpcd_error_clear(con);
70 wpa_dialog(_("Error saving configuration"),
71 _("Failed to save wpa_supplicant configuration.\n\nYou should add update_config=1 to /etc/wpa_supplicant.conf.\nThis warning will not appear again until program restarted."));
76 if (!dbus_g_proxy_call(dbus, "Disconnect", &error,
77 G_TYPE_STRING, ifname,
81 g_warning("Disconnect: %s", error->message);
85 if (!dhcpcd_wpa_command(con, i, "Reassociate", -1))
91 onEnter(_unused GtkWidget *widget, gpointer *data)
93 gtk_dialog_response(GTK_DIALOG(data), GTK_RESPONSE_ACCEPT);
97 wpa_configure(DHCPCD_CONNECTION *con, DHCPCD_IF *i, DHCPCD_WI_SCAN *s)
99 GtkWidget *dialog, *label, *psk, *vbox, *hbox;
100 const char *var, *mgt;
104 dialog = gtk_dialog_new_with_buttons(s->ssid,
107 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
108 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
110 gtk_window_set_resizable(GTK_WINDOW(dialog), false);
111 gtk_window_set_icon_name(GTK_WINDOW(dialog),
112 "network-wireless-encrypted");
113 gtk_dialog_set_default_response(GTK_DIALOG(dialog),
114 GTK_RESPONSE_ACCEPT);
115 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
117 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
118 label = gtk_label_new(_("Pre Shared Key:"));
119 gtk_box_pack_start(GTK_BOX(hbox), label, false, false, 0);
120 psk = gtk_entry_new();
121 gtk_entry_set_max_length(GTK_ENTRY(psk), 130);
122 g_signal_connect(G_OBJECT(psk), "activate",
123 G_CALLBACK(onEnter), dialog);
124 gtk_box_pack_start(GTK_BOX(hbox), psk, true, true, 0);
125 gtk_container_add(GTK_CONTAINER(vbox), hbox);
127 gtk_widget_show_all(dialog);
129 result = gtk_dialog_run(GTK_DIALOG(dialog));
133 if (result == GTK_RESPONSE_ACCEPT) {
134 id = dhcpcd_wpa_find_network_new(con, i, s->ssid);
135 if (g_strcmp0(s->flags, "[WEP]") == 0) {
143 retval = configure_network(con, i, id, mgt, var,
144 gtk_entry_get_text(GTK_ENTRY(psk)), true);
146 if (!retval && dhcpcd_error(con)) {
147 wpa_dialog(_("Error"), dhcpcd_error(con));
151 gtk_widget_destroy(dialog);