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