2010-10-17 [colin] 3.7.6cvs56
[claws.git] / src / gtk / progressdialog.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2009 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 *label;
52         GtkWidget *cancel_btn;
53         GtkWidget *progressbar;
54         GtkWidget *scrolledwin;
55         GtkWidget *treeview;
56         GtkListStore *store;
57         GtkTreeSelection *selection;
58         GtkTreeViewColumn *column;
59         GtkCellRenderer *renderer;
60
61         debug_print("Creating progress dialog...\n");
62         progress = g_new0(ProgressDialog, 1);
63
64         dialog = gtk_dialog_new();
65         gtk_container_set_border_width(GTK_CONTAINER(dialog), 8);
66         gtk_window_set_resizable(GTK_WINDOW(dialog), TRUE);
67         gtk_widget_realize(dialog);
68
69         gtk_container_set_border_width
70                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 0);
71         gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(dialog)->vbox), 8);
72
73         hbox = gtk_hbox_new(FALSE, 0);
74         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox,
75                            FALSE, FALSE, 8);
76         gtk_widget_show(hbox);
77
78         label = gtk_label_new("");
79         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 8);
80         gtk_widget_show(label);
81
82         cancel_btn = gtk_dialog_add_button(GTK_DIALOG(dialog),
83                                            GTK_STOCK_CANCEL,
84                                            GTK_RESPONSE_NONE);
85         gtk_widget_grab_default(cancel_btn);
86         gtk_widget_grab_focus(cancel_btn);
87
88         progressbar = gtk_progress_bar_new();
89         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), progressbar,
90                            FALSE, FALSE, 0);
91         gtk_widget_show(progressbar);
92
93         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
94         gtk_widget_show(scrolledwin);
95         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), scrolledwin,
96                            TRUE, TRUE, 0);
97         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
98                                        GTK_POLICY_AUTOMATIC,
99                                        GTK_POLICY_AUTOMATIC);
100
101
102         store = gtk_list_store_new(N_PROGRESS_COLUMNS, GDK_TYPE_PIXBUF, G_TYPE_STRING,
103                                    G_TYPE_STRING, G_TYPE_POINTER);
104
105         treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
106         g_object_unref(G_OBJECT(store));
107         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview), TRUE);
108         gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(treeview),
109                                      prefs_common.use_stripes_everywhere);
110         gtk_widget_show(treeview);
111         gtk_container_add(GTK_CONTAINER(scrolledwin), treeview);
112         gtk_widget_set_size_request(treeview, -1, 120);
113
114         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
115         gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
116
117         renderer = gtk_cell_renderer_pixbuf_new();
118         g_object_set(renderer, "xalign", 0.5, NULL);
119         column = gtk_tree_view_column_new_with_attributes
120                 (NULL, renderer, "pixbuf", PROGRESS_IMAGE, NULL);
121         gtk_tree_view_column_set_alignment(column, 0.5);
122         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
123         gtk_tree_view_column_set_fixed_width(column, 20);
124         gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
125
126         renderer = gtk_cell_renderer_text_new();
127         column = gtk_tree_view_column_new_with_attributes
128                 (_("Account"), renderer, "text", PROGRESS_ACCOUNT, NULL);
129         gtk_tree_view_column_set_resizable(column, TRUE);
130         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
131         gtk_tree_view_column_set_fixed_width(column, 160);
132         gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
133
134         renderer = gtk_cell_renderer_text_new();
135         column = gtk_tree_view_column_new_with_attributes
136                 (_("Status"), renderer, "text", PROGRESS_STATE, NULL);
137         gtk_tree_view_column_set_resizable(column, TRUE);
138         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
139         gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
140         
141         progress->window      = dialog;
142         progress->label       = label;
143         progress->cancel_btn  = cancel_btn;
144         progress->progressbar = progressbar;
145         progress->treeview    = treeview;
146         progress->store       = store;
147
148         return progress;
149 }
150
151 void progress_dialog_set_label(ProgressDialog *progress, gchar *str)
152 {
153         gtk_label_set_text(GTK_LABEL(progress->label), str);
154 }
155
156 void progress_dialog_get_fraction(ProgressDialog *progress)
157 {
158         gtk_progress_bar_get_fraction(GTK_PROGRESS_BAR(progress->progressbar));
159 }
160
161 void progress_dialog_set_fraction(ProgressDialog *progress,
162                                   gfloat percentage)
163 {
164         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->progressbar),
165                                       percentage);
166 }
167
168 void progress_dialog_destroy(ProgressDialog *progress)
169 {
170         if (progress) {
171                 gtk_widget_destroy(progress->window);
172                 g_free(progress);
173         }
174 }
175
176 /*!
177  *\return       gint Row where status was set
178  */
179 gint progress_dialog_list_set_status(ProgressDialog *progress,
180                                      gint            row,
181                                      const gchar    *status)
182 {
183         return progress_dialog_insert_account(progress, row, NULL,
184                                               status, NULL);
185 }
186
187 /*!
188  *\return       gint Row where data were set 
189  */
190 gint progress_dialog_list_set(ProgressDialog    *progress,
191                               gint               row,
192                               GdkPixbuf         *image,
193                               const gchar       *account_name,
194                               const gchar       *status)
195 {
196         return progress_dialog_insert_account(progress, row, account_name,
197                                               status, image);
198 }
199
200 void progress_dialog_scroll_to_row(ProgressDialog *progress, gint row)
201 {
202         GtkTreeModel *model = GTK_TREE_MODEL(progress->store);
203         GtkTreeIter iter;
204         GtkTreePath *path;
205
206         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
207                 return;
208
209         path = gtk_tree_model_get_path(model, &iter);
210         gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(progress->treeview),
211                                      path, NULL, FALSE, 0.0, 0.0);
212         gtk_tree_path_free(path);
213 }
214
215 static gint progress_dialog_insert_account(ProgressDialog *progress,
216                                            gint            row,
217                                            const gchar    *account,
218                                            const gchar    *status,
219                                            GdkPixbuf      *image)
220 {
221         GtkTreeIter iter;
222         GtkListStore *store = progress->store;
223         gint result = -1;                                       
224         
225         if (account == NULL && status == NULL && image == NULL)
226                 return -1;
227
228         /* see if row exists, if not append */
229         if (row < 0 || !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(store), 
230                                                       &iter, NULL, row)) {
231                 result = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(store),
232                                                         NULL);
233                 gtk_list_store_append(store, &iter);
234         } else
235                 result = row;
236
237         /*
238          * XXX: uhm, when does the iter invalidate? sure not while
239          * just setting a row's column i hope?
240          */
241         
242         if (account)
243                 gtk_list_store_set(store, &iter, 
244                                    PROGRESS_ACCOUNT, account,
245                                    -1);
246         if (status)
247                 gtk_list_store_set(store, &iter,
248                                    PROGRESS_STATE, status,
249                                    -1);
250         if (image) 
251                 gtk_list_store_set(store, &iter,
252                                    PROGRESS_IMAGE, image,
253                                    -1);
254
255         return result;
256 }