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