remove IRRECO_PAUSE
[irreco.git] / irreco / src / core / irreco_webdb_register_dlg.c
blobd689937cbfdc0b7e6d41e8fab7136afcfa568c94
1 /*
2 * irreco - Ir Remote Control
3 * Copyright (C) 2008 Sami Mäki (kasmra@xob.kapsi.fi),
4 * Harri Vattulainen (t5vaha01@students.oamk.fi)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
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.
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.
21 #include "irreco_webdb_register_dlg.h"
22 #include "irreco_sha1.h"
24 /**
25 * @addtogroup IrrecoWebdbRegisterDlg
26 * @ingroup Irreco
28 * Dialog based interface for user registration to Irreco webDB.
30 * @{
33 /**
34 * @file
35 * Source file of @ref IrrecoWebdbRegisterDlg.
38 G_DEFINE_TYPE (IrrecoWebdbRegisterDlg, irreco_webdb_register_dlg,
39 IRRECO_TYPE_DLG)
42 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
43 /* Prototypes */
44 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
45 gboolean irreco_webdb_register_dlg_check_user_exists(
46 IrrecoWebdbRegisterDlg *self);
47 gboolean irreco_webdb_register_dlg_add_user(IrrecoWebdbRegisterDlg *self);
48 gboolean irreco_webdb_register_dlg_add_user_cache(
49 IrrecoWebdbRegisterDlg *self);
51 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
52 /* Construction & Destruction */
53 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
55 /**
56 * @name Construction & Destruction
57 * @{
60 static void irreco_webdb_register_dlg_dispose (GObject *object)
62 if (G_OBJECT_CLASS (irreco_webdb_register_dlg_parent_class)->dispose)
63 G_OBJECT_CLASS (
64 irreco_webdb_register_dlg_parent_class)->dispose (object);
67 static void irreco_webdb_register_dlg_finalize (GObject *object)
69 if (G_OBJECT_CLASS (irreco_webdb_register_dlg_parent_class)->finalize)
70 G_OBJECT_CLASS (
71 irreco_webdb_register_dlg_parent_class)->finalize (object);
74 static void
75 irreco_webdb_register_dlg_class_init (IrrecoWebdbRegisterDlgClass *klass)
77 GObjectClass *object_class = G_OBJECT_CLASS (klass);
80 object_class->dispose = irreco_webdb_register_dlg_dispose;
81 object_class->finalize = irreco_webdb_register_dlg_finalize;
84 static void irreco_webdb_register_dlg_init (IrrecoWebdbRegisterDlg *self)
86 GtkWidget *table;
87 GtkWidget *label_name;
88 GtkWidget *label_email;
89 GtkWidget *label_passwd;
90 GtkWidget *label_repasswd;
91 GtkWidget *entry_name;
92 GtkWidget *entry_email;
93 GtkWidget *entry_passwd;
94 GtkWidget *entry_repasswd;
95 GtkWidget *align;
96 GtkWidget *spacemaker;
97 IRRECO_ENTER
99 /* add buttons and stuff to register dlg */
100 gtk_window_set_title(GTK_WINDOW(self),_("Register to Irreco Database"));
101 gtk_window_set_modal(GTK_WINDOW(self), TRUE);
102 gtk_window_set_destroy_with_parent(GTK_WINDOW(self), TRUE);
103 gtk_dialog_set_has_separator(GTK_DIALOG(self), FALSE);
104 gtk_dialog_add_buttons(GTK_DIALOG(self),
105 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
106 GTK_STOCK_OK, GTK_RESPONSE_OK,
107 NULL);
109 /* rest of stuff in a table */
110 table = gtk_table_new(5, 2, FALSE);
112 /* labels */
113 label_name = gtk_label_new("Nickname:");
114 label_email = gtk_label_new("E-mail:");
115 label_passwd = gtk_label_new("Password:");
116 label_repasswd = gtk_label_new("Retype password:");
117 gtk_table_attach_defaults(GTK_TABLE(table), label_name, 0, 1, 0, 1);
118 gtk_table_attach_defaults(GTK_TABLE(table), label_email, 0, 1, 1, 2);
119 gtk_table_attach_defaults(GTK_TABLE(table), label_passwd, 0, 1, 2, 3);
120 gtk_table_attach_defaults(GTK_TABLE(table), label_repasswd, 0, 1, 3, 4);
121 gtk_misc_set_alignment(GTK_MISC(label_name), 0, 0.5);
122 gtk_misc_set_alignment(GTK_MISC(label_email), 0, 0.5);
123 gtk_misc_set_alignment(GTK_MISC(label_passwd), 0, 0.5);
124 gtk_misc_set_alignment(GTK_MISC(label_repasswd), 0, 0.5);
126 /* text boxes */
127 entry_name = gtk_entry_new();
128 entry_email = gtk_entry_new();
129 entry_passwd = gtk_entry_new();
130 entry_repasswd = gtk_entry_new();
131 gtk_entry_set_visibility(GTK_ENTRY(entry_passwd), FALSE);
132 gtk_entry_set_visibility(GTK_ENTRY(entry_repasswd), FALSE);
134 /* Hack to make entries take more than 16 characters */
135 gtk_entry_set_text(GTK_ENTRY(entry_name),
136 "...and they all lived happily ever after");
137 gtk_entry_set_text(GTK_ENTRY(entry_name), "");
138 gtk_entry_set_text(GTK_ENTRY(entry_email),
139 "...and they all lived happily ever after");
140 gtk_entry_set_text(GTK_ENTRY(entry_email), "");
141 gtk_entry_set_text(GTK_ENTRY(entry_passwd),
142 "...and they all lived happily ever after");
143 gtk_entry_set_text(GTK_ENTRY(entry_passwd), "");
144 gtk_entry_set_text(GTK_ENTRY(entry_repasswd),
145 "...and they all lived happily ever after");
146 gtk_entry_set_text(GTK_ENTRY(entry_repasswd), "");
148 self->name = gtk_entry_get_text(GTK_ENTRY(entry_name));
149 self->email = gtk_entry_get_text(GTK_ENTRY(entry_email));
150 self->passwd = gtk_entry_get_text(GTK_ENTRY(entry_passwd));
151 self->repasswd = gtk_entry_get_text(GTK_ENTRY(entry_repasswd));
152 gtk_entry_set_max_length(GTK_ENTRY(entry_name), 40);
153 gtk_entry_set_max_length(GTK_ENTRY(entry_email), 40);
154 gtk_entry_set_max_length(GTK_ENTRY(entry_passwd), 40);
155 gtk_entry_set_max_length(GTK_ENTRY(entry_repasswd), 40);
156 gtk_table_attach(GTK_TABLE(table), entry_name, 1, 2, 0, 1,
157 GTK_FILL, GTK_FILL, 0,0);
158 gtk_table_attach(GTK_TABLE(table), entry_email , 1, 2, 1, 2,
159 GTK_FILL, GTK_FILL, 0,0);
160 gtk_table_attach(GTK_TABLE(table), entry_passwd , 1, 2, 2, 3,
161 GTK_FILL, GTK_FILL, 0,0);
162 gtk_table_attach(GTK_TABLE(table), entry_repasswd, 1, 2, 3, 4,
163 GTK_FILL, GTK_FILL, 0,0);
164 /* hack for textboxes to fill available area */
165 /* would require window resize, so doesn't work at the moment */
166 spacemaker = gtk_hbox_new(TRUE, 0);
167 gtk_table_attach(GTK_TABLE(table), spacemaker, 1, 2, 4, 5,
168 GTK_EXPAND, GTK_SHRINK, 0,0);
170 /* 12px padding around table */
171 align = gtk_alignment_new(0.5, 0.5, 1, 1);
172 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 12, 12, 12, 12);
173 gtk_container_add(GTK_CONTAINER(align), GTK_WIDGET(table));
174 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(self)->vbox),
175 GTK_WIDGET(align));
177 gtk_widget_show_all(GTK_WIDGET(self));
179 IRRECO_RETURN
182 /** @} */
186 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
187 /* Private Functions */
188 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
191 * @name Private Functions
192 * @{
196 IrrecoWebdbRegisterDlg
197 *irreco_webdb_register_dlg_new (IrrecoData *irreco_data, GtkWindow *parent)
199 IrrecoWebdbRegisterDlg *self;
200 IRRECO_ENTER
202 self = g_object_new(IRRECO_TYPE_WEBDB_REGISTER_DLG, NULL);
203 irreco_dlg_set_parent(IRRECO_DLG(self), parent);
204 self->irreco_data = irreco_data;
205 IRRECO_RETURN_PTR(self);
208 gboolean irreco_webdb_register_dlg_add_user_cache(IrrecoWebdbRegisterDlg *self)
210 IrrecoWebdbCache *webdb_cache = NULL;
211 IRRECO_ENTER
213 webdb_cache = irreco_data_get_webdb_cache(self->irreco_data, FALSE);
215 if(irreco_webdb_cache_add_user(webdb_cache, self->name, self-> email,
216 self->passwd)) {
217 IRRECO_RETURN_BOOL(TRUE);
218 } else {
219 irreco_error_dlg(GTK_WINDOW(self),
220 irreco_webdb_cache_get_error(webdb_cache));
222 IRRECO_RETURN_BOOL(FALSE);
226 gboolean irreco_webdb_register_dlg_add_user(IrrecoWebdbRegisterDlg *self)
228 gchar *character;
229 const gchar *passwd_unhashed;
230 IRRECO_ENTER
232 /* checks if username is already in webdb */
233 if(irreco_webdb_register_dlg_check_user_exists(self)) {
234 irreco_error_dlg(GTK_WINDOW(self), "Username already taken.");
236 IRRECO_RETURN_BOOL(FALSE);
239 /* Check if password and retyped password doesn't match */
240 if(strcmp(self->passwd, self->repasswd) != 0) {
241 irreco_error_dlg(GTK_WINDOW(self),
242 "Mismatching password and retyped password.");
243 IRRECO_RETURN_BOOL(FALSE);
246 /* Check that name, email and passwd are long enough */
247 if(strlen(self->name) < 6) {
248 irreco_error_dlg(GTK_WINDOW(self),
249 "Name too short.\n"
250 "Minimum length is 6 characters.");
251 IRRECO_RETURN_BOOL(FALSE);
253 if(strlen(self->email) < 6) {
254 irreco_error_dlg(GTK_WINDOW(self),
255 "E-mail too short.\n"
256 "Minimum length is 6 characters.");
257 IRRECO_RETURN_BOOL(FALSE);
259 if(strlen(self->passwd) < 6) {
260 irreco_error_dlg(GTK_WINDOW(self),
261 "Password too short.\n"
262 "Minimum length is 6 characters.");
263 IRRECO_RETURN_BOOL(FALSE);
266 /* Check email to include @ and . (dot) */
267 character = "@";
268 if(g_strrstr(self->email, character) == NULL) {
269 irreco_error_dlg(GTK_WINDOW(self),
270 "Not valid E-mail\n"
271 "Missing @ (at).");
272 IRRECO_RETURN_BOOL(FALSE);
274 character = ".";
275 if(g_strrstr(self->email, character) == NULL) {
276 irreco_error_dlg(GTK_WINDOW(self),
277 "Not valid E-mail\n"
278 "Missing . (dot).");
279 IRRECO_RETURN_BOOL(FALSE);
282 /* Store passwd before hashing */
283 passwd_unhashed = self->passwd;
285 /* Convert passwd to SHA-1 */
286 self->passwd = sha_compute_checksum_for_string(G_CHECKSUM_SHA1,
287 self->passwd,
288 strlen(self->passwd));
290 if(irreco_webdb_register_dlg_add_user_cache(self)) {
291 IRRECO_RETURN_BOOL(TRUE);
292 } else {
293 self->passwd = passwd_unhashed;
294 IRRECO_RETURN_BOOL(FALSE);
298 gboolean irreco_webdb_register_dlg_check_user_exists(
299 IrrecoWebdbRegisterDlg *self)
301 IrrecoWebdbCache *webdb_cache = NULL;
302 gboolean user_exists;
303 IRRECO_ENTER
305 webdb_cache = irreco_data_get_webdb_cache(self->irreco_data, FALSE);
307 if(irreco_webdb_cache_get_user_exists(webdb_cache, self->name,
308 &user_exists)) {
309 /* Successfully called webdb */
310 } else {
311 /* Failed to call db */
312 irreco_error_dlg(GTK_WINDOW(self),
313 irreco_webdb_cache_get_error(
314 webdb_cache));
317 IRRECO_RETURN_BOOL(user_exists);
320 /** @} */
324 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
325 /* Functions */
326 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
329 * @name Public Functions
330 * @{
333 void irreco_show_webdb_register_dlg(IrrecoData *irreco_data, GtkWindow *parent)
335 IrrecoWebdbRegisterDlg *self;
336 gint response;
337 gboolean loop = TRUE;
339 IRRECO_ENTER
341 /* create register dialog */
342 self = IRRECO_WEBDB_REGISTER_DLG(
343 irreco_webdb_register_dlg_new(irreco_data, parent));
347 /* show register dlg */
348 response = gtk_dialog_run(GTK_DIALOG(self));
350 switch(response)
352 case GTK_RESPONSE_CANCEL:
353 loop = FALSE;
354 break;
355 case GTK_RESPONSE_OK:
356 /* Add user to webdb */
357 if(irreco_webdb_register_dlg_add_user(self)) {
358 /* User added succesfully */
359 irreco_info_dlg(GTK_WINDOW(self),
360 "Registration Successful");
361 loop = FALSE;
363 break;
364 default:
365 break;
369 }while(loop);
371 gtk_widget_destroy(GTK_WIDGET(self));
372 IRRECO_RETURN
375 /** @} */
379 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
380 /* Events and Callbacks */
381 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
382 /** @} */