fix bug where actionsrc was not updated after mailbox name change
[claws.git] / src / statusbar.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2013 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 #include "claws-features.h"
23 #endif
24
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 #include <gtk/gtk.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 #define BUFFSIZE 1024
38
39 static GList *statusbar_list = NULL;
40 gint statusbar_puts_all_hook_id = -1;
41
42 GtkWidget *statusbar_create(void)
43 {
44         GtkWidget *statusbar;
45         GtkWidget *child;
46         GtkWidget *parent;
47         GtkWidget *hbox;
48
49         statusbar = gtk_statusbar_new();
50         statusbar_list = g_list_append(statusbar_list, statusbar);
51 #if !GTK_CHECK_VERSION(3, 0, 0)
52         gtk_statusbar_set_has_resize_grip(GTK_STATUSBAR(statusbar), 
53                                           FALSE);
54 #endif
55         gtk_container_set_border_width(GTK_CONTAINER(statusbar), 1);
56         child = gtk_statusbar_get_message_area(GTK_STATUSBAR(statusbar));
57         parent = gtk_widget_get_parent(child);
58         gtk_container_remove(GTK_CONTAINER(parent), g_object_ref(child));
59         hbox = gtk_hbox_new(FALSE, 0);
60         gtk_container_add(GTK_CONTAINER(parent), hbox);
61         gtk_widget_show(hbox);
62         gtk_box_pack_start(GTK_BOX(hbox), child, TRUE, TRUE, 0);
63         g_object_unref(child);  
64
65         return statusbar;
66 }
67
68 void statusbar_puts(GtkStatusbar *statusbar, const gchar *str)
69 {
70         gint cid;
71         gchar *buf;
72         gchar *tmp;
73
74         tmp = g_strdup(str);
75         strretchomp(tmp);
76         buf = trim_string(tmp, 76);
77         g_free(tmp);
78
79         cid = gtk_statusbar_get_context_id(statusbar, "Standard Output");
80         gtk_statusbar_pop(statusbar, cid);
81         gtk_statusbar_push(statusbar, cid, buf);
82         gtkut_widget_draw_now(GTK_WIDGET(statusbar));
83
84         g_free(buf);
85 }
86
87 void statusbar_puts_all(const gchar *str)
88 {
89         GList *cur;
90
91         for (cur = statusbar_list; cur != NULL; cur = cur->next)
92                 statusbar_puts(GTK_STATUSBAR(cur->data), str);
93 }
94
95 void statusbar_print(GtkStatusbar *statusbar, const gchar *format, ...)
96 {
97         va_list args;
98         gchar buf[BUFFSIZE];
99
100         va_start(args, format);
101         g_vsnprintf(buf, sizeof(buf), format, args);
102         va_end(args);
103
104         statusbar_puts(statusbar, buf);
105 }
106
107 void statusbar_print_all(const gchar *format, ...)
108 {
109         va_list args;
110         gchar buf[BUFFSIZE];
111         GList *cur;
112
113         va_start(args, format);
114         g_vsnprintf(buf, sizeof(buf), format, args);
115         va_end(args);
116
117         for (cur = statusbar_list; cur != NULL; cur = cur->next)
118                 statusbar_puts(GTK_STATUSBAR(cur->data), buf);
119 }
120
121 void statusbar_pop_all(void)
122 {
123         GList *cur;
124         gint cid;
125
126         for (cur = statusbar_list; cur != NULL; cur = cur->next) {
127                 cid = gtk_statusbar_get_context_id(GTK_STATUSBAR(cur->data),
128                                                    "Standard Output");
129                 gtk_statusbar_pop(GTK_STATUSBAR(cur->data), cid);
130         }
131 }
132
133 static gboolean statusbar_puts_all_hook (gpointer source, gpointer data)
134 {
135         LogText *logtext = (LogText *) source;
136
137         cm_return_val_if_fail(logtext != NULL, TRUE);
138         cm_return_val_if_fail(logtext->text != NULL, TRUE);
139
140         statusbar_pop_all();
141         if (logtext->type == LOG_NORMAL) {
142                 statusbar_puts_all(logtext->text + LOG_TIME_LEN);
143         } else if (logtext->type == LOG_MSG) {
144                 statusbar_puts_all(logtext->text);
145         }
146
147         return FALSE;
148 }
149
150 void statusbar_verbosity_set(gboolean verbose)
151 {
152         if (verbose && (statusbar_puts_all_hook_id == -1)) {
153                 statusbar_puts_all_hook_id =
154                         hooks_register_hook(LOG_APPEND_TEXT_HOOKLIST, statusbar_puts_all_hook, NULL);
155         } else if (!verbose && (statusbar_puts_all_hook_id != -1)) {
156                 hooks_unregister_hook(LOG_APPEND_TEXT_HOOKLIST, statusbar_puts_all_hook_id);
157                 statusbar_puts_all_hook_id = -1;
158                 statusbar_pop_all();
159         }
160 }
161
162 void statusbar_progress_all (gint done, gint total, gint step) 
163 {
164         GtkProgressBar *progressbar = GTK_PROGRESS_BAR(
165                                         mainwindow_get_mainwindow()->progressbar);
166         gchar buf[32];
167         
168         if (total && done % step == 0) {
169 #ifdef GENERIC_UMPC
170                 /* use a more compact format */
171                 const gchar *format = "%d/%d";
172 #else
173                 const gchar *format = "%d / %d";
174 #endif
175                 g_snprintf(buf, sizeof(buf), format, done, total);
176                 gtk_progress_bar_set_text(progressbar, buf);
177                 gtk_progress_bar_set_fraction(progressbar,
178                          (gfloat)done / (gfloat)total);
179                 if (!gtk_widget_get_visible(GTK_WIDGET(progressbar)))
180                         gtk_widget_show(GTK_WIDGET(progressbar));
181         } else if (total == 0) {
182                 gtk_progress_bar_set_text(progressbar, "");
183                 gtk_progress_bar_set_fraction(progressbar, 0.0);
184                 gtk_widget_hide(GTK_WIDGET(progressbar));
185         }
186 }