~ubuntu-branches/debian/experimental/xfce4-panel/experimental

« back to all changes in this revision

Viewing changes to plugins/mailcheck/mailcheck.c

  • Committer: Bazaar Package Importer
  • Author(s): Yves-Alexis Perez
  • Date: 2008-05-19 08:08:22 UTC
  • mfrom: (1.1.16 upstream)
  • Revision ID: james.westby@ubuntu.com-20080519080822-c8ptdv1s8o9r4ou0
Tags: 4.4.2-6
* switch to triggers:
  - debian/postinst: remove xfce-mcs-manager refresh.
  - debian/prerm dropped.
  - debian/control: conflicts against non-triggers-enable xfce4-mcs-manager.
* debian/control: remove useless Conflicts/Replaces against Sarge stuff.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  $Id: mailcheck.c 4263 2004-12-29 03:50:20Z kelnos $
2
 
 *
3
 
 *  Copyright (C) 2002-2004 Jasper Huijsmans (jasper@xfce.org)
4
 
 *
5
 
 *  - 2004 Add Maildir support Julien NOEL (dev@no-l.org)
6
 
 *
7
 
 *  This program is free software; you can redistribute it and/or modify
8
 
 *  it under the terms of the GNU General Public License as published by
9
 
 *  the Free Software Foundation; either version 2 of the License, or
10
 
 *  (at your option) any later version.
11
 
 *
12
 
 *  This program is distributed in the hope that it will be useful,
13
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 *  GNU Library General Public License for more details.
16
 
 *
17
 
 *  You should have received a copy of the GNU General Public License
18
 
 *  along with this program; if not, write to the Free Software
19
 
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
 
 */
21
 
 
22
 
#ifdef HAVE_CONFIG_H
23
 
#include <config.h>
24
 
#endif
25
 
 
26
 
#ifdef HAVE_SYS_STAT_H
27
 
#include <sys/stat.h>
28
 
#endif
29
 
#ifdef HAVE_SYS_WAIT_H
30
 
#include <sys/wait.h>
31
 
#endif
32
 
 
33
 
#ifdef HAVE_NETINET_IN_H
34
 
#include <netinet/in.h>
35
 
#endif
36
 
 
37
 
#include <stdio.h>
38
 
 
39
 
#include <gtk/gtk.h>
40
 
 
41
 
#include <netdb.h>
42
 
#include <sys/socket.h>
43
 
 
44
 
#include <libxfce4util/libxfce4util.h>
45
 
#include <libxfcegui4/xfce_iconbutton.h>
46
 
 
47
 
#include <panel/xfce.h>
48
 
#include <panel/settings.h>
49
 
#include <panel/plugins.h>
50
 
#include <panel/item_dialog.h>
51
 
 
52
 
#include <dirent.h>
53
 
 
54
 
#define MAILCHECK_ROOT "Mailcheck"
55
 
 
56
 
#define BORDER 6
57
 
 
58
 
static GtkTooltips *tooltips = NULL;
59
 
 
60
 
typedef struct
61
 
{
62
 
    char *mbox;
63
 
    char *command;
64
 
    char *newmail_command;
65
 
    gboolean term;
66
 
    gboolean use_sn;
67
 
    int interval;
68
 
 
69
 
    gboolean pop3;
70
 
    char pop3_username[256];
71
 
    char pop3_password[256];
72
 
    char pop3_hostname[256];
73
 
    int pop3_port;
74
 
 
75
 
    int timeout_id;
76
 
    int status;
77
 
 
78
 
    GdkPixbuf *nomail_pb;
79
 
    GdkPixbuf *newmail_pb;
80
 
    GdkPixbuf *oldmail_pb;
81
 
 
82
 
    GtkWidget *button;
83
 
}
84
 
t_mailcheck;
85
 
 
86
 
/*  Icons
87
 
 *  -----
88
 
*/
89
 
enum
90
 
{
91
 
    NO_MAIL,
92
 
    NEW_MAIL,
93
 
    OLD_MAIL
94
 
};
95
 
 
96
 
static char *mailcheck_icon_names[] = {
97
 
    "xfce-mail",
98
 
    "mail",
99
 
    "gnome-mailcheck",
100
 
    NULL
101
 
};
102
 
 
103
 
static GdkPixbuf *
104
 
get_mail_pixbuf (void)
105
 
{
106
 
    GdkPixbuf *pb;
107
 
 
108
 
    pb = themed_pixbuf_from_name_list (mailcheck_icon_names,
109
 
                                       icon_size[settings.size]);
110
 
 
111
 
    if (G_UNLIKELY (!pb))
112
 
    {
113
 
        g_critical ("Mail icon could not be found");
114
 
        pb = get_pixbuf_by_id (UNKNOWN_ICON);
115
 
    }
116
 
 
117
 
    return pb;
118
 
}
119
 
 
120
 
static void
121
 
_dimm_icon (GdkPixbuf * pixbuf)
122
 
{
123
 
    int x, y, pixel_stride, row_stride;
124
 
    guchar *row, *pixels;
125
 
    int w, h;
126
 
 
127
 
    if (!pixbuf)
128
 
        return;
129
 
        
130
 
    if (!gdk_pixbuf_get_has_alpha (pixbuf))
131
 
    {
132
 
        gdk_pixbuf_saturate_and_pixelate (pixbuf, pixbuf, 0, TRUE);
133
 
        return;
134
 
    }
135
 
 
136
 
    w = gdk_pixbuf_get_width (pixbuf);
137
 
    h = gdk_pixbuf_get_height (pixbuf);
138
 
 
139
 
    pixel_stride = 4;
140
 
 
141
 
    row = gdk_pixbuf_get_pixels (pixbuf);
142
 
    row_stride = gdk_pixbuf_get_rowstride (pixbuf);
143
 
 
144
 
    for (y = 0; y < h; y++)
145
 
    {
146
 
        pixels = row;
147
 
 
148
 
        for (x = 0; x < w; x++)
149
 
        {
150
 
            pixels[3] /= 2;
151
 
 
152
 
            pixels += pixel_stride;
153
 
        }
154
 
 
155
 
        row += row_stride;
156
 
    }
157
 
 
158
 
    gdk_pixbuf_saturate_and_pixelate (pixbuf, pixbuf, 0, TRUE);
159
 
}
160
 
 
161
 
static void
162
 
reset_mailcheck_icons (t_mailcheck * mc)
163
 
{
164
 
    if (mc->newmail_pb)
165
 
        g_object_unref (mc->newmail_pb);
166
 
 
167
 
    if (mc->nomail_pb)
168
 
        g_object_unref (mc->nomail_pb);
169
 
 
170
 
    if (mc->oldmail_pb)
171
 
        g_object_unref (mc->oldmail_pb);
172
 
 
173
 
    mc->newmail_pb = get_mail_pixbuf ();
174
 
 
175
 
    g_return_if_fail (mc->newmail_pb != NULL);
176
 
 
177
 
    mc->nomail_pb = gdk_pixbuf_copy (mc->newmail_pb);
178
 
    _dimm_icon (mc->nomail_pb);
179
 
 
180
 
    mc->oldmail_pb = mc->nomail_pb;
181
 
    g_object_ref (mc->oldmail_pb);
182
 
}
183
 
 
184
 
/*  Configuration
185
 
 *  -------------
186
 
*/
187
 
static void
188
 
mailcheck_set_tip (t_mailcheck * mc)
189
 
{
190
 
    char *tip;
191
 
 
192
 
    if (!tooltips)
193
 
        tooltips = gtk_tooltips_new ();
194
 
 
195
 
    if (mc->command && strlen (mc->command))
196
 
    {
197
 
        tip = g_strdup (mc->command);
198
 
 
199
 
        tip[0] = g_ascii_toupper (tip[0]);
200
 
 
201
 
        gtk_tooltips_set_tip (tooltips, mc->button, tip, NULL);
202
 
 
203
 
        g_free (tip);
204
 
    }
205
 
}
206
 
 
207
 
static gboolean
208
 
set_mail_icon (t_mailcheck * mc)
209
 
{
210
 
    if (mc->status == NO_MAIL)
211
 
    {
212
 
        xfce_iconbutton_set_pixbuf (XFCE_ICONBUTTON (mc->button),
213
 
                                    mc->nomail_pb);
214
 
    }
215
 
    else if (mc->status == OLD_MAIL)
216
 
    {
217
 
        xfce_iconbutton_set_pixbuf (XFCE_ICONBUTTON (mc->button),
218
 
                                    mc->oldmail_pb);
219
 
    }
220
 
    else
221
 
    {
222
 
        xfce_iconbutton_set_pixbuf (XFCE_ICONBUTTON (mc->button),
223
 
                                    mc->newmail_pb);
224
 
    }
225
 
 
226
 
    return FALSE;
227
 
}
228
 
 
229
 
/* POP3 routines */
230
 
 
231
 
static int
232
 
pop3_read_response (int fd, char *buff, int size)
233
 
{
234
 
    int bytes_read = 0;
235
 
    int state = 0;
236
 
 
237
 
    while (bytes_read < size && state != 2)
238
 
    {
239
 
        bytes_read += read (fd, &buff[bytes_read], sizeof (char));
240
 
 
241
 
        switch (state)
242
 
        {
243
 
            case 0:
244
 
                if (buff[bytes_read - 1] == '\r')
245
 
                    state = 1;
246
 
                break;
247
 
            case 1:
248
 
                if (buff[bytes_read - 1] == '\n')
249
 
                    state = 2;
250
 
                else
251
 
                    state = 0;
252
 
                break;
253
 
        }
254
 
    }
255
 
 
256
 
    if (state == 2)
257
 
        buff[bytes_read - 2 * sizeof (char)] = '\0';
258
 
 
259
 
    if (strncmp (buff, "+OK", 3 * sizeof (char)) == 0)
260
 
        return 1;
261
 
    else
262
 
        return 0;
263
 
}
264
 
 
265
 
static int
266
 
pop3_send_command (int fd, char *buff)
267
 
{
268
 
    return write (fd, buff, strlen (buff));
269
 
}
270
 
 
271
 
static int
272
 
pop3_check_mail (char *username, char *password, char *hostname, int port)
273
 
{
274
 
    struct sockaddr_in sa;
275
 
    struct hostent *hp;
276
 
    int msg_count;
277
 
    char buff[1024];
278
 
    char command[256];
279
 
    int sd;
280
 
    int i;
281
 
 
282
 
    /* Lookup hostname */
283
 
    hp = gethostbyname (hostname);
284
 
    if (hp == NULL)
285
 
        return NO_MAIL;
286
 
 
287
 
    sa.sin_port = htons (port);
288
 
    sa.sin_family = hp->h_addrtype;
289
 
 
290
 
    /* Create the socket */
291
 
    sd = socket (AF_INET, SOCK_STREAM, 0);
292
 
    if (sd == -1)
293
 
        return NO_MAIL;
294
 
 
295
 
    /* Try each address until we get one that works */
296
 
    for (i = 0;; i++)
297
 
    {
298
 
        if (hp->h_addr_list[i] == NULL)
299
 
        {
300
 
            close (sd);
301
 
            return NO_MAIL;
302
 
        }
303
 
 
304
 
        memcpy ((char *) &sa.sin_addr, hp->h_addr_list[i], hp->h_length);
305
 
 
306
 
        if (connect (sd, (struct sockaddr *) &sa, sizeof (sa)) == 0)
307
 
            break;
308
 
    }
309
 
 
310
 
    if (!pop3_read_response (sd, buff, 1024))
311
 
    {
312
 
        close (sd);
313
 
        return NO_MAIL;
314
 
    }
315
 
 
316
 
    g_snprintf (command, 256, "USER %s\r\n", username);
317
 
    pop3_send_command (sd, command);
318
 
    if (!pop3_read_response (sd, buff, 1024))
319
 
    {
320
 
        pop3_send_command (sd, "QUIT\r\n");
321
 
        close (sd);
322
 
        return NO_MAIL;
323
 
    }
324
 
 
325
 
    g_snprintf (command, 256, "PASS %s\r\n", password);
326
 
    pop3_send_command (sd, command);
327
 
    if (!pop3_read_response (sd, buff, 1024))
328
 
    {
329
 
        pop3_send_command (sd, "QUIT\r\n");
330
 
        close (sd);
331
 
        return NO_MAIL;
332
 
    }
333
 
 
334
 
    pop3_send_command (sd, "STAT\r\n");
335
 
    if (!pop3_read_response (sd, buff, 1024))
336
 
    {
337
 
        pop3_send_command (sd, "QUIT\r\n");
338
 
        close (sd);
339
 
        return NO_MAIL;
340
 
    }
341
 
 
342
 
    sscanf (buff, "+OK %d", &msg_count);
343
 
 
344
 
    pop3_send_command (sd, "QUIT\r\n");
345
 
 
346
 
    close (sd);
347
 
 
348
 
    if (msg_count > 0)
349
 
        return NEW_MAIL;
350
 
    else
351
 
        return NO_MAIL;
352
 
}
353
 
 
354
 
static gboolean
355
 
exec_newmail_cmd (const char *cmd)
356
 
{
357
 
    exec_cmd (cmd, FALSE, FALSE);
358
 
 
359
 
    return FALSE;
360
 
}
361
 
 
362
 
static gboolean
363
 
check_mail (t_mailcheck * mailcheck)
364
 
{
365
 
    int mail = NO_MAIL;
366
 
    struct stat s;
367
 
 
368
 
    DBG ("Checking mail ... ");
369
 
 
370
 
    if (mailcheck->pop3 == TRUE)
371
 
    {
372
 
        mail = pop3_check_mail (mailcheck->pop3_username,
373
 
                                mailcheck->pop3_password,
374
 
                                mailcheck->pop3_hostname,
375
 
                                mailcheck->pop3_port);
376
 
    }
377
 
    else
378
 
    {
379
 
        if (stat (mailcheck->mbox, &s) < 0)
380
 
        {
381
 
            mail = NO_MAIL;
382
 
        }
383
 
        else if (S_ISREG (s.st_mode))
384
 
        {
385
 
            DBG ("mbox format");
386
 
 
387
 
            if (!s.st_size)
388
 
                mail = NO_MAIL;
389
 
            else if (s.st_mtime <= s.st_atime)
390
 
                mail = OLD_MAIL;
391
 
            else
392
 
                mail = NEW_MAIL;
393
 
 
394
 
        }
395
 
        else if (S_ISDIR (s.st_mode))
396
 
        {
397
 
            DIR *dr;
398
 
            struct dirent *de;
399
 
            char *c_list[] = { "tmp", "cur", "new" };
400
 
            char *c_tmp;
401
 
            int i_tmp;
402
 
 
403
 
            /* mailbox is a maildir */
404
 
            DBG ("maildir format");
405
 
 
406
 
            mail = NO_MAIL;
407
 
 
408
 
            /* Verify the Maildir integrity */
409
 
            for (i_tmp = 0; i_tmp < 3; i_tmp++)
410
 
            {
411
 
                c_tmp =
412
 
                    g_build_filename (mailcheck->mbox, c_list[i_tmp], NULL);
413
 
 
414
 
                if (stat (c_tmp, &s) >= 0 && S_ISDIR (s.st_mode))
415
 
                {
416
 
                    dr = opendir (c_tmp);
417
 
 
418
 
                    if (dr != NULL)
419
 
                    {
420
 
                        while ((de = readdir (dr)))
421
 
                        {
422
 
                            if (strlen (de->d_name) >= 1 &&
423
 
                                de->d_name[0] != '.')
424
 
                            {
425
 
                                if (i_tmp < 2)
426
 
                                {
427
 
                                    mail = OLD_MAIL;
428
 
                                    break;
429
 
                                }
430
 
                                else
431
 
                                {
432
 
                                    mail = NEW_MAIL;
433
 
                                    break;
434
 
                                }
435
 
                            }
436
 
                        }
437
 
 
438
 
                        closedir (dr);
439
 
                    }
440
 
                }
441
 
            }
442
 
        }
443
 
    }
444
 
 
445
 
    DBG ("Done");
446
 
 
447
 
    if (mail != mailcheck->status)
448
 
    {
449
 
        if (mail == NEW_MAIL && mailcheck->status != NEW_MAIL &&
450
 
            mailcheck->newmail_command &&
451
 
            strlen (mailcheck->newmail_command) != 0)
452
 
        {
453
 
            g_idle_add ((GSourceFunc) exec_newmail_cmd,
454
 
                        mailcheck->newmail_command);
455
 
        }
456
 
 
457
 
        mailcheck->status = mail;
458
 
        g_idle_add ((GSourceFunc) set_mail_icon, mailcheck);
459
 
    }
460
 
 
461
 
    DBG ("Done\n");
462
 
 
463
 
    /* keep the g_timeout running */
464
 
    return TRUE;
465
 
}
466
 
 
467
 
 
468
 
static void
469
 
run_mailcheck (t_mailcheck * mc)
470
 
{
471
 
    if (mc->timeout_id > 0)
472
 
        return;
473
 
 
474
 
    if (mc->interval > 0)
475
 
    {
476
 
        mc->timeout_id = g_timeout_add (mc->interval * 1000,
477
 
                                        (GSourceFunc) check_mail, mc);
478
 
    }
479
 
}
480
 
 
481
 
/* set mailbox type */
482
 
static void
483
 
set_mbox_type(t_mailcheck *mc)
484
 
{
485
 
    if (strncmp (mc->mbox, "pop3://", 7 * sizeof (char)) == 0)
486
 
    {
487
 
        mc->pop3 = TRUE;
488
 
        mc->pop3_port = 110;
489
 
        sscanf (mc->mbox, "pop3://%[^:]:%[^@]@%[^:]:%d",
490
 
                mc->pop3_username,
491
 
                mc->pop3_password, mc->pop3_hostname, &mc->pop3_port);
492
 
    }
493
 
494
 
 
495
 
static void
496
 
mailcheck_read_config (Control * control, xmlNodePtr node)
497
 
{
498
 
    xmlChar *value;
499
 
    int n;
500
 
 
501
 
    t_mailcheck *mc = (t_mailcheck *) control->data;
502
 
 
503
 
    if (!node || !node->children)
504
 
        return;
505
 
 
506
 
    node = node->children;
507
 
 
508
 
    if (!xmlStrEqual (node->name, (const xmlChar *) MAILCHECK_ROOT))
509
 
        return;
510
 
 
511
 
    value = xmlGetProp (node, (const xmlChar *) "interval");
512
 
 
513
 
    if (value)
514
 
    {
515
 
        n = atoi (value);
516
 
 
517
 
        if (n > 0)
518
 
            mc->interval = n;
519
 
 
520
 
        g_free (value);
521
 
    }
522
 
 
523
 
    for (node = node->children; node; node = node->next)
524
 
    {
525
 
        if (xmlStrEqual (node->name, (const xmlChar *) "Mbox"))
526
 
        {
527
 
            value = DATA (node);
528
 
            if (value)
529
 
            {
530
 
                g_free (mc->mbox);
531
 
                mc->mbox = (char *) value;
532
 
                
533
 
                set_mbox_type(mc);
534
 
            }
535
 
        }
536
 
        else if (xmlStrEqual
537
 
                 (node->name, (const xmlChar *) "newmail-command"))
538
 
        {
539
 
            value = DATA (node);
540
 
 
541
 
            if (value)
542
 
            {
543
 
                g_free (mc->newmail_command);
544
 
                mc->newmail_command = (char *) value;
545
 
            }
546
 
        }
547
 
        else if (xmlStrEqual (node->name, (const xmlChar *) "Command"))
548
 
        {
549
 
            value = DATA (node);
550
 
 
551
 
            if (value)
552
 
            {
553
 
                g_free (mc->command);
554
 
                mc->command = (char *) value;
555
 
            }
556
 
 
557
 
            value = xmlGetProp (node, "term");
558
 
 
559
 
            if (value)
560
 
            {
561
 
                int n = atoi (value);
562
 
 
563
 
                if (n == 1)
564
 
                    mc->term = TRUE;
565
 
                else
566
 
                    mc->term = FALSE;
567
 
 
568
 
                g_free (value);
569
 
            }
570
 
 
571
 
            value = xmlGetProp (node, "sn");
572
 
 
573
 
            if (value)
574
 
            {
575
 
                int n = atoi (value);
576
 
 
577
 
                if (n == 1)
578
 
                    mc->use_sn = TRUE;
579
 
                else
580
 
                    mc->use_sn = FALSE;
581
 
 
582
 
                g_free (value);
583
 
            }
584
 
        }
585
 
    }
586
 
 
587
 
    run_mailcheck (mc);
588
 
 
589
 
    mailcheck_set_tip (mc);
590
 
}
591
 
 
592
 
static void
593
 
mailcheck_write_config (Control * control, xmlNodePtr parent)
594
 
{
595
 
    xmlNodePtr root, node;
596
 
    char value[MAXSTRLEN + 1];
597
 
 
598
 
    t_mailcheck *mc = (t_mailcheck *) control->data;
599
 
 
600
 
    root = xmlNewTextChild (parent, NULL, MAILCHECK_ROOT, NULL);
601
 
 
602
 
    g_snprintf (value, 4, "%d", mc->interval);
603
 
    xmlSetProp (root, "interval", value);
604
 
 
605
 
    xmlNewTextChild (root, NULL, "Mbox", mc->mbox);
606
 
 
607
 
    node = xmlNewTextChild (root, NULL, "Command", mc->command);
608
 
 
609
 
    g_snprintf (value, 2, "%d", mc->term);
610
 
    xmlSetProp (node, "term", value);
611
 
 
612
 
    g_snprintf (value, 2, "%d", mc->use_sn);
613
 
    xmlSetProp (node, "sn", value);
614
 
 
615
 
    xmlNewTextChild (root, NULL, "newmail-command", mc->newmail_command);
616
 
}
617
 
 
618
 
static void
619
 
mailcheck_attach_callback (Control * control, const char *signal,
620
 
                           GCallback callback, gpointer data)
621
 
{
622
 
    t_mailcheck *mc = control->data;
623
 
 
624
 
    g_signal_connect (mc->button, signal, callback, data);
625
 
}
626
 
 
627
 
static void
628
 
run_mail_command (t_mailcheck * mc)
629
 
{
630
 
    exec_cmd (mc->command, mc->term, mc->use_sn);
631
 
}
632
 
 
633
 
static t_mailcheck *
634
 
mailcheck_new (void)
635
 
{
636
 
    t_mailcheck *mailcheck;
637
 
    const char *mail;
638
 
 
639
 
    mailcheck = g_new0 (t_mailcheck, 1);
640
 
 
641
 
    mailcheck->status = NO_MAIL;
642
 
    mailcheck->interval = 30;
643
 
    mailcheck->timeout_id = 0;
644
 
 
645
 
    reset_mailcheck_icons (mailcheck);
646
 
 
647
 
    mailcheck->newmail_command = g_strdup ("");
648
 
 
649
 
    mail = g_getenv ("MAIL");
650
 
 
651
 
    if (mail)
652
 
    {
653
 
        mailcheck->mbox = g_strdup (mail);
654
 
    }
655
 
    else
656
 
    {
657
 
        const char *logname = g_getenv ("LOGNAME");
658
 
 
659
 
        mailcheck->mbox = g_strconcat ("/var/spool/mail/", logname, NULL);
660
 
    }
661
 
 
662
 
    mailcheck->button =
663
 
        xfce_iconbutton_new_from_pixbuf (mailcheck->nomail_pb);
664
 
    gtk_widget_show (mailcheck->button);
665
 
    gtk_button_set_relief (GTK_BUTTON (mailcheck->button), GTK_RELIEF_NONE);
666
 
 
667
 
    g_signal_connect_swapped (mailcheck->button,
668
 
                              "clicked", G_CALLBACK (run_mail_command),
669
 
                              mailcheck);
670
 
 
671
 
    mailcheck_set_tip (mailcheck);
672
 
 
673
 
    return mailcheck;
674
 
}
675
 
 
676
 
static void
677
 
mailcheck_free (Control * control)
678
 
{
679
 
    t_mailcheck *mailcheck = (t_mailcheck *) control->data;
680
 
 
681
 
    if (mailcheck->timeout_id > 0)
682
 
        g_source_remove (mailcheck->timeout_id);
683
 
 
684
 
    g_free (mailcheck->mbox);
685
 
    g_free (mailcheck->command);
686
 
    g_free (mailcheck->newmail_command);
687
 
 
688
 
    g_object_unref (mailcheck->nomail_pb);
689
 
    g_object_unref (mailcheck->oldmail_pb);
690
 
    g_object_unref (mailcheck->newmail_pb);
691
 
 
692
 
    g_free (mailcheck);
693
 
}
694
 
 
695
 
static void
696
 
mailcheck_set_theme (Control * control, const char *theme)
697
 
{
698
 
    t_mailcheck *mailcheck = (t_mailcheck *) control->data;
699
 
 
700
 
    reset_mailcheck_icons (mailcheck);
701
 
 
702
 
    if (mailcheck->status == NO_MAIL)
703
 
    {
704
 
        xfce_iconbutton_set_pixbuf (XFCE_ICONBUTTON (mailcheck->button),
705
 
                                    mailcheck->nomail_pb);
706
 
    }
707
 
    else if (mailcheck->status == OLD_MAIL)
708
 
    {
709
 
        xfce_iconbutton_set_pixbuf (XFCE_ICONBUTTON (mailcheck->button),
710
 
                                    mailcheck->oldmail_pb);
711
 
    }
712
 
    else
713
 
    {
714
 
        xfce_iconbutton_set_pixbuf (XFCE_ICONBUTTON (mailcheck->button),
715
 
                                    mailcheck->newmail_pb);
716
 
    }
717
 
}
718
 
 
719
 
/*  Options dialog
720
 
 *  --------------
721
 
*/
722
 
typedef struct
723
 
{
724
 
    t_mailcheck *mc;
725
 
 
726
 
    /* control dialog */
727
 
    GtkWidget *dialog;
728
 
 
729
 
    /* options */
730
 
    GtkWidget *mbox_entry;
731
 
    GtkWidget *interval_spin;
732
 
    GtkWidget *newmail_cmd_entry;
733
 
    CommandOptions *cmd_opts;
734
 
}
735
 
MailDialog;
736
 
 
737
 
static void
738
 
free_maildialog (MailDialog * md)
739
 
{
740
 
    g_free (md);
741
 
}
742
 
 
743
 
/* update control */
744
 
static void
745
 
mailcheck_apply_options (MailDialog * md)
746
 
{
747
 
    const char *tmp;
748
 
    t_mailcheck *mc = md->mc;
749
 
 
750
 
    g_free (mc->command);
751
 
 
752
 
    command_options_get_command (md->cmd_opts, &(mc->command), &(mc->term),
753
 
                                 &(mc->use_sn));
754
 
 
755
 
    tmp = gtk_entry_get_text (GTK_ENTRY (md->mbox_entry));
756
 
 
757
 
    if (tmp && *tmp)
758
 
    {
759
 
        g_free (mc->mbox);
760
 
        mc->mbox = g_strdup (tmp);
761
 
        set_mbox_type(mc);
762
 
    }
763
 
 
764
 
    tmp = gtk_entry_get_text (GTK_ENTRY (md->newmail_cmd_entry));
765
 
 
766
 
    if (tmp && *tmp)
767
 
    {
768
 
        g_free (mc->newmail_command);
769
 
        mc->newmail_command = g_strdup (tmp);
770
 
    }
771
 
 
772
 
    mc->interval =
773
 
        gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
774
 
                                          (md->interval_spin));
775
 
 
776
 
    mailcheck_set_tip (mc);
777
 
 
778
 
    run_mailcheck (mc);
779
 
}
780
 
 
781
 
/* mbox */
782
 
static void
783
 
mbox_browse_cb (GtkWidget * b, MailDialog * md)
784
 
{
785
 
    const char *text;
786
 
    char *file;
787
 
 
788
 
    text = gtk_entry_get_text (GTK_ENTRY (md->mbox_entry));
789
 
 
790
 
    file = select_file_name (NULL, text, md->dialog);
791
 
 
792
 
    if (file)
793
 
    {
794
 
        gtk_entry_set_text (GTK_ENTRY (md->mbox_entry), file);
795
 
        g_free (file);
796
 
        mailcheck_apply_options (md);
797
 
    }
798
 
}
799
 
 
800
 
/* newmail command */
801
 
static void
802
 
newmail_cmd_brows_cb (GtkWidget * b, MailDialog * md)
803
 
{
804
 
    const char *text;
805
 
    char *file;
806
 
 
807
 
    text = gtk_entry_get_text (GTK_ENTRY (md->newmail_cmd_entry));
808
 
 
809
 
    file = select_file_name (NULL, text, md->dialog);
810
 
 
811
 
    if (file)
812
 
    {
813
 
        gtk_entry_set_text (GTK_ENTRY (md->newmail_cmd_entry), file);
814
 
        g_free (file);
815
 
        mailcheck_apply_options (md);
816
 
    }
817
 
}
818
 
 
819
 
static gboolean
820
 
entry_lost_focus (MailDialog * md)
821
 
{
822
 
    mailcheck_apply_options (md);
823
 
 
824
 
    /* needed to let entry handle focus-out as well */
825
 
    return FALSE;
826
 
}
827
 
 
828
 
static void
829
 
add_mbox_box (GtkWidget * vbox, GtkSizeGroup * sg, MailDialog * md)
830
 
{
831
 
    GtkWidget *hbox, *label, *button, *image;
832
 
    t_mailcheck *mc = md->mc;
833
 
 
834
 
    hbox = gtk_hbox_new (FALSE, BORDER);
835
 
    gtk_widget_show (hbox);
836
 
    gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
837
 
 
838
 
    label = gtk_label_new (_("Mail box:"));
839
 
    gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
840
 
    gtk_size_group_add_widget (sg, label);
841
 
    gtk_widget_show (label);
842
 
    gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
843
 
 
844
 
    md->mbox_entry = gtk_entry_new ();
845
 
    if (mc->mbox)
846
 
        gtk_entry_set_text (GTK_ENTRY (md->mbox_entry), mc->mbox);
847
 
    gtk_widget_show (md->mbox_entry);
848
 
    gtk_box_pack_start (GTK_BOX (hbox), md->mbox_entry, TRUE, TRUE, 0);
849
 
 
850
 
    button = gtk_button_new ();
851
 
    gtk_widget_show (button);
852
 
    gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
853
 
 
854
 
    image = gtk_image_new_from_stock (GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON);
855
 
    gtk_widget_show (image);
856
 
    gtk_container_add (GTK_CONTAINER (button), image);
857
 
 
858
 
    g_signal_connect (button, "clicked", G_CALLBACK (mbox_browse_cb), md);
859
 
 
860
 
    /* only set label on focus out */
861
 
    g_signal_connect_swapped (md->mbox_entry, "focus-out-event",
862
 
                              G_CALLBACK (entry_lost_focus), md);
863
 
}
864
 
 
865
 
static void
866
 
add_newmail_cmd_box (GtkWidget * vbox, GtkSizeGroup * sg, MailDialog * md)
867
 
{
868
 
    GtkWidget *hbox, *label, *button, *image;
869
 
    t_mailcheck *mc = md->mc;
870
 
 
871
 
    hbox = gtk_hbox_new (FALSE, BORDER);
872
 
    gtk_widget_show (hbox);
873
 
    gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
874
 
 
875
 
    label = gtk_label_new (_("New mail command:"));
876
 
    gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
877
 
    gtk_size_group_add_widget (sg, label);
878
 
    gtk_widget_show (label);
879
 
    gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
880
 
 
881
 
    md->newmail_cmd_entry = gtk_entry_new ();
882
 
    if (mc->newmail_command)
883
 
        gtk_entry_set_text (GTK_ENTRY (md->newmail_cmd_entry),
884
 
                            mc->newmail_command);
885
 
    gtk_widget_show (md->newmail_cmd_entry);
886
 
    gtk_box_pack_start (GTK_BOX (hbox), md->newmail_cmd_entry, TRUE, TRUE, 0);
887
 
 
888
 
    gtk_tooltips_set_tip (tooltips, md->newmail_cmd_entry,
889
 
                          _("Command to run when new mail arrives"), NULL);
890
 
 
891
 
    button = gtk_button_new ();
892
 
    gtk_widget_show (button);
893
 
    gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
894
 
 
895
 
    image = gtk_image_new_from_stock (GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON);
896
 
    gtk_widget_show (image);
897
 
    gtk_container_add (GTK_CONTAINER (button), image);
898
 
 
899
 
    g_signal_connect (button, "clicked", G_CALLBACK (newmail_cmd_brows_cb),
900
 
                      md);
901
 
 
902
 
    /* only set label on focus out */
903
 
    g_signal_connect_swapped (md->mbox_entry, "focus-out-event",
904
 
                              G_CALLBACK (entry_lost_focus), md);
905
 
}
906
 
 
907
 
/* command */
908
 
static void
909
 
add_command_box (GtkWidget * vbox, GtkSizeGroup * sg, MailDialog * md)
910
 
{
911
 
    md->cmd_opts = create_command_options (sg);
912
 
 
913
 
    command_options_set_command (md->cmd_opts, md->mc->command,
914
 
                                 md->mc->term, md->mc->use_sn);
915
 
 
916
 
    gtk_box_pack_start (GTK_BOX (vbox), md->cmd_opts->base, FALSE, TRUE, 0);
917
 
 
918
 
    gtk_tooltips_set_tip (tooltips, md->cmd_opts->command_entry,
919
 
                          _("Command to run when the button "
920
 
                            "on the panel is clicked"), NULL);
921
 
}
922
 
 
923
 
/* interval */
924
 
static void
925
 
interval_changed (GtkSpinButton * spin, MailDialog * md)
926
 
{
927
 
    md->mc->interval = gtk_spin_button_get_value_as_int (spin);
928
 
}
929
 
 
930
 
static void
931
 
add_interval_box (GtkWidget * vbox, GtkSizeGroup * sg, MailDialog * md)
932
 
{
933
 
    GtkWidget *hbox, *label;
934
 
    t_mailcheck *mc = md->mc;
935
 
 
936
 
    hbox = gtk_hbox_new (FALSE, BORDER);
937
 
    gtk_widget_show (hbox);
938
 
    gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
939
 
 
940
 
    label = gtk_label_new (_("Interval (sec):"));
941
 
    gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
942
 
    gtk_size_group_add_widget (sg, label);
943
 
    gtk_widget_show (label);
944
 
    gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
945
 
 
946
 
    md->interval_spin = gtk_spin_button_new_with_range (1, 600, 1);
947
 
    gtk_spin_button_set_value (GTK_SPIN_BUTTON (md->interval_spin),
948
 
                               mc->interval);
949
 
    gtk_widget_show (md->interval_spin);
950
 
    gtk_box_pack_start (GTK_BOX (hbox), md->interval_spin, FALSE, FALSE, 0);
951
 
 
952
 
    g_signal_connect (md->interval_spin, "value-changed",
953
 
                      G_CALLBACK (interval_changed), md);
954
 
}
955
 
 
956
 
/* the dialog */
957
 
static void
958
 
mailcheck_create_options (Control * control, GtkContainer * container,
959
 
                          GtkWidget * done)
960
 
{
961
 
    GtkWidget *vbox;
962
 
    GtkSizeGroup *sg = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
963
 
    MailDialog *md;
964
 
 
965
 
    xfce_textdomain (GETTEXT_PACKAGE, LOCALEDIR, "UTF-8");
966
 
 
967
 
    md = g_new0 (MailDialog, 1);
968
 
 
969
 
    md->mc = (t_mailcheck *) control->data;
970
 
 
971
 
    md->dialog = gtk_widget_get_toplevel (done);
972
 
 
973
 
    vbox = gtk_vbox_new (FALSE, BORDER);
974
 
    gtk_widget_show (vbox);
975
 
 
976
 
    add_mbox_box (vbox, sg, md);
977
 
 
978
 
    add_interval_box (vbox, sg, md);
979
 
 
980
 
    add_newmail_cmd_box (vbox, sg, md);
981
 
 
982
 
    add_command_box (vbox, sg, md);
983
 
 
984
 
    /* signals */
985
 
    g_signal_connect_swapped (done, "clicked",
986
 
                              G_CALLBACK (mailcheck_apply_options), md);
987
 
 
988
 
    g_signal_connect_swapped (md->dialog, "destroy-event",
989
 
                              G_CALLBACK (free_maildialog), md);
990
 
 
991
 
    gtk_container_add (container, vbox);
992
 
}
993
 
 
994
 
/* create panel control */
995
 
static gboolean
996
 
create_mailcheck_control (Control * control)
997
 
{
998
 
    t_mailcheck *mailcheck = mailcheck_new ();
999
 
    GtkWidget *b = mailcheck->button;
1000
 
 
1001
 
    gtk_container_add (GTK_CONTAINER (control->base), b);
1002
 
 
1003
 
    control->data = (gpointer) mailcheck;
1004
 
    control->with_popup = FALSE;
1005
 
 
1006
 
 
1007
 
    return TRUE;
1008
 
}
1009
 
 
1010
 
G_MODULE_EXPORT void
1011
 
xfce_control_class_init (ControlClass * cc)
1012
 
{
1013
 
    xfce_textdomain (GETTEXT_PACKAGE, LOCALEDIR, "UTF-8");
1014
 
 
1015
 
    cc->name = "mailcheck";
1016
 
    cc->caption = _("Mail checker");
1017
 
 
1018
 
    cc->create_control = (CreateControlFunc) create_mailcheck_control;
1019
 
 
1020
 
    cc->free = mailcheck_free;
1021
 
    cc->read_config = mailcheck_read_config;
1022
 
    cc->write_config = mailcheck_write_config;
1023
 
    cc->attach_callback = mailcheck_attach_callback;
1024
 
 
1025
 
    cc->create_options = (gpointer) mailcheck_create_options;
1026
 
 
1027
 
    cc->set_theme = mailcheck_set_theme;
1028
 
}
1029
 
 
1030
 
 
1031
 
XFCE_PLUGIN_CHECK_INIT