2012-02-03 [wwp] 3.8.0cvs24
[claws.git] / src / gtk / progressdialog.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2011 Hiroyuki Yamamoto and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
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.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27
28 #include "progressdialog.h"
29 #include "gtkutils.h"
30 #include "utils.h"
31 #include "prefs_common.h"
32
33 enum {
34         PROGRESS_IMAGE,
35         PROGRESS_ACCOUNT,
36         PROGRESS_STATE,
37         N_PROGRESS_COLUMNS
38 };
39
40 static gint progress_dialog_insert_account(ProgressDialog *progress,
41                                            gint            row,
42                                            const gchar    *account,
43                                            const gchar    *status,
44                                            GdkPixbuf      *image);
45
46 ProgressDialog *progress_dialog_create(void)
47 {
48         ProgressDialog *progress;
49         GtkWidget *dialog;
50         GtkWidget *hbox;
51         GtkWidget *vbox;
52         GtkWidget *label;
53         GtkWidget *showlog_btn;
54         GtkWidget *cancel_btn;
55         GtkWidget *progressbar;
56         GtkWidget *scrolledwin;
57         GtkWidget *treeview;
58         GtkListStore *store;
59         GtkTreeSelection *selection;
60         GtkTreeViewColumn *column;
61         GtkCellRenderer *renderer;
62
63         debug_print("Creating progress dialog...\n");
64         progress = g_new0(ProgressDialog, 1);
65
66         dialog = gtk_dialog_new();
67         gtk_container_set_border_width(GTK_CONTAINER(dialog), 8);
68         gtk_window_set_resizable(GTK_WINDOW(dialog), TRUE);
69         gtk_widget_realize(dialog);
70
71         gtk_container_set_border_width
72                 (GTK_CONTAINER(gtk_dialog_get_action_area(GTK_DIALOG(dialog))), 0);
73         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
74         gtk_box_set_spacing(GTK_BOX(vbox), 8);
75
76         hbox = gtk_hbox_new(FALSE, 0);
77         gtk_box_pack_start(GTK_BOX(vbox), hbox,
78                            FALSE, FALSE, 8);
79         gtk_widget_show(hbox);
80
81         label = gtk_label_new("");
82         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 8);
83         gtk_widget_show(label);
84
85         showlog_btn = gtk_dialog_add_button(GTK_DIALOG(dialog),
86                                            _("_View log"),
87                                            GTK_RESPONSE_NONE);
88         cancel_btn = gtk_dialog_add_button(GTK_DIALOG(dialog),
89                                            GTK_STOCK_CANCEL,
90                                            GTK_RESPONSE_NONE);
91         gtk_widget_grab_default(cancel_btn);
92         gtk_widget_grab_focus(cancel_btn);
93
94         progressbar = gtk_progress_bar_new();
95         gtk_box_pack_start(GTK_BOX(vbox), progressbar,
96                            FALSE, FALSE, 0);
97         gtk_widget_show(progressbar);
98
99         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
100         gtk_widget_show(scrolledwin);
101         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin,
102                            TRUE, TRUE, 0);
103         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
104                                        GTK_POLICY_AUTOMATIC,
105                                        GTK_POLICY_AUTOMATIC);
106
107         store = gtk_list_store_new(N_PROGRESS_COLUMNS, GDK_TYPE_PIXBUF, G_TYPE_STRING,
108                                    G_TYPE_STRING, G_TYPE_POINTER);
109
110         treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
111         g_object_unref(G_OBJECT(store));
112         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview), TRUE);
113         gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(treeview),
114                                      prefs_common.use_stripes_everywhere);
115         gtk_widget_show(treeview);
116         gtk_container_add(GTK_CONTAINER(scrolledwin), treeview);
117         gtk_widget_set_size_request(treeview, -1, 120);
118
119         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
120         gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
121
122         renderer = gtk_cell_renderer_pixbuf_new();
123         g_object_set(renderer, "xalign", 0.5, NULL);
124         column = gtk_tree_view_column_new_with_attributes
125                 (NULL, renderer, "pixbuf", PROGRESS_IMAGE, NULL);
126         gtk_tree_view_column_set_alignment(column, 0.5);
127         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
128         gtk_tree_view_column_set_fixed_width(column, 20);
129         gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
130
131         renderer = gtk_cell_renderer_text_new();
132         column = gtk_tree_view_column_new_with_attributes
133                 (_("Account"), renderer, "text", PROGRESS_ACCOUNT, NULL);
134         gtk_tree_view_column_set_resizable(column, TRUE);
135         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
136         gtk_tree_view_column_set_fixed_width(column, 160);
137         gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
138
139         renderer = gtk_cell_renderer_text_new();
140         column = gtk_tree_view_column_new_with_attributes
141                 (_("Status"), renderer, "text", PROGRESS_STATE, NULL);
142         gtk_tree_view_column_set_resizable(column, TRUE);
143         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
144         gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
145         
146         progress->window      = dialog;
147         progress->label       = label;
148         progress->showlog_btn  = showlog_btn;
149         progress->cancel_btn  = cancel_btn;
150         progress->progressbar = progressbar;
151         progress->treeview    = treeview;
152         progress->store       = store;
153
154         return progress;
155 }
156
157 void progress_dialog_set_label(ProgressDialog *progress, gchar *str)
158 {
159         gtk_label_set_text(GTK_LABEL(progress->label), str);
160 }
161
162 void progress_dialog_get_fraction(ProgressDialog *progress)
163 {
164         gtk_progress_bar_get_fraction(GTK_PROGRESS_BAR(progress->progressbar));
165 }
166
167 void progress_dialog_set_fraction(ProgressDialog *progress,
168                                   gfloat percentage)
169 {
170         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->progressbar),
171                                       percentage);
172 }
173
174 void progress_dialog_destroy(ProgressDialog *progress)
175 {
176         if (progress) {
177                 gtk_widget_destroy(progress->window);
178                 g_free(progress);
179         }
180 }
181
182 /*!
183  *\return       gint Row where status was set
184  */
185 gint progress_dialog_list_set_status(ProgressDialog *progress,
186                                      gint            row,
187                                      const gchar    *status)
188 {
189         return progress_dialog_insert_account(progress, row, NULL,
190                                               status, NULL);
191 }
192
193 /*!
194  *\return       gint Row where data were set 
195  */
196 gint progress_dialog_list_set(ProgressDialog    *progress,
197                               gint               row,
198                               GdkPixbuf         *image,
199                               const gchar       *account_name,
200                               const gchar       *status)
201 {
202         return progress_dialog_insert_account(progress, row, account_name,
203                                               status, image);
204 }
205
206 void progress_dialog_scroll_to_row(ProgressDialog *progress, gint row)
207 {
208         GtkTreeModel *model = GTK_TREE_MODEL(progress->store);
209         GtkTreeIter iter;
210         GtkTreePath *path;
211
212         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
213                 return;
214
215         path = gtk_tree_model_get_path(model, &iter);
216         gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(progress->treeview),
217                                      path, NULL, FALSE, 0.0, 0.0);
218         gtk_tree_path_free(path);
219 }
220
221 static gint progress_dialog_insert_account(ProgressDialog *progress,
222                                            gint            row,
223                                            const gchar    *account,
224                                            const gchar    *status,
225                                            GdkPixbuf      *image)
226 {
227         GtkTreeIter iter;
228         GtkListStore *store = progress->store;
229         gint result = -1;                                       
230         
231         if (account == NULL && status == NULL && image == NULL)
232                 return -1;
233
234         /* see if row exists, if not append */
235         if (row < 0 || !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(store), 
236                                                       &iter, NULL, row)) {
237                 result = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(store),
238                                                         NULL);
239                 gtk_list_store_append(store, &iter);
240         } else
241                 result = row;
242
243         /*
244          * XXX: uhm, when does the iter invalidate? sure not while
245          * just setting a row's column i hope?
246          */
247         
248         if (account)
249                 gtk_list_store_set(store, &iter, 
250                                    PROGRESS_ACCOUNT, account,
251                                    -1);
252         if (status)
253                 gtk_list_store_set(store, &iter,
254                                    PROGRESS_STATE, status,
255                                    -1);
256         if (image) 
257                 gtk_list_store_set(store, &iter,
258                                    PROGRESS_IMAGE, image,
259                                    -1);
260
261         return result;
262 }