Blocked Build In theme uploading
[irreco.git] / irreco / src / core / irreco_remote_upload_dlg.c
blob76535ced088880488c8dc51eafc8ce74144c695c
1 /*
2 * irreco - Ir Remote Control
3 * Copyright (C) 2008 Joni Kokko (t5kojo01@students.oamk.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_remote_upload_dlg.h"
21 #include "irreco_login_dlg.h"
22 #include "irreco_theme_upload_dlg.h"
23 #include "irreco_webdb_client.h"
25 /** Loader states. */
26 enum
28 LOADER_STATE_INIT,
29 LOADER_STATE_CONFIGURATIONS,
30 LOADER_STATE_THEMES,
31 LOADER_STATE_END
35 /**
36 * @addtogroup IrrecoRemoteUploadDlg
37 * @ingroup Irreco
39 * @todo PURPOCE OF CLASS.
41 * @{
44 /**
45 * @file
46 * Source file of @ref IrrecoRemoteUploadDlg.
50 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
51 /* Prototypes */
52 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
54 void irreco_remote_upload_dlg_comment_size_request(GtkWidget *widget,
55 GtkRequisition *requisition,
56 IrrecoRemoteUploadDlg *self);
58 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
59 /* Construction & Destruction */
60 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
62 /**
63 * @name Construction & Destruction
64 * @{
67 G_DEFINE_TYPE(IrrecoRemoteUploadDlg, irreco_remote_upload_dlg, IRRECO_TYPE_DLG)
69 static void irreco_remote_upload_dlg_dispose(GObject *object)
71 G_OBJECT_CLASS(irreco_remote_upload_dlg_parent_class)->dispose(object);
74 static void irreco_remote_upload_dlg_finalize(GObject *object)
76 G_OBJECT_CLASS(irreco_remote_upload_dlg_parent_class)->finalize(object);
79 static void irreco_remote_upload_dlg_class_init(
80 IrrecoRemoteUploadDlgClass *klass)
82 GObjectClass *object_class = G_OBJECT_CLASS (klass);
85 object_class->dispose = irreco_remote_upload_dlg_dispose;
86 object_class->finalize = irreco_remote_upload_dlg_finalize;
89 static void irreco_remote_upload_dlg_init(IrrecoRemoteUploadDlg *self)
91 GtkWidget *table;
92 GtkWidget *label_category;
93 GtkWidget *label_manufacturer;
94 GtkWidget *label_model;
95 GtkWidget *label_comment;
96 GtkWidget *align;
97 GtkWidget *comment_frame;
98 IRRECO_ENTER
100 self->remote_uploaded = FALSE;
102 /* Build the dialog */
103 gtk_window_set_title(GTK_WINDOW(self), _("Upload Remote"));
104 gtk_window_set_modal(GTK_WINDOW(self), TRUE);
105 gtk_window_set_destroy_with_parent(GTK_WINDOW(self), TRUE);
106 gtk_dialog_set_has_separator(GTK_DIALOG(self), FALSE);
107 gtk_dialog_add_buttons(GTK_DIALOG(self),
108 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
109 GTK_STOCK_OK, GTK_RESPONSE_OK,
110 NULL);
112 /* labels */
114 label_category = gtk_label_new("Category: ");
115 label_manufacturer = gtk_label_new("Manufacturer: ");
116 label_model = gtk_label_new("Model: ");
117 label_comment = gtk_label_new("Comment: ");
119 gtk_misc_set_alignment(GTK_MISC(label_category), 0, 0.5);
120 gtk_misc_set_alignment(GTK_MISC(label_manufacturer), 0, 0.5);
121 gtk_misc_set_alignment(GTK_MISC(label_model), 0, 0.5);
122 gtk_misc_set_alignment(GTK_MISC(label_comment), 0, 0.5);
124 /* entries */
126 self->category = gtk_combo_box_entry_new_text();
127 self->manufacturer = gtk_combo_box_entry_new_text();
128 self->model = gtk_entry_new();
130 /* comment textview */
132 self->comment = gtk_text_view_new();
133 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(self->comment),
134 GTK_WRAP_WORD_CHAR);
136 comment_frame = gtk_frame_new(NULL);
137 gtk_container_add(GTK_CONTAINER(comment_frame), self->comment);
139 /* table */
141 table = gtk_table_new(5, 2, FALSE);
143 gtk_table_attach(GTK_TABLE(table), label_category, 0, 1, 0, 1,
144 GTK_FILL, GTK_FILL, 0, 2);
145 gtk_table_attach(GTK_TABLE(table), label_manufacturer, 0, 1, 1, 2,
146 GTK_FILL, GTK_FILL, 0, 2);
147 gtk_table_attach(GTK_TABLE(table), label_model, 0, 1, 2, 3,
148 GTK_FILL, GTK_FILL, 0, 2);
149 gtk_table_attach(GTK_TABLE(table), label_comment, 0, 1, 3, 4,
150 GTK_FILL, GTK_FILL, 0, 2);
151 gtk_table_attach(GTK_TABLE(table), self->category, 1, 2, 0, 1,
152 GTK_FILL, GTK_FILL, 0, 2);
153 gtk_table_attach(GTK_TABLE(table), self->manufacturer, 1, 2, 1, 2,
154 GTK_FILL, GTK_FILL, 0, 2);
155 gtk_table_attach(GTK_TABLE(table), self->model, 1, 2, 2, 3,
156 GTK_FILL, GTK_FILL, 0, 0);
157 gtk_table_attach(GTK_TABLE(table), comment_frame, 0, 2, 4, 5,
158 GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 0, 0);
160 /* scroll */
162 self->scroll = gtk_scrolled_window_new(NULL, NULL);
163 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(self->scroll),
164 GTK_POLICY_NEVER,
165 GTK_POLICY_AUTOMATIC);
166 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(self->scroll),
167 table);
169 /* alignment */
171 align = gtk_alignment_new(0.5, 0.5, 1, 1);
172 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 12, 12, 12, 12);
174 gtk_container_add(GTK_CONTAINER(align), self->scroll);
175 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(self)->vbox),
176 GTK_WIDGET(align));
178 /* Signals */
179 g_signal_connect(G_OBJECT(self->comment), "size-request", G_CALLBACK(
180 irreco_remote_upload_dlg_comment_size_request), self);
182 gtk_widget_set_size_request(GTK_WIDGET(self), -1, 280);
184 gtk_widget_show_all(GTK_WIDGET(self));
186 IRRECO_RETURN
189 IrrecoRemoteUploadDlg* irreco_remote_upload_dlg_new(IrrecoData *irreco_data,
190 GtkWindow *parent)
192 IrrecoRemoteUploadDlg *self;
193 IRRECO_ENTER
195 self = g_object_new(IRRECO_TYPE_REMOTE_UPLOAD_DLG, NULL);
196 irreco_dlg_set_parent(IRRECO_DLG(self), parent);
197 self->irreco_data = irreco_data;
198 IRRECO_RETURN_PTR(self);
202 /** @} */
206 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
207 /* Private Functions */
208 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
211 * @name Private Functions
212 * @{
215 gboolean irreco_remote_upload_dlg_populate_category(IrrecoRemoteUploadDlg *self)
217 IrrecoStringTable *categories = NULL;
218 IrrecoWebdbCache *webdb_cache;
219 IRRECO_ENTER
221 webdb_cache = irreco_data_get_webdb_cache(self->irreco_data, FALSE);
223 /* Get categories */
224 if(irreco_webdb_cache_get_all_categories(webdb_cache, &categories)) {
225 IRRECO_STRING_TABLE_FOREACH_KEY(categories, key)
227 gtk_combo_box_append_text(
228 GTK_COMBO_BOX(self->category), key);
230 IRRECO_STRING_TABLE_FOREACH_END
231 } else {
232 irreco_error_dlg(GTK_WINDOW(self),
233 irreco_webdb_cache_get_error(webdb_cache));
234 IRRECO_RETURN_BOOL(FALSE);
237 if (categories != NULL) irreco_string_table_free(categories);
239 IRRECO_RETURN_BOOL(TRUE);
242 gboolean irreco_remote_upload_dlg_populate_manufacturer(
243 IrrecoRemoteUploadDlg *self)
245 IrrecoStringTable *manufacturers = NULL;
246 IrrecoWebdbCache *webdb_cache;
247 IRRECO_ENTER
249 webdb_cache = irreco_data_get_webdb_cache(self->irreco_data, FALSE);
251 /* Get manufacturers */
252 if(irreco_webdb_cache_get_all_manufacturers(webdb_cache,
253 &manufacturers)) {
254 IRRECO_STRING_TABLE_FOREACH_KEY(manufacturers, key)
256 gtk_combo_box_append_text(
257 GTK_COMBO_BOX(self->manufacturer), key);
259 IRRECO_STRING_TABLE_FOREACH_END
260 } else {
261 irreco_error_dlg(GTK_WINDOW(self),
262 irreco_webdb_cache_get_error(webdb_cache));
263 IRRECO_RETURN_BOOL(FALSE);
266 if (manufacturers != NULL) irreco_string_table_free(manufacturers);
267 IRRECO_RETURN_BOOL(TRUE);
270 gboolean irreco_remote_upload_dlg_send(IrrecoRemoteUploadDlg *self)
272 IrrecoWebdbCache *webdb_cache;
273 IrrecoButtonLayout *layout;
274 gboolean rvalue = FALSE;
275 IRRECO_ENTER
276 webdb_cache = irreco_data_get_webdb_cache(self->irreco_data, FALSE);
277 layout = self->irreco_data->window_manager->current_layout;
279 switch (self->loader_state) {
281 case LOADER_STATE_INIT: {
282 gchar *category = NULL;
283 gchar *manufacturer = NULL;
284 const gchar *model;
285 gchar *comment = NULL;
286 GtkTextIter start;
287 GtkTextIter end;
288 GtkTextBuffer *buffer;
289 GString *path = g_string_new(irreco_get_config_dir("irreco"));
290 gchar *file_data = NULL;
291 gint file_length;
292 IRRECO_DEBUG("LOADER_STATE_INIT");
294 category = gtk_combo_box_get_active_text(
295 GTK_COMBO_BOX(self->category));
297 manufacturer = gtk_combo_box_get_active_text(
298 GTK_COMBO_BOX(self->manufacturer));
300 model = gtk_entry_get_text(GTK_ENTRY(self->model));
302 /* Set new name */
303 if (irreco_string_table_exists(
304 self->irreco_data->irreco_layout_array, model) &&
305 !g_str_equal(layout->name->str, model)) {
306 irreco_error_dlg(GTK_WINDOW(self),
307 _(IRRECO_LAYOUT_NAME_COLLISION));
308 goto end_init;
311 if (!irreco_string_table_change_key(
312 self->irreco_data->irreco_layout_array, layout->name->str,
313 model)) {
314 irreco_error_dlg(GTK_WINDOW(self),
315 "Incompatible model");
316 goto end_init;
319 irreco_config_save_layouts(self->irreco_data);
321 /* comment */
323 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(self->comment));
324 gtk_text_buffer_get_start_iter(GTK_TEXT_BUFFER(buffer), &start);
325 gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER(buffer), &end);
326 comment = gtk_text_buffer_get_text(GTK_TEXT_BUFFER(buffer),
327 &start, &end, FALSE);
329 /* data */
331 g_string_append_printf(path,"/%s",layout->filename->str);
332 file_length = irreco_file_length(path->str);
333 file_data = g_malloc0(file_length);
335 /* check errors */
337 if (!irreco_read_text_file(path->str, file_data, file_length)) {
338 irreco_error_dlg(GTK_WINDOW(self),
339 "Can't open layout file.");
340 goto end_init;
343 if (strlen(category) == 0) {
344 irreco_error_dlg(GTK_WINDOW(self),
345 "Empty category field.");
346 goto end_init;
349 if (strlen(manufacturer) == 0) {
350 irreco_error_dlg(GTK_WINDOW(self),
351 "Empty manufacturer field.");
352 goto end_init;
355 if (strlen(model) == 0) {
356 irreco_error_dlg(GTK_WINDOW(self),
357 "Empty model field.");
358 goto end_init;
361 if (strlen(comment) == 0) {
362 irreco_error_dlg(GTK_WINDOW(self),
363 "Empty comment field.");
366 /* Show login dialog */
368 if (!irreco_show_login_dlg(self->irreco_data, GTK_WINDOW(self),
369 &self->user_name, &self->password)) {
370 IRRECO_DEBUG("Failed login\n");
371 goto end_init;
374 /* create remote to db */
376 self->remote_id = irreco_webdb_client_create_new_remote(
377 webdb_cache->private, comment, category,
378 manufacturer, model, layout->filename->str,
379 file_data, self->user_name, self->password);
381 if (self->remote_id != 0) {
382 rvalue = TRUE;
384 end_init:
385 if (category != NULL) g_free(category);
386 if (manufacturer != NULL) g_free(manufacturer);
387 if (comment != NULL) g_free(comment);
388 if (file_data != NULL) g_free(file_data);
389 g_string_free(path, FALSE);
391 if (rvalue == TRUE) {
392 self->loader_state = LOADER_STATE_CONFIGURATIONS;
393 } else {
394 self->loader_state = LOADER_STATE_END;
397 IRRECO_RETURN_BOOL(TRUE);
399 case LOADER_STATE_CONFIGURATIONS:
400 IRRECO_DEBUG("LOADER_STATE_CONFIGURATIONS\n");
401 self->loader_state = LOADER_STATE_THEMES;
402 IRRECO_RETURN_BOOL(TRUE);
403 case LOADER_STATE_THEMES: {
404 gboolean rvalue = TRUE;
405 IrrecoStringTable *themes = NULL;
406 IRRECO_DEBUG("LOADER_STATE_THEMES\n");
408 if (!irreco_layout_get_themes(self->irreco_data,
409 layout->name->str, &themes)) {
410 goto end_themes;
413 IRRECO_STRING_TABLE_FOREACH(themes, name, gchar *, date) {
414 gint id;
415 id = irreco_webdb_client_get_theme_id_by_name_and_date(
416 webdb_cache->private, name, date);
418 if (id == 0) {
420 /* Get theme */
422 IrrecoTheme *theme;
423 irreco_string_table_get(
424 self->irreco_data->theme_manager->themes,
425 name, (gpointer*) &theme);
427 /* Upload theme to database */
429 if (!irreco_theme_upload_dlg_run(GTK_WINDOW(self),
430 self->irreco_data,
431 theme)) {
432 rvalue = FALSE;
435 id = irreco_webdb_client_get_theme_id_by_name_and_date(
436 webdb_cache->private, theme->name->str,
437 theme->version->str);
440 /* Append theme to remote */
442 if (id != 0) {
443 irreco_webdb_client_add_theme_to_remote(
444 webdb_cache->private, self->remote_id,
445 id, self->user_name, self->password);
448 IRRECO_STRING_TABLE_FOREACH_END
450 /* Set remote downloadable according to the rvalue */
452 irreco_webdb_client_set_remote_downloadable(
453 webdb_cache->private, self->remote_id,
454 rvalue, self->user_name, self->password);
456 irreco_info_dlg(GTK_WINDOW(self),
457 "Remote uploaded successfully!");
458 self->remote_uploaded = TRUE;
459 end_themes:
460 if (themes != NULL) irreco_string_table_free(themes);
462 self->loader_state = LOADER_STATE_END;
463 IRRECO_RETURN_BOOL(TRUE);
465 case LOADER_STATE_END:
466 IRRECO_DEBUG("LOADER_STATE_END\n");
467 g_source_remove(self->loader_func_id);
468 if (self->remote_uploaded == TRUE) {
469 gtk_dialog_response(GTK_DIALOG(self),
470 GTK_RESPONSE_CANCEL);
474 IRRECO_RETURN_BOOL(FALSE);
477 /** @} */
481 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
482 /* Public Functions */
483 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
486 * @name Public Functions
487 * @{
490 void irreco_show_remote_upload_dlg(IrrecoData *irreco_data, GtkWindow *parent)
492 IrrecoRemoteUploadDlg *self;
493 gboolean loop = TRUE;
494 IrrecoButtonLayout *layout =
495 irreco_data->window_manager->current_layout;
496 IrrecoStringTable *themes;
497 gboolean deb_theme_in_layout = FALSE;
498 IRRECO_ENTER
500 self = irreco_remote_upload_dlg_new(irreco_data, parent);
502 irreco_remote_upload_dlg_populate_category(self);
503 irreco_remote_upload_dlg_populate_manufacturer(self);
504 gtk_entry_set_text(GTK_ENTRY(self->model),
505 irreco_button_layout_get_name(layout));
507 irreco_layout_get_themes(irreco_data, layout->name->str, &themes);
508 IRRECO_STRING_TABLE_FOREACH_KEY(themes, theme_name)
509 IrrecoTheme *theme;
510 irreco_string_table_get(irreco_data->theme_manager->themes,
511 theme_name, (gpointer *) &theme);
512 if (g_str_equal(theme->source->str, "deb")) {
513 deb_theme_in_layout = TRUE;
515 IRRECO_STRING_TABLE_FOREACH_END
517 if (deb_theme_in_layout) {
518 irreco_error_dlg(GTK_WINDOW(self),
519 "You can't upload remotes with Build In themes ");
520 goto end;
523 do {
524 gint response = gtk_dialog_run(GTK_DIALOG(self));
525 switch (response) {
526 case GTK_RESPONSE_CANCEL:
527 IRRECO_DEBUG("GTK_RESPONSE_CANCEL\n");
528 loop = FALSE;
529 break;
531 case GTK_RESPONSE_OK:
532 IRRECO_DEBUG("GTK_RESPONSE_OK\n");
533 self->loader_state = LOADER_STATE_INIT;
534 self->loader_func_id = g_idle_add((GSourceFunc)
535 irreco_remote_upload_dlg_send, self);
536 break;
539 } while (loop);
541 end:
542 irreco_string_table_free(themes);
543 gtk_widget_destroy(GTK_WIDGET(self));
544 IRRECO_RETURN
547 /** @} */
551 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
552 /* Events and Callbacks */
553 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
556 * @name Events and Callbacks
557 * @{
560 void irreco_remote_upload_dlg_comment_size_request(GtkWidget *widget,
561 GtkRequisition *requisition,
562 IrrecoRemoteUploadDlg *self)
564 GtkAdjustment* vadjustment = gtk_scrolled_window_get_vadjustment(
565 GTK_SCROLLED_WINDOW(self->scroll));
566 IRRECO_ENTER
568 if (requisition->height > self->cursor_position) {
569 vadjustment->value += 27;
570 } else if (requisition->height < self->cursor_position) {
571 vadjustment->value -= 27;
573 self->cursor_position = requisition->height;
575 IRRECO_RETURN
578 /** @} */
580 /** @} */