7ad66fc6a6b70219c8e062e2812980a4b00093a5
[claws.git] / src / gtk / progressdialog.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2005 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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_policy(GTK_WINDOW(dialog), FALSE, TRUE, 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), TRUE);
122         gtk_widget_show(treeview);
123         gtk_container_add(GTK_CONTAINER(scrolledwin), treeview);
124         gtk_widget_set_size_request(treeview, -1, 120);
125
126         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
127         gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
128
129         renderer = gtk_cell_renderer_pixbuf_new();
130         g_object_set(renderer, "xalign", 0.5, NULL);
131         column = gtk_tree_view_column_new_with_attributes
132                 (NULL, renderer, "pixbuf", PROGRESS_IMAGE, NULL);
133         gtk_tree_view_column_set_alignment(column, 0.5);
134         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
135         gtk_tree_view_column_set_fixed_width(column, 20);
136         gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
137
138         renderer = gtk_cell_renderer_text_new();
139         column = gtk_tree_view_column_new_with_attributes
140                 (_("Account"), renderer, "text", PROGRESS_ACCOUNT, NULL);
141         gtk_tree_view_column_set_resizable(column, TRUE);
142         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
143         gtk_tree_view_column_set_fixed_width(column, 160);
144         gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
145
146         renderer = gtk_cell_renderer_text_new();
147         column = gtk_tree_view_column_new_with_attributes
148                 (_("Status"), renderer, "text", PROGRESS_STATE, NULL);
149         gtk_tree_view_column_set_resizable(column, TRUE);
150         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
151         gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
152         
153         progress->window      = dialog;
154         progress->label       = label;
155         progress->cancel_btn  = cancel_btn;
156         progress->progressbar = progressbar;
157         progress->treeview    = treeview;
158         progress->store       = store;
159
160         return progress;
161 }
162
163 void progress_dialog_set_label(ProgressDialog *progress, gchar *str)
164 {
165         gtk_label_set_text(GTK_LABEL(progress->label), str);
166 }
167
168 void progress_dialog_get_fraction(ProgressDialog *progress)
169 {
170         gtk_progress_bar_get_fraction(GTK_PROGRESS_BAR(progress->progressbar));
171 }
172
173 void progress_dialog_set_fraction(ProgressDialog *progress,
174                                   gfloat percentage)
175 {
176         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->progressbar),
177                                       percentage);
178 }
179
180 void progress_dialog_destroy(ProgressDialog *progress)
181 {
182         if (progress) {
183                 gtk_widget_destroy(progress->window);
184                 g_free(progress);
185         }
186 }
187
188 /*!
189  *\return       gint Row where account was set
190  */
191 gint progress_dialog_list_set_account(ProgressDialog *progress,
192                                       gint            row,
193                                       const gchar    *account_name)
194 {
195         return progress_dialog_insert_account(progress, row, account_name,
196                                               NULL, NULL);
197 }
198
199 /*!
200  *\return       gint Row where image was set
201  */
202 gint progress_dialog_list_set_image(ProgressDialog *progress,
203                                     gint            row,
204                                     GdkPixbuf      *image)
205 {
206         return progress_dialog_insert_account(progress, row, NULL,
207                                               NULL, image);
208 }
209
210 /*!
211  *\return       gint Row where status was set
212  */
213 gint progress_dialog_list_set_status(ProgressDialog *progress,
214                                      gint            row,
215                                      const gchar    *status)
216 {
217         return progress_dialog_insert_account(progress, row, NULL,
218                                               status, NULL);
219 }
220
221 /*!
222  *\return       gint Row where data were set 
223  */
224 gint progress_dialog_list_set(ProgressDialog    *progress,
225                               gint               row,
226                               GdkPixbuf         *image,
227                               const gchar       *account_name,
228                               const gchar       *status)
229 {
230         return progress_dialog_insert_account(progress, row, account_name,
231                                               status, image);
232 }
233
234 void progress_dialog_scroll_to_row(ProgressDialog *progress, gint row)
235 {
236         GtkTreeModel *model = GTK_TREE_MODEL(progress->store);
237         GtkTreeIter iter;
238         GtkTreePath *path;
239
240         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
241                 return;
242
243         path = gtk_tree_model_get_path(model, &iter);
244         gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(progress->treeview),
245                                      path, NULL, FALSE, 0.0, 0.0);
246         gtk_tree_path_free(path);
247 }
248
249 static gint progress_dialog_insert_account(ProgressDialog *progress,
250                                            gint            row,
251                                            const gchar    *account,
252                                            const gchar    *status,
253                                            GdkPixbuf      *image)
254 {
255         GtkTreeIter iter;
256         GtkListStore *store = progress->store;
257         gint result = -1;                                       
258         
259         if (account == NULL && status == NULL && image == NULL)
260                 return -1;
261
262         /* see if row exists, if not append */
263         if (row < 0 || !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(store), 
264                                                       &iter, NULL, row)) {
265                 result = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(store),
266                                                         NULL);
267                 gtk_list_store_append(store, &iter);
268         } else
269                 result = row;
270
271         /*
272          * XXX: uhm, when does the iter invalidate? sure not while
273          * just setting a row's column i hope?
274          */
275         
276         if (account)
277                 gtk_list_store_set(store, &iter, 
278                                    PROGRESS_ACCOUNT, account,
279                                    -1);
280         if (status)
281                 gtk_list_store_set(store, &iter,
282                                    PROGRESS_STATE, status,
283                                    -1);
284         if (image)
285                 gtk_list_store_set(store, &iter,
286                                    PROGRESS_IMAGE, image,
287                                    -1);
288
289         return result;
290 }