1/* SOGoUser+Mailer.m - this file is part of SOGo
2 *
3 * Copyright (C) 2010 Inverse inc.
4 *
5 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6 *
7 * This file 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, or (at your option)
10 * any later version.
11 *
12 * This file 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 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; see the file COPYING.  If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23#import <Foundation/NSDictionary.h>
24#import <Foundation/NSString.h>
25
26#import <SOGo/NSArray+Utilities.h>
27#import <SOGo/SOGoUserSettings.h>
28
29#import "SOGoUser+Mailer.h"
30
31@implementation SOGoUser (SOGoMailExtensions)
32
33- (NSArray *) mailDelegators
34{
35  BOOL dirty;
36  NSDictionary *mailSettings, *delegatorSettings;
37  NSArray *mailDelegators, *delegates;
38  NSMutableArray *validMailDelegators;
39  NSString *delegatorLogin;
40  SOGoUser *delegatorUser;
41  unsigned int max, count;
42
43  dirty = NO;
44  validMailDelegators = [NSMutableArray array];
45  mailSettings = [[self userSettings] objectForKey: @"Mail"];
46  mailDelegators = [mailSettings objectForKey: @"DelegateFrom"];
47  if (mailDelegators)
48    {
49      max = [mailDelegators count];
50      for (count = 0; count < max; count++)
51        {
52          // 1. Verify if delegator is valid
53          delegatorLogin = [mailDelegators objectAtIndex: count];
54          delegatorUser = [SOGoUser userWithLogin: delegatorLogin];
55          if (delegatorUser)
56            {
57              // 2. Verify if delegator still delegates to user
58              delegatorSettings = [[delegatorUser userSettings] objectForKey: @"Mail"];
59              delegates = [delegatorSettings objectForKey: @"DelegateTo"];
60              if ([delegates containsObject: [self login]])
61                [validMailDelegators addObject: delegatorLogin];
62              else
63                dirty = YES;
64            }
65          else
66            dirty = YES;
67        }
68    }
69
70  if (dirty)
71    [self _setMailDelegators: validMailDelegators];
72
73  return validMailDelegators;
74}
75
76- (void) _setMailDelegators: (NSArray *) newDelegators
77{
78  NSMutableDictionary *mailSettings;
79  SOGoUserSettings *settings;
80
81  settings = [self userSettings];
82  mailSettings = [settings objectForKey: @"Mail"];
83  if (!mailSettings)
84    {
85      mailSettings = [NSMutableDictionary dictionaryWithCapacity: 1];
86      [settings setObject: mailSettings forKey: @"Mail"];
87    }
88  [mailSettings setObject: newDelegators
89                   forKey: @"DelegateFrom"];
90  [settings synchronize];
91}
92
93- (void) addMailDelegator: (NSString *) newDelegator
94{
95  NSMutableArray *delegators;
96
97  delegators = [[self mailDelegators] mutableCopy];
98  [delegators autorelease];
99  [delegators addObjectUniquely: newDelegator];
100
101  [self _setMailDelegators: delegators];
102}
103
104- (void) removeMailDelegator: (NSString *) oldDelegator
105{
106  NSMutableArray *delegators;
107
108  delegators = [[self mailDelegators] mutableCopy];
109  [delegators autorelease];
110  [delegators removeObject: oldDelegator];
111
112  [self _setMailDelegators: delegators];
113}
114
115@end
116