2 * irreco - Ir Remote Control
3 * Copyright (C) 2008 Sami Mäki (kasmra@xob.kapsi.fi)
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "irreco_login_dlg.h"
21 #include "irreco_webdb_register_dlg.h"
23 /* Include the prototypes for GConf client functions. */
24 #include <gconf/gconf-client.h>
27 * @addtogroup IrrecoLoginDlg
30 * Allow user to login into Irreco WebDB.
37 * Source file of @ref IrrecoLoginDlg.
42 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
44 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
46 #define APP_NAME "irreco"
47 #define GC_ROOT "/apps/Maemo/" APP_NAME "/"
49 /** Button response codes. */
55 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
57 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
61 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
62 /* Construction & Destruction */
63 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
66 * @name Construction & Destruction
70 /* Add functions here. */
72 G_DEFINE_TYPE (IrrecoLoginDlg
, irreco_login_dlg
, IRRECO_TYPE_DLG
)
74 static void irreco_login_dlg_dispose (GObject
*object
)
76 if (G_OBJECT_CLASS (irreco_login_dlg_parent_class
)->dispose
)
78 irreco_login_dlg_parent_class
)->dispose (object
);
81 static void irreco_login_dlg_finalize (GObject
*object
)
83 if (G_OBJECT_CLASS (irreco_login_dlg_parent_class
)->finalize
)
85 irreco_login_dlg_parent_class
)->finalize (object
);
88 static void irreco_login_dlg_class_init (IrrecoLoginDlgClass
*klass
)
90 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
92 object_class
->dispose
= irreco_login_dlg_dispose
;
93 object_class
->finalize
= irreco_login_dlg_finalize
;
96 static void irreco_login_dlg_init (IrrecoLoginDlg
*self
)
99 GtkWidget
*label_user
;
100 GtkWidget
*label_password
;
101 GtkWidget
*entry_password
;
105 /* Build the dialog */
106 gtk_window_set_title(GTK_WINDOW(self
), _("Login to Irreco Database"));
107 gtk_window_set_modal(GTK_WINDOW(self
), TRUE
);
108 gtk_window_set_destroy_with_parent(GTK_WINDOW(self
), TRUE
);
109 gtk_dialog_set_has_separator(GTK_DIALOG(self
), FALSE
);
110 gtk_dialog_add_buttons(GTK_DIALOG(self
),
111 "Register", IRRECO_LOGIN_REGISTER
,
112 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
113 GTK_STOCK_OK
, GTK_RESPONSE_OK
,
116 table
= gtk_table_new(2, 2, FALSE
);
118 label_user
= gtk_label_new("Nickname: ");
119 label_password
= gtk_label_new("Password: ");
120 gtk_table_attach_defaults(GTK_TABLE(table
), label_user
, 0, 1, 0, 1);
121 gtk_table_attach_defaults(GTK_TABLE(table
), label_password
, 0, 1, 1, 2);
123 gtk_misc_set_alignment(GTK_MISC(label_user
), 0, 0.5);
124 gtk_misc_set_alignment(GTK_MISC(label_password
), 0, 0.5);
126 self
->entry_user
= gtk_entry_new();
127 entry_password
= gtk_entry_new();
128 gtk_entry_set_visibility(GTK_ENTRY(entry_password
), FALSE
);
130 /* Get data from entries */
131 self
->user
= gtk_entry_get_text(GTK_ENTRY(self
->entry_user
));
132 self
->password
= gtk_entry_get_text(GTK_ENTRY(entry_password
));
134 gtk_table_attach(GTK_TABLE(table
), self
->entry_user
, 1, 2, 0, 1,
135 GTK_FILL
, GTK_FILL
, 0, 0);
136 gtk_table_attach(GTK_TABLE(table
), entry_password
, 1, 2, 1, 2,
137 GTK_FILL
, GTK_FILL
, 0, 0);
139 align
= gtk_alignment_new(0.5, 0.5, 1, 1);
140 gtk_alignment_set_padding(GTK_ALIGNMENT(align
), 12, 12, 12, 12);
141 gtk_container_add(GTK_CONTAINER(align
), GTK_WIDGET(table
));
142 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(self
)->vbox
),
145 gtk_widget_show_all(GTK_WIDGET(self
));
150 void irreco_login_dlg_store_user(const gchar
*default_user
)
152 GConfClient
* gcClient
= NULL
;
155 g_assert(default_user
);
157 gcClient
= gconf_client_get_default();
159 g_assert(GCONF_IS_CLIENT(gcClient
));
161 if (!gconf_client_set_string(gcClient
, GC_ROOT
"prev_user", default_user
,
163 IRRECO_PRINTF("Error.\n");
166 g_object_unref(gcClient
);
172 const gchar
* irreco_login_dlg_get_stored_user()
174 GConfClient
*gcClient
= NULL
;
175 GConfValue
*val
= NULL
;
176 const gchar
*previous_user
;
180 gcClient
= gconf_client_get_default();
181 g_assert(GCONF_IS_CLIENT(gcClient
));
183 val
= gconf_client_get_without_default(gcClient
, GC_ROOT
186 IRRECO_PRINTF("prev_user not found\n");
187 IRRECO_RETURN_STR("");
190 if (val
->type
== GCONF_VALUE_STRING
) {
191 previous_user
= gconf_value_get_string(val
);
193 IRRECO_PRINTF("prev_user is not a string\n");
194 IRRECO_RETURN_STR("");
197 prev_user
= g_string_new(previous_user
);
199 gconf_value_free(val
);
201 g_object_unref(gcClient
);
204 IRRECO_RETURN_STR(prev_user
->str
);
207 gboolean
irreco_login_dlg_login(IrrecoLoginDlg
*self
)
212 /* Check some things */
213 if(strlen(self
->user
) < 6)
215 irreco_error_dlg(GTK_WINDOW(self
),
216 "Invalid user name.");
217 IRRECO_RETURN_BOOL(FALSE
);
220 if(strlen(self
->password
) < 6)
222 irreco_error_dlg(GTK_WINDOW(self
),
223 "Invalid password.");
224 IRRECO_RETURN_BOOL(FALSE
);
229 pwhash
= sha_compute_checksum_for_string(
230 G_CHECKSUM_SHA1
, self
->password
, -1);
234 if(irreco_login_dlg_login_cache(self
, pwhash
)) {
236 /* Save "default" username for the next time */
237 irreco_login_dlg_store_user(self
->user
);
239 /* Get username and password hash */
240 self
->userptr
= g_strdup(self
->user
);
241 self
->passwdptr
= g_strdup(pwhash
);
243 IRRECO_RETURN_BOOL(TRUE
);
245 IRRECO_RETURN_BOOL(FALSE
);
249 gboolean
irreco_login_dlg_login_cache(
250 IrrecoLoginDlg
*self
, gchar
*pwhash
)
252 IrrecoWebdbCache
*webdb_cache
= NULL
;
255 webdb_cache
= irreco_data_get_webdb_cache(self
->irreco_data
, FALSE
);
257 if(irreco_webdb_cache_login(webdb_cache
, self
->user
, pwhash
))
259 IRRECO_RETURN_BOOL(TRUE
);
262 irreco_error_dlg(GTK_WINDOW(self
),
263 irreco_webdb_cache_get_error(webdb_cache
));
264 IRRECO_RETURN_BOOL(FALSE
);
272 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
273 /* Private Functions */
274 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
277 * @name Private Functions
281 /* Add functions here. */
283 IrrecoLoginDlg
* irreco_login_dlg_new (IrrecoData
*irreco_data
,
286 IrrecoLoginDlg
*self
;
289 self
= g_object_new(IRRECO_TYPE_LOGIN_DLG
, NULL
);
290 irreco_dlg_set_parent(IRRECO_DLG(self
), parent
);
291 self
->irreco_data
= irreco_data
;
293 IRRECO_RETURN_PTR(self
);
300 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
301 /* Public Functions */
302 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
305 * @name Public Functions
309 /* Add functions here. */
311 gboolean
irreco_show_login_dlg(IrrecoData
*irreco_data
, GtkWindow
*parent
,
312 gchar
**userptr
, gchar
**passwdptr
)
314 IrrecoLoginDlg
*self
;
316 gboolean loop
= TRUE
;
317 gboolean rvalue
= TRUE
;
318 const gchar
*temp_user
;
322 /* Create login dialog */
323 self
= g_object_new(IRRECO_TYPE_LOGIN_DLG
, NULL
);
324 irreco_dlg_set_parent(IRRECO_DLG(self
), parent
);
325 self
->irreco_data
= irreco_data
;
327 /* Previously used username to text entry */
328 temp_user
= irreco_login_dlg_get_stored_user();
330 if(temp_user
!= NULL
) {
331 gtk_entry_set_text(GTK_ENTRY(self
->entry_user
), temp_user
);
337 response
= gtk_dialog_run(GTK_DIALOG(self
));
341 case IRRECO_LOGIN_REGISTER
:
342 irreco_show_webdb_register_dlg(
343 self
->irreco_data
, GTK_WINDOW(
346 case GTK_RESPONSE_CANCEL
:
350 case GTK_RESPONSE_OK
:
352 if(irreco_login_dlg_login(self
))
354 /* Get username and pw-hash */
355 *userptr
= g_strdup(self
->userptr
);
356 *passwdptr
= g_strdup(self
->passwdptr
);
358 irreco_info_dlg(GTK_WINDOW(self
),
359 "Login successful.");
374 gtk_widget_destroy(GTK_WIDGET(self
));
375 IRRECO_RETURN_BOOL(rvalue
);
382 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
383 /* Events and Callbacks */
384 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
387 * @name Events and Callbacks
391 /* Add functions here. */