1 /*
2  * Copyright (C) 2008 Fred Chien <fred@lxde.org>
3  *
4  * This file is a part of LXPanel project.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 
24 #include <string.h>
25 #include <stdlib.h>
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include "netstat.h"
29 #include "lxnm_client.h"
30 #include "wireless.h"
31 #include "passwd_gui.h"
32 #include "gtk-compat.h"
33 
34 struct passwd_resp {
35     ap_setting *aps;
36     GtkEntry *entry;
37 };
38 
passwd_gui_on_response(GtkDialog * dlg,gint response,struct passwd_resp * pr)39 static void passwd_gui_on_response(GtkDialog* dlg, gint response, struct passwd_resp *pr)
40 {
41     char *cmdargs;
42     //GtkEntry* entry = (GtkEntry*)user_data;
43 
44     if(G_LIKELY(response == GTK_RESPONSE_OK)) {
45         cmdargs = lxnm_wireless_command_make(pr->aps->ifname, pr->aps->apinfo->essid,
46                                              pr->aps->apinfo->apaddr, gtk_entry_get_text(pr->entry),
47                                              pr->aps->apinfo->en_method, pr->aps->apinfo->key_mgmt,
48                                              pr->aps->apinfo->group, pr->aps->apinfo->pairwise);
49         lxnm_send_command(pr->aps->gio, LXNM_WIRELESS_CONNECT, cmdargs);
50     	g_free(cmdargs);
51     }
52 
53     g_source_remove_by_user_data(pr->entry); /* remove timeout */
54     gtk_widget_destroy((GtkWidget*)dlg);
55 }
56 
passwd_gui_set_style(struct pgui * pg,GtkStyle * style)57 void passwd_gui_set_style(struct pgui *pg, GtkStyle *style)
58 {
59     gtk_widget_set_style(pg->dlg, style);
60 }
61 
passwd_gui_free(gpointer ptr,GObject * dummy)62 static void passwd_gui_free(gpointer ptr, GObject *dummy)
63 {
64     struct passwd_resp *pr = ptr;
65     g_free(pr->aps->ifname);
66     g_free(pr->aps->apinfo->essid);
67     g_free(pr->aps->apinfo->apaddr);
68     g_free(pr->aps->apinfo);
69     g_free(pr->aps);
70     g_free(pr);
71 }
72 
passwd_gui_destroy(struct pgui * pg)73 void passwd_gui_destroy(struct pgui *pg)
74 {
75     gtk_widget_destroy((GtkWidget*)pg->dlg);
76 }
77 
passwd_gui_new(ap_setting * aps)78 struct pgui *passwd_gui_new(ap_setting *aps)
79 {
80     GtkWidget *msg, *inputlabel;
81     GtkWidget *inputbox;
82     GtkBox *dialog_vbox;
83     struct pgui *pwdgui;
84     struct passwd_resp *pr;
85 
86     pwdgui = malloc(sizeof(struct pgui));
87     pr = malloc(sizeof(struct passwd_resp));
88 
89     /* create dialog */
90     pwdgui->dlg = gtk_dialog_new_with_buttons(_("Setting Encryption Key"),
91                                        NULL,
92                                        GTK_DIALOG_NO_SEPARATOR,
93                                        GTK_STOCK_OK, GTK_RESPONSE_OK,
94                                        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
95                                        NULL );
96     gtk_dialog_set_default_response(GTK_DIALOG(pwdgui->dlg), GTK_RESPONSE_OK);
97     gtk_window_set_position(GTK_WINDOW(pwdgui->dlg), GTK_WIN_POS_CENTER);
98 
99     /* messages */
100     dialog_vbox = GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(pwdgui->dlg)));
101     msg = gtk_label_new(_("This wireless network was encrypted.\nYou must have the encryption key."));
102     gtk_box_pack_start(dialog_vbox, msg, FALSE, FALSE, 8);
103 
104     /* entry Box */
105     inputbox = gtk_hbox_new(FALSE, 0);
106     inputlabel = gtk_label_new(_("Encryption Key:"));
107     gtk_box_pack_start(GTK_BOX(inputbox), inputlabel, TRUE, TRUE, 4);
108     pwdgui->pentry = gtk_entry_new();
109     gtk_entry_set_visibility(GTK_ENTRY(pwdgui->pentry), FALSE);
110     gtk_entry_set_activates_default(GTK_ENTRY(pwdgui->pentry), TRUE);
111     gtk_box_pack_start(GTK_BOX(inputbox), pwdgui->pentry, FALSE, FALSE, 4);
112     gtk_box_pack_start(dialog_vbox, inputbox, FALSE, FALSE, 8);
113 
114     /* passwd_resp structure */
115     pr->aps = aps;
116     pr->entry = GTK_ENTRY(pwdgui->pentry);
117 
118     /* g_signal */
119     g_signal_connect(pwdgui->dlg, "response", G_CALLBACK(passwd_gui_on_response), pr);
120     g_object_weak_ref(G_OBJECT(pwdgui->dlg), passwd_gui_free, pr);
121 
122     gtk_widget_show_all(pwdgui->dlg);
123 
124     return pwdgui;
125 }
126