2007-08-23 [wwp] 2.10.0cvs158
[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 #include <hildon-widgets/hildon-banner.h>
39 #endif
40
41
42 #define BUFFSIZE 1024
43
44 static GList *statusbar_list = NULL;
45 gint statusbar_puts_all_hook_id = -1;
46
47 GtkWidget *statusbar_create(void)
48 {
49         GtkWidget *statusbar;
50
51         statusbar = gtk_statusbar_new();
52         gtk_widget_set_size_request(statusbar, 1, -1);
53         statusbar_list = g_list_append(statusbar_list, statusbar);
54         gtk_statusbar_set_has_resize_grip(GTK_STATUSBAR(statusbar), 
55                                           FALSE);
56         gtk_container_set_border_width(GTK_CONTAINER(statusbar), 1);
57
58         gtk_widget_ref(GTK_STATUSBAR(statusbar)->label);
59         gtk_container_remove(GTK_CONTAINER(GTK_STATUSBAR(statusbar)->frame),
60                 GTK_STATUSBAR(statusbar)->label);
61         gtk_widget_hide(GTK_STATUSBAR(statusbar)->frame);
62         gtk_box_pack_start (GTK_BOX(statusbar), GTK_STATUSBAR(statusbar)->label, 
63                 TRUE, TRUE, 0);
64         gtk_widget_unref(GTK_STATUSBAR(statusbar)->label);
65         gtk_container_remove(GTK_CONTAINER(statusbar),
66                 GTK_STATUSBAR(statusbar)->frame);
67         GTK_STATUSBAR(statusbar)->frame = gtk_frame_new(NULL);
68
69         return statusbar;
70 }
71
72 void statusbar_puts(GtkStatusbar *statusbar, const gchar *str)
73 {
74         gint cid;
75         gchar *buf;
76         gchar *tmp;
77
78         tmp = g_strdup(str);
79         strretchomp(tmp);
80         buf = trim_string(tmp, 76);
81         g_free(tmp);
82
83         cid = gtk_statusbar_get_context_id(statusbar, "Standard Output");
84         gtk_statusbar_pop(statusbar, cid);
85         gtk_statusbar_push(statusbar, cid, buf);
86         gtkut_widget_draw_now(GTK_WIDGET(statusbar));
87
88         g_free(buf);
89 }
90
91 void statusbar_puts_all(const gchar *str)
92 {
93         GList *cur;
94
95         for (cur = statusbar_list; cur != NULL; cur = cur->next)
96                 statusbar_puts(GTK_STATUSBAR(cur->data), str);
97 }
98
99 void statusbar_print(GtkStatusbar *statusbar, const gchar *format, ...)
100 {
101         va_list args;
102         gchar buf[BUFFSIZE];
103
104         va_start(args, format);
105         g_vsnprintf(buf, sizeof(buf), format, args);
106         va_end(args);
107
108         statusbar_puts(statusbar, buf);
109 }
110
111 #ifdef MAEMO
112 static GSList *banner_texts = NULL;
113 static GtkWidget *banner = NULL;
114 #endif
115
116 void statusbar_print_all(const gchar *format, ...)
117 {
118         va_list args;
119         gchar buf[BUFFSIZE];
120         GList *cur;
121
122         va_start(args, format);
123         g_vsnprintf(buf, sizeof(buf), format, args);
124         va_end(args);
125
126         for (cur = statusbar_list; cur != NULL; cur = cur->next)
127                 statusbar_puts(GTK_STATUSBAR(cur->data), buf);
128 #ifdef MAEMO
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 #endif
149 }
150
151 void statusbar_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 #ifdef MAEMO
162         if (banner && banner_texts) {
163                 gchar *old_text = (gchar *)banner_texts->data;
164                 gchar *prev_text = NULL;
165                 banner_texts = g_slist_remove(banner_texts, old_text);  
166                 g_free(old_text);
167                 if (banner_texts) {
168                         prev_text = (gchar *)banner_texts->data;
169                         hildon_banner_set_text(HILDON_BANNER(banner), prev_text);
170                 } else {
171                         gtk_widget_destroy(banner);
172                         g_object_unref(banner);
173                         banner = NULL;
174                 }
175         }
176 #endif
177 }
178
179 static gboolean statusbar_puts_all_hook (gpointer source, gpointer data)
180 {
181         LogText *logtext = (LogText *) source;
182
183         g_return_val_if_fail(logtext != NULL, TRUE);
184         g_return_val_if_fail(logtext->text != NULL, TRUE);
185
186         statusbar_pop_all();
187         if (logtext->type == LOG_NORMAL) {
188                 statusbar_puts_all(logtext->text + LOG_TIME_LEN);
189         } else if (logtext->type == LOG_MSG) {
190                 statusbar_puts_all(logtext->text);
191         }
192
193         return FALSE;
194 }
195
196 void statusbar_verbosity_set(gboolean verbose)
197 {
198         if (verbose && (statusbar_puts_all_hook_id == -1)) {
199                 statusbar_puts_all_hook_id =
200                         hooks_register_hook(LOG_APPEND_TEXT_HOOKLIST, statusbar_puts_all_hook, NULL);
201         } else if (!verbose && (statusbar_puts_all_hook_id != -1)) {
202                 hooks_unregister_hook(LOG_APPEND_TEXT_HOOKLIST, statusbar_puts_all_hook_id);
203                 statusbar_puts_all_hook_id = -1;
204                 statusbar_pop_all();
205         }
206 }
207
208 void statusbar_progress_all (gint done, gint total, gint step) 
209 {
210         gchar buf[32];
211         if (total && done % step == 0) {
212                 g_snprintf(buf, sizeof(buf), "%d / %d", done, total);
213                 gtk_progress_bar_set_text
214                         (GTK_PROGRESS_BAR(mainwindow_get_mainwindow()->progressbar), buf);
215                 gtk_progress_bar_set_fraction
216                         (GTK_PROGRESS_BAR(mainwindow_get_mainwindow()->progressbar),
217                          (total == 0) ? 0 : (gfloat)done / (gfloat)total);
218         } else if (total == 0) {
219                 gtk_progress_bar_set_text
220                         (GTK_PROGRESS_BAR(mainwindow_get_mainwindow()->progressbar), "");
221                 gtk_progress_bar_set_fraction
222                         (GTK_PROGRESS_BAR(mainwindow_get_mainwindow()->progressbar), 0.0);
223         }
224 }