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