Doxygen documentation updated
[irreco.git] / irreco / src / core / irreco_webdb_upload_dlg.c
blobdba780adf3df46f6abfb34f400c3c2466a00bca1
1 /*
2 * irreco - Ir Remote Control
3 * Copyright (C) 2007,2008 Sami Mäki (kasmra@xob.kapsi.fi)
4 * Joni Kokko (t5kojo01@students.oamk.fi)
5 * Harri Vattulainen (t5vaha01@students.oamk.fi)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "irreco_webdb_upload_dlg.h"
23 #include "irreco_webdb_register_dlg.h"
24 #include "irreco_select_instance_dlg.h"
26 /* Include the prototypes for GConf client functions. */
27 #include <gconf/gconf-client.h>
29 /**
30 * @addtogroup IrrecoWebdbUploadDlg
31 * @ingroup Irreco
33 * This file builds IrrecoWebdbUploadDlg, which is used for uploading your own
34 * device configurations into the Irreco Web Database.
36 * @{
39 /**
40 * @file
41 * Source file of @ref IrrecoWebdbUploadDlg.
44 #define APP_NAME "irreco"
45 #define GC_ROOT "/apps/Maemo/" APP_NAME "/"
47 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
48 /* Prototypes */
49 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
51 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
52 /* Datatypes */
53 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
55 /** Button response codes. */
56 enum {
57 IRRECO_UPLOAD_REGISTER
60 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
61 /* Construction & Destruction */
62 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
64 /**
65 * @name Construction & Destruction
66 * @{
69 G_DEFINE_TYPE (IrrecoWebdbUploadDlg, irreco_webdb_upload_dlg, IRRECO_TYPE_DLG)
71 static void irreco_webdb_upload_dlg_dispose (GObject *object)
73 if (G_OBJECT_CLASS (irreco_webdb_upload_dlg_parent_class)->dispose)
74 G_OBJECT_CLASS (
75 irreco_webdb_upload_dlg_parent_class)->dispose (object);
78 static void irreco_webdb_upload_dlg_finalize (GObject *object)
80 if (G_OBJECT_CLASS (irreco_webdb_upload_dlg_parent_class)->finalize)
81 G_OBJECT_CLASS (
82 irreco_webdb_upload_dlg_parent_class)->finalize (object);
85 static void
86 irreco_webdb_upload_dlg_class_init (IrrecoWebdbUploadDlgClass *klass)
88 GObjectClass *object_class = G_OBJECT_CLASS (klass);
90 object_class->dispose = irreco_webdb_upload_dlg_dispose;
91 object_class->finalize = irreco_webdb_upload_dlg_finalize;
94 static void irreco_webdb_upload_dlg_init (IrrecoWebdbUploadDlg *self)
96 GtkWidget *table;
97 GtkWidget *label_category;
98 GtkWidget *label_manufacturer;
99 GtkWidget *label_model;
100 GtkWidget *label_user;
101 GtkWidget *label_password;
102 GtkWidget *entry_password;
103 GtkWidget *align;
104 IRRECO_ENTER
106 /* Build the dialog */
107 gtk_window_set_title(GTK_WINDOW(self), _("Upload device to Irreco Database"));
108 gtk_window_set_modal(GTK_WINDOW(self), TRUE);
109 gtk_window_set_destroy_with_parent(GTK_WINDOW(self), TRUE);
110 gtk_dialog_set_has_separator(GTK_DIALOG(self), FALSE);
111 gtk_dialog_add_buttons(GTK_DIALOG(self),
112 "Register", IRRECO_UPLOAD_REGISTER,
113 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
114 GTK_STOCK_OK, GTK_RESPONSE_OK,
115 NULL);
117 table = gtk_table_new(7, 2, FALSE);
119 label_category = gtk_label_new("Category: ");
120 label_manufacturer = gtk_label_new("Manufacturer: ");
121 label_model = gtk_label_new("Model: ");
122 label_user = gtk_label_new("User: ");
123 label_password = gtk_label_new("Password: ");
124 gtk_table_attach_defaults(GTK_TABLE(table), label_category, 0, 1, 0, 1);
125 gtk_table_attach_defaults(GTK_TABLE(table), label_manufacturer,
126 0, 1, 1, 2);
127 gtk_table_attach_defaults(GTK_TABLE(table), label_model, 0, 1, 2, 3);
128 gtk_table_attach_defaults(GTK_TABLE(table), label_user, 0, 1, 3, 4);
129 gtk_table_attach_defaults(GTK_TABLE(table), label_password, 0, 1, 4, 5);
130 gtk_misc_set_alignment(GTK_MISC(label_category), 0, 0.5);
131 gtk_misc_set_alignment(GTK_MISC(label_manufacturer), 0, 0.5);
132 gtk_misc_set_alignment(GTK_MISC(label_model), 0, 0.5);
133 gtk_misc_set_alignment(GTK_MISC(label_user), 0, 0.5);
134 gtk_misc_set_alignment(GTK_MISC(label_password), 0, 0.5);
136 self->categories = gtk_combo_box_entry_new_text();
137 self->manufacturers = gtk_combo_box_entry_new_text();
138 self->models = gtk_entry_new();
139 self->entry_user = gtk_entry_new();
140 entry_password = gtk_entry_new();
141 gtk_entry_set_visibility(GTK_ENTRY(entry_password), FALSE);
143 /* Get data from entries */
144 self->user = gtk_entry_get_text(GTK_ENTRY(self->entry_user));
145 self->password = gtk_entry_get_text(GTK_ENTRY(entry_password));
147 gtk_table_attach(GTK_TABLE(table), self->categories, 1, 2, 0, 1,
148 GTK_FILL, GTK_FILL, 0, 2);
149 gtk_table_attach(GTK_TABLE(table), self->manufacturers, 1, 2, 1, 2,
150 GTK_FILL, GTK_FILL, 0, 2);
151 gtk_table_attach(GTK_TABLE(table), self->models, 1, 2, 2, 3,
152 GTK_FILL, GTK_FILL, 0, 0);
153 gtk_table_attach(GTK_TABLE(table), self->entry_user, 1, 2, 3, 4,
154 GTK_FILL, GTK_FILL, 0, 0);
155 gtk_table_attach(GTK_TABLE(table), entry_password, 1, 2, 4, 5,
156 GTK_FILL, GTK_FILL, 0, 0);
158 align = gtk_alignment_new(0.5, 0.5, 1, 1);
159 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 12, 12, 12, 12);
160 gtk_container_add(GTK_CONTAINER(align), GTK_WIDGET(table));
161 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(self)->vbox),
162 GTK_WIDGET(align));
164 gtk_widget_set_size_request(GTK_WIDGET(self), 455, -1);
166 gtk_widget_show_all(GTK_WIDGET(self));
168 IRRECO_RETURN
171 void irreco_webdb_upload_dlg_store_user(const gchar *default_user)
173 GConfClient* gcClient = NULL;
174 IRRECO_ENTER
176 g_assert(default_user);
178 gcClient = gconf_client_get_default();
180 g_assert(GCONF_IS_CLIENT(gcClient));
182 if (!gconf_client_set_string(gcClient, GC_ROOT "prev_user", default_user,
183 NULL)) {
184 IRRECO_PRINTF("Error.\n");
187 g_object_unref(gcClient);
188 gcClient = NULL;
190 IRRECO_RETURN
193 const gchar* irreco_webdb_upload_dlg_get_stored_user()
195 GConfClient *gcClient = NULL;
196 GConfValue *val = NULL;
197 const gchar *previous_user;
198 GString *prev_user;
199 IRRECO_ENTER
201 gcClient = gconf_client_get_default();
202 g_assert(GCONF_IS_CLIENT(gcClient));
204 val = gconf_client_get_without_default(gcClient, GC_ROOT
205 "prev_user", NULL);
206 if (val == NULL) {
207 IRRECO_PRINTF("prev_user not found\n");
208 IRRECO_RETURN_STR("");
211 if (val->type == GCONF_VALUE_STRING) {
212 previous_user = gconf_value_get_string(val);
213 } else {
214 IRRECO_PRINTF("prev_user is not a string\n");
215 IRRECO_RETURN_STR("");
218 prev_user = g_string_new(previous_user);
220 gconf_value_free(val);
221 val = NULL;
222 g_object_unref(gcClient);
223 gcClient = NULL;
225 IRRECO_RETURN_STR(prev_user->str);
228 gboolean irreco_webdb_upload_dlg_add_configuration(IrrecoWebdbUploadDlg *self)
230 gchar *pwhash;
231 IRRECO_ENTER
233 /* Check some things */
234 self->category = gtk_combo_box_get_active_text(GTK_COMBO_BOX(
235 self->categories));
236 self->manufacturer = gtk_combo_box_get_active_text(
237 GTK_COMBO_BOX( self->manufacturers));
239 if(strlen(self->category) == 0)
241 irreco_error_dlg(GTK_WINDOW(self),
242 "Empty category field.");
243 IRRECO_RETURN_BOOL(FALSE);
246 if(strlen(self->manufacturer) == 0)
248 irreco_error_dlg(GTK_WINDOW(self),
249 "Empty manufacturer field.");
250 IRRECO_RETURN_BOOL(FALSE);
253 if(strlen(self->model) == 0)
255 irreco_error_dlg(GTK_WINDOW(self),
256 "Empty model field.");
257 IRRECO_RETURN_BOOL(FALSE);
260 if(strlen(self->user) < 6)
262 irreco_error_dlg(GTK_WINDOW(self),
263 "Invalid user name.");
264 IRRECO_RETURN_BOOL(FALSE);
267 if(strlen(self->password) < 6)
269 irreco_error_dlg(GTK_WINDOW(self),
270 "Invalid password.");
271 IRRECO_RETURN_BOOL(FALSE);
274 else
276 pwhash = sha_compute_checksum_for_string(
277 G_CHECKSUM_SHA1, self->password, -1);
280 /* Add configuration to webdb */
281 if(irreco_webdb_upload_dlg_add_configuration_cache(self, pwhash)) {
283 /* Save "default" username for the next time */
284 irreco_webdb_upload_dlg_store_user(self->user);
286 IRRECO_RETURN_BOOL(TRUE);
287 } else {
288 IRRECO_RETURN_BOOL(FALSE);
292 gboolean irreco_webdb_upload_dlg_add_configuration_cache(
293 IrrecoWebdbUploadDlg *self, gchar *pwhash)
295 IrrecoWebdbCache *webdb_cache = NULL;
296 IRRECO_ENTER
298 webdb_cache = irreco_data_get_webdb_cache(self->irreco_data, FALSE);
300 if(irreco_webdb_cache_upload_configuration(webdb_cache,
301 self->backend,
302 self->category, self->file_hash,
303 self->file_name, self->manufacturer,
304 self->model, pwhash, self->user,
305 self->data)) {
306 IRRECO_RETURN_BOOL(TRUE);
307 } else {
308 irreco_error_dlg(GTK_WINDOW(self),
309 irreco_webdb_cache_get_error(webdb_cache));
310 IRRECO_RETURN_BOOL(FALSE);
314 /** @} */
318 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
319 /* Private Functions */
320 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
323 * @name Private Functions
324 * @{
327 IrrecoWebdbUploadDlg *irreco_webdb_upload_dlg_new (IrrecoData *irreco_data,
328 GtkWindow *parent)
330 IrrecoWebdbUploadDlg *self;
331 IRRECO_ENTER
333 self = g_object_new(IRRECO_TYPE_WEBDB_UPLOAD_DLG, NULL);
334 irreco_dlg_set_parent(IRRECO_DLG(self), parent);
335 self->irreco_data = irreco_data;
337 IRRECO_RETURN_PTR(self);
340 gboolean irreco_webdb_upload_dlg_populate_category_cbentry(
341 IrrecoWebdbUploadDlg *self)
343 IrrecoStringTable *categories = NULL;
344 IrrecoWebdbCache *webdb_cache = NULL;
345 IRRECO_ENTER
347 webdb_cache = irreco_data_get_webdb_cache(self->irreco_data, FALSE);
349 /* Get categories */
350 if(irreco_webdb_cache_get_all_categories(webdb_cache, &categories)) {
351 IRRECO_STRING_TABLE_FOREACH_KEY(categories, key)
353 gtk_combo_box_append_text(GTK_COMBO_BOX(self->categories),
354 key);
356 IRRECO_STRING_TABLE_FOREACH_END
357 irreco_string_table_free(categories);
359 } else {
360 irreco_error_dlg(GTK_WINDOW(self), irreco_webdb_cache_get_error(
361 webdb_cache));
362 IRRECO_RETURN_BOOL(FALSE);
365 IRRECO_RETURN_BOOL(TRUE);
368 gboolean irreco_webdb_upload_dlg_populate_manufacturer_cbentry(
369 IrrecoWebdbUploadDlg *self)
371 IrrecoStringTable *manufacturers = NULL;
372 IrrecoWebdbCache *webdb_cache = NULL;
373 IRRECO_ENTER
375 webdb_cache = irreco_data_get_webdb_cache(self->irreco_data,
376 FALSE);
378 /* Get manufacturers */
379 if(irreco_webdb_cache_get_all_manufacturers(webdb_cache,
380 &manufacturers)){
382 IRRECO_STRING_TABLE_FOREACH_KEY(manufacturers, key)
384 gtk_combo_box_append_text(GTK_COMBO_BOX(
385 self->manufacturers),
386 key);
388 IRRECO_STRING_TABLE_FOREACH_END
389 irreco_string_table_free(manufacturers);
390 } else {
391 irreco_error_dlg(GTK_WINDOW(self),
392 irreco_webdb_cache_get_error(
393 webdb_cache));
396 IRRECO_RETURN_BOOL(TRUE);
399 /** @} */
403 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
404 /* Functions */
405 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
408 * @name Public Functions
409 * @{
412 void irreco_show_webdb_upload_dlg(IrrecoData *irreco_data, GtkWindow *parent,
413 IrrecoBackendFileContainer *file_container)
415 IrrecoWebdbUploadDlg *self;
416 gint response;
417 gboolean loop = TRUE;
418 const gchar *temp_user;
420 IRRECO_ENTER
422 /* Create upload dialog */
423 self = IRRECO_WEBDB_UPLOAD_DLG(
424 irreco_webdb_upload_dlg_new(irreco_data, parent));
426 /* Populate some ComboBoxEntries */
427 if(irreco_webdb_upload_dlg_populate_category_cbentry(self)) {
428 gtk_combo_box_set_active(GTK_COMBO_BOX(self->categories), 0);
429 irreco_webdb_upload_dlg_populate_manufacturer_cbentry(self);
430 gtk_combo_box_set_active(GTK_COMBO_BOX(self->manufacturers), 0);
431 } else {
432 gtk_widget_destroy(GTK_WIDGET(self));
433 IRRECO_RETURN
436 if(file_container->category->len > 0) {
437 gtk_combo_box_append_text(GTK_COMBO_BOX(self->categories),
438 file_container->category->str);
441 if(file_container->manufacturer->len > 0) {
442 gtk_combo_box_append_text(GTK_COMBO_BOX(self->manufacturers),
443 file_container->manufacturer->str);
446 /* Previously uploaded username to text entry */
447 temp_user = irreco_webdb_upload_dlg_get_stored_user();
449 if(temp_user != NULL) {
450 gtk_entry_set_text(GTK_ENTRY(self->entry_user), temp_user);
453 /* Get model from TextEntry
454 Get backend, filename and filehash from file_container */
455 gtk_entry_set_text(GTK_ENTRY(self->models), file_container->model->str);
456 self->model = gtk_entry_get_text(GTK_ENTRY(self->models));
457 self->backend = file_container->backend->str;
458 self->file_hash = file_container->hash->str;
459 self->file_name = file_container->name->str;
460 self->data = file_container->data->str;
464 /* Show upload dlg */
465 response = gtk_dialog_run(GTK_DIALOG(self));
467 switch(response)
469 case IRRECO_UPLOAD_REGISTER:
470 irreco_show_webdb_register_dlg(
471 self->irreco_data, GTK_WINDOW(
472 self));
473 break;
474 case GTK_RESPONSE_CANCEL:
475 loop = FALSE;
476 break;
477 case GTK_RESPONSE_OK:
478 if(irreco_webdb_upload_dlg_add_configuration
479 (self)) {
480 irreco_info_dlg(GTK_WINDOW(self),
481 "Upload successful");
482 } else {
483 break;
486 loop = FALSE;
487 break;
488 default:
489 break;
491 }while(loop);
493 gtk_widget_destroy(GTK_WIDGET(self));
494 IRRECO_RETURN
497 /** @} */
501 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
502 /* Events and Callbacks */
503 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
505 /** @} */