~cairo-dock-team/cairo-dock-plug-ins/experimental

« back to all changes in this revision

Viewing changes to mail/src/mailwatch-mailbox-maildir.c

  • Committer: tofe
  • Date: 2008-03-15 10:37:45 UTC
  • Revision ID: svn-v4:620951bb-bb42-0410-82c3-830e739ed170:trunk/plug-ins:644
nouvelle applet mail, basée sur le plugin xfce mailwatch. Pour l'instant, ne gère pas la suppresion de compte mail.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  xfce4-mailwatch-plugin - a mail notification applet for the xfce4 panel
 
3
 *  Copyright (c) 2005 Pasi Orovuo <pasi.ov@gmail.com>
 
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 2 of the License ONLY.
 
8
 *
 
9
 *  This program is distributed in the hope that it will be useful,
 
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 *  GNU Library General Public License for more details.
 
13
 *
 
14
 *  You should have received a copy of the GNU General Public License
 
15
 *  along with this program; if not, write to the Free Software
 
16
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 */
 
18
 
 
19
#ifdef HAVE_CONFIG_H
 
20
#include <config.h>
 
21
#endif
 
22
 
 
23
#ifdef HAVE_STRING_H
 
24
#include <string.h>
 
25
#endif
 
26
 
 
27
#ifdef HAVE_SYS_TYPES_H
 
28
#include <sys/types.h>
 
29
#endif
 
30
 
 
31
#ifdef HAVE_SYS_STAT_H
 
32
#include <sys/stat.h>
 
33
#endif
 
34
 
 
35
#ifdef HAVE_UNISTD_H
 
36
#include <unistd.h>
 
37
#endif
 
38
 
 
39
#ifdef HAVE_STDLIB_H
 
40
#include <stdlib.h>
 
41
#endif
 
42
 
 
43
#ifdef HAVE_ERRNO_H
 
44
#include <errno.h>
 
45
#endif
 
46
 
 
47
#include <glib.h>
 
48
#include <gtk/gtk.h>
 
49
 
 
50
#include "mailwatch.h"
 
51
#include "mailwatch-utils.h"
 
52
#include "cairo-dock.h"
 
53
 
 
54
#define XFCE_MAILWATCH_MAILDIR_MAILBOX( p ) ( (XfceMailwatchMaildirMailbox *) p )
 
55
#define BORDER                              ( 8 )
 
56
 
 
57
typedef enum {
 
58
    MAILDIR_MSG_START = 1,
 
59
    MAILDIR_MSG_PAUSE,
 
60
    MAILDIR_MSG_FORCE_UPDATE,
 
61
    MAILDIR_MSG_INTERVAL_CHANGED,
 
62
    MAILDIR_MSG_QUIT
 
63
} XfceMailwatchMaildirMessage;
 
64
 
 
65
typedef struct {
 
66
    XfceMailwatchMailbox    xfce_mailwatch_mailbox;
 
67
 
 
68
    XfceMailwatch           *mailwatch;
 
69
 
 
70
    gchar                   *path;
 
71
    gboolean                active;
 
72
    time_t                  mtime;
 
73
 
 
74
    guint                   interval;
 
75
    guint                   last_update;
 
76
 
 
77
    GMutex                  *mutex;
 
78
    GThread                 *thread;
 
79
    GAsyncQueue             *aqueue;
 
80
} XfceMailwatchMaildirMailbox;
 
81
 
 
82
static void
 
83
maildir_check_mail( XfceMailwatchMaildirMailbox *maildir )
 
84
{
 
85
    gchar           *path = NULL;
 
86
    struct stat     st;
 
87
 
 
88
    DBG( "-->>" );
 
89
 
 
90
    g_mutex_lock( maildir->mutex );
 
91
    if ( !maildir->path || !*(maildir->path) ) {
 
92
        goto out;
 
93
    }
 
94
 
 
95
    path = g_build_filename( maildir->path, "new", NULL );
 
96
    if ( stat( path, &st ) < 0 ) {
 
97
        xfce_mailwatch_log_message( maildir->mailwatch,
 
98
                                    XFCE_MAILWATCH_MAILBOX( maildir ),
 
99
                                    XFCE_MAILWATCH_LOG_ERROR,
 
100
                                    _( "Failed to get status of file %s: %s" ),
 
101
                                    path, g_strerror( errno ) );
 
102
        goto out;
 
103
    }
 
104
 
 
105
    if ( !S_ISDIR( st.st_mode ) ) {
 
106
        xfce_mailwatch_log_message( maildir->mailwatch,
 
107
                                    XFCE_MAILWATCH_MAILBOX( maildir ),
 
108
                                    XFCE_MAILWATCH_LOG_ERROR,
 
109
                                    _( "%s is not a directory. Is %s really a valid maildir?" ),
 
110
                                    path, maildir->path );
 
111
        goto out;
 
112
    }
 
113
 
 
114
    if ( st.st_mtime > maildir->mtime ) {
 
115
        GDir        *dir;
 
116
        GError      *error = NULL;
 
117
 
 
118
        dir = g_dir_open( path, 0, &error );
 
119
 
 
120
        if ( dir ) {
 
121
            int             count_new = 0;
 
122
            const gchar     *entry;
 
123
 
 
124
            while ( ( entry = g_dir_read_name( dir ) ) ) {
 
125
                count_new++;
 
126
            }
 
127
            g_dir_close( dir );
 
128
 
 
129
            xfce_mailwatch_signal_new_messages( maildir->mailwatch,
 
130
                    (XfceMailwatchMailbox *) maildir, count_new );
 
131
        }
 
132
        else {
 
133
            xfce_mailwatch_log_message( maildir->mailwatch,
 
134
                                        XFCE_MAILWATCH_MAILBOX( maildir ),
 
135
                                        XFCE_MAILWATCH_LOG_ERROR,
 
136
                                        "%s", error->message );
 
137
            g_error_free( error );
 
138
        }
 
139
        maildir->mtime = st.st_mtime;
 
140
    }
 
141
 
 
142
out:
 
143
    g_mutex_unlock( maildir->mutex );
 
144
    if ( path ) {
 
145
        g_free( path );
 
146
    }
 
147
 
 
148
    DBG( "<<--" );
 
149
}
 
150
 
 
151
static gpointer
 
152
maildir_main_thread( gpointer data ) {
 
153
    XfceMailwatchMaildirMailbox     *maildir = data;
 
154
    GTimeVal                        tv;
 
155
 
 
156
    DBG( "-->>" );
 
157
 
 
158
    g_async_queue_ref( maildir->aqueue );
 
159
 
 
160
    for ( ;; ) {
 
161
        XfceMailwatchMaildirMessage msg;
 
162
 
 
163
        g_get_current_time( &tv );
 
164
        g_time_val_add( &tv, G_USEC_PER_SEC * 5 );
 
165
 
 
166
        msg = GPOINTER_TO_INT( g_async_queue_timed_pop( maildir->aqueue, &tv ) );
 
167
 
 
168
        if ( msg ) {
 
169
            switch ( msg ) {
 
170
                case MAILDIR_MSG_START:
 
171
                    maildir->active = TRUE;
 
172
                    break;
 
173
 
 
174
                case MAILDIR_MSG_PAUSE:
 
175
                    maildir->active = FALSE;
 
176
                    break;
 
177
 
 
178
                case MAILDIR_MSG_FORCE_UPDATE:
 
179
                    maildir->last_update = 0;
 
180
                    break;
 
181
 
 
182
                case MAILDIR_MSG_INTERVAL_CHANGED:
 
183
                    maildir->interval = GPOINTER_TO_INT( g_async_queue_pop( maildir->aqueue ) );
 
184
                    break;
 
185
 
 
186
                case MAILDIR_MSG_QUIT:
 
187
                    g_async_queue_unref( maildir->aqueue );
 
188
                    g_thread_exit( NULL );
 
189
                    break;
 
190
 
 
191
                default:
 
192
                    DBG( "msg: %d", msg );
 
193
                    break;
 
194
            }
 
195
        }
 
196
 
 
197
        if ( ( maildir->active )
 
198
                && tv.tv_sec - maildir->last_update > maildir->interval ) {
 
199
            maildir_check_mail( maildir );
 
200
            maildir->last_update = tv.tv_sec;
 
201
        }
 
202
    }
 
203
    return ( NULL );
 
204
}
 
205
 
 
206
static XfceMailwatchMailbox *
 
207
maildir_new( XfceMailwatch *mailwatch, XfceMailwatchMailboxType *type )
 
208
{
 
209
    XfceMailwatchMaildirMailbox *maildir = NULL;
 
210
 
 
211
    DBG( "entering" );
 
212
 
 
213
    maildir = g_new0( XfceMailwatchMaildirMailbox, 1 );
 
214
 
 
215
    maildir->mailwatch      = mailwatch;
 
216
    maildir->path           = NULL;
 
217
    maildir->active         = FALSE;
 
218
    maildir->interval       = XFCE_MAILWATCH_DEFAULT_TIMEOUT;
 
219
    maildir->mutex          = g_mutex_new();
 
220
    maildir->aqueue         = g_async_queue_new();
 
221
    maildir->thread         = g_thread_create( maildir_main_thread, maildir, TRUE, NULL );
 
222
 
 
223
    return ( (XfceMailwatchMailbox *) maildir );
 
224
}
 
225
 
 
226
static void
 
227
maildir_free( XfceMailwatchMailbox *mailbox )
 
228
{
 
229
    XfceMailwatchMaildirMailbox *maildir = XFCE_MAILWATCH_MAILDIR_MAILBOX( mailbox );
 
230
 
 
231
    DBG( "-->>" );
 
232
 
 
233
    g_async_queue_push( maildir->aqueue, GINT_TO_POINTER( MAILDIR_MSG_QUIT ) );
 
234
    g_thread_join( maildir->thread );
 
235
    g_async_queue_unref( maildir->aqueue );
 
236
 
 
237
    if ( maildir->path ) {
 
238
        g_free( maildir->path );
 
239
    }
 
240
    g_free( maildir );
 
241
 
 
242
    DBG( "<<--" );
 
243
}
 
244
 
 
245
static GList *
 
246
maildir_save_param_list( XfceMailwatchMailbox *mailbox )
 
247
{
 
248
    XfceMailwatchMaildirMailbox *maildir = XFCE_MAILWATCH_MAILDIR_MAILBOX( mailbox );
 
249
    XfceMailwatchParam          *param;
 
250
    GList                       *settings = NULL;
 
251
 
 
252
    DBG( "-->>" );
 
253
 
 
254
    g_mutex_lock( maildir->mutex );
 
255
 
 
256
    param           = g_new( XfceMailwatchParam, 1 );
 
257
    param->key      = g_strdup( "path" );
 
258
    param->value    = g_strdup( ( maildir->path ) ? maildir->path : "" );
 
259
    settings        = g_list_append( settings, param );
 
260
 
 
261
    param           = g_new( XfceMailwatchParam, 1 );
 
262
    param->key      = g_strdup( "mtime" );
 
263
    param->value    = g_strdup_printf( "%li", maildir->mtime );
 
264
    settings        = g_list_append( settings, param );
 
265
 
 
266
    param           = g_new( XfceMailwatchParam, 1 );
 
267
    param->key      = g_strdup( "interval" );
 
268
    param->value    = g_strdup_printf( "%u", maildir->interval );
 
269
    settings        = g_list_append( settings, param );
 
270
 
 
271
    g_mutex_unlock( maildir->mutex );
 
272
 
 
273
    DBG( "<<--" );
 
274
 
 
275
    return ( settings );
 
276
}
 
277
 
 
278
static void
 
279
maildir_restore_param_list( XfceMailwatchMailbox *mailbox, GList *params )
 
280
{
 
281
    XfceMailwatchMaildirMailbox *maildir = XFCE_MAILWATCH_MAILDIR_MAILBOX( mailbox );
 
282
    GList                       *li;
 
283
 
 
284
    DBG( "-->>" );
 
285
 
 
286
    g_mutex_lock( maildir->mutex );
 
287
 
 
288
    for ( li = g_list_first( params ); li != NULL; li = g_list_next( li ) ) {
 
289
        XfceMailwatchParam  *param = (XfceMailwatchParam *) li->data;
 
290
 
 
291
        if ( !strcmp( param->key, "path" ) ) {
 
292
            if ( maildir->path ) {
 
293
                g_free( maildir->path );
 
294
            }
 
295
            maildir->path = g_strdup( param->value );
 
296
        }
 
297
        else if ( !strcmp( param->key, "mtime" ) ) {
 
298
            maildir->mtime = atol( param->value );
 
299
        }
 
300
        else if ( !strcmp( param->key, "interval" ) ) {
 
301
            maildir->interval = (guint) atol( param->value );
 
302
        }
 
303
    }
 
304
 
 
305
    g_mutex_unlock( maildir->mutex );
 
306
 
 
307
    DBG( "<<--" );
 
308
}
 
309
 
 
310
static gboolean
 
311
maildir_path_entry_changed_cb( GtkWidget *widget, XfceMailwatchMaildirMailbox *maildir )
 
312
{
 
313
    const gchar                 *text;
 
314
 
 
315
    DBG( "-->>" );
 
316
 
 
317
    g_mutex_lock( maildir->mutex );
 
318
    if ( maildir->path ) {
 
319
        g_free( maildir->path );
 
320
    }
 
321
 
 
322
    text = gtk_entry_get_text( GTK_ENTRY( widget ) );
 
323
    maildir->path = g_strdup( text ? text : "" );
 
324
 
 
325
    g_mutex_unlock( maildir->mutex );
 
326
 
 
327
    DBG( "<<--" );
 
328
 
 
329
    return ( FALSE );
 
330
}
 
331
 
 
332
static void
 
333
maildir_browse_button_clicked_cb( GtkWidget *button,
 
334
        XfceMailwatchMaildirMailbox *maildir )
 
335
{
 
336
    GtkWidget       *chooser;
 
337
    gint            result;
 
338
    GtkWidget       *parent;
 
339
 
 
340
    DBG( "-->>" );
 
341
 
 
342
    /* _TOFE_
 
343
    xfce_textdomain( GETTEXT_PACKAGE, LOCALEDIR, "UTF-8" );
 
344
    */
 
345
 
 
346
    parent = gtk_widget_get_toplevel( button );
 
347
    chooser = gtk_file_chooser_dialog_new( _( "Select Maildir Folder" ),
 
348
            GTK_WINDOW( parent ),
 
349
            GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
 
350
            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
 
351
            GTK_STOCK_OPEN, GTK_RESPONSE_OK,
 
352
            NULL );
 
353
    if ( maildir->path ) {
 
354
        gtk_file_chooser_set_filename( GTK_FILE_CHOOSER( chooser ), maildir->path );
 
355
    }
 
356
 
 
357
    result = gtk_dialog_run( GTK_DIALOG( chooser ) );
 
358
    if ( result == GTK_RESPONSE_OK ) {
 
359
        gchar       *path =
 
360
            gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( chooser ) );
 
361
        GtkWidget   *entry = g_object_get_data( G_OBJECT( button ), "maildir_entry" );
 
362
 
 
363
        gtk_entry_set_text( GTK_ENTRY( entry ), ( path ) ? path : "" );
 
364
        g_free( path );
 
365
    }
 
366
 
 
367
    gtk_widget_destroy( chooser );
 
368
 
 
369
    DBG( "<<--" );
 
370
}
 
371
 
 
372
static void
 
373
maildir_interval_changed_cb( GtkWidget *spinner, XfceMailwatchMaildirMailbox *maildir ) {
 
374
 
 
375
    DBG( "-->>" );
 
376
 
 
377
    g_async_queue_push( maildir->aqueue,
 
378
                        GINT_TO_POINTER( MAILDIR_MSG_INTERVAL_CHANGED ) );
 
379
    g_async_queue_push( maildir->aqueue,
 
380
        GINT_TO_POINTER( gtk_spin_button_get_value_as_int( GTK_SPIN_BUTTON( spinner ) ) * 60 ) );
 
381
 
 
382
    DBG( "<<--" );
 
383
}
 
384
 
 
385
static GtkContainer *
 
386
maildir_get_setup_page( XfceMailwatchMailbox *mailbox )
 
387
{
 
388
    XfceMailwatchMaildirMailbox *maildir = XFCE_MAILWATCH_MAILDIR_MAILBOX( mailbox );
 
389
    GtkWidget                   *vbox, *hbox;
 
390
    GtkWidget                   *label, *entry;
 
391
    GtkWidget                   *button, *image;
 
392
    GtkWidget                   *spin;
 
393
    GtkSizeGroup                *sg;
 
394
 
 
395
    DBG( "-->>" );
 
396
 
 
397
    /* _TOFE_
 
398
    xfce_textdomain( GETTEXT_PACKAGE, LOCALEDIR, "UTF-8" );
 
399
    */
 
400
 
 
401
    vbox = gtk_vbox_new( FALSE, BORDER / 2 );
 
402
    gtk_widget_show( vbox );
 
403
 
 
404
    hbox = gtk_hbox_new( FALSE, BORDER );
 
405
    gtk_widget_show( hbox );
 
406
    gtk_box_pack_start( GTK_BOX( vbox ), hbox, FALSE, FALSE, 0 );
 
407
 
 
408
    sg = gtk_size_group_new( GTK_SIZE_GROUP_HORIZONTAL );
 
409
 
 
410
    label = gtk_label_new_with_mnemonic( _( "Maildir _Path:" ) );
 
411
    gtk_widget_show( label );
 
412
    gtk_box_pack_start( GTK_BOX( hbox ), label, FALSE, FALSE, 0 );
 
413
    gtk_size_group_add_widget( sg, label );
 
414
 
 
415
    entry = gtk_entry_new();
 
416
    g_mutex_lock( maildir->mutex );
 
417
    if ( maildir->path ) {
 
418
        gtk_entry_set_text( GTK_ENTRY( entry ), maildir->path );
 
419
    }
 
420
    g_mutex_unlock( maildir->mutex );
 
421
 
 
422
    gtk_widget_show( entry );
 
423
    gtk_box_pack_start( GTK_BOX( hbox ), entry, FALSE, FALSE, 0 );
 
424
    gtk_label_set_mnemonic_widget( GTK_LABEL( label ), entry );
 
425
 
 
426
    g_signal_connect( G_OBJECT( entry ), "changed",
 
427
            G_CALLBACK( maildir_path_entry_changed_cb ), maildir );
 
428
 
 
429
    button = gtk_button_new();
 
430
    gtk_widget_show( button );
 
431
 
 
432
    image = gtk_image_new_from_stock( GTK_STOCK_OPEN, GTK_ICON_SIZE_LARGE_TOOLBAR );
 
433
    gtk_widget_show( image );
 
434
 
 
435
    gtk_container_add( GTK_CONTAINER( button ), image );
 
436
    gtk_box_pack_start( GTK_BOX( hbox ), button, FALSE, FALSE, 0 );
 
437
 
 
438
    g_object_set_data( G_OBJECT( button ), "maildir_entry", entry );
 
439
    g_signal_connect( G_OBJECT( button ), "clicked",
 
440
            G_CALLBACK( maildir_browse_button_clicked_cb ), maildir );
 
441
 
 
442
    hbox = gtk_hbox_new( FALSE, BORDER );
 
443
    gtk_widget_show( hbox );
 
444
    gtk_box_pack_start( GTK_BOX( vbox ), hbox, FALSE, FALSE, 0 );
 
445
 
 
446
    label = gtk_label_new_with_mnemonic( _( "_Interval:" ) );
 
447
    gtk_widget_show( label );
 
448
    gtk_misc_set_alignment( GTK_MISC( label ), 1, 0.5 );
 
449
    gtk_box_pack_start( GTK_BOX( hbox ), label, FALSE, FALSE, 0 );
 
450
    gtk_size_group_add_widget( sg, label );
 
451
 
 
452
    spin = gtk_spin_button_new_with_range( 1.0, 1440.0, 1.0 );
 
453
    gtk_spin_button_set_numeric( GTK_SPIN_BUTTON( spin ), TRUE );
 
454
    gtk_spin_button_set_wrap( GTK_SPIN_BUTTON( spin ), FALSE );
 
455
    gtk_spin_button_set_value( GTK_SPIN_BUTTON( spin ), maildir->interval / 60 );
 
456
    gtk_widget_show( spin );
 
457
    gtk_box_pack_start( GTK_BOX( hbox ), spin, FALSE, FALSE, 0 );
 
458
    g_signal_connect( G_OBJECT( spin ), "value-changed",
 
459
            G_CALLBACK( maildir_interval_changed_cb ), maildir );
 
460
    gtk_label_set_mnemonic_widget( GTK_LABEL( label ), spin );
 
461
 
 
462
    label = gtk_label_new( _( "minute(s)." ) );
 
463
    gtk_widget_show( label );
 
464
    gtk_box_pack_start( GTK_BOX( hbox ), label, FALSE, FALSE, 0 );
 
465
 
 
466
    DBG( "<<--" );
 
467
 
 
468
    return ( GTK_CONTAINER( vbox ) );
 
469
}
 
470
 
 
471
static void
 
472
maildir_force_update_cb( XfceMailwatchMailbox *mailbox ) {
 
473
    XfceMailwatchMaildirMailbox     *maildir = XFCE_MAILWATCH_MAILDIR_MAILBOX( mailbox );
 
474
    DBG( "-->>" );
 
475
 
 
476
    g_async_queue_push( maildir->aqueue, GINT_TO_POINTER( MAILDIR_MSG_FORCE_UPDATE ) );
 
477
 
 
478
    DBG( "<<--" );
 
479
}
 
480
 
 
481
static void
 
482
maildir_set_activated( XfceMailwatchMailbox *mailbox, gboolean activated )
 
483
{
 
484
    XfceMailwatchMaildirMailbox     *maildir = XFCE_MAILWATCH_MAILDIR_MAILBOX( mailbox );
 
485
 
 
486
    DBG( "-->>" );
 
487
 
 
488
    g_async_queue_push( maildir->aqueue,
 
489
                        GINT_TO_POINTER( activated ? MAILDIR_MSG_START : MAILDIR_MSG_PAUSE ) );
 
490
 
 
491
    DBG( "<<--" );
 
492
}
 
493
 
 
494
XfceMailwatchMailboxType    builtin_mailbox_type_maildir = {
 
495
    "maildir",
 
496
    N_( "Local Maildir Spool" ),
 
497
    N_( "The Maildir plugin can watch a local maildir-style mail spool for new messages." ),
 
498
    maildir_new,
 
499
    maildir_set_activated,
 
500
    maildir_force_update_cb,
 
501
    maildir_get_setup_page,
 
502
    maildir_restore_param_list,
 
503
    maildir_save_param_list,
 
504
    maildir_free
 
505
};