]> err.no Git - scalable-opengroupware.org/blob - SOGo/SoObjects/Mailer/SOGoMailBaseObject.m
fixed SOGo bug #1054, deactivated some unused toolbar buttons
[scalable-opengroupware.org] / SOGo / SoObjects / Mailer / SOGoMailBaseObject.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 #include "SOGoMailBaseObject.h"
23 #include "SOGoMailManager.h"
24 #include "common.h"
25 #include <NGObjWeb/SoObject+SoDAV.h>
26 #include <NGObjWeb/SoHTTPAuthenticator.h>
27 #include <NGExtensions/NSURL+misc.h>
28
29 @implementation SOGoMailBaseObject
30
31 #if 0
32 static BOOL debugOn = YES;
33 #endif
34
35 - (id)initWithImap4URL:(NSURL *)_url inContainer:(id)_container {
36   NSString *n;
37   
38   n = [[_url path] lastPathComponent];
39   if ((self = [self initWithName:n inContainer:_container])) {
40     self->imap4URL = [_url retain];
41   }
42   return self;
43 }
44
45 - (void)dealloc {
46   [self->imap4URL release];
47   [super dealloc];
48 }
49
50 /* hierarchy */
51
52 - (SOGoMailAccount *)mailAccountFolder {
53   if (![[self container] respondsToSelector:_cmd]) {
54     [self warnWithFormat:@"weird container of mailfolder: %@",
55             [self container]];
56     return nil;
57   }
58   
59   return [[self container] mailAccountFolder];
60 }
61
62 /* IMAP4 */
63
64 - (SOGoMailManager *)mailManager {
65   return [SOGoMailManager defaultMailManager];
66 }
67
68 - (NSString *)relativeImap4Name {
69   [self warnWithFormat:@"subclass should override %@", 
70           NSStringFromSelector(_cmd)];
71   return nil;
72 }
73 - (NSURL *)baseImap4URL {
74   if (![[self container] respondsToSelector:@selector(imap4URL)]) {
75     [self warnWithFormat:@"container does not implement -imap4URL!"];
76     return nil;
77   }
78   
79   return [[self container] imap4URL];
80 }
81 - (NSURL *)imap4URL {
82   NSString *sn;
83   NSURL    *base;
84   
85   if (self->imap4URL != nil) 
86     return self->imap4URL;
87   
88   if ((sn = [self relativeImap4Name]) == nil)
89     return nil;
90   
91   if (![[self container] respondsToSelector:_cmd]) {
92     [self warnWithFormat:@"container does not implement -imap4URL!"];
93     return nil;
94   }
95   
96   if ((base = [self baseImap4URL]) == nil)
97     return nil;
98   
99   sn = [[base path] stringByAppendingPathComponent:sn];
100   self->imap4URL = [[NSURL alloc] initWithString:sn relativeToURL:base];
101   return self->imap4URL;
102 }
103
104 - (NSString *)imap4FolderName {
105   return [[self mailManager] imap4FolderNameForURL:[self imap4URL]];
106 }
107
108 - (NSString *)imap4Password {
109   /*
110     Extract password from basic authentication.
111     
112     TODO: we might want to
113     a) move the primary code to SOGoMailAccount
114     b) cache the password
115   */
116   WORequest *rq;
117   NSString  *auth;
118   NSArray   *creds;
119   
120   rq = [[(WOApplication *)[WOApplication application] context] request];
121   if ((auth = [rq headerForKey:@"authorization"]) == nil) {
122     /* no basic auth */
123     return nil;
124   }
125   
126   creds = [SoHTTPAuthenticator parseCredentials:auth];
127   if ([creds count] < 2)
128     /* somehow invalid */
129     return nil;
130   
131   return [creds objectAtIndex:1]; /* the password */
132 }
133
134 - (NGImap4Client *)imap4ClientForURL:(NSURL *)_url password:(NSString *)_pwd {
135   return [[self mailManager] imap4ClientForURL:_url password:_pwd];
136 }
137
138 - (NGImap4Client *)imap4Client {
139   return [self imap4ClientForURL:[self imap4URL]
140                password:[self imap4Password]];
141 }
142
143 - (void)flushMailCaches {
144   [[self mailManager] flushCachesForURL:[self imap4URL]];
145 }
146
147 /* debugging */
148
149 - (NSString *)loggingPrefix {
150   /* improve perf ... */
151   return [NSString stringWithFormat:@"<0x%08X[%@]:%@>",
152                      self, NSStringFromClass([self class]),
153                      [self nameInContainer]];
154 }
155 @end /* SOGoMailBaseObject */