]> err.no Git - scalable-opengroupware.org/blob - SoObjects/Mailer/SOGoUser+Mail.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1146 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SoObjects / Mailer / SOGoUser+Mail.m
1 /*
2   Copyright (C) 2004-2005 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #import <Foundation/NSArray.h>
23 #import <Foundation/NSUserDefaults.h>
24 #import <Foundation/NSKeyValueCoding.h>
25
26 #import <NGExtensions/NSNull+misc.h>
27 #import <NGExtensions/NSObject+Logs.h>
28
29 #import "SOGoMailIdentity.h"
30
31 #import "SOGoUser+Mail.h"
32
33 @implementation SOGoUser(Mail)
34
35 - (NSString *)agenorSentFolderName {
36   /* Note: specialty: the Sent folder is called the same in all accounts */
37   static NSString *s = nil;
38   if (s == nil) {
39     NSUserDefaults *ud;
40     
41     ud = [NSUserDefaults standardUserDefaults];
42     s = [[ud stringForKey:@"SOGoSentFolderName"] copy];
43     if (![s isNotEmpty]) s = @"Sent";
44     [self logWithFormat:@"Note: using SOGoSentFolderName: '%@'", s];
45   }
46   return s;
47 }
48
49 - (NSString *)agenorSentFolderForAccount:(NSString *)_account {
50   // TODO: support different locations for shares!
51   NSString *p;
52   
53   if (![_account isNotEmpty]) 
54     return nil;
55   
56   // if ([_account rangeOfString:@".-."].length == 0)
57   // TODO: check whether we need special handling for shares!
58   p = [_account stringByAppendingString:@"/"];
59   p = [p stringByAppendingString:[self agenorSentFolderName]];
60   return p;
61 }
62
63 - (SOGoMailIdentity *)primaryMailIdentity {
64   SOGoMailIdentity *identity;
65   NSString *account;
66
67   account = [self valueForKey:@"primaryIMAP4AccountString"];
68   
69   identity = [[[SOGoMailIdentity alloc] init] autorelease];
70   [identity setName: [self cn]];
71   [identity setEmail: [self primaryEmail]];
72   [identity setSentFolderName:[self agenorSentFolderForAccount:account]];
73   return identity;
74 }
75
76 - (SOGoMailIdentity *)mailIdentityForAccount:(NSString *)_account
77   emitter:(NSString *)_em
78 {
79   SOGoMailIdentity *identity;
80   
81   identity = [[[SOGoMailIdentity alloc] init] autorelease];
82   [identity setName:[self cn]]; // TODO: should we use something else?
83   if ([_em isNotEmpty]) [identity setEmail:_em];
84   [identity setSentFolderName:[self agenorSentFolderForAccount:_account]];
85   return identity;
86 }
87
88 - (NSArray *)fetchAllMailIdentitiesWithOnlyEmitterAccess:(BOOL)_onlyGC {
89   NSMutableArray *identities;
90   NSEnumerator *accounts;
91   NSDictionary *shares;
92   NSString  *account;
93   id        identity;
94   
95   identity = [self primaryMailIdentity];
96   shares = [self valueForKey:@"additionalIMAP4AccountsAndEMails"];
97   if ([shares count] == 0)
98     return [NSArray arrayWithObject: identity];
99   
100   identities = [NSMutableArray arrayWithCapacity:[shares count] + 1];
101   if (identity != nil) [identities addObject:identity];
102   
103   accounts = [shares keyEnumerator];
104   while ((account = [accounts nextObject]) != nil) {
105     NSString *emitter;
106     
107     emitter = [shares objectForKey:account];
108     if (_onlyGC && ![emitter isNotNull]) continue;
109     
110     identity = [self mailIdentityForAccount:account emitter:emitter];
111     if (identity != nil)
112       [identities addObject:identity];
113   }
114   
115   return identities;
116 }
117
118 - (SOGoMailIdentity *)primaryMailIdentityForAccount:(NSString *)_account {
119   NSEnumerator *accounts;
120   NSDictionary *shares;
121   NSString  *account;
122   id        identity;
123   
124   identity = [self primaryMailIdentity];
125   shares = [self valueForKey:@"additionalIMAP4AccountsAndEMails"];
126   if ([shares count] == 0)
127     return identity;
128   
129   /* scan shares for ID */
130   accounts = [shares keyEnumerator];
131   while ((account = [accounts nextObject]) != nil) {
132     NSString *emitter;
133     
134     if (![account isEqualToString:_account])
135       continue;
136     
137     emitter  = [shares objectForKey:account];
138     identity = [self mailIdentityForAccount:_account emitter:emitter];
139     if ([identity isNotNull])
140       return identity;
141   }
142   
143   return identity;
144 }
145
146 @end /* SOGoUser(Mail) */