1 /* $Id$ */
2 /* Copyright (c) 2013-2015 Pierre Pronchery <khorben@defora.org> */
3 /* This file is part of DeforaOS Desktop Phone */
4 /* This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, version 3 of the License.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>. */
15 
16 
17 
18 #include <string.h>
19 #include <System.h>
20 #include <Desktop.h>
21 #include "Phone.h"
22 
23 
24 /* Password */
25 /* private */
26 /* types */
27 typedef struct _PhonePlugin
28 {
29 	PhonePluginHelper * helper;
30 
31 	/* password */
32 	GtkWidget * window;
33 	GtkWidget * entry;
34 	GtkWidget * oldpassword;
35 	GtkWidget * newpassword;
36 	GtkWidget * newpassword2;
37 } PasswordPhonePlugin;
38 
39 
40 /* prototypes */
41 /* plug-in */
42 static PasswordPhonePlugin * _password_init(PhonePluginHelper * helper);
43 static void _password_destroy(PasswordPhonePlugin * password);
44 static int _password_event(PasswordPhonePlugin * password, PhoneEvent * event);
45 static void _password_settings(PasswordPhonePlugin * password);
46 
47 
48 /* public */
49 /* variables */
50 PhonePluginDefinition plugin =
51 {
52 	"Password",
53 	"stock_lock",
54 	NULL,
55 	_password_init,
56 	_password_destroy,
57 	_password_event,
58 	_password_settings
59 };
60 
61 
62 /* private */
63 /* functions */
64 /* password_init */
_password_init(PhonePluginHelper * helper)65 static PasswordPhonePlugin * _password_init(PhonePluginHelper * helper)
66 {
67 	PasswordPhonePlugin * password;
68 
69 	if((password = object_new(sizeof(*password))) == NULL)
70 		return NULL;
71 	password->helper = helper;
72 	password->window = NULL;
73 	return password;
74 }
75 
76 
77 /* password_destroy */
_password_destroy(PasswordPhonePlugin * password)78 static void _password_destroy(PasswordPhonePlugin * password)
79 {
80 	object_delete(password);
81 }
82 
83 
84 /* password_event */
_password_event(PasswordPhonePlugin * password,PhoneEvent * event)85 static int _password_event(PasswordPhonePlugin * password, PhoneEvent * event)
86 {
87 	switch(event->type)
88 	{
89 		default:
90 			break;
91 	}
92 	return 0;
93 }
94 
95 
96 /* password_settings */
97 static void _on_settings_cancel(gpointer data);
98 static gboolean _on_settings_closex(gpointer data);
99 static void _on_settings_ok(gpointer data);
100 static void _on_settings_ok_error(PasswordPhonePlugin * password,
101 		char const * error);
102 
_password_settings(PasswordPhonePlugin * password)103 static void _password_settings(PasswordPhonePlugin * password)
104 {
105 	GtkSizeGroup * group;
106 	GtkWidget * vbox;
107 	GtkWidget * hbox;
108 	GtkWidget * widget;
109 
110 	if(password->window != NULL)
111 	{
112 		gtk_window_present(GTK_WINDOW(password->window));
113 		return;
114 	}
115 	group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
116 	password->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
117 	gtk_container_set_border_width(GTK_CONTAINER(password->window), 4);
118 	gtk_window_set_default_size(GTK_WINDOW(password->window), 200, 300);
119 #if GTK_CHECK_VERSION(2, 6, 0)
120 	/* XXX find something more appropriate */
121 	gtk_window_set_icon_name(GTK_WINDOW(password->window), "stock_lock");
122 #endif
123 	gtk_window_set_title(GTK_WINDOW(password->window), "Password");
124 	g_signal_connect_swapped(password->window, "delete-event", G_CALLBACK(
125 				_on_settings_closex), password);
126 #if GTK_CHECK_VERSION(3, 0, 0)
127 	vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 4);
128 #else
129 	vbox = gtk_vbox_new(FALSE, 4);
130 #endif
131 	/* entry */
132 #if GTK_CHECK_VERSION(3, 0, 0)
133 	hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
134 #else
135 	hbox = gtk_hbox_new(FALSE, 0);
136 #endif
137 	widget = gtk_label_new("Name: ");
138 #if GTK_CHECK_VERSION(3, 0, 0)
139 	g_object_set(widget, "halign", GTK_ALIGN_START, NULL);
140 #else
141 	gtk_misc_set_alignment(GTK_MISC(widget), 0.0, 0.5);
142 #endif
143 	gtk_size_group_add_widget(group, widget);
144 	gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, TRUE, 0);
145 #if GTK_CHECK_VERSION(2, 24, 0)
146 	password->entry = gtk_combo_box_text_new_with_entry();
147 	gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(password->entry),
148 			"SIM PIN");
149 #else
150 	password->entry = gtk_combo_box_entry_new_text();
151 	gtk_combo_box_append_text(GTK_COMBO_BOX(password->entry), "SIM PIN");
152 #endif
153 	gtk_box_pack_start(GTK_BOX(hbox), password->entry, TRUE, TRUE, 0);
154 	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
155 	/* old password */
156 #if GTK_CHECK_VERSION(3, 0, 0)
157 	hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
158 #else
159 	hbox = gtk_hbox_new(FALSE, 0);
160 #endif
161 	widget = gtk_label_new("Old password: ");
162 #if GTK_CHECK_VERSION(3, 0, 0)
163 	g_object_set(widget, "halign", GTK_ALIGN_START, NULL);
164 #else
165 	gtk_misc_set_alignment(GTK_MISC(widget), 0.0, 0.5);
166 #endif
167 	gtk_size_group_add_widget(group, widget);
168 	gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, TRUE, 0);
169 	password->oldpassword = gtk_entry_new();
170 	gtk_entry_set_visibility(GTK_ENTRY(password->oldpassword), FALSE);
171 	gtk_box_pack_start(GTK_BOX(hbox), password->oldpassword, TRUE, TRUE, 0);
172 	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
173 	/* new password */
174 #if GTK_CHECK_VERSION(3, 0, 0)
175 	hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
176 #else
177 	hbox = gtk_hbox_new(FALSE, 0);
178 #endif
179 	widget = gtk_label_new("New password: ");
180 #if GTK_CHECK_VERSION(3, 0, 0)
181 	g_object_set(widget, "halign", GTK_ALIGN_START, NULL);
182 #else
183 	gtk_misc_set_alignment(GTK_MISC(widget), 0.0, 0.5);
184 #endif
185 	gtk_size_group_add_widget(group, widget);
186 	gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, TRUE, 0);
187 	password->newpassword = gtk_entry_new();
188 	gtk_entry_set_visibility(GTK_ENTRY(password->newpassword), FALSE);
189 	gtk_box_pack_start(GTK_BOX(hbox), password->newpassword, TRUE, TRUE, 0);
190 	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
191 	/* new password */
192 #if GTK_CHECK_VERSION(3, 0, 0)
193 	hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
194 #else
195 	hbox = gtk_hbox_new(FALSE, 0);
196 #endif
197 	widget = gtk_label_new("Confirm: ");
198 #if GTK_CHECK_VERSION(3, 0, 0)
199 	g_object_set(widget, "halign", GTK_ALIGN_START, NULL);
200 #else
201 	gtk_misc_set_alignment(GTK_MISC(widget), 0.0, 0.5);
202 #endif
203 	gtk_size_group_add_widget(group, widget);
204 	gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, TRUE, 0);
205 	password->newpassword2 = gtk_entry_new();
206 	gtk_entry_set_visibility(GTK_ENTRY(password->newpassword2), FALSE);
207 	gtk_box_pack_start(GTK_BOX(hbox), password->newpassword2, TRUE, TRUE,
208 			0);
209 	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
210 	/* buttons */
211 #if GTK_CHECK_VERSION(3, 0, 0)
212 	hbox = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
213 #else
214 	hbox = gtk_hbutton_box_new();
215 #endif
216 	gtk_button_box_set_layout(GTK_BUTTON_BOX(hbox), GTK_BUTTONBOX_END);
217 	gtk_box_set_spacing(GTK_BOX(hbox), 4);
218 	widget = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
219 	g_signal_connect_swapped(widget, "clicked", G_CALLBACK(
220 				_on_settings_cancel), password);
221 	gtk_container_add(GTK_CONTAINER(hbox), widget);
222 	widget = gtk_button_new_from_stock(GTK_STOCK_OK);
223 	g_signal_connect_swapped(widget, "clicked", G_CALLBACK(_on_settings_ok),
224 			password);
225 	gtk_container_add(GTK_CONTAINER(hbox), widget);
226 	gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
227 	gtk_container_add(GTK_CONTAINER(password->window), vbox);
228 	gtk_widget_show_all(password->window);
229 }
230 
_on_settings_cancel(gpointer data)231 static void _on_settings_cancel(gpointer data)
232 {
233 	PasswordPhonePlugin * password = data;
234 
235 	gtk_widget_hide(password->window);
236 	gtk_entry_set_text(GTK_ENTRY(password->oldpassword), "");
237 	gtk_entry_set_text(GTK_ENTRY(password->newpassword), "");
238 	gtk_entry_set_text(GTK_ENTRY(password->newpassword2), "");
239 }
240 
_on_settings_closex(gpointer data)241 static gboolean _on_settings_closex(gpointer data)
242 {
243 	PasswordPhonePlugin * password = data;
244 
245 	gtk_widget_hide(password->window);
246 	return TRUE;
247 }
248 
_on_settings_ok(gpointer data)249 static void _on_settings_ok(gpointer data)
250 {
251 	PasswordPhonePlugin * password = data;
252 	PhonePluginHelper * helper = password->helper;
253 	GtkWidget * widget;
254 	char const * name;
255 	char const * oldpassword;
256 	char const * newpassword;
257 	char const * newpassword2;
258 	ModemRequest request;
259 
260 	widget = gtk_bin_get_child(GTK_BIN(password->entry));
261 	name = gtk_entry_get_text(GTK_ENTRY(widget));
262 	oldpassword = gtk_entry_get_text(GTK_ENTRY(password->oldpassword));
263 	newpassword = gtk_entry_get_text(GTK_ENTRY(password->newpassword));
264 	newpassword2 = gtk_entry_get_text(GTK_ENTRY(password->newpassword2));
265 	if(strcmp(newpassword, newpassword2) != 0)
266 	{
267 		_on_settings_ok_error(password,
268 				"The new passwords do not match");
269 		return;
270 	}
271 	/* issue the request */
272 	memset(&request, 0, sizeof(request));
273 	request.type = MODEM_REQUEST_PASSWORD_SET;
274 	request.password_set.name = name;
275 	request.password_set.oldpassword = oldpassword;
276 	request.password_set.newpassword = newpassword;
277 	helper->request(helper->phone, &request);
278 	gtk_widget_hide(password->window);
279 }
280 
_on_settings_ok_error(PasswordPhonePlugin * password,char const * error)281 static void _on_settings_ok_error(PasswordPhonePlugin * password,
282 		char const * error)
283 {
284 	GtkWidget * dialog;
285 
286 	dialog = gtk_message_dialog_new(GTK_WINDOW(password->window),
287 			GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
288 			GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
289 #if GTK_CHECK_VERSION(2, 6, 0)
290 			"%s", "Error");
291 	gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
292 #endif
293 			"%s", error);
294 	gtk_window_set_title(GTK_WINDOW(dialog), "Error");
295 	gtk_dialog_run(GTK_DIALOG(dialog));
296 	gtk_widget_destroy(dialog);
297 }
298