2008-09-17 [colin] 3.5.0cvs112
[claws.git] / src / statusbar.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 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 #include <stdarg.h>
28
29 #include "mainwindow.h"
30 #include "statusbar.h"
31 #include "gtkutils.h"
32 #include "utils.h"
33 #include "log.h"
34 #include "hooks.h"
35
36 #ifdef MAEMO
37 #ifdef CHINOOK
38 #include <hildon/hildon-banner.h>
39 #else
40 #include <hildon-widgets/hildon-banner.h>
41 #endif
42 #endif
43
44
45 #define BUFFSIZE 1024
46
47 static GList *statusbar_list = NULL;
48 gint statusbar_puts_all_hook_id = -1;
49
50 GtkWidget *statusbar_create(void)
51 {
52         GtkWidget *statusbar;
53
54         statusbar = gtk_statusbar_new();
55         gtk_widget_set_size_request(statusbar, 1, -1);
56         statusbar_list = g_list_append(statusbar_list, statusbar);
57         gtk_statusbar_set_has_resize_grip(GTK_STATUSBAR(statusbar), 
58                                           FALSE);
59         gtk_container_set_border_width(GTK_CONTAINER(statusbar), 1);
60
61         g_object_ref(GTK_STATUSBAR(statusbar)->label);
62         gtk_container_remove(GTK_CONTAINER(GTK_STATUSBAR(statusbar)->frame),
63                 GTK_STATUSBAR(statusbar)->label);
64         gtk_widget_hide(GTK_STATUSBAR(statusbar)->frame);
65         gtk_box_pack_start (GTK_BOX(statusbar), GTK_STATUSBAR(statusbar)->label, 
66                 TRUE, TRUE, 0);
67         g_object_unref(GTK_STATUSBAR(statusbar)->label);
68         gtk_container_remove(GTK_CONTAINER(statusbar),
69                 GTK_STATUSBAR(statusbar)->frame);
70         GTK_STATUSBAR(statusbar)->frame = gtk_frame_new(NULL);
71
72         return statusbar;
73 }
74
75 void statusbar_puts(GtkStatusbar *statusbar, const gchar *str)
76 {
77         gint cid;
78         gchar *buf;
79         gchar *tmp;
80
81         tmp = g_strdup(str);
82         strretchomp(tmp);
83         buf = trim_string(tmp, 76);
84         g_free(tmp);
85
86         cid = gtk_statusbar_get_context_id(statusbar, "Standard Output");
87         gtk_statusbar_pop(statusbar, cid);
88         gtk_statusbar_push(statusbar, cid, buf);
89         gtkut_widget_draw_now(GTK_WIDGET(statusbar));
90
91         g_free(buf);
92 }
93
94 void statusbar_puts_all(const gchar *str)
95 {
96         GList *cur;
97
98         for (cur = statusbar_list; cur != NULL; cur = cur->next)
99                 statusbar_puts(GTK_STATUSBAR(cur->data), str);
100 }
101
102 void statusbar_print(GtkStatusbar *statusbar, const gchar *format, ...)
103 {
104         va_list args;
105         gchar buf[BUFFSIZE];
106
107         va_start(args, format);
108         g_vsnprintf(buf, sizeof(buf), format, args);
109         va_end(args);
110
111         statusbar_puts(statusbar, buf);
112 }
113
114 #ifdef MAEMO
115 static GSList *banner_texts = NULL;
116 static GtkWidget *banner = NULL;
117 void statuswindow_print_all(const gchar *format, ...)
118 {
119         va_list args;
120         gchar buf[BUFFSIZE];
121         GList *cur;
122
123         va_start(args, format);
124         g_vsnprintf(buf, sizeof(buf), format, args);
125         va_end(args);
126
127         for (cur = statusbar_list; cur != NULL; cur = cur->next)
128                 statusbar_puts(GTK_STATUSBAR(cur->data), buf);
129         if (mainwindow_get_mainwindow()) {
130                 if (banner != NULL) {
131                         gchar *last_text = (gchar *)banner_texts->data;
132                         if (!strcmp2(last_text, buf))
133                                 return;
134                 }
135                 statusbar_pop_all();
136                 if (banner == NULL) {
137                         banner = hildon_banner_show_animation(
138                                 mainwindow_get_mainwindow()->window,
139                                 NULL,
140                                 buf);
141                         g_object_ref(banner);
142                         banner_texts = g_slist_prepend(banner_texts, g_strdup(buf));
143                 } else {
144                         hildon_banner_set_text(HILDON_BANNER(banner), buf);
145                         banner_texts = g_slist_prepend(banner_texts, g_strdup(buf));
146                 }
147         }
148 }
149
150 void statuswindow_pop_all(void)
151 {
152         GList *cur;
153         gint cid;
154
155         for (cur = statusbar_list; cur != NULL; cur = cur->next) {
156                 cid = gtk_statusbar_get_context_id(GTK_STATUSBAR(cur->data),
157                                                    "Standard Output");
158                 gtk_statusbar_pop(GTK_STATUSBAR(cur->data), cid);
159         }
160         if (banner && banner_texts) {
161                 gchar *old_text = (gchar *)banner_texts->data;
162                 gchar *prev_text = NULL;
163                 banner_texts = g_slist_remove(banner_texts, old_text);  
164                 g_free(old_text);
165                 if (banner_texts) {
166                         prev_text = (gchar *)banner_texts->data;
167                         hildon_banner_set_text(HILDON_BANNER(banner), prev_text);
168                 } else {
169                         gtk_widget_destroy(banner);
170                         g_object_unref(banner);
171                         banner = NULL;
172                 }
173         }
174 }
175 #endif
176
177 void statusbar_print_all(const gchar *format, ...)
178 {
179         va_list args;
180         gchar buf[BUFFSIZE];
181         GList *cur;
182
183         va_start(args, format);
184         g_vsnprintf(buf, sizeof(buf), format, args);
185         va_end(args);
186
187         for (cur = statusbar_list; cur != NULL; cur = cur->next)
188                 statusbar_puts(GTK_STATUSBAR(cur->data), buf);
189 }
190
191 void statusbar_pop_all(void)
192 {
193         GList *cur;
194         gint cid;
195
196         for (cur = statusbar_list; cur != NULL; cur = cur->next) {
197                 cid = gtk_statusbar_get_context_id(GTK_STATUSBAR(cur->data),
198                                                    "Standard Output");
199                 gtk_statusbar_pop(GTK_STATUSBAR(cur->data), cid);
200         }
201 }
202
203 static gboolean statusbar_puts_all_hook (gpointer source, gpointer data)
204 {
205         LogText *logtext = (LogText *) source;
206
207         g_return_val_if_fail(logtext != NULL, TRUE);
208         g_return_val_if_fail(logtext->text != NULL, TRUE);
209
210         statusbar_pop_all();
211         if (logtext->type == LOG_NORMAL) {
212                 statusbar_puts_all(logtext->text + LOG_TIME_LEN);
213         } else if (logtext->type == LOG_MSG) {
214                 statusbar_puts_all(logtext->text);
215         }
216
217         return FALSE;
218 }
219
220 void statusbar_verbosity_set(gboolean verbose)
221 {
222         if (verbose && (statusbar_puts_all_hook_id == -1)) {
223                 statusbar_puts_all_hook_id =
224                         hooks_register_hook(LOG_APPEND_TEXT_HOOKLIST, statusbar_puts_all_hook, NULL);
225         } else if (!verbose && (statusbar_puts_all_hook_id != -1)) {
226                 hooks_unregister_hook(LOG_APPEND_TEXT_HOOKLIST, statusbar_puts_all_hook_id);
227                 statusbar_puts_all_hook_id = -1;
228                 statusbar_pop_all();
229         }
230 }
231
232 void statusbar_progress_all (gint done, gint total, gint step) 
233 {
234         gchar buf[32];
235         if (total && done % step == 0) {
236                 g_snprintf(buf, sizeof(buf), "%d / %d", done, total);
237                 gtk_progress_bar_set_text
238                         (GTK_PROGRESS_BAR(mainwindow_get_mainwindow()->progressbar), buf);
239                 gtk_progress_bar_set_fraction
240                         (GTK_PROGRESS_BAR(mainwindow_get_mainwindow()->progressbar),
241                          (total == 0) ? 0 : (gfloat)done / (gfloat)total);
242         } else if (total == 0) {
243                 gtk_progress_bar_set_text
244                         (GTK_PROGRESS_BAR(mainwindow_get_mainwindow()->progressbar), "");
245                 gtk_progress_bar_set_fraction
246                         (GTK_PROGRESS_BAR(mainwindow_get_mainwindow()->progressbar), 0.0);
247         }
248 }