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