~ubuntu-branches/ubuntu/karmic/gtk-gnutella/karmic

« back to all changes in this revision

Viewing changes to src/statusbar_gui.c

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Kleineidam
  • Date: 2004-05-22 15:26:55 UTC
  • Revision ID: james.westby@ubuntu.com-20040522152655-lyi6iaswmy4hq4wy
Tags: upstream-0.93.3.0
ImportĀ upstreamĀ versionĀ 0.93.3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: statusbar_gui.c,v 1.9 2004/01/14 20:52:33 rmanfredi Exp $
 
3
 *
 
4
 * Copyright (c) 2001-2003, Raphael Manfredi, Richard Eckart
 
5
 *
 
6
 * GUI stuff used by share.c
 
7
 *
 
8
 *----------------------------------------------------------------------
 
9
 * This file is part of gtk-gnutella.
 
10
 *
 
11
 *  gtk-gnutella is free software; you can redistribute it and/or modify
 
12
 *  it under the terms of the GNU General Public License as published by
 
13
 *  the Free Software Foundation; either version 2 of the License, or
 
14
 *  (at your option) any later version.
 
15
 *
 
16
 *  gtk-gnutella is distributed in the hope that it will be useful,
 
17
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
 *  GNU General Public License for more details.
 
20
 *
 
21
 *  You should have received a copy of the GNU General Public License
 
22
 *  along with gtk-gnutella; if not, write to the Free Software
 
23
 *  Foundation, Inc.:
 
24
 *      59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
25
 *----------------------------------------------------------------------
 
26
 */
 
27
 
 
28
#include "gui.h"
 
29
#include "statusbar_gui.h"
 
30
#include "override.h"           /* Must be the last header included */
 
31
 
 
32
RCSID("$Id: statusbar_gui.c,v 1.9 2004/01/14 20:52:33 rmanfredi Exp $");
 
33
 
 
34
/*
 
35
 * Timeout entry for statusbar messages.
 
36
 */
 
37
typedef struct statusbar_timeout {
 
38
    statusbar_msgid_t id; /* message id of the message */
 
39
        time_t timeout; /* time after which the message should be removed */
 
40
} statusbar_timeout_t;
 
41
 
 
42
/*
 
43
 * statusbar context ids 
 
44
 */
 
45
guint scid_bottom              = (guint) -1;
 
46
guint scid_hostsfile           = (guint) -1;
 
47
guint scid_search_autoselected = (guint) -1;
 
48
guint scid_queue_freezed       = (guint) -1;
 
49
guint scid_info                = (guint) -1;
 
50
guint scid_ip_changed          = (guint) -1;
 
51
guint scid_warn                = (guint) -1;
 
52
 
 
53
/* 
 
54
 * List with timeout entries for statusbar messages 
 
55
 */
 
56
static GSList *sl_statusbar_timeouts = NULL;
 
57
 
 
58
/*
 
59
 * Status bar
 
60
 */
 
61
static gchar *statbar_botstr = NULL;
 
62
static gchar *statbar_botstr_new = NULL;
 
63
 
 
64
static void statusbar_gui_free_timeout(struct statusbar_timeout * t);
 
65
static void statusbar_gui_free_timeout_list(void);
 
66
static void statusbar_gui_add_timeout(statusbar_msgid_t, guint timeout);
 
67
 
 
68
void statusbar_gui_init(void)
 
69
{
 
70
    GtkStatusbar *statusbar;
 
71
 
 
72
    statusbar = GTK_STATUSBAR
 
73
        (lookup_widget(main_window, "statusbar"));
 
74
 
 
75
    /* statusbar stuff */
 
76
        scid_bottom    = 
 
77
                gtk_statusbar_get_context_id(statusbar, "default");
 
78
        scid_hostsfile = 
 
79
                gtk_statusbar_get_context_id(statusbar, "reading hosts file");
 
80
        scid_search_autoselected = 
 
81
                gtk_statusbar_get_context_id(statusbar, "autoselected search items");
 
82
        scid_queue_freezed = 
 
83
                gtk_statusbar_get_context_id(statusbar, "queue freezed");       
 
84
 
 
85
        scid_info = 
 
86
                gtk_statusbar_get_context_id(statusbar, "information"); 
 
87
 
 
88
    scid_ip_changed =
 
89
        gtk_statusbar_get_context_id(statusbar, "ip changed");
 
90
 
 
91
    scid_warn =
 
92
        gtk_statusbar_get_context_id(statusbar, "warning");
 
93
 
 
94
        /*
 
95
         * This message lies at the bottom of the statusbar, and is never removed,
 
96
         * but to be replaced by an updated message.
 
97
         *
 
98
         * The current string held at the bottom is stored in `statbar_botstr'.
 
99
         * If a new string is pending replacement in `statbar_botstr_new', then
 
100
         * it will replace the current one when the last timeout for pushed
 
101
         * messages expires, at which time we'll know the bottom message is shown.
 
102
         *              --RAM, 27/06/2002
 
103
         */
 
104
 
 
105
        statbar_botstr = g_strdup(GTA_WEBSITE);
 
106
        statusbar_gui_push(SB_MESSAGE, scid_bottom, 0, statbar_botstr);
 
107
}
 
108
 
 
109
void statusbar_gui_shutdown(void)
 
110
{
 
111
    statusbar_gui_free_timeout_list();
 
112
 
 
113
        if (statbar_botstr_new)
 
114
                g_free(statbar_botstr_new);
 
115
        if (statbar_botstr)
 
116
                g_free(statbar_botstr);
 
117
 
 
118
}
 
119
 
 
120
void statusbar_gui_set_default(const char *format, ...)
 
121
{
 
122
    static gchar buf[1024];
 
123
    va_list args;
 
124
 
 
125
    va_start(args, format);
 
126
 
 
127
    if (statbar_botstr_new != NULL)
 
128
        g_free(statbar_botstr_new);
 
129
 
 
130
    if (format != NULL) {
 
131
        gm_vsnprintf(buf, sizeof(buf), format, args);
 
132
        statbar_botstr_new = g_strdup(buf);
 
133
    } else {
 
134
        statbar_botstr_new = g_strdup(GTA_WEBSITE);
 
135
    }
 
136
 
 
137
    va_end(args);
 
138
}
 
139
 
 
140
/*
 
141
 * statusbar_gui_message:
 
142
 *
 
143
 * Put a message on the statusbar. The message will by displayed for
 
144
 * a number of seconds given by timeout. If timeout is 0 the message
 
145
 * will not be automatically removed.
 
146
 *
 
147
 * Returns: message id of the added message
 
148
 */
 
149
statusbar_msgid_t statusbar_gui_push
 
150
    (sb_types_t type, guint scid, guint timeout, const gchar *format , ...)
 
151
{
 
152
    static gchar buf[1024];
 
153
    va_list args;
 
154
    statusbar_msgid_t id = {0, 0};
 
155
    GtkStatusbar *statusbar;
 
156
 
 
157
    va_start(args, format);
 
158
    
 
159
    if (format != NULL) {
 
160
        switch (type) {
 
161
        case SB_MESSAGE:
 
162
            gm_vsnprintf(buf, sizeof(buf), format, args);
 
163
            break;
 
164
        case SB_WARNING:
 
165
            gm_vsnprintf(buf, sizeof(buf), format, args);
 
166
            gdk_beep();
 
167
            break;
 
168
        }
 
169
    } else {
 
170
        buf[0] = '\0';
 
171
    }
 
172
 
 
173
    statusbar = GTK_STATUSBAR
 
174
        (lookup_widget(main_window, "statusbar"));
 
175
 
 
176
    id.scid = scid;
 
177
    id.msgid = gtk_statusbar_push(GTK_STATUSBAR(statusbar), scid, buf);
 
178
    
 
179
    if (timeout != 0)
 
180
        statusbar_gui_add_timeout(id, timeout);    
 
181
 
 
182
    va_end(args);
 
183
 
 
184
    return id;
 
185
}
 
186
 
 
187
static void statusbar_gui_pop(guint scid)
 
188
{
 
189
    GtkStatusbar *statusbar;
 
190
 
 
191
    statusbar = GTK_STATUSBAR
 
192
        (lookup_widget(main_window, "statusbar"));
 
193
 
 
194
    gtk_statusbar_pop(GTK_STATUSBAR(statusbar), scid);
 
195
}
 
196
 
 
197
void statusbar_gui_remove(statusbar_msgid_t id)
 
198
{
 
199
    GtkStatusbar *statusbar;
 
200
 
 
201
    statusbar = GTK_STATUSBAR
 
202
        (lookup_widget(main_window, "statusbar"));
 
203
 
 
204
    gtk_statusbar_remove(GTK_STATUSBAR(statusbar), id.scid, id.msgid);
 
205
}
 
206
 
 
207
/* 
 
208
 * statusbar_gui_add_timeout:
 
209
 * 
 
210
 * Add a statusbar message id to the timeout list, so it will be removed
 
211
 * automatically after a number of seconds.
 
212
 */
 
213
static void statusbar_gui_add_timeout(statusbar_msgid_t id, guint timeout)
 
214
{
 
215
        struct statusbar_timeout * t = NULL;
 
216
 
 
217
    t = g_malloc0(sizeof(struct statusbar_timeout));
 
218
        
 
219
        t->id = id;
 
220
        t->timeout = time((time_t *) NULL) + timeout;
 
221
 
 
222
        sl_statusbar_timeouts = g_slist_prepend(sl_statusbar_timeouts, t);
 
223
}
 
224
 
 
225
/*
 
226
 * statusbar_gui_free_timeout:
 
227
 *
 
228
 * Remove the timeout from the timeout list and free allocated memory.
 
229
 */
 
230
static void statusbar_gui_free_timeout(struct statusbar_timeout * t)
 
231
{
 
232
        g_return_if_fail(t);
 
233
 
 
234
        statusbar_gui_remove(t->id);
 
235
 
 
236
        sl_statusbar_timeouts = g_slist_remove(sl_statusbar_timeouts, t);
 
237
        
 
238
        g_free(t);
 
239
}
 
240
 
 
241
/*
 
242
 * statusbar_gui_clear_timeouts
 
243
 *
 
244
 * Check whether statusbar items have expired and remove them from the
 
245
 * statusbar.
 
246
 */
 
247
void statusbar_gui_clear_timeouts(time_t now)
 
248
{
 
249
        GSList *to_remove = NULL;
 
250
        GSList *l;
 
251
        
 
252
        for (l = sl_statusbar_timeouts; l; l = l->next) {
 
253
                struct statusbar_timeout *t = (struct statusbar_timeout *) l->data;
 
254
 
 
255
                if (now > t->timeout)  
 
256
                        to_remove = g_slist_prepend(to_remove, t);
 
257
        }
 
258
 
 
259
        for (l = to_remove; l; l = l->next)
 
260
                statusbar_gui_free_timeout((struct statusbar_timeout *) l->data);
 
261
 
 
262
        g_slist_free(to_remove);
 
263
 
 
264
        /*
 
265
         * When there are no more timeouts left, and there's a pending
 
266
         * new statusbar string to display, pop the old one and add the new.
 
267
         *              --RAM, 27/06/2002
 
268
         */
 
269
 
 
270
        if (sl_statusbar_timeouts == NULL && statbar_botstr_new) {
 
271
                statusbar_gui_pop(scid_bottom);
 
272
                g_free(statbar_botstr);
 
273
                statbar_botstr = statbar_botstr_new;
 
274
                statbar_botstr_new = NULL;
 
275
                statusbar_gui_push(SB_MESSAGE, scid_bottom, 0, statbar_botstr);
 
276
        }
 
277
}
 
278
 
 
279
/*
 
280
 * statusbar_gui_free_timeout_list:
 
281
 *
 
282
 * Clear the whole timeout list and free allocated memory.
 
283
 */
 
284
static void statusbar_gui_free_timeout_list(void) 
 
285
{
 
286
        GSList *l;
 
287
 
 
288
        for (l = sl_statusbar_timeouts; l; l = sl_statusbar_timeouts) {
 
289
                struct statusbar_timeout *t = (struct statusbar_timeout *) l->data;
 
290
                
 
291
                statusbar_gui_free_timeout(t);
 
292
        }
 
293
}