2
* xfce4-mailwatch-plugin - a mail notification applet for the xfce4 panel
3
* Copyright (c) 2005 Pasi Orovuo <pasi.ov@gmail.com>
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.
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.
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.
27
#ifdef HAVE_SYS_TYPES_H
28
#include <sys/types.h>
31
#ifdef HAVE_SYS_STAT_H
50
#include "mailwatch.h"
51
#include "mailwatch-utils.h"
52
#include "cairo-dock.h"
54
#define XFCE_MAILWATCH_MAILDIR_MAILBOX( p ) ( (XfceMailwatchMaildirMailbox *) p )
58
MAILDIR_MSG_START = 1,
60
MAILDIR_MSG_FORCE_UPDATE,
61
MAILDIR_MSG_INTERVAL_CHANGED,
63
} XfceMailwatchMaildirMessage;
66
XfceMailwatchMailbox xfce_mailwatch_mailbox;
68
XfceMailwatch *mailwatch;
80
} XfceMailwatchMaildirMailbox;
83
maildir_check_mail( XfceMailwatchMaildirMailbox *maildir )
90
g_mutex_lock( maildir->mutex );
91
if ( !maildir->path || !*(maildir->path) ) {
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 ) );
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 );
114
if ( st.st_mtime > maildir->mtime ) {
116
GError *error = NULL;
118
dir = g_dir_open( path, 0, &error );
124
while ( ( entry = g_dir_read_name( dir ) ) ) {
129
xfce_mailwatch_signal_new_messages( maildir->mailwatch,
130
(XfceMailwatchMailbox *) maildir, count_new );
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 );
139
maildir->mtime = st.st_mtime;
143
g_mutex_unlock( maildir->mutex );
152
maildir_main_thread( gpointer data ) {
153
XfceMailwatchMaildirMailbox *maildir = data;
158
g_async_queue_ref( maildir->aqueue );
161
XfceMailwatchMaildirMessage msg;
163
g_get_current_time( &tv );
164
g_time_val_add( &tv, G_USEC_PER_SEC * 5 );
166
msg = GPOINTER_TO_INT( g_async_queue_timed_pop( maildir->aqueue, &tv ) );
170
case MAILDIR_MSG_START:
171
maildir->active = TRUE;
174
case MAILDIR_MSG_PAUSE:
175
maildir->active = FALSE;
178
case MAILDIR_MSG_FORCE_UPDATE:
179
maildir->last_update = 0;
182
case MAILDIR_MSG_INTERVAL_CHANGED:
183
maildir->interval = GPOINTER_TO_INT( g_async_queue_pop( maildir->aqueue ) );
186
case MAILDIR_MSG_QUIT:
187
g_async_queue_unref( maildir->aqueue );
188
g_thread_exit( NULL );
192
DBG( "msg: %d", msg );
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;
206
static XfceMailwatchMailbox *
207
maildir_new( XfceMailwatch *mailwatch, XfceMailwatchMailboxType *type )
209
XfceMailwatchMaildirMailbox *maildir = NULL;
213
maildir = g_new0( XfceMailwatchMaildirMailbox, 1 );
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 );
223
return ( (XfceMailwatchMailbox *) maildir );
227
maildir_free( XfceMailwatchMailbox *mailbox )
229
XfceMailwatchMaildirMailbox *maildir = XFCE_MAILWATCH_MAILDIR_MAILBOX( mailbox );
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 );
237
if ( maildir->path ) {
238
g_free( maildir->path );
246
maildir_save_param_list( XfceMailwatchMailbox *mailbox )
248
XfceMailwatchMaildirMailbox *maildir = XFCE_MAILWATCH_MAILDIR_MAILBOX( mailbox );
249
XfceMailwatchParam *param;
250
GList *settings = NULL;
254
g_mutex_lock( maildir->mutex );
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 );
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 );
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 );
271
g_mutex_unlock( maildir->mutex );
279
maildir_restore_param_list( XfceMailwatchMailbox *mailbox, GList *params )
281
XfceMailwatchMaildirMailbox *maildir = XFCE_MAILWATCH_MAILDIR_MAILBOX( mailbox );
286
g_mutex_lock( maildir->mutex );
288
for ( li = g_list_first( params ); li != NULL; li = g_list_next( li ) ) {
289
XfceMailwatchParam *param = (XfceMailwatchParam *) li->data;
291
if ( !strcmp( param->key, "path" ) ) {
292
if ( maildir->path ) {
293
g_free( maildir->path );
295
maildir->path = g_strdup( param->value );
297
else if ( !strcmp( param->key, "mtime" ) ) {
298
maildir->mtime = atol( param->value );
300
else if ( !strcmp( param->key, "interval" ) ) {
301
maildir->interval = (guint) atol( param->value );
305
g_mutex_unlock( maildir->mutex );
311
maildir_path_entry_changed_cb( GtkWidget *widget, XfceMailwatchMaildirMailbox *maildir )
317
g_mutex_lock( maildir->mutex );
318
if ( maildir->path ) {
319
g_free( maildir->path );
322
text = gtk_entry_get_text( GTK_ENTRY( widget ) );
323
maildir->path = g_strdup( text ? text : "" );
325
g_mutex_unlock( maildir->mutex );
333
maildir_browse_button_clicked_cb( GtkWidget *button,
334
XfceMailwatchMaildirMailbox *maildir )
343
xfce_textdomain( GETTEXT_PACKAGE, LOCALEDIR, "UTF-8" );
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,
353
if ( maildir->path ) {
354
gtk_file_chooser_set_filename( GTK_FILE_CHOOSER( chooser ), maildir->path );
357
result = gtk_dialog_run( GTK_DIALOG( chooser ) );
358
if ( result == GTK_RESPONSE_OK ) {
360
gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( chooser ) );
361
GtkWidget *entry = g_object_get_data( G_OBJECT( button ), "maildir_entry" );
363
gtk_entry_set_text( GTK_ENTRY( entry ), ( path ) ? path : "" );
367
gtk_widget_destroy( chooser );
373
maildir_interval_changed_cb( GtkWidget *spinner, XfceMailwatchMaildirMailbox *maildir ) {
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 ) );
385
static GtkContainer *
386
maildir_get_setup_page( XfceMailwatchMailbox *mailbox )
388
XfceMailwatchMaildirMailbox *maildir = XFCE_MAILWATCH_MAILDIR_MAILBOX( mailbox );
389
GtkWidget *vbox, *hbox;
390
GtkWidget *label, *entry;
391
GtkWidget *button, *image;
398
xfce_textdomain( GETTEXT_PACKAGE, LOCALEDIR, "UTF-8" );
401
vbox = gtk_vbox_new( FALSE, BORDER / 2 );
402
gtk_widget_show( vbox );
404
hbox = gtk_hbox_new( FALSE, BORDER );
405
gtk_widget_show( hbox );
406
gtk_box_pack_start( GTK_BOX( vbox ), hbox, FALSE, FALSE, 0 );
408
sg = gtk_size_group_new( GTK_SIZE_GROUP_HORIZONTAL );
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 );
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 );
420
g_mutex_unlock( maildir->mutex );
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 );
426
g_signal_connect( G_OBJECT( entry ), "changed",
427
G_CALLBACK( maildir_path_entry_changed_cb ), maildir );
429
button = gtk_button_new();
430
gtk_widget_show( button );
432
image = gtk_image_new_from_stock( GTK_STOCK_OPEN, GTK_ICON_SIZE_LARGE_TOOLBAR );
433
gtk_widget_show( image );
435
gtk_container_add( GTK_CONTAINER( button ), image );
436
gtk_box_pack_start( GTK_BOX( hbox ), button, FALSE, FALSE, 0 );
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 );
442
hbox = gtk_hbox_new( FALSE, BORDER );
443
gtk_widget_show( hbox );
444
gtk_box_pack_start( GTK_BOX( vbox ), hbox, FALSE, FALSE, 0 );
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 );
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 );
462
label = gtk_label_new( _( "minute(s)." ) );
463
gtk_widget_show( label );
464
gtk_box_pack_start( GTK_BOX( hbox ), label, FALSE, FALSE, 0 );
468
return ( GTK_CONTAINER( vbox ) );
472
maildir_force_update_cb( XfceMailwatchMailbox *mailbox ) {
473
XfceMailwatchMaildirMailbox *maildir = XFCE_MAILWATCH_MAILDIR_MAILBOX( mailbox );
476
g_async_queue_push( maildir->aqueue, GINT_TO_POINTER( MAILDIR_MSG_FORCE_UPDATE ) );
482
maildir_set_activated( XfceMailwatchMailbox *mailbox, gboolean activated )
484
XfceMailwatchMaildirMailbox *maildir = XFCE_MAILWATCH_MAILDIR_MAILBOX( mailbox );
488
g_async_queue_push( maildir->aqueue,
489
GINT_TO_POINTER( activated ? MAILDIR_MSG_START : MAILDIR_MSG_PAUSE ) );
494
XfceMailwatchMailboxType builtin_mailbox_type_maildir = {
496
N_( "Local Maildir Spool" ),
497
N_( "The Maildir plugin can watch a local maildir-style mail spool for new messages." ),
499
maildir_set_activated,
500
maildir_force_update_cb,
501
maildir_get_setup_page,
502
maildir_restore_param_list,
503
maildir_save_param_list,