Changed and added some author names etc.
[irreco.git] / irreco / trunk / src / core / irreco_webdb_upload_dlg.c
blob76e07dee1bdc540bb178acd2230b8bd97e71c273
1 /*
2 * irreco - Ir Remote Control
3 * Copyright (C) 2007,2008 Sami Mäki (t5masa02@students.oamk.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 * PURPOSE OF FILE. TODO TODO TODO
29 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
30 /* Prototypes */
31 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
33 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
34 /* Datatypes */
35 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
37 /** Button response codes. */
38 enum {
39 IRRECO_UPLOAD_REGISTER
42 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
43 /* Construction & Destruction */
44 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
46 G_DEFINE_TYPE (IrrecoWebdbUploadDlg, irreco_webdb_upload_dlg, IRRECO_TYPE_DLG)
48 static void irreco_webdb_upload_dlg_dispose (GObject *object)
50 if (G_OBJECT_CLASS (irreco_webdb_upload_dlg_parent_class)->dispose)
51 G_OBJECT_CLASS (
52 irreco_webdb_upload_dlg_parent_class)->dispose (object);
55 static void irreco_webdb_upload_dlg_finalize (GObject *object)
57 if (G_OBJECT_CLASS (irreco_webdb_upload_dlg_parent_class)->finalize)
58 G_OBJECT_CLASS (
59 irreco_webdb_upload_dlg_parent_class)->finalize (object);
62 static void
63 irreco_webdb_upload_dlg_class_init (IrrecoWebdbUploadDlgClass *klass)
65 GObjectClass *object_class = G_OBJECT_CLASS (klass);
67 object_class->dispose = irreco_webdb_upload_dlg_dispose;
68 object_class->finalize = irreco_webdb_upload_dlg_finalize;
71 static void irreco_webdb_upload_dlg_init (IrrecoWebdbUploadDlg *self)
73 GtkWidget *table;
74 GtkWidget *label_category;
75 GtkWidget *label_manufacturer;
76 GtkWidget *label_model;
77 GtkWidget *label_user;
78 GtkWidget *label_password;
79 GtkWidget *entry_user;
80 GtkWidget *entry_password;
81 GtkWidget *align;
82 IRRECO_ENTER
84 /* Build the dialog */
85 gtk_window_set_title(GTK_WINDOW(self), _("Upload device"));
86 gtk_window_set_modal(GTK_WINDOW(self), TRUE);
87 gtk_window_set_destroy_with_parent(GTK_WINDOW(self), TRUE);
88 gtk_dialog_set_has_separator(GTK_DIALOG(self), FALSE);
89 gtk_dialog_add_buttons(GTK_DIALOG(self),
90 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
91 "Register", IRRECO_UPLOAD_REGISTER,
92 GTK_STOCK_OK, GTK_RESPONSE_OK,
93 NULL);
95 table = gtk_table_new(7, 2, FALSE);
97 label_category = gtk_label_new("Category: ");
98 label_manufacturer = gtk_label_new("Manufacturer: ");
99 label_model = gtk_label_new("Model: ");
100 label_user = gtk_label_new("User: ");
101 label_password = gtk_label_new("Password: ");
102 gtk_table_attach_defaults(GTK_TABLE(table), label_category, 0, 1, 0, 1);
103 gtk_table_attach_defaults(GTK_TABLE(table), label_manufacturer,
104 0, 1, 1, 2);
105 gtk_table_attach_defaults(GTK_TABLE(table), label_model, 0, 1, 2, 3);
106 gtk_table_attach_defaults(GTK_TABLE(table), label_user, 0, 1, 3, 4);
107 gtk_table_attach_defaults(GTK_TABLE(table), label_password, 0, 1, 4, 5);
108 gtk_misc_set_alignment(GTK_MISC(label_category), 0, 0.5);
109 gtk_misc_set_alignment(GTK_MISC(label_manufacturer), 0, 0.5);
110 gtk_misc_set_alignment(GTK_MISC(label_model), 0, 0.5);
111 gtk_misc_set_alignment(GTK_MISC(label_user), 0, 0.5);
112 gtk_misc_set_alignment(GTK_MISC(label_password), 0, 0.5);
114 self->categories = gtk_combo_box_entry_new_text();
115 self->manufacturers = gtk_combo_box_entry_new_text();
116 self->models = gtk_entry_new();
117 entry_user = gtk_entry_new();
118 entry_password = gtk_entry_new();
119 gtk_entry_set_visibility(GTK_ENTRY(entry_password), FALSE);
121 /* Get data from entries */
122 self->user = gtk_entry_get_text(GTK_ENTRY(entry_user));
123 self->password = gtk_entry_get_text(GTK_ENTRY(entry_password));
125 gtk_table_attach(GTK_TABLE(table), self->categories, 1, 2, 0, 1,
126 GTK_FILL, GTK_FILL, 0, 2);
127 gtk_table_attach(GTK_TABLE(table), self->manufacturers, 1, 2, 1, 2,
128 GTK_FILL, GTK_FILL, 0, 2);
129 gtk_table_attach(GTK_TABLE(table), self->models, 1, 2, 2, 3,
130 GTK_FILL, GTK_FILL, 0, 0);
131 gtk_table_attach(GTK_TABLE(table), entry_user, 1, 2, 3, 4,
132 GTK_FILL, GTK_FILL, 0, 0);
133 gtk_table_attach(GTK_TABLE(table), entry_password, 1, 2, 4, 5,
134 GTK_FILL, GTK_FILL, 0, 0);
136 align = gtk_alignment_new(0.5, 0.5, 1, 1);
137 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 12, 12, 12, 12);
138 gtk_container_add(GTK_CONTAINER(align), GTK_WIDGET(table));
139 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(self)->vbox),
140 GTK_WIDGET(align));
142 gtk_widget_show_all(GTK_WIDGET(self));
144 IRRECO_RETURN
147 gboolean irreco_webdb_upload_dlg_add_configuration(IrrecoWebdbUploadDlg *self)
149 gchar *pwhash;
150 IRRECO_ENTER
152 /* Check some things */
153 self->category = gtk_combo_box_get_active_text(GTK_COMBO_BOX(
154 self->categories));
155 self->manufacturer = gtk_combo_box_get_active_text(
156 GTK_COMBO_BOX( self->manufacturers));
158 if(strlen(self->category) == 0)
160 irreco_error_dlg(GTK_WINDOW(self),
161 "Empty category field.");
162 IRRECO_RETURN_BOOL(FALSE);
165 if(strlen(self->manufacturer) == 0)
167 irreco_error_dlg(GTK_WINDOW(self),
168 "Empty manufacturer field.");
169 IRRECO_RETURN_BOOL(FALSE);
172 if(strlen(self->model) == 0)
174 irreco_error_dlg(GTK_WINDOW(self),
175 "Empty model field.");
176 IRRECO_RETURN_BOOL(FALSE);
179 if(strlen(self->user) < 6)
181 irreco_error_dlg(GTK_WINDOW(self),
182 "Invalid user name.");
183 IRRECO_RETURN_BOOL(FALSE);
186 if(strlen(self->password) < 6)
188 irreco_error_dlg(GTK_WINDOW(self),
189 "Invalid password.");
190 IRRECO_RETURN_BOOL(FALSE);
193 else
195 pwhash = sha_compute_checksum_for_string(
196 G_CHECKSUM_SHA1, self->password, -1);
199 /* Add configuration to webdb */
200 if(irreco_webdb_upload_dlg_add_configuration_cache(self, pwhash)) {
201 IRRECO_RETURN_BOOL(TRUE);
202 } else {
203 IRRECO_RETURN_BOOL(FALSE);
207 gboolean irreco_webdb_upload_dlg_add_configuration_cache(
208 IrrecoWebdbUploadDlg *self, gchar *pwhash)
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_upload_configuration(webdb_cache,
216 self->backend,
217 self->category, self->file_hash,
218 self->file_name, self->manufacturer,
219 self->model, pwhash, self->user,
220 self->data)) {
221 IRRECO_RETURN_BOOL(TRUE);
222 } else {
223 irreco_error_dlg(GTK_WINDOW(self),
224 irreco_webdb_cache_get_error(webdb_cache));
225 IRRECO_RETURN_BOOL(FALSE);
229 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
230 /* Private Functions */
231 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
233 IrrecoWebdbUploadDlg *irreco_webdb_upload_dlg_new (IrrecoData *irreco_data,
234 GtkWindow *parent)
236 IrrecoWebdbUploadDlg *self;
237 IRRECO_ENTER
239 self = g_object_new(IRRECO_TYPE_WEBDB_UPLOAD_DLG, NULL);
240 irreco_dlg_set_parent(IRRECO_DLG(self), parent);
241 self->irreco_data = irreco_data;
243 IRRECO_RETURN_PTR(self);
246 gboolean irreco_webdb_upload_dlg_populate_category_cbentry(
247 IrrecoWebdbUploadDlg *self)
249 IrrecoStringTable *categories = NULL;
250 IrrecoWebdbCache *webdb_cache = NULL;
251 IRRECO_ENTER
253 webdb_cache = irreco_data_get_webdb_cache(self->irreco_data, FALSE);
255 /* Get categories */
256 if(irreco_webdb_cache_get_all_categories(webdb_cache, &categories)) {
257 IRRECO_STRING_TABLE_FOREACH_KEY(categories, key)
259 gtk_combo_box_append_text(GTK_COMBO_BOX(self->categories),
260 key);
262 IRRECO_STRING_TABLE_FOREACH_END
263 irreco_string_table_free(categories);
265 } else {
266 irreco_error_dlg(GTK_WINDOW(self), irreco_webdb_cache_get_error(
267 webdb_cache));
268 IRRECO_RETURN_BOOL(FALSE);
271 IRRECO_RETURN_BOOL(TRUE);
274 gboolean irreco_webdb_upload_dlg_populate_manufacturer_cbentry(
275 IrrecoWebdbUploadDlg *self)
277 IrrecoStringTable *manufacturers = NULL;
278 IrrecoWebdbCache *webdb_cache = NULL;
279 IRRECO_ENTER
281 webdb_cache = irreco_data_get_webdb_cache(self->irreco_data,
282 FALSE);
284 /* Get manufacturers */
285 if(irreco_webdb_cache_get_all_manufacturers(webdb_cache,
286 &manufacturers)){
288 IRRECO_STRING_TABLE_FOREACH_KEY(manufacturers, key)
290 gtk_combo_box_append_text(GTK_COMBO_BOX(
291 self->manufacturers),
292 key);
294 IRRECO_STRING_TABLE_FOREACH_END
295 irreco_string_table_free(manufacturers);
296 } else {
297 irreco_error_dlg(GTK_WINDOW(self),
298 irreco_webdb_cache_get_error(
299 webdb_cache));
302 IRRECO_RETURN_BOOL(TRUE);
306 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
307 /* Functions */
308 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
310 void irreco_show_webdb_upload_dlg(IrrecoData *irreco_data, GtkWindow *parent,
311 IrrecoBackendFileContainer *file_container)
313 IrrecoWebdbUploadDlg *self;
314 gint response;
315 gboolean loop = TRUE;
317 IRRECO_ENTER
319 /* Create upload dialog */
320 self = IRRECO_WEBDB_UPLOAD_DLG(
321 irreco_webdb_upload_dlg_new(irreco_data, parent));
323 /* Populate some ComboBoxEntries */
324 if(irreco_webdb_upload_dlg_populate_category_cbentry(self)) {
325 gtk_combo_box_set_active(GTK_COMBO_BOX(self->categories), 0);
326 irreco_webdb_upload_dlg_populate_manufacturer_cbentry(self);
327 gtk_combo_box_set_active(GTK_COMBO_BOX(self->manufacturers), 0);
328 } else {
329 gtk_widget_destroy(GTK_WIDGET(self));
330 IRRECO_RETURN
333 if(file_container->category->len > 0) {
334 gtk_combo_box_append_text(GTK_COMBO_BOX(self->categories),
335 file_container->category->str);
338 if(file_container->manufacturer->len > 0) {
339 gtk_combo_box_append_text(GTK_COMBO_BOX(self->manufacturers),
340 file_container->manufacturer->str);
343 /* Get model from TextEntry
344 Get backend, filename and filehash from file_container */
346 gtk_entry_set_text(GTK_ENTRY(self->models), file_container->model->str);
347 self->model = gtk_entry_get_text(GTK_ENTRY(self->models));
348 self->backend = file_container->backend->str;
349 self->file_hash = file_container->hash->str;
350 self->file_name = file_container->name->str;
351 self->data = file_container->data->str;
355 /* Show upload dlg */
356 response = gtk_dialog_run(GTK_DIALOG(self));
358 switch(response)
360 case GTK_RESPONSE_CANCEL:
361 loop = FALSE;
362 break;
363 case IRRECO_UPLOAD_REGISTER:
364 irreco_show_webdb_register_dlg(
365 self->irreco_data, GTK_WINDOW(
366 self));
367 break;
368 case GTK_RESPONSE_OK:
369 if(irreco_webdb_upload_dlg_add_configuration
370 (self)) {
371 irreco_info_dlg(GTK_WINDOW(self),
372 "Upload successful");
373 } else {
374 break;
377 loop = FALSE;
378 break;
379 default:
380 break;
382 }while(loop);
384 gtk_widget_destroy(GTK_WIDGET(self));
385 IRRECO_RETURN
388 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
389 /* Events and Callbacks */
390 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/