RemoteDownloadDialog will shutdown automatically after downloading
[irreco.git] / irreco / src / core / irreco_remote_download_dlg.c
blob9317a243e417dd2cffc2cdd28ee511f9780b3bae
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_download_dlg.h"
21 #include "irreco_select_instance_dlg.h"
22 #include "irreco_theme_download_dlg.h"
23 #include "irreco_webdb_client.h"
24 #include <hildon/hildon-banner.h>
26 /**
27 * @addtogroup IrrecoRemoteDownloadDlg
28 * @ingroup Irreco
30 * @todo PURPOCE OF CLASS.
32 * @{
35 /**
36 * @file
37 * Source file of @ref IrrecoRemoteDownloadDlg.
41 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
42 /* Datatypes */
43 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
45 /** Button responce codes. */
46 enum {
47 IRRECO_REMOTE_REFRESH,
48 IRRECO_REMOTE_DOWNLOAD,
49 IRRECO_REMOTE_DONE
52 /** GtkTreeStore colums. */
53 enum
55 TEXT_COL,
56 FLAG_COL,
57 DATA_COL,
58 N_COLUMNS
61 /** Loader states. */
62 enum
64 LOADER_STATE_INIT,
65 LOADER_STATE_LOOP,
66 LOADER_STATE_END,
67 LOADER_STATE_CLEANUP
70 /** Row flags. */
71 enum
73 ROW_CHILDREN_LOADED = 1 << 1,
74 ROW_TYPE_CATEGORY = 1 << 2,
75 ROW_TYPE_MANUFACTURER = 1 << 3,
76 ROW_TYPE_MODEL = 1 << 4,
77 ROW_TYPE_CREATOR = 1 << 5,
78 ROW_TYPE_REMOTE = 1 << 6,
79 ROW_TYPE_LOADING = 1 << 7
82 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
83 /* Prototypes */
84 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
86 static gboolean irreco_remote_download_dlg_map_event(
87 IrrecoRemoteDownloadDlg *self,
88 GdkEvent *event,
89 gpointer user_data);
90 static void irreco_remote_download_dlg_destroy_event(
91 IrrecoRemoteDownloadDlg *self,
92 gpointer user_data);
93 static void irreco_remote_download_dlg_row_activated_event(
94 GtkTreeView *tree_view,
95 GtkTreePath *path,
96 GtkTreeViewColumn *column,
97 IrrecoRemoteDownloadDlg *self);
98 static void irreco_remote_download_dlg_row_expanded_event(
99 GtkTreeView *tree_view,
100 GtkTreeIter *iter,
101 GtkTreePath *path,
102 IrrecoRemoteDownloadDlg *self);
103 static void irreco_remote_download_dlg_row_collapsed_event(
104 GtkTreeView *tree_view,
105 GtkTreeIter *iter,
106 GtkTreePath *path,
107 IrrecoRemoteDownloadDlg *self);
108 static void irreco_remote_download_dlg_row_selected_event(GtkTreeSelection *sel,
109 IrrecoRemoteDownloadDlg *self);
111 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
112 /* Construction & Destruction */
113 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
116 * @name Construction & Destruction
117 * @{
120 G_DEFINE_TYPE(IrrecoRemoteDownloadDlg, irreco_remote_download_dlg, IRRECO_TYPE_DLG)
122 static void irreco_remote_download_dlg_dispose(GObject *object)
124 G_OBJECT_CLASS(
125 irreco_remote_download_dlg_parent_class)->dispose(object);
128 static void irreco_remote_download_dlg_finalize(GObject *object)
130 G_OBJECT_CLASS(
131 irreco_remote_download_dlg_parent_class)->finalize(object);
134 static void irreco_remote_download_dlg_class_init(
135 IrrecoRemoteDownloadDlgClass *klass)
137 GObjectClass *object_class = G_OBJECT_CLASS (klass);
140 object_class->dispose = irreco_remote_download_dlg_dispose;
141 object_class->finalize = irreco_remote_download_dlg_finalize;
144 static void irreco_remote_download_dlg_init(IrrecoRemoteDownloadDlg *self)
146 GtkWidget *hbox;
147 PangoFontDescription *title_font;
148 PangoFontDescription *initial_font;
149 PangoFontDescription *menu_font;
151 /* Remotes */
152 GtkWidget *scrollbar;
153 GtkTreeViewColumn *column;
154 GtkCellRenderer *renderer;
155 GtkTreeSelection *select;
156 GtkWidget *tree_view_frame;
157 GtkWidget *tree_view_hbox;
159 /* Details */
160 GtkWidget *help_hbox = NULL;
161 GtkWidget *details_alignment;
162 GtkWidget *details_frame;
163 GtkWidget *details_table;
164 GtkWidget *label;
165 IRRECO_ENTER
167 self->remote = NULL;
169 /* Create fonts */
170 initial_font = pango_font_description_from_string("Sans 11");
171 menu_font = pango_font_description_from_string("Sans 13");
172 title_font = pango_font_description_from_string("Sans Bold 12");
174 /* Build the dialog */
175 gtk_window_set_title(GTK_WINDOW(self), _("Download Remote"));
176 gtk_window_set_modal(GTK_WINDOW(self), TRUE);
177 gtk_window_set_destroy_with_parent(GTK_WINDOW(self), TRUE);
178 gtk_dialog_set_has_separator(GTK_DIALOG(self), FALSE);
179 gtk_dialog_add_buttons(GTK_DIALOG(self),
180 GTK_STOCK_REFRESH, IRRECO_REMOTE_REFRESH,
181 _("Download"), IRRECO_REMOTE_DOWNLOAD,
182 _("Done"), IRRECO_REMOTE_DONE,
183 NULL);
185 /* Create hbox */
186 hbox = g_object_new(GTK_TYPE_HBOX, NULL);
187 gtk_box_set_spacing(GTK_BOX(hbox), 8);
188 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(self)->vbox),
189 irreco_gtk_align(GTK_WIDGET(hbox),
190 0, 0, 1, 1, 8, 8, 8, 8));
192 /* Create tree_view_hbox */
193 tree_view_hbox = g_object_new(GTK_TYPE_HBOX, NULL);
195 /* Create Treeview */
196 self->tree_store = gtk_tree_store_new(
197 N_COLUMNS, G_TYPE_STRING, G_TYPE_INT, G_TYPE_POINTER);
198 self->tree_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(
199 GTK_TREE_MODEL(self->tree_store)));
201 renderer = gtk_cell_renderer_text_new();
202 g_object_set(renderer, "font-desc", menu_font, NULL);
204 column = gtk_tree_view_column_new_with_attributes(
205 NULL, renderer, "text", TEXT_COL, NULL);
206 gtk_tree_view_append_column(self->tree_view, column);
208 gtk_box_pack_start(GTK_BOX(tree_view_hbox), GTK_WIDGET(self->tree_view),
209 TRUE, TRUE, 0);
211 /* Create scrollbar for Treeview */
212 scrollbar = gtk_vscrollbar_new(gtk_tree_view_get_vadjustment(
213 GTK_TREE_VIEW(self->tree_view)));
214 gtk_box_pack_start(GTK_BOX(tree_view_hbox), GTK_WIDGET(scrollbar),
215 FALSE, TRUE, 0);
217 /* Create Frame for Treeview */
218 tree_view_frame = gtk_frame_new("");
219 gtk_frame_set_label_widget(GTK_FRAME(tree_view_frame),
220 irreco_gtk_label_bold("Remotes", 0, 0, 0, 0, 0, 0));
222 gtk_container_add(GTK_CONTAINER(tree_view_frame),
223 GTK_WIDGET(tree_view_hbox));
225 gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(tree_view_frame),
226 TRUE, TRUE, 0);
228 /* Create details_table */
229 details_table = gtk_table_new(7, 4, FALSE);
230 gtk_table_set_row_spacings(GTK_TABLE(details_table), 4);
232 label = gtk_label_new("Model: ");
233 gtk_widget_modify_font(label, title_font);
234 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
235 gtk_table_attach(GTK_TABLE(details_table), label,
236 0, 1, 0, 1, GTK_FILL | GTK_SHRINK, GTK_FILL, 0, 0);
238 label = gtk_label_new("Creator: ");
239 gtk_widget_modify_font(label, title_font);
240 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
241 gtk_table_attach(GTK_TABLE(details_table), label,
242 0, 1, 1, 2, GTK_FILL | GTK_SHRINK, GTK_FILL, 0, 0);
244 label = gtk_label_new("Backends: ");
245 gtk_widget_modify_font(label, title_font);
246 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
247 gtk_table_attach(GTK_TABLE(details_table), label,
248 0, 1, 2, 3, GTK_FILL | GTK_SHRINK, GTK_FILL, 0, 0);
250 label = gtk_label_new("Themes: ");
251 gtk_widget_modify_font(label, title_font);
252 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
253 gtk_table_attach(GTK_TABLE(details_table), label,
254 0, 1, 3, 4, GTK_FILL | GTK_SHRINK, GTK_FILL, 0, 0);
256 label = gtk_label_new("Downloaded: ");
257 gtk_widget_modify_font(label, title_font);
258 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
259 gtk_table_attach(GTK_TABLE(details_table), label,
260 0, 1, 4, 5, GTK_FILL | GTK_SHRINK, GTK_FILL, 0, 0);
262 label = gtk_label_new("Comment: ");
263 gtk_widget_modify_font(label, title_font);
264 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
265 gtk_table_attach(GTK_TABLE(details_table), label,
266 0, 1, 5, 6, GTK_FILL | GTK_SHRINK, GTK_FILL, 0, 0);
268 self->details_model = gtk_label_new("");
269 gtk_widget_modify_font(self->details_model, initial_font);
270 gtk_misc_set_alignment(GTK_MISC(self->details_model), 0, 0);
271 gtk_table_attach(GTK_TABLE(details_table), self->details_model,
272 1, 2, 0, 1, GTK_FILL, GTK_FILL, 0, 0);
274 self->details_creator = gtk_label_new("");
275 gtk_widget_modify_font(self->details_creator, initial_font);
276 gtk_misc_set_alignment(GTK_MISC(self->details_creator), 0, 0);
277 gtk_table_attach(GTK_TABLE(details_table), self->details_creator,
278 1, 2, 1, 2, GTK_FILL, GTK_FILL, 0, 0);
280 self->details_backends = gtk_label_new("");
281 gtk_widget_modify_font(self->details_backends, initial_font);
282 gtk_misc_set_alignment(GTK_MISC(self->details_backends), 0, 0);
283 gtk_table_attach(GTK_TABLE(details_table), self->details_backends,
284 1, 2, 2, 3, GTK_FILL, GTK_FILL, 0, 0);
286 self->details_themes = gtk_label_new("");
287 gtk_widget_modify_font(self->details_themes, initial_font);
288 gtk_misc_set_alignment(GTK_MISC(self->details_themes), 0, 0);
289 gtk_table_attach(GTK_TABLE(details_table), self->details_themes,
290 1, 2, 3, 4, GTK_FILL, GTK_FILL, 0, 0);
292 self->details_downloaded = gtk_label_new("");
293 gtk_widget_modify_font(self->details_downloaded, initial_font);
294 gtk_misc_set_alignment(GTK_MISC(self->details_downloaded), 0, 0);
295 gtk_table_attach(GTK_TABLE(details_table), self->details_downloaded,
296 1, 2, 4, 5, GTK_FILL, GTK_FILL, 0, 0);
298 self->details_comment = gtk_text_view_new();
299 gtk_widget_modify_font(self->details_comment, initial_font);
300 gtk_misc_set_alignment(GTK_MISC(self->details_comment), 0, 0);
301 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(self->details_comment),
302 FALSE);
303 gtk_text_view_set_editable(GTK_TEXT_VIEW(self->details_comment),
304 FALSE);
305 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(self->details_comment),
306 GTK_WRAP_WORD_CHAR);
307 gtk_table_attach(GTK_TABLE(details_table), self->details_comment,
308 0, 4, 6, 7, GTK_FILL, GTK_FILL, 0, 0);
309 gtk_widget_set_size_request(GTK_WIDGET(self->details_comment), 311, -1);
311 /* Details */
312 self->details = gtk_scrolled_window_new(NULL, NULL);
313 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(self->details),
314 GTK_POLICY_AUTOMATIC,
315 GTK_POLICY_AUTOMATIC);
317 gtk_widget_set_size_request(GTK_WIDGET(self->details), 355, -1);
319 /* Create frame for Details */
320 details_frame = gtk_frame_new("");
321 gtk_frame_set_label_widget(GTK_FRAME(details_frame),
322 irreco_gtk_label_bold("Details", 0, 0, 0, 0, 0, 0));
324 gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(details_frame),
325 FALSE, TRUE, 0);
327 /* Create alignment */
328 details_alignment = gtk_alignment_new(0, 0, 0, 0);
329 gtk_alignment_set_padding(GTK_ALIGNMENT(details_alignment), 0, 8, 8, 8);
331 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(
332 self->details),
333 details_alignment);
335 gtk_container_add(GTK_CONTAINER(details_alignment), details_table);
337 /* Help text */
338 self->help_text = gtk_label_new("Select Remote");
339 gtk_label_set_justify(GTK_LABEL(self->help_text), GTK_JUSTIFY_CENTER);
340 gtk_label_set_line_wrap(GTK_LABEL(self->help_text), TRUE);
341 self->help_text = irreco_gtk_align(self->help_text,
342 0.5, 0.5, 1, 1, 8, 8, 8, 8);
344 help_hbox = gtk_hbox_new(0, 0);
346 gtk_box_pack_start(GTK_BOX(help_hbox), GTK_WIDGET(self->details),
347 TRUE, TRUE, 0);
348 gtk_box_pack_start(GTK_BOX(help_hbox), GTK_WIDGET(self->help_text),
349 TRUE, TRUE, 0);
351 gtk_container_add(GTK_CONTAINER(details_frame), GTK_WIDGET(help_hbox));
353 /* Setup the selection handler for TREE */
354 select = gtk_tree_view_get_selection(self->tree_view);
355 gtk_tree_selection_set_mode(select, GTK_SELECTION_SINGLE);
357 /* Signal handlers. */
359 g_signal_connect(G_OBJECT(self), "map-event", G_CALLBACK(
360 irreco_remote_download_dlg_map_event), NULL);
361 g_signal_connect(G_OBJECT(self), "destroy", G_CALLBACK(
362 irreco_remote_download_dlg_destroy_event), NULL);
363 g_signal_connect(G_OBJECT(self->tree_view), "row-activated", G_CALLBACK(
364 irreco_remote_download_dlg_row_activated_event), self);
365 g_signal_connect(G_OBJECT(self->tree_view), "row-expanded", G_CALLBACK(
366 irreco_remote_download_dlg_row_expanded_event), self);
367 g_signal_connect(G_OBJECT(self->tree_view), "row-collapsed", G_CALLBACK(
368 irreco_remote_download_dlg_row_collapsed_event), self);
369 g_signal_connect(G_OBJECT(select), "changed", G_CALLBACK(
370 irreco_remote_download_dlg_row_selected_event), self);
372 gtk_tree_view_set_enable_tree_lines(self->tree_view, TRUE);
373 g_object_set (G_OBJECT (self->tree_view), "show-expanders", TRUE, NULL);
374 g_object_set (G_OBJECT (self->tree_view), "level-indentation", 0, NULL);
375 gtk_tree_view_set_rubber_banding(self->tree_view, FALSE);
377 gtk_widget_set_size_request(GTK_WIDGET(self), 696, 396);
379 gtk_widget_show_all(GTK_WIDGET(self));
380 gtk_widget_hide(GTK_WIDGET(self->details));
382 IRRECO_RETURN
385 IrrecoRemoteDownloadDlg* irreco_remote_download_dlg_new(IrrecoData *irreco_data,
386 GtkWindow *parent)
388 IrrecoRemoteDownloadDlg *self;
389 IRRECO_ENTER
391 self = g_object_new(IRRECO_TYPE_REMOTE_DOWNLOAD_DLG, NULL);
392 irreco_dlg_set_parent(IRRECO_DLG(self), parent);
393 self->irreco_data = irreco_data;
394 IRRECO_RETURN_PTR(self);
398 /** @} */
402 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
403 /* Private Functions */
404 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
407 * @name Private Functions
408 * @{
412 * Clean detail list
415 static void irreco_remote_download_dlg_clean_details(
416 IrrecoRemoteDownloadDlg *self)
418 GtkTextBuffer *buffer;
419 IRRECO_ENTER
421 self->remote = NULL;
422 gtk_label_set_text(GTK_LABEL(self->details_model), "");
423 gtk_label_set_text(GTK_LABEL(self->details_creator), "");
424 gtk_label_set_text(GTK_LABEL(self->details_backends), "");
425 gtk_label_set_text(GTK_LABEL(self->details_themes), "");
426 gtk_label_set_text(GTK_LABEL(self->details_downloaded), "");
427 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(self->details_comment));
428 gtk_text_buffer_set_text(buffer, " ", 1);
429 gtk_text_view_set_buffer(GTK_TEXT_VIEW(self->details_comment), buffer);
431 IRRECO_RETURN
435 * Have the childern of this item been loaded.
437 * @return TRUE if children have been loade, FALSE otherwise.
439 static gboolean irreco_remote_download_dlg_row_is_loaded(
440 IrrecoRemoteDownloadDlg *self,
441 GtkTreeIter *iter)
443 gint i;
444 IRRECO_ENTER
446 gtk_tree_model_get(GTK_TREE_MODEL(self->tree_store),
447 iter, FLAG_COL, &i, -1);
448 if (i & ROW_CHILDREN_LOADED) IRRECO_RETURN_BOOL(TRUE);
449 IRRECO_RETURN_BOOL(FALSE);
453 * Enable / Disable ROW_CHILDREN_LOADED flag from a row.
455 * @param value If set, ROW_CHILDREN_LOADED will be enabled.
457 static void irreco_remote_download_dlg_row_set_loaded(IrrecoRemoteDownloadDlg *self,
458 GtkTreeIter *iter,
459 gboolean value)
461 gint i;
462 IRRECO_ENTER
464 gtk_tree_model_get(GTK_TREE_MODEL(self->tree_store),
465 iter, FLAG_COL, &i, -1);
466 if (value) {
467 i = i | ROW_CHILDREN_LOADED;
468 } else {
469 i = i & ~ROW_CHILDREN_LOADED;
471 gtk_tree_store_set(self->tree_store, iter, FLAG_COL, i, -1);
473 IRRECO_RETURN
477 * Get type of row.
479 static gint irreco_remote_download_dlg_row_get_type(
480 IrrecoRemoteDownloadDlg *self,
481 GtkTreeIter *iter)
483 gint i;
484 IRRECO_ENTER
486 gtk_tree_model_get(GTK_TREE_MODEL(self->tree_store),
487 iter, FLAG_COL, &i, -1);
488 if (i & ROW_TYPE_CATEGORY)
489 IRRECO_RETURN_ENUM(ROW_TYPE_CATEGORY);
490 if (i & ROW_TYPE_MANUFACTURER)
491 IRRECO_RETURN_ENUM(ROW_TYPE_MANUFACTURER);
492 if (i & ROW_TYPE_MODEL)
493 IRRECO_RETURN_ENUM(ROW_TYPE_MODEL);
494 if (i & ROW_TYPE_CREATOR)
495 IRRECO_RETURN_ENUM(ROW_TYPE_CREATOR);
496 if (i & ROW_TYPE_REMOTE)
497 IRRECO_RETURN_ENUM(ROW_TYPE_REMOTE);
498 if (i & ROW_TYPE_LOADING)
499 IRRECO_RETURN_ENUM(ROW_TYPE_LOADING);
500 IRRECO_RETURN_INT(0);
504 * Show hildon progressbar banner.
506 * This function will create a new banner if one has not been created yet,
507 * if banner already exists, it's properties will be changed.
509 * @param text Text to show.
510 * @param fraction Value of progress.
512 static void irreco_remote_download_dlg_set_banner(IrrecoRemoteDownloadDlg *self,
513 const gchar *text,
514 gdouble fraction)
516 IRRECO_ENTER
517 if (self->banner == NULL) {
518 self->banner = hildon_banner_show_progress(GTK_WIDGET(self),
519 NULL, "");
522 hildon_banner_set_text(HILDON_BANNER(self->banner), text);
523 hildon_banner_set_fraction(HILDON_BANNER(self->banner), fraction);
524 IRRECO_RETURN
528 * Destroy banner, if it exists.
530 static void irreco_remote_download_dlg_hide_banner(
531 IrrecoRemoteDownloadDlg *self)
533 IRRECO_ENTER
534 if (self->banner) {
535 gtk_widget_destroy(self->banner);
536 self->banner = NULL;
538 IRRECO_RETURN
542 * Start a loader state machine if one is not running already.
544 static void irreco_remote_download_dlg_loader_start(
545 IrrecoRemoteDownloadDlg *self,
546 GSourceFunc function,
547 GtkTreeIter *parent_iter)
549 IRRECO_ENTER
551 if (self->loader_func_id == 0) {
552 if (parent_iter) {
553 self->loader_parent_iter = gtk_tree_iter_copy(
554 parent_iter);
557 if (function) {
558 self->loader_func_id = g_idle_add(function, self);
559 } else {
560 IRRECO_ERROR("Loader function pointer not given.\n");
564 IRRECO_RETURN
568 * Stop and cleanup loader if a loader is running.
570 static void irreco_remote_download_dlg_loader_stop(
571 IrrecoRemoteDownloadDlg *self)
573 IRRECO_ENTER
574 if (self->loader_func_id != 0) {
575 g_source_remove(self->loader_func_id);
576 self->loader_func_id = 0;
577 self->loader_state = 0;
578 if (self->loader_parent_iter) {
579 gtk_tree_iter_free(self->loader_parent_iter);
581 self->loader_parent_iter = NULL;
583 IRRECO_RETURN
586 static gboolean irreco_remote_download_dlg_loader_categ(
587 IrrecoRemoteDownloadDlg *self)
589 IRRECO_ENTER
591 switch (self->loader_state) {
592 case LOADER_STATE_INIT:
593 irreco_remote_download_dlg_set_banner(self, _("Loading ..."),
595 self->loader_state = LOADER_STATE_LOOP;
596 irreco_remote_download_dlg_clean_details(self);
597 IRRECO_RETURN_BOOL(TRUE);
599 case LOADER_STATE_LOOP: {
600 GtkTreeIter iter;
601 GtkTreeIter iter_loading;
602 IrrecoStringTable *categories;
603 IrrecoWebdbCache *webdb_cache = NULL;
605 webdb_cache = irreco_data_get_webdb_cache(self->irreco_data,
606 FALSE);
607 if(irreco_webdb_cache_get_remote_categories(webdb_cache,
608 &categories)) {
609 IRRECO_STRING_TABLE_FOREACH_KEY(categories, key)
611 /* Append categogy. */
612 gtk_tree_store_append(self->tree_store,
613 &iter, NULL);
614 gtk_tree_store_set(self->tree_store,
615 &iter, TEXT_COL, key,
616 FLAG_COL, ROW_TYPE_CATEGORY,
617 DATA_COL, NULL, -1);
619 /* Add loading item into category. */
620 gtk_tree_store_append(self->tree_store,
621 &iter_loading, &iter);
622 gtk_tree_store_set(self->tree_store,
623 &iter_loading,
624 TEXT_COL, _("Loading ..."),
625 FLAG_COL, ROW_TYPE_LOADING,
626 DATA_COL, NULL, -1);
627 IRRECO_STRING_TABLE_FOREACH_END
628 } else {
629 irreco_error_dlg(GTK_WINDOW(self),
630 irreco_webdb_cache_get_error(
631 webdb_cache));
634 irreco_remote_download_dlg_set_banner(self, _("Loading ..."),
636 self->loader_state = LOADER_STATE_END;
637 IRRECO_RETURN_BOOL(TRUE);
640 case LOADER_STATE_END:
641 irreco_remote_download_dlg_hide_banner(self);
642 irreco_remote_download_dlg_loader_stop(self);
645 IRRECO_RETURN_BOOL(FALSE);
648 static gboolean irreco_remote_download_dlg_loader_manuf(
649 IrrecoRemoteDownloadDlg *self)
651 IRRECO_ENTER
653 switch (self->loader_state) {
654 case LOADER_STATE_INIT:
655 irreco_remote_download_dlg_set_banner(self, _("Loading ..."),
657 self->loader_state = LOADER_STATE_LOOP;
658 IRRECO_RETURN_BOOL(TRUE);
660 case LOADER_STATE_LOOP: {
661 GtkTreeIter iter;
662 GtkTreeIter iter_loading;
663 IrrecoStringTable *manufacturers;
664 IrrecoWebdbCache *webdb_cache = NULL;
665 gchar *category;
667 gtk_tree_model_get(GTK_TREE_MODEL(self->tree_store),
668 self->loader_parent_iter,
669 TEXT_COL, &category, -1);
671 webdb_cache = irreco_data_get_webdb_cache(self->irreco_data,
672 FALSE);
674 if(irreco_webdb_cache_get_remote_manufacturers(webdb_cache,
675 category,
676 &manufacturers)) {
677 IRRECO_STRING_TABLE_FOREACH_KEY(manufacturers, key)
679 /* Add manufacturer item into category. */
680 gtk_tree_store_append(self->tree_store,
681 &iter, self->loader_parent_iter);
682 gtk_tree_store_set(self->tree_store,
683 &iter,
684 TEXT_COL, key,
685 FLAG_COL, ROW_TYPE_MANUFACTURER,
686 DATA_COL, NULL, -1);
688 /* Add loading item into manufacturer. */
689 gtk_tree_store_append(self->tree_store,
690 &iter_loading, &iter);
691 gtk_tree_store_set(self->tree_store,
692 &iter_loading,
693 TEXT_COL, _("Loading ..."),
694 FLAG_COL, ROW_TYPE_LOADING,
695 DATA_COL, NULL, -1);
697 IRRECO_STRING_TABLE_FOREACH_END
698 } else {
699 irreco_error_dlg(GTK_WINDOW(self),
700 irreco_webdb_cache_get_error(
701 webdb_cache));
704 g_free (category);
706 /* Delete loading item from category. */
707 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(self->tree_store),
708 &iter,self->loader_parent_iter,0);
709 if(irreco_remote_download_dlg_row_get_type(self,
710 &iter) == ROW_TYPE_LOADING) {
711 gtk_tree_store_remove(self->tree_store,&iter);
713 irreco_remote_download_dlg_set_banner(self, _("Loading ..."),
715 self->loader_state = LOADER_STATE_END;
716 IRRECO_RETURN_BOOL(TRUE);
719 case LOADER_STATE_END:
720 irreco_remote_download_dlg_row_set_loaded(
721 self, self->loader_parent_iter,
722 TRUE);
723 irreco_remote_download_dlg_hide_banner(self);
724 irreco_remote_download_dlg_loader_stop(self);
727 IRRECO_RETURN_BOOL(FALSE);
730 static gboolean irreco_remote_download_dlg_loader_model(
731 IrrecoRemoteDownloadDlg *self)
733 IRRECO_ENTER
735 switch (self->loader_state) {
736 case LOADER_STATE_INIT:
737 irreco_remote_download_dlg_set_banner(self, _("Loading ..."),
739 self->loader_state = LOADER_STATE_LOOP;
740 IRRECO_RETURN_BOOL(TRUE);
742 case LOADER_STATE_LOOP: {
743 GtkTreeIter iter;
744 GtkTreeIter iter_loading;
745 GtkTreeIter category_iter;
746 IrrecoStringTable *models = NULL;
747 IrrecoWebdbCache *webdb_cache = NULL;
748 gchar *category;
749 gchar *manufacturer;
751 /*Get category and manufacturer*/
753 gtk_tree_model_get(GTK_TREE_MODEL(self->tree_store),
754 self->loader_parent_iter,
755 TEXT_COL, &manufacturer, -1);
757 gtk_tree_model_iter_parent(GTK_TREE_MODEL(self->tree_store),
758 &category_iter,
759 self->loader_parent_iter);
761 gtk_tree_model_get(GTK_TREE_MODEL(self->tree_store),
762 &category_iter,
763 TEXT_COL, &category, -1);
765 webdb_cache = irreco_data_get_webdb_cache(self->irreco_data,
766 FALSE);
768 if(irreco_webdb_cache_get_remote_models(webdb_cache, category,
769 manufacturer, &models)){
771 IRRECO_STRING_TABLE_FOREACH_KEY(models, key)
773 /* Add model item into manufacturer. */
774 gtk_tree_store_append(self->tree_store,
775 &iter, self->loader_parent_iter);
776 gtk_tree_store_set(self->tree_store,
777 &iter,
778 TEXT_COL, key,
779 FLAG_COL, ROW_TYPE_MODEL,
780 DATA_COL, NULL, -1);
782 /* Add loading item into model. */
783 gtk_tree_store_append(self->tree_store,
784 &iter_loading, &iter);
785 gtk_tree_store_set(self->tree_store,
786 &iter_loading,
787 TEXT_COL, _("Loading ..."),
788 FLAG_COL, ROW_TYPE_LOADING,
789 DATA_COL, NULL, -1);
791 IRRECO_STRING_TABLE_FOREACH_END
792 } else {
793 irreco_error_dlg(GTK_WINDOW(self),
794 irreco_webdb_cache_get_error(
795 webdb_cache));
797 g_free (category);
798 g_free (manufacturer);
800 /* Delete loading item from manufacturer. */
801 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(self->tree_store),
802 &iter, self->loader_parent_iter,
804 if(irreco_remote_download_dlg_row_get_type(self,
805 &iter) == ROW_TYPE_LOADING) {
806 gtk_tree_store_remove(self->tree_store, &iter);
809 irreco_remote_download_dlg_set_banner(self, _("Loading ..."),
811 self->loader_state = LOADER_STATE_END;
813 IRRECO_RETURN_BOOL(TRUE);
815 case LOADER_STATE_END:
816 irreco_remote_download_dlg_row_set_loaded(
817 self, self->loader_parent_iter,
818 TRUE);
819 irreco_remote_download_dlg_hide_banner(self);
820 irreco_remote_download_dlg_loader_stop(self);
823 IRRECO_RETURN_BOOL(FALSE);
826 static gboolean irreco_remote_download_dlg_loader_creators(
827 IrrecoRemoteDownloadDlg *self)
829 IRRECO_ENTER
831 switch (self->loader_state) {
832 case LOADER_STATE_INIT:
833 irreco_remote_download_dlg_set_banner(self, _("Loading ..."),
835 self->loader_state = LOADER_STATE_LOOP;
836 IRRECO_RETURN_BOOL(TRUE);
838 case LOADER_STATE_LOOP: {
839 IrrecoWebdbCache *webdb_cache = NULL;
841 GtkTreeIter category_iter;
842 GtkTreeIter manufacturer_iter;
843 GtkTreeIter iter;
844 GtkTreeIter iter_loading;
846 gchar *category;
847 gchar *manufacturer;
848 gchar *model;
850 IrrecoStringTable *creators;
852 /*Get manufacturer_iter and category_iter */
854 gtk_tree_model_iter_parent(GTK_TREE_MODEL(self->tree_store),
855 &manufacturer_iter,
856 self->loader_parent_iter);
858 gtk_tree_model_iter_parent(GTK_TREE_MODEL(self->tree_store),
859 &category_iter,
860 &manufacturer_iter);
862 /*Get category, manufacturer and model*/
864 gtk_tree_model_get(GTK_TREE_MODEL(self->tree_store),
865 &category_iter,
866 TEXT_COL, &category, -1);
868 gtk_tree_model_get(GTK_TREE_MODEL(self->tree_store),
869 &manufacturer_iter,
870 TEXT_COL, &manufacturer, -1);
872 gtk_tree_model_get(GTK_TREE_MODEL(self->tree_store),
873 self->loader_parent_iter,
874 TEXT_COL, &model, -1);
877 webdb_cache = irreco_data_get_webdb_cache(self->irreco_data,
878 FALSE);
880 if(irreco_webdb_cache_get_remote_creators(webdb_cache,
881 category, manufacturer, model, &creators)){
883 IRRECO_STRING_TABLE_FOREACH_KEY(creators, key)
885 /* Add model item into manufacturer. */
886 gtk_tree_store_append(self->tree_store,
887 &iter, self->loader_parent_iter);
888 gtk_tree_store_set(self->tree_store,
889 &iter,
890 TEXT_COL, key,
891 FLAG_COL, ROW_TYPE_CREATOR,
892 DATA_COL, NULL, -1);
894 /* Add loading item into model. */
895 gtk_tree_store_append(self->tree_store,
896 &iter_loading, &iter);
897 gtk_tree_store_set(self->tree_store,
898 &iter_loading,
899 TEXT_COL, _("Loading ..."),
900 FLAG_COL, ROW_TYPE_LOADING,
901 DATA_COL, NULL, -1);
903 IRRECO_STRING_TABLE_FOREACH_END
904 } else {
905 irreco_error_dlg(GTK_WINDOW(self),
906 irreco_webdb_cache_get_error(
907 webdb_cache));
909 g_free (category);
910 g_free (manufacturer);
911 g_free (model);
913 /* Delete loading item from manufacturer. */
914 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(self->tree_store),
915 &iter, self->loader_parent_iter,
917 if(irreco_remote_download_dlg_row_get_type(self,
918 &iter) == ROW_TYPE_LOADING) {
919 gtk_tree_store_remove(self->tree_store, &iter);
922 irreco_remote_download_dlg_set_banner(self, _("Loading ..."),
924 self->loader_state = LOADER_STATE_END;
926 IRRECO_RETURN_BOOL(TRUE);
928 case LOADER_STATE_END:
929 irreco_remote_download_dlg_row_set_loaded(
930 self, self->loader_parent_iter,
931 TRUE);
932 irreco_remote_download_dlg_hide_banner(self);
933 irreco_remote_download_dlg_loader_stop(self);
936 IRRECO_RETURN_BOOL(FALSE);
939 static gboolean irreco_remote_download_dlg_loader_remotes(
940 IrrecoRemoteDownloadDlg *self)
942 IRRECO_ENTER
944 switch (self->loader_state) {
945 case LOADER_STATE_INIT:
946 irreco_remote_download_dlg_set_banner(self, _("Loading ..."),
948 self->loader_state = LOADER_STATE_LOOP;
949 self->loader_pos = 0;
950 IRRECO_RETURN_BOOL(TRUE);
952 case LOADER_STATE_LOOP: {
953 IrrecoWebdbCache *webdb_cache = NULL;
955 GtkTreeIter category_iter;
956 GtkTreeIter manufacturer_iter;
957 GtkTreeIter model_iter;
958 GtkTreeIter iter;
960 gchar *category;
961 gchar *manufacturer;
962 gchar *model;
963 gchar *creator;
965 /* Remote info */
966 gint remote_id;
967 IrrecoWebdbRemote *remote;
968 GList *remotes = NULL;
969 GList *configs;
970 GList *themes;
972 /* Banner info */
973 gint remote_count = 0;
974 gfloat banner;
976 /*Get manufacturer_iter and category_iter */
978 gtk_tree_model_iter_parent(GTK_TREE_MODEL(self->tree_store),
979 &model_iter,
980 self->loader_parent_iter);
982 gtk_tree_model_iter_parent(GTK_TREE_MODEL(self->tree_store),
983 &manufacturer_iter,
984 &model_iter);
986 gtk_tree_model_iter_parent(GTK_TREE_MODEL(self->tree_store),
987 &category_iter,
988 &manufacturer_iter);
990 /*Get category, manufacturer and model*/
992 gtk_tree_model_get(GTK_TREE_MODEL(self->tree_store),
993 &category_iter,
994 TEXT_COL, &category, -1);
996 gtk_tree_model_get(GTK_TREE_MODEL(self->tree_store),
997 &manufacturer_iter,
998 TEXT_COL, &manufacturer, -1);
1000 gtk_tree_model_get(GTK_TREE_MODEL(self->tree_store),
1001 &model_iter,
1002 TEXT_COL, &model, -1);
1004 gtk_tree_model_get(GTK_TREE_MODEL(self->tree_store),
1005 self->loader_parent_iter,
1006 TEXT_COL, &creator, -1);
1009 webdb_cache = irreco_data_get_webdb_cache(self->irreco_data,
1010 FALSE);
1012 if(!irreco_webdb_cache_get_remotes(webdb_cache,
1013 category, manufacturer, model, creator, &remotes)) {
1014 irreco_error_dlg(GTK_WINDOW(self),
1015 irreco_webdb_cache_get_error(
1016 webdb_cache));
1017 goto end;
1020 remote_id = GPOINTER_TO_INT(g_list_nth_data(remotes,
1021 self->loader_pos));
1023 if(!irreco_webdb_cache_get_remote_by_id(webdb_cache, remote_id,
1024 &remote)) {
1025 irreco_error_dlg(GTK_WINDOW(self),
1026 irreco_webdb_cache_get_error(
1027 webdb_cache));
1028 goto end;
1031 /* Get information of configurations */
1032 if (!irreco_webdb_cache_get_configurations_of_remote(
1033 webdb_cache, remote_id, &configs)) {
1034 irreco_error_dlg(GTK_WINDOW(self),
1035 irreco_webdb_cache_get_error(
1036 webdb_cache));
1037 goto end;
1040 configs = g_list_first(configs);
1041 while(configs) {
1042 IrrecoWebdbConf *config;
1043 if (GPOINTER_TO_INT(configs->data) != 0 &&
1044 !irreco_webdb_cache_get_configuration(webdb_cache,
1045 GPOINTER_TO_INT(configs->data), &config)) {
1046 irreco_error_dlg(GTK_WINDOW(self),
1047 irreco_webdb_cache_get_error(
1048 webdb_cache));
1050 configs = configs->next;
1053 /* Get information of themes*/
1054 if (!irreco_webdb_cache_get_themes_of_remote(
1055 webdb_cache, remote_id, &themes)) {
1056 irreco_error_dlg(GTK_WINDOW(self),
1057 irreco_webdb_cache_get_error(
1058 webdb_cache));
1059 goto end;
1062 themes = g_list_first(themes);
1063 while(themes) {
1064 IrrecoWebdbTheme *theme;
1065 if (GPOINTER_TO_INT(themes->data) != 0 &&
1066 !irreco_webdb_cache_get_theme_by_id(webdb_cache,
1067 GPOINTER_TO_INT(themes->data), &theme)) {
1068 irreco_error_dlg(GTK_WINDOW(self),
1069 irreco_webdb_cache_get_error(
1070 webdb_cache));
1072 themes = themes->next;
1075 /* Add config item into model. */
1076 gtk_tree_store_append(self->tree_store, &iter,
1077 self->loader_parent_iter);
1079 gtk_tree_store_set(self->tree_store, &iter,
1080 TEXT_COL, remote->uploaded->str,
1081 FLAG_COL, ROW_TYPE_REMOTE,
1082 DATA_COL, GINT_TO_POINTER(remote_id), -1);
1084 /* Delete loading item from model. */
1085 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(self->tree_store),
1086 &iter, self->loader_parent_iter,
1089 if (irreco_remote_download_dlg_row_get_type(self,
1090 &iter) == ROW_TYPE_LOADING) {
1091 gtk_tree_store_remove(self->tree_store, &iter);
1094 end:
1095 g_free (category);
1096 g_free (manufacturer);
1097 g_free (model);
1098 g_free (creator);
1100 /* Banner */
1102 if (remotes != NULL) {
1103 remote_count = g_list_length(remotes);
1106 banner = ((float) self->loader_pos + 1) / (float) remote_count;
1107 irreco_remote_download_dlg_set_banner(self, _("Loading ..."),
1108 banner);
1110 if (self->loader_pos + 1 >= remote_count) {
1111 self->loader_state = LOADER_STATE_END;
1112 IRRECO_RETURN_BOOL(TRUE);
1113 } else {
1114 self->loader_pos++;
1115 IRRECO_RETURN_BOOL(TRUE);
1119 IRRECO_RETURN_BOOL(TRUE);
1121 case LOADER_STATE_END:
1122 irreco_remote_download_dlg_row_set_loaded(
1123 self, self->loader_parent_iter,
1124 TRUE);
1125 irreco_remote_download_dlg_hide_banner(self);
1126 irreco_remote_download_dlg_loader_stop(self);
1129 IRRECO_RETURN_BOOL(FALSE);
1132 static gboolean irreco_remote_download_dlg_load_configs(
1133 IrrecoRemoteDownloadDlg *self)
1135 IrrecoWebdbCache *webdb_cache = NULL;
1136 IrrecoBackendInstance *inst = NULL;
1137 GString *error_msg = g_string_new("");
1138 IrrecoBackendManager *manager;
1139 IrrecoBackendFileContainer *file_container = NULL;
1140 GString *file_data = NULL;
1141 gchar *sha1 = NULL;
1142 gboolean success = TRUE;
1143 gboolean new_instance = FALSE;
1144 gint instances;
1145 GList *configurations;
1146 IrrecoWebdbConf *config;
1147 IRRECO_ENTER
1149 webdb_cache = irreco_data_get_webdb_cache(self->irreco_data, FALSE);
1151 configurations = g_list_first(self->remote->configurations);
1152 if (configurations == NULL ||
1153 GPOINTER_TO_INT(configurations->data) == 0) {
1154 goto end;
1156 while(configurations) {
1158 if (!irreco_webdb_cache_get_configuration(webdb_cache,
1159 GPOINTER_TO_INT(configurations->data), &config)) {
1160 g_string_printf(error_msg, "%s",
1161 irreco_webdb_cache_get_error(
1162 webdb_cache));
1165 if(config == NULL) {
1166 g_string_printf(error_msg, "Configuration is NULL");
1167 goto end;
1170 /* Get file_data */
1171 if (!irreco_webdb_cache_get_file(webdb_cache,
1172 config->file_hash->str,
1173 config->file_name->str,
1174 &file_data)) {
1175 g_string_printf(error_msg, "%s",
1176 irreco_webdb_cache_get_error(
1177 webdb_cache));
1178 goto end;
1181 sha1 = sha_compute_checksum_for_string(G_CHECKSUM_SHA1,
1182 file_data->str, -1);
1184 if (g_utf8_collate(config->file_hash->str, sha1) != 0) {
1185 g_string_printf(error_msg, "sha1 checksum failed.");
1186 goto end;
1190 /* Search backend */
1191 manager = self->irreco_data->irreco_backend_manager;
1192 IRRECO_BACKEND_MANAGER_FOREACH_LIB(manager, lib)
1193 if (!g_str_equal(lib->name, config->backend->str)) {
1194 continue;
1196 if (!(lib->api->flags &
1197 IRRECO_BACKEND_EDITABLE_DEVICES)) {
1198 g_string_printf(error_msg,
1199 "\"%s\" backend is not editable...",
1200 config->backend->str);
1201 goto end;
1203 else if (!(lib->api->flags &
1204 IRRECO_BACKEND_CONFIGURATION_EXPORT)) {
1205 g_string_printf(error_msg,
1206 "\"%s\" backend doesn't support "
1207 "exporting its configuration...",
1208 config->backend->str);
1209 goto end;
1211 /* Check if many instances */
1212 instances = 0;
1213 IRRECO_BACKEND_MANAGER_FOREACH_INSTANCE(manager,
1214 instance)
1215 irreco_backend_instance_get_description(
1216 instance);
1217 if (g_str_equal(instance->lib->name,
1218 config->backend->str)) {
1219 instances++;
1221 /* if there is only one instance,
1222 * then use it
1224 inst = instance;
1226 IRRECO_STRING_TABLE_FOREACH_END
1227 IRRECO_DEBUG("INSTACES: %d\n", instances);
1229 /* Create new instance if it comes to the crunch */
1230 if (instances == 0 || lib->api->flags &
1231 IRRECO_BACKEND_NEW_INST_ON_CONF_IMPORT) {
1232 inst = irreco_backend_manager_create_instance(
1233 manager, lib, NULL, NULL);
1234 new_instance = TRUE;
1235 break;
1238 /* Select instace */
1239 if (instances > 1) {
1240 if (!irreco_show_select_instance_dlg(
1241 self->irreco_data, GTK_WINDOW(self),
1242 config->backend->str, &inst)) {
1243 g_string_printf(error_msg,
1244 "Operation aborted by user...");
1245 goto end;
1248 goto instance_ready;
1249 IRRECO_STRING_TABLE_FOREACH_END
1251 if (inst == NULL) {
1252 g_string_printf(error_msg,
1253 "\"%s\" backend is not installed...",
1254 config->backend->str);
1255 goto end;
1258 irreco_backend_instance_configure(inst, GTK_WINDOW(self));
1259 irreco_backend_instance_save_to_conf(inst);
1260 irreco_config_save_backends(manager);
1262 instance_ready:
1263 file_container = irreco_backend_file_container_new();
1264 irreco_backend_file_container_set(file_container,
1265 inst->lib->name,
1266 config->category->str,
1267 config->manufacturer->str,
1268 config->model->str,
1269 config->file_name->str,
1270 file_data->str);
1272 if (irreco_backend_instance_check_conf(inst, file_container)) {
1273 g_string_printf(error_msg,
1274 "\"%s\" backend already contains\n"
1275 "device \"%s\".\n"
1276 "Do you want to overwrite?",
1277 inst->lib->name, config->model->str);
1278 success = irreco_yes_no_dlg(GTK_WINDOW(self),
1279 error_msg->str);
1280 g_string_erase(error_msg, 0, -1);
1282 if(success == FALSE) {
1283 goto end;
1287 /* Send file_data for backend */
1288 if(irreco_backend_instance_import_conf(inst, file_container)) {
1289 irreco_backend_manager_get_devcmd_lists(
1290 self->irreco_data->irreco_backend_manager);
1291 } else {
1292 g_string_printf(error_msg, "Backend error");
1294 if (new_instance) {
1295 irreco_backend_manager_destroy_instance(manager,
1296 inst);
1298 goto end;
1300 configurations = configurations->next;
1301 irreco_backend_file_container_free(file_container);
1302 file_container = NULL;
1303 g_free(sha1);
1304 sha1 = NULL;
1305 g_string_free(file_data, FALSE);
1306 file_data = NULL;
1309 end:
1310 if(error_msg->len > 0) {
1311 irreco_error_dlg(GTK_WINDOW(self), error_msg->str);
1312 success = FALSE;
1314 g_string_free(error_msg, FALSE);
1315 if (sha1 != NULL) g_free(sha1);
1316 if (file_container != NULL) {
1317 irreco_backend_file_container_free(file_container);
1319 if (file_data != NULL) g_string_free(file_data, FALSE);
1321 IRRECO_RETURN_BOOL(success);
1324 /** @} */
1328 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
1329 /* Public Functions */
1330 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
1333 * @name Public Functions
1334 * @{
1337 void irreco_show_remote_download_dlg(IrrecoData *irreco_data, GtkWindow *parent)
1339 IrrecoRemoteDownloadDlg *self;
1340 gboolean loop = TRUE;
1341 IrrecoWebdbCache *webdb_cache;
1342 IRRECO_ENTER
1344 self = irreco_remote_download_dlg_new(irreco_data, parent);
1346 do {
1347 gint response = gtk_dialog_run(GTK_DIALOG(self));
1348 if (self->loader_func_id != 0) {
1349 continue;
1351 switch (response) {
1352 case IRRECO_REMOTE_REFRESH:
1353 IRRECO_DEBUG("IRRECO_REMOTE_REFRESH\n");
1354 gtk_tree_store_clear(self->tree_store);
1355 webdb_cache = irreco_data_get_webdb_cache(
1356 self->irreco_data, FALSE);
1358 if (webdb_cache->remote_id_hash != NULL) {
1359 g_hash_table_remove_all(
1360 webdb_cache->remote_id_hash);
1362 if (webdb_cache->remote_categories != NULL) {
1363 irreco_string_table_free(
1364 webdb_cache->remote_categories);
1365 webdb_cache->remote_categories = NULL;
1368 irreco_remote_download_dlg_loader_start(self,
1369 G_SOURCEFUNC(
1370 irreco_remote_download_dlg_loader_categ), NULL);
1372 break;
1373 case IRRECO_REMOTE_DOWNLOAD: {
1374 IrrecoWebdbCache *webdb_cache;
1375 IrrecoWebdbRemote *remote = self->remote;
1376 IrrecoThemeManager *manager =
1377 self->irreco_data->theme_manager;
1378 GList *themes;
1379 gint layout_index = 1;
1380 GString *layout_filename = g_string_new("");
1381 GString *layout_data = NULL;
1382 const gchar *layout_name;
1383 IrrecoButtonLayout *layout;
1384 GString *bg_image = g_string_new("");
1386 IRRECO_DEBUG("IRRECO_REMOTE_DOWNLOAD\n");
1387 webdb_cache = irreco_data_get_webdb_cache(
1388 self->irreco_data, FALSE);
1390 if (self->remote == NULL) {
1391 irreco_error_dlg(GTK_WINDOW(self),
1392 "Select Remote first");
1393 break;
1396 /* Download configurations */
1397 if (!irreco_remote_download_dlg_load_configs(self)) {
1398 goto end;
1401 /* Download themes */
1402 themes = g_list_first(remote->themes);
1403 if (themes == NULL) {
1404 goto end;
1406 while(themes) {
1407 IrrecoWebdbTheme *new_theme;
1408 IrrecoTheme *old_theme = NULL;
1409 if (GPOINTER_TO_INT(themes->data) == 0) {
1410 goto end;
1412 if (!irreco_webdb_cache_get_theme_by_id(
1413 webdb_cache,
1414 GPOINTER_TO_INT(themes->data),
1415 &new_theme)) {
1416 irreco_error_dlg(GTK_WINDOW(self),
1417 irreco_webdb_cache_get_error(
1418 webdb_cache));
1419 goto end;
1421 if (!irreco_string_table_get(manager->themes,
1422 new_theme->name->str, (gpointer *)
1423 &old_theme) || !g_str_equal(
1424 old_theme->version->str,
1425 new_theme->uploaded->str)) {
1426 if (!irreco_theme_download_dlg_run(
1427 self->irreco_data,
1428 GPOINTER_TO_INT(themes->data),
1429 GTK_WINDOW(self))) {
1430 goto end;
1433 themes = themes->next;
1435 irreco_theme_manager_update_theme_manager(manager);
1437 /* Download layout */
1439 /*Create filename */
1440 while(1) {
1441 g_string_printf(layout_filename,
1442 "%s/layout%d.conf",
1443 irreco_get_config_dir("irreco"),
1444 layout_index);
1445 if (!irreco_file_exists(layout_filename->str)) {
1446 IRRECO_DEBUG("%s\n",
1447 layout_filename->str);
1448 break;
1450 layout_index++;
1453 /* Get layout_data */
1454 if (!irreco_webdb_cache_get_file(webdb_cache,
1455 remote->file_hash->str,
1456 remote->file_name->str,
1457 &layout_data)) {
1458 irreco_error_dlg(GTK_WINDOW(self),
1459 irreco_webdb_cache_get_error(
1460 webdb_cache));
1461 goto end;
1464 /* Check if there is same remotename already */
1465 if (irreco_string_table_exists(
1466 irreco_data->irreco_layout_array,
1467 remote->name->str)) {
1468 gint i = 2;
1469 gboolean run = TRUE;
1470 gchar *new_name;
1471 GKeyFile *keyfile = g_key_file_new();
1472 GError *error;
1473 GString *cmd;
1475 /* Create new name */
1476 while (run) {
1477 new_name = g_strdup_printf("%s_%d",
1478 remote->name->str, i++);
1479 if (!irreco_string_table_exists(
1480 irreco_data->irreco_layout_array,
1481 new_name)) {
1482 run = FALSE;
1483 IRRECO_DEBUG("NEW NAME: %s\n",
1484 new_name);
1485 } else {
1486 g_free(new_name);
1490 /* Create key file */
1491 if (!g_key_file_load_from_data(keyfile,
1492 layout_data->str, layout_data->len,
1493 G_KEY_FILE_KEEP_TRANSLATIONS, &error)) {
1494 IRRECO_DEBUG("%s\n", error->message);
1495 g_error_free(error);
1496 goto end;
1499 /* Set new name for remote */
1500 g_key_file_set_string(keyfile, "layout",
1501 "name", new_name);
1503 /* Save keyfile */
1504 irreco_write_keyfile(keyfile,
1505 layout_filename->str);
1507 /* Set new name for every button */
1508 cmd = g_string_new("");
1509 g_string_printf(cmd,
1510 "cat %s | sed s/^layout=%s$/layout=%s/ > %s",
1511 layout_filename->str,
1512 remote->name->str, new_name,
1513 layout_filename->str);
1514 system(cmd->str);
1515 g_string_free(cmd, FALSE);
1517 } else if (!irreco_write_file(layout_filename->str,
1518 layout_data->str, layout_data->len)) {
1519 irreco_error_dlg(GTK_WINDOW(self),
1520 "Save file error!");
1521 goto end;
1523 irreco_config_read_layouts_from_files(irreco_data);
1524 irreco_string_table_index(
1525 irreco_data->irreco_layout_array,
1526 layout_index - 1 , &layout_name,
1527 (gpointer *) &layout);
1529 g_string_printf(bg_image, "%s",
1530 irreco_button_layout_get_bg_image(
1531 layout));
1533 /* Find bg_image from mmc1 */
1534 bg_image = g_string_erase(bg_image, 10, 1);
1535 bg_image = g_string_insert_c(bg_image, 10, '1');
1537 /* Use mmc2 if bg_image does not exists */
1538 if (!irreco_file_exists(bg_image->str)) {
1539 bg_image = g_string_erase(bg_image, 10, 1);
1540 bg_image = g_string_insert_c(bg_image, 10, '2');
1543 /* Set bg-image to layout */
1544 irreco_button_layout_set_bg_image(layout,
1545 bg_image->str);
1546 irreco_config_save_layouts(irreco_data);
1548 irreco_window_manager_set_layout(
1549 self->irreco_data->window_manager,
1550 layout);
1552 irreco_info_dlg(GTK_WINDOW(self),
1553 "Remote downloaded successfully!");
1554 loop = FALSE;
1556 end:
1557 irreco_webdb_remote_print(self->remote);
1558 g_string_free(layout_filename, FALSE);
1559 g_string_free(bg_image, FALSE);
1560 if (layout_data != NULL) {
1561 g_string_free(layout_data, FALSE);
1563 break;
1565 case IRRECO_REMOTE_DONE:
1566 IRRECO_DEBUG("IRRECO_REMOTE_DONE\n");
1567 loop = FALSE;
1568 break;
1571 } while (loop);
1573 gtk_widget_destroy(GTK_WIDGET(self));
1574 IRRECO_RETURN
1577 /** @} */
1581 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
1582 /* Events and Callbacks */
1583 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
1586 * @name Events and Callbacks
1587 * @{
1590 static gboolean irreco_remote_download_dlg_map_event(
1591 IrrecoRemoteDownloadDlg *self,
1592 GdkEvent *event,
1593 gpointer user_data)
1595 IRRECO_ENTER
1596 irreco_remote_download_dlg_loader_start(self,
1597 G_SOURCEFUNC(irreco_remote_download_dlg_loader_categ), NULL);
1598 IRRECO_RETURN_BOOL(FALSE);
1601 static void irreco_remote_download_dlg_destroy_event(
1602 IrrecoRemoteDownloadDlg *self,
1603 gpointer user_data)
1605 IRRECO_ENTER
1606 irreco_remote_download_dlg_loader_stop(self);
1607 IRRECO_RETURN
1610 static void irreco_remote_download_dlg_row_activated_event(
1611 GtkTreeView *tree_view,
1612 GtkTreePath *path,
1613 GtkTreeViewColumn *column,
1614 IrrecoRemoteDownloadDlg *self)
1616 IRRECO_ENTER
1618 if (gtk_tree_view_row_expanded(tree_view, path)) {
1619 gtk_tree_view_expand_row(tree_view, path, FALSE);
1620 } else {
1621 gtk_tree_view_collapse_row(tree_view, path);
1623 IRRECO_RETURN
1626 static void irreco_remote_download_dlg_row_expanded_event(
1627 GtkTreeView *tree_view,
1628 GtkTreeIter *iter,
1629 GtkTreePath *path,
1630 IrrecoRemoteDownloadDlg *self)
1632 IRRECO_ENTER
1634 irreco_remote_download_dlg_clean_details(self);
1636 if (self->loader_func_id != 0) {
1637 gtk_tree_view_collapse_row(tree_view, path);
1640 if (!irreco_remote_download_dlg_row_is_loaded(self, iter)) {
1641 switch (irreco_remote_download_dlg_row_get_type(self, iter)) {
1642 case ROW_TYPE_CATEGORY:
1643 irreco_remote_download_dlg_loader_start(self,
1644 G_SOURCEFUNC(
1645 irreco_remote_download_dlg_loader_manuf), iter);
1646 break;
1648 case ROW_TYPE_MANUFACTURER:
1649 irreco_remote_download_dlg_loader_start(self,
1650 G_SOURCEFUNC(
1651 irreco_remote_download_dlg_loader_model), iter);
1652 break;
1654 case ROW_TYPE_MODEL:
1655 irreco_remote_download_dlg_loader_start(self,
1656 G_SOURCEFUNC(
1657 irreco_remote_download_dlg_loader_creators),
1658 iter);
1659 break;
1661 case ROW_TYPE_CREATOR:
1662 irreco_remote_download_dlg_loader_start(self,
1663 G_SOURCEFUNC(
1664 irreco_remote_download_dlg_loader_remotes),
1665 iter);
1666 break;
1670 IRRECO_RETURN
1673 static void irreco_remote_download_dlg_row_collapsed_event(
1674 GtkTreeView *tree_view,
1675 GtkTreeIter *iter,
1676 GtkTreePath *path,
1677 IrrecoRemoteDownloadDlg *self)
1679 IRRECO_ENTER
1681 irreco_remote_download_dlg_clean_details(self);
1683 IRRECO_RETURN
1686 static void irreco_remote_download_dlg_row_selected_event(GtkTreeSelection *sel,
1687 IrrecoRemoteDownloadDlg *self)
1689 GtkTreeIter iter;
1690 GtkTreeModel *model;
1692 IRRECO_ENTER
1694 if (gtk_tree_selection_get_selected (sel, &model, &iter))
1696 switch (irreco_remote_download_dlg_row_get_type(self, &iter)) {
1698 case ROW_TYPE_REMOTE:
1699 IRRECO_DEBUG("ROW_TYPE_REMOTE\n");
1701 IrrecoWebdbCache *webdb_cache;
1702 IrrecoWebdbRemote *remote;
1703 GString *download_count;
1704 GString *theme_names = g_string_new("");
1705 GString *backend_names = g_string_new("");
1706 GList *configs;
1707 GList *backends = NULL;
1708 GList *dummy_list = NULL;
1709 GList *themes = NULL;
1710 GtkTextBuffer *buffer;
1711 gint id;
1713 /* Get remote_id */
1714 gtk_tree_model_get(GTK_TREE_MODEL(self->tree_store),
1715 &iter, DATA_COL,(gpointer) &id, -1);
1717 /* Get remote by id */
1718 webdb_cache = irreco_data_get_webdb_cache(
1719 self->irreco_data, FALSE);
1720 if (!irreco_webdb_cache_get_remote_by_id(webdb_cache,
1722 &remote)) {
1723 irreco_error_dlg(GTK_WINDOW(self),
1724 irreco_webdb_cache_get_error(
1725 webdb_cache));
1726 goto end;
1730 /* Model */
1731 gtk_label_set_text(GTK_LABEL(self->details_model),
1732 remote->model->str);
1734 /* Creator */
1735 gtk_label_set_text(GTK_LABEL(self->details_creator),
1736 remote->creator->str);
1738 /* Downloaded */
1739 download_count = g_string_new("");
1740 g_string_printf(download_count, "%d times",
1741 remote->download_count);
1742 gtk_label_set_text(GTK_LABEL(self->details_downloaded),
1743 download_count->str);
1744 g_string_free(download_count, FALSE);
1746 /* Comment */
1747 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(
1748 self->details_comment));
1749 gtk_text_buffer_set_text(buffer, remote->comment->str,
1750 remote->comment->len);
1751 gtk_text_view_set_buffer(GTK_TEXT_VIEW(
1752 self->details_comment),
1753 buffer);
1755 /* Get configurations and get backends */
1756 if (!irreco_webdb_cache_get_configurations_of_remote(
1757 webdb_cache, id, &configs)) {
1758 irreco_error_dlg(GTK_WINDOW(self),
1759 irreco_webdb_cache_get_error(
1760 webdb_cache));
1761 goto end;
1763 configs = g_list_first(configs);
1764 while(configs) {
1765 IrrecoWebdbConf *config = NULL;
1766 if (GPOINTER_TO_INT(configs->data) != 0 &&
1767 !irreco_webdb_cache_get_configuration(
1768 webdb_cache, GPOINTER_TO_INT(configs->data),
1769 &config)) {
1770 irreco_error_dlg(GTK_WINDOW(self),
1771 irreco_webdb_cache_get_error(
1772 webdb_cache));
1773 goto end;
1776 if (config != NULL) {
1777 backends = g_list_append(backends,
1778 config->backend->str);
1780 configs = configs->next;
1783 backends = g_list_first(backends);
1784 dummy_list = backends;
1785 if (backends == NULL) {
1786 g_string_append_printf(backend_names, "-");
1787 } else {
1788 /* deletion of duplicates */
1789 while(backends) {
1790 const char *backend = backends->data;
1791 GList *next = backends->next;
1792 while(next) {
1793 if (g_str_equal(backend,
1794 next->data)) {
1795 backends =
1796 g_list_remove(backends,
1797 next->data);
1798 dummy_list = backends;
1799 next = backends;
1801 next = next->next;
1803 backends = backends->next;
1807 /* Create Backends string */
1808 backends = g_list_first(dummy_list);
1809 while(backends) {
1810 const char *backend = backends->data;
1811 g_string_append_printf(backend_names,
1812 "%s", backend);
1813 backends = backends->next;
1814 if (backends) {
1815 if (!backends->next) {
1816 g_string_append_printf(
1817 backend_names,
1818 _(" and\n"));
1819 } else {
1820 g_string_append_printf(
1821 backend_names,
1822 ",\n");
1828 gtk_label_set_text(GTK_LABEL(self->details_backends),
1829 backend_names->str);
1831 /* Create themes string */
1832 if (!irreco_webdb_cache_get_themes_of_remote(
1833 webdb_cache, id, &themes)) {
1834 irreco_error_dlg(GTK_WINDOW(self),
1835 irreco_webdb_cache_get_error(
1836 webdb_cache));
1837 goto end;
1839 themes = g_list_first(themes);
1840 if (themes == NULL ||
1841 GPOINTER_TO_INT(themes->data) == 0) {
1842 g_string_append(theme_names, "-");
1844 while(themes) {
1845 IrrecoWebdbTheme *theme = NULL;
1846 if (GPOINTER_TO_INT(themes->data) != 0 &&
1847 !irreco_webdb_cache_get_theme_by_id(
1848 webdb_cache, GPOINTER_TO_INT(themes->data),
1849 &theme)) {
1850 irreco_error_dlg(GTK_WINDOW(self),
1851 irreco_webdb_cache_get_error(
1852 webdb_cache));
1853 goto end;
1855 themes = themes->next;
1856 if (theme != NULL) {
1857 g_string_append_printf(theme_names,
1858 "%s", theme->name->str);
1860 if (themes) {
1861 if (!themes->next) {
1862 g_string_append_printf(
1863 theme_names,
1864 _(" and\n"));
1865 } else {
1866 g_string_append_printf(
1867 theme_names,
1868 ",\n");
1872 gtk_label_set_text(GTK_LABEL(self->details_themes),
1873 theme_names->str);
1875 self->remote = remote;
1876 gtk_widget_hide(GTK_WIDGET(self->help_text));
1877 gtk_widget_show(GTK_WIDGET(self->details));
1878 end:
1879 g_string_free(theme_names, TRUE);
1880 g_string_free(backend_names, TRUE);
1882 break;
1884 default:
1885 irreco_remote_download_dlg_clean_details(self);
1886 gtk_widget_show(GTK_WIDGET(self->help_text));
1887 gtk_widget_hide(GTK_WIDGET(self->details));
1888 break;
1892 IRRECO_RETURN
1895 /** @} */
1897 /** @} */