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