9a3f809c9f34efa8e7b0916a0fc5c0c4f2cbc6e5
[claws.git] / src / gtk / progressdialog.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto
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 2 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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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/gtkdialog.h>
27 #include <gtk/gtkhbox.h>
28 #include <gtk/gtklabel.h>
29 #include <gtk/gtkprogressbar.h>
30 #include <gtk/gtkscrolledwindow.h>
31 #include <gtk/gtkliststore.h>
32 #include <gtk/gtktreeview.h>
33 #include <gtk/gtktreeselection.h>
34 #include <gtk/gtkcellrendererpixbuf.h>
35 #include <gtk/gtkcellrenderertext.h>
36 #include <gtk/gtkbutton.h>
37 #include <gtk/gtkstock.h>
38
39 #include "progressdialog.h"
40 #include "gtkutils.h"
41 #include "utils.h"
42 #include "prefs_common.h"
43
44 enum {
45         PROGRESS_IMAGE,
46         PROGRESS_ACCOUNT,
47         PROGRESS_STATE,
48         N_PROGRESS_COLUMNS
49 };
50
51 static gint progress_dialog_insert_account(ProgressDialog *progress,
52                                            gint            row,
53                                            const gchar    *account,
54                                            const gchar    *status,
55                                            GdkPixbuf      *image);
56
57 ProgressDialog *progress_dialog_create(void)
58 {
59         ProgressDialog *progress;
60         GtkWidget *dialog;
61         GtkWidget *hbox;
62         GtkWidget *label;
63         GtkWidget *cancel_btn;
64         GtkWidget *progressbar;
65         GtkWidget *scrolledwin;
66         GtkWidget *treeview;
67         GtkListStore *store;
68         GtkTreeSelection *selection;
69         GtkTreeViewColumn *column;
70         GtkCellRenderer *renderer;
71
72         debug_print("Creating progress dialog...\n");
73         progress = g_new0(ProgressDialog, 1);
74
75         dialog = gtk_dialog_new();
76         gtk_widget_set_size_request(dialog, 460, -1);
77         gtk_container_set_border_width(GTK_CONTAINER(dialog), 8);
78         gtk_window_set_resizable(GTK_WINDOW(dialog), TRUE);
79         gtk_widget_realize(dialog);
80
81         gtk_container_set_border_width
82                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 0);
83         gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(dialog)->vbox), 8);
84         gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
85
86         hbox = gtk_hbox_new(FALSE, 0);
87         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox,
88                            FALSE, FALSE, 8);
89         gtk_widget_show(hbox);
90
91         label = gtk_label_new("");
92         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 8);
93         gtk_widget_show(label);
94
95         cancel_btn = gtk_dialog_add_button(GTK_DIALOG(dialog),
96                                            GTK_STOCK_CANCEL,
97                                            GTK_RESPONSE_NONE);
98         gtk_widget_grab_default(cancel_btn);
99         gtk_widget_grab_focus(cancel_btn);
100
101         progressbar = gtk_progress_bar_new();
102         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), progressbar,
103                            FALSE, FALSE, 0);
104         gtk_widget_show(progressbar);
105
106         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
107         gtk_widget_show(scrolledwin);
108         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), scrolledwin,
109                            TRUE, TRUE, 0);
110         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
111                                        GTK_POLICY_AUTOMATIC,
112                                        GTK_POLICY_AUTOMATIC);
113
114
115         store = gtk_list_store_new(N_PROGRESS_COLUMNS, GDK_TYPE_PIXBUF, G_TYPE_STRING,
116                                    G_TYPE_STRING, G_TYPE_POINTER);
117
118         treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
119         g_object_unref(G_OBJECT(store));
120         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview), TRUE);
121         gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(treeview),
122                                      prefs_common.enable_rules_hint);
123         gtk_widget_show(treeview);
124         gtk_container_add(GTK_CONTAINER(scrolledwin), treeview);
125         gtk_widget_set_size_request(treeview, -1, 120);
126
127         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
128         gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
129
130         renderer = gtk_cell_renderer_pixbuf_new();
131         g_object_set(renderer, "xalign", 0.5, NULL);
132         column = gtk_tree_view_column_new_with_attributes
133                 (NULL, renderer, "pixbuf", PROGRESS_IMAGE, NULL);
134         gtk_tree_view_column_set_alignment(column, 0.5);
135         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
136         gtk_tree_view_column_set_fixed_width(column, 20);
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                 (_("Account"), renderer, "text", PROGRESS_ACCOUNT, 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_column_set_fixed_width(column, 160);
145         gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
146
147         renderer = gtk_cell_renderer_text_new();
148         column = gtk_tree_view_column_new_with_attributes
149                 (_("Status"), renderer, "text", PROGRESS_STATE, NULL);
150         gtk_tree_view_column_set_resizable(column, TRUE);
151         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
152         gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
153         
154         progress->window      = dialog;
155         progress->label       = label;
156         progress->cancel_btn  = cancel_btn;
157         progress->progressbar = progressbar;
158         progress->treeview    = treeview;
159         progress->store       = store;
160
161         return progress;
162 }
163
164 void progress_dialog_set_label(ProgressDialog *progress, gchar *str)
165 {
166         gtk_label_set_text(GTK_LABEL(progress->label), str);
167 }
168
169 void progress_dialog_get_fraction(ProgressDialog *progress)
170 {
171         gtk_progress_bar_get_fraction(GTK_PROGRESS_BAR(progress->progressbar));
172 }
173
174 void progress_dialog_set_fraction(ProgressDialog *progress,
175                                   gfloat percentage)
176 {
177         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->progressbar),
178                                       percentage);
179 }
180
181 void progress_dialog_destroy(ProgressDialog *progress)
182 {
183         if (progress) {
184                 gtk_widget_destroy(progress->window);
185                 g_free(progress);
186         }
187 }
188
189 /*!
190  *\return       gint Row where account was set
191  */
192 gint progress_dialog_list_set_account(ProgressDialog *progress,
193                                       gint            row,
194                                       const gchar    *account_name)
195 {
196         return progress_dialog_insert_account(progress, row, account_name,
197                                               NULL, NULL);
198 }
199
200 /*!
201  *\return       gint Row where image was set
202  */
203 gint progress_dialog_list_set_image(ProgressDialog *progress,
204                                     gint            row,
205                                     GdkPixbuf      *image)
206 {
207         return progress_dialog_insert_account(progress, row, NULL,
208                                               NULL, image);
209 }
210
211 /*!
212  *\return       gint Row where status was set
213  */
214 gint progress_dialog_list_set_status(ProgressDialog *progress,
215                                      gint            row,
216                                      const gchar    *status)
217 {
218         return progress_dialog_insert_account(progress, row, NULL,
219                                               status, NULL);
220 }
221
222 /*!
223  *\return       gint Row where data were set 
224  */
225 gint progress_dialog_list_set(ProgressDialog    *progress,
226                               gint               row,
227                               GdkPixbuf         *image,
228                               const gchar       *account_name,
229                               const gchar       *status)
230 {
231         return progress_dialog_insert_account(progress, row, account_name,
232                                               status, image);
233 }
234
235 void progress_dialog_scroll_to_row(ProgressDialog *progress, gint row)
236 {
237         GtkTreeModel *model = GTK_TREE_MODEL(progress->store);
238         GtkTreeIter iter;
239         GtkTreePath *path;
240
241         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
242                 return;
243
244         path = gtk_tree_model_get_path(model, &iter);
245         gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(progress->treeview),
246                                      path, NULL, FALSE, 0.0, 0.0);
247         gtk_tree_path_free(path);
248 }
249
250 static gint progress_dialog_insert_account(ProgressDialog *progress,
251                                            gint            row,
252                                            const gchar    *account,
253                                            const gchar    *status,
254                                            GdkPixbuf      *image)
255 {
256         GtkTreeIter iter;
257         GtkListStore *store = progress->store;
258         gint result = -1;                                       
259         
260         if (account == NULL && status == NULL && image == NULL)
261                 return -1;
262
263         /* see if row exists, if not append */
264         if (row < 0 || !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(store), 
265                                                       &iter, NULL, row)) {
266                 result = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(store),
267                                                         NULL);
268                 gtk_list_store_append(store, &iter);
269         } else
270                 result = row;
271
272         /*
273          * XXX: uhm, when does the iter invalidate? sure not while
274          * just setting a row's column i hope?
275          */
276         
277         if (account)
278                 gtk_list_store_set(store, &iter, 
279                                    PROGRESS_ACCOUNT, account,
280                                    -1);
281         if (status)
282                 gtk_list_store_set(store, &iter,
283                                    PROGRESS_STATE, status,
284                                    -1);
285         if (image) 
286                 gtk_list_store_set(store, &iter,
287                                    PROGRESS_IMAGE, image,
288                                    -1);
289
290         return result;
291 }