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