File: properties.c

package info (click to toggle)
wmmail 0.63a-4
  • links: PTS
  • area: main
  • in suites: potato
  • size: 868 kB
  • ctags: 235
  • sloc: ansic: 2,936; sh: 388; makefile: 144; perl: 111
file content (690 lines) | stat: -rw-r--r-- 17,497 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/*
 * WMMail - Window Maker Mail
 *
 * Copyright (c) 1996, 1997, 1998  Per Liden
 * Copyright (c) 1997, 1998  Bryan Chan
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * properties.c: routines that read libPL-style properties (some stolen
 *               code courtesy of Peter Bentley's Malaprop
 *               (http://www.sorted.org/~pete/wmaker):
 * 
 * Copyright (c) 1998 Peter Bentley (pete@sorted.org)
 * 
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice and this permission notice
 * appear in all copies of the software and related documentation
 * 
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 * 
 * IN NO EVENT SHALL PETER BENTLEY BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT
 * ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY,
 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 *
 * $Id: properties.c,v 1.7 1999/03/29 13:05:46 bryan.chan Exp $
 *
 */

#include <stdlib.h>
#include <strings.h>
#include <pwd.h>
#include <proplist.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "wmmail.h"
#include "wmutil.h"
#include "list.h"


#define GET_ENTRY(x,y,z) { proplist_t sub_key = PLMakeString(z); \
                           y = PLGetDictionaryEntry(x, sub_key); \
                           PLRelease(sub_key); }

/* function prototypes */
static proplist_t domain_load(char *);

static int get_boolean(proplist_t, proplist_t, int *);
static int get_coord(proplist_t, proplist_t, int *, int *);
static int get_int(proplist_t, proplist_t, int *);

static int get_num_of_msg_mode(proplist_t, proplist_t, int *);

static int get_animations(proplist_t, proplist_t);
static int get_animation(proplist_t, proplist_t, int);
static void anim_add_frame(int, char *);
static void anim_load_frame(XpmIcon *, char *);

static int get_mailboxes(proplist_t, proplist_t);
static int init_mailbox(Mailbox *, proplist_t);


/* parse the WMMail domain; what's a cleaner, simpler way to do this? */
int parse_gnustep_domain(char *path)
{
  unsigned int  i, number_of_keys;
  proplist_t    keys, key, value;
  proplist_t    domain = domain_load(path);
  char          *str;

  if (domain != NULL && PLIsDictionary(domain))
  {
    if ( (keys = PLGetAllDictionaryKeys(domain)) != NULL )
    {
      number_of_keys = PLGetNumberOfElements(keys);
  
      for (i = 0; i < number_of_keys; i++)
      {
        key = PLGetArrayElement(keys, i);
        value = PLGetDictionaryEntry(domain, key);
        str = PLGetString(key);
  
        if (PLIsDictionary(value))
        {
          if (!strcasecmp("Animations", str))
          {
            get_animations(key, value);
          }
          else
            croak("unrecognized dictionary \"%s\" ignored", str);
        }
        else if (PLIsString(value))
        {
          if (!strcasecmp("DisableBeep", str))
          {
            get_boolean(key, value, &use_beep);
            use_beep = ! use_beep;  /* if yes, then don't use beep */
          }
          else if (!strcasecmp("DoubleClickTime", str))
          {
            get_int(key, value, &double_click_time);
          }
          else if (!strcasecmp("DisplayMessageCount", str))
          {
            get_num_of_msg_mode(key, value, &num_of_msg_mode);
          }
          else if (!strcasecmp("DisplayColor", str))
          {
            wmmail_color = wstrdup(PLGetString(value));
          }
          else if (!strcasecmp("DisplayFont", str))
          {
            wmmail_font = XLoadFont(disp, PLGetString(value));
          }
          else if (!strcasecmp("ExecuteOnNew", str))
          {
            exec_on_new = wstrdup(PLGetString(value));
          }
          else if (!strcasecmp("ExecuteOnClick", str))
          {
            exec_on_click = wstrdup(PLGetString(value));
          }
          else
            croak("unrecognized key \"%s\" ignored", str);
        }
        else if (PLIsArray(value))
        {
          if (!strcasecmp("Mailboxes", str))
          {
            get_mailboxes(key, value);
          }
          else if (!strcasecmp("DisplayLocation", str))
          {
            get_coord(key, value, &num_of_msg_x, &num_of_msg_y);
          }
          else
            croak("unrecognized array \"%s\" ignored", str);
        }

        wfree(str);
      }
    }

    return True;
  }
  else
  {
    croak("defaults domain %s corrupted", path);
    return False;
  }
}


/* load the specified domain from disk */
static proplist_t domain_load(char *path)
{
  proplist_t  list = NULL;

  if (path)
  {
    list = PLGetProplistWithPath(path);
    wfree((void *) path);
  }

  return list;
}


/* get a boolean value from a property list entry */
static int get_boolean(proplist_t key, proplist_t value, int *addr)
{
  static int  data;
  char        *s;

  if (!PLIsString(value)) 
  {
    croak("wrong option format for key \"%s\"; boolean expected",
          PLGetString(key));
    return False;
  }

  s = PLGetString(value);

  if (!strcasecmp("Yes", s))
    data = True;
  else if (!strcasecmp("No", s))
    data = False;
  else
  {
    croak("wrong option format for key \"%s\"; boolean expected",
          PLGetString(key));
    return False;
  }

  wfree(s);

  if (addr)
    *addr = data;

  return True;
}


/* get a coordinate from a property list entry */
static int get_coord(proplist_t key, proplist_t value, int *addr_x, int *addr_y)
{
  int dimensions, data[2], i;

  if (!PLIsArray(value))
  {
    croak("wrong option format for key \"%s\"; coordinates expected",
          PLGetString(key));
    return False;
  }

  if (( dimensions = PLGetNumberOfElements(value) ) != 2)
  {
    croak("wrong option format for key \"%s\"; coordinates expected",
          PLGetString(key));
    return False;
  }

  for (i = 0; i < dimensions; i++)
  {
    proplist_t element = PLGetArrayElement(value, i);

    if (!PLIsString(element))
    {
      croak("wrong option format for key \"%s\"; coordinates expected",
            PLGetString(key));
      return False;
    }
    
    if (sscanf(PLGetString(element), "%i", &data[i]) != 1)
    {
      croak("integer pair expected for coordinates in key \"%s\"",
            PLGetString(key));
      return False;
    }
  }  

  *addr_x = data[0]; 
  *addr_y = data[1]; 

  return True;
}


/* get an integer from a property list entry */
static int get_int(proplist_t key, proplist_t value, int *addr)
{
  static int  data;
  char        *s;

  if (!PLIsString(value)) 
  {
    croak("wrong option format for key \"%s\"; integer expected",
          PLGetString(key));
    return False;
  }

  s = PLGetString(value);

  if (sscanf(s, "%i", &data) != 1)
  {
    croak("cannot convert \"%s\" to integer for key \"%s\"", 
          s, PLGetString(key));
    return False;
  }

  if (addr)
    *addr = data;

  return True;
}


static int get_num_of_msg_mode(proplist_t key, proplist_t value, int *addr)
{
  static int data;
  char       *s;

  if (!PLIsString(value)) 
  {
    croak("wrong option format for key \"%s\"; expected \"NewOnly\", "
          "\"TotalOnly\", \"NewOverTotal\", or \"None\"",
          PLGetString(key));
    return False;
  }

  s = PLGetString(value);

  if (!strcasecmp("None", s))
    data = SHOW_NONE;
  else if (!strcasecmp("NewOverTotal", s))
    data = SHOW_NEW_OVER_TOTAL;
  else if (!strcasecmp("NewOnly", s))
    data = SHOW_NEW_ONLY;
  else if (!strcasecmp("TotalOnly", s))
    data = SHOW_TOTAL_ONLY;
  else
  {
    croak("wrong option format for key \"%s\"; expected \"NewOnly\", "
          "\"TotalOnly\", \"NewOverTotal\", or \"None\"",
          PLGetString(key));
    return False;
  }

  if (addr)
    *addr = data;

  return True;
}


/* ANIMATIONS */

static int get_animations(proplist_t key, proplist_t value)
{
  proplist_t animation_properties[3];
  proplist_t delay_key, delay_value;
  proplist_t frame_key, frame_value;
  
  GET_ENTRY(value, animation_properties[NO_MAIL], "Empty");
  GET_ENTRY(value, animation_properties[OLD_MAIL], "Old");
  GET_ENTRY(value, animation_properties[NEW_MAIL], "New");

  delay_key = PLMakeString("Delay");
  frame_key = PLMakeString("Frames");

  if (animation_properties[NO_MAIL] != NULL)
  {
    delay_value = PLGetDictionaryEntry(animation_properties[NO_MAIL],
                                       delay_key);
    if (delay_value != NULL)
    {
      get_int(delay_key, delay_value, &anim_speed[NO_MAIL]);
      anim_speed[NO_MAIL] *= 100;
    }

    frame_value = PLGetDictionaryEntry(animation_properties[NO_MAIL],
                                       frame_key);
    if (frame_value != NULL)
      get_animation(frame_key, frame_value, NO_MAIL);
  }

  if (animation_properties[OLD_MAIL] != NULL)
  {
    delay_value = PLGetDictionaryEntry(animation_properties[OLD_MAIL],
                                       delay_key);
    if (delay_value != NULL)
    {
      get_int(delay_key, delay_value, &anim_speed[OLD_MAIL]);
      anim_speed[OLD_MAIL] *= 100;
    }

    frame_value = PLGetDictionaryEntry(animation_properties[OLD_MAIL],
                                       frame_key);
    if (frame_value != NULL)
      get_animation(frame_key, frame_value, OLD_MAIL);
  }

  if (animation_properties[NEW_MAIL] != NULL)
  {
    delay_value = PLGetDictionaryEntry(animation_properties[NEW_MAIL],
                                       delay_key);
    if (delay_value != NULL)
    {
      get_int(delay_key, delay_value, &anim_speed[NEW_MAIL]);
      anim_speed[NEW_MAIL] *= 100;
    }

    frame_value = PLGetDictionaryEntry(animation_properties[NEW_MAIL],
                                       frame_key);
    if (frame_value != NULL)
      get_animation(frame_key, frame_value, NEW_MAIL);
  }

  PLRelease(delay_key);
  PLRelease(frame_key);

  return True;
}


static int get_animation(proplist_t key, proplist_t frames, int type)
{
  int i, number_of_frames;

  if (!PLIsArray(frames))
  {
    croak("wrong option format for key \"%s\"; array expected",
          PLGetString(key));
    return False;
  }

  number_of_frames = PLGetNumberOfElements(frames);

  for (i = 0; i < number_of_frames; i++)
  {
    char *s = PLGetString(PLGetArrayElement(frames, i));
    anim_add_frame(type, s);
    wfree(s);
  }  

  return True;
}


static void anim_add_frame(int which, char *path)
{
  XpmIcon *current;

  if (animations[which] == NULL)
  {
    animations[which] = (XpmIcon *) wmalloc(sizeof(XpmIcon));
    current = animations[which];
  }
  else
  {
    for (current = animations[which]; 
         current->next != animations[which];	/* until wrapped around */
         current = current->next);

    current->next = (XpmIcon *) wmalloc(sizeof(XpmIcon));
    current = current->next;
  }

  anim_load_frame(current, path);
  current->next = animations[which];	/* make a looping animation */
}


static void anim_load_frame(XpmIcon *anim, char *path)
{
  int ret;

  anim->attributes.valuemask   = (XpmExactColors | XpmCloseness);
  anim->attributes.exactColors = FALSE;
  anim->attributes.closeness   = 40000;

  ret = XpmReadFileToPixmap(disp, root, path, &anim->pixmap, &anim->mask,
                            &anim->attributes);

  switch(ret)
  {
    case XpmSuccess: 
      break;

    case XpmColorError: 
    case XpmColorFailed: 
      croak("not enough free colours loading '%s'", path);
      break;

    case XpmOpenFailed:
      croak("cannot open '%s'", path);
      break;

    case XpmFileInvalid:
      croak("invalid XPM file '%s'", path);
      break;

    case XpmNoMemory:
      croak("not enough free memory loading '%s'", path);
      break;

    default:
      croak("unknown error (%d) loading '%s'", ret, path);
      break;
  }

  if (ret < 0)
    exit(-1);

  if (anim->mask == 0)
  {
    croak("XPM file '%s' does not have a transparent colour");
    exit(-1);
  }
}


/* MAILBOXES */

static int get_mailboxes(proplist_t key, proplist_t value)
{
  int         i,
              number_of_mailboxes = PLGetNumberOfElements(value);
  Mailbox    *mailbox;
  proplist_t  mailbox_definition;

  if (number_of_mailboxes > 0)  
  {
    for (i = 0; i < number_of_mailboxes; i++)
    {
      mailbox_definition = PLGetArrayElement(value, i);

      if (PLIsDictionary(mailbox_definition))
      {
        mailbox = (Mailbox *) wmalloc(sizeof(Mailbox));

        /* parse mailbox definition; destroy the mailbox if it is bad */
        if (init_mailbox(mailbox, mailbox_definition))
        {
#ifdef DEBUG
          croak("mailbox \"%s\" added", mailbox->name);
#endif
          mailbox_list = list_cons(mailbox, mailbox_list);
          mailbox_count = list_length(mailbox_list);
        }
        else
        {
          croak("mailbox \"%s\" ignored", (mailbox->name ? mailbox->name : ""));
          wfree(mailbox->execute_on_update);
          wfree(mailbox->name);
          wfree(mailbox);
        }
      }
      else
      {
        /* skip this mailbox if its definition is corrupt */
        croak("wrong option format for key \"%s\"; mailbox definition "
              "expected", PLGetString(key));
      }
    }
  }

  return True;
}


static int init_mailbox(Mailbox *mailbox, proplist_t mailbox_definition)
{
  proplist_t  mailbox_name,
              mailbox_type,
              execute_on_update,
              update_interval;

  char       *mailbox_type_text;

  mailbox->status           = NO_MAIL;
  mailbox->total_mail_count = 0;
  mailbox->new_mail_count   = 0;
  mailbox->size             = -1;
  mailbox->last_update      = 0;

  GET_ENTRY(mailbox_definition, mailbox_name, "Name");
  if (mailbox_name != NULL)
  {
    if (!PLIsString(mailbox_name))
    {
      croak("wrong option format for key \"Name\"; string expected");
      return False;
    }
    else
      mailbox->name = PLGetString(mailbox_name);
  }
  else
  {
    croak("mailbox missing key \"Name\"; ignored");
    return False;
  }

  GET_ENTRY(mailbox_definition, mailbox_type, "Type");
  if (mailbox_type != NULL)
  {
    if (!PLIsString(mailbox_type))
    {
      croak("wrong option format for key \"Type\"; mailbox type expected");
      return False;
    }
    else
      mailbox_type_text = PLGetString(mailbox_type);
  
#ifdef MBOX_SUPPORT
    if (!strcasecmp(mailbox_type_text, "mbox"))
      mailbox->type = TYPE_MBOX;
    else
#endif
#ifdef MH_SUPPORT
    if (!strcasecmp(mailbox_type_text, "mh"))
      mailbox->type = TYPE_MH;
    else
#endif
#ifdef MAILDIR_SUPPORT
    if (!strcasecmp(mailbox_type_text, "maildir"))
      mailbox->type = TYPE_MAILDIR;
    else
#endif
#ifdef POP3_SUPPORT
    if (!strcasecmp(mailbox_type_text, "pop3"))
      mailbox->type = TYPE_POP3;
    else
#endif
#ifdef IMAP_SUPPORT
    if (!strcasecmp(mailbox_type_text, "imap"))
      mailbox->type = TYPE_IMAP;
    else
#endif
    {
      croak("unknown mailbox type \"%s\"; expected: "
#ifdef MBOX_SUPPORT
            "\"mbox\", "
#endif
#ifdef MH_SUPPORT
            "\"mh\", "
#endif
#ifdef MAILDIR_SUPPORT
            "\"MailDir\", "
#endif
#ifdef POP3_SUPPORT
            "\"POP3\", "
#endif
#ifdef IMAP_SUPPORT
            "\"IMAP\""
#endif
            , mailbox_type_text);

      return False;
    }
  }
  else
  {
    croak("mailbox \"%s\" missing key \"Type\"; ignored", mailbox->name);
    return False;
  }

  GET_ENTRY(mailbox_definition, execute_on_update, "ExecuteOnUpdate");
  if (execute_on_update != NULL)
  {
    if (!PLIsString(execute_on_update))
    {
      croak("wrong option format for key \"ExecuteOnUpdate\"; string expected");
      return False;
    }
    else
      mailbox->execute_on_update = PLGetString(execute_on_update);
  }
  else
    mailbox->execute_on_update = NULL;

  GET_ENTRY(mailbox_definition, update_interval, "UpdateInterval");
  {
    proplist_t label = PLMakeString("UpdateInterval");

    if (update_interval == NULL ||
        !get_int(label, update_interval, &mailbox->update_interval))
    {
      croak("mailbox \"%s\" missing key \"UpdateInterval\"; %d assumed",
            mailbox->name, DEFAULT_INTERVAL);
      mailbox->update_interval = DEFAULT_INTERVAL;
    }

    PLRelease(label);
  }

  GET_ENTRY(mailbox_definition, mailbox->options, "Options");
  if (mailbox->options == NULL)
  {
    croak("mailbox \"%s\" missing key \"Options\"; ignored", mailbox->name);
    return False;
  }
  else if (!PLIsDictionary(mailbox->options))
  {
    croak("wrong option format for key \"Options\"; dictionary expected");
    return False;
  }

  return True;
}