]> err.no Git - scalable-opengroupware.org/blob - UI/MailerUI/UIxMailMainFrame.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1131 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / MailerUI / UIxMailMainFrame.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/NSUserDefaults.h>
23 #import <NGObjWeb/WORequest.h>
24 #import <NGObjWeb/SoComponent.h>
25 #import <NGExtensions/NSString+misc.h>
26
27 #import <SoObjects/Mailer/SOGoMailObject.h>
28 #import <SoObjects/Mailer/SOGoMailAccounts.h>
29 #import <SoObjects/SOGo/NSString+Utilities.h>
30 #import <SoObjects/SOGo/NSObject+Utilities.h>
31 #import <SOGoUI/UIxComponent.h>
32
33 #import "UIxMailMainFrame.h"
34
35 @implementation UIxMailMainFrame
36
37 static NSString *treeRootClassName = nil;
38
39 + (void)initialize {
40   NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
41   
42   treeRootClassName = [[ud stringForKey:@"SOGoMailTreeRootClass"] copy];
43   if (treeRootClassName)
44     NSLog(@"Note: use class '%@' as root for mail tree.", treeRootClassName);
45   else
46     treeRootClassName = @"SOGoMailAccounts";
47 }
48
49 - (void)dealloc {
50   [self->rootURL     release];
51   [self->userRootURL release];
52   [super dealloc];
53 }
54
55 /* accessors */
56 - (NSString *) mailAccounts
57 {
58   SOGoMailAccounts *co;
59
60   co = [self clientObject];
61
62   return [[co fetchAllIdentities] jsonRepresentation];
63 }
64
65 - (NSString *)treeRootClassName {
66   return treeRootClassName;
67 }
68
69 - (void)setHideFolderTree:(BOOL)_flag {
70    self->mmfFlags.hideFolderTree = _flag ? 1 : 0;
71 }
72 - (BOOL)hideFolderTree {
73   return self->mmfFlags.hideFolderTree ? YES : NO;
74 }
75
76 - (NSString *) pageFormURL {
77   NSString *u;
78   NSRange  r;
79   
80   u = [[[self context] request] uri];
81   if ((r = [u rangeOfString:@"?"]).length > 0) {
82     /* has query parameters */
83     // TODO: this is ugly, create reusable link facility in SOPE
84     // TODO: remove 'search' and 'filterpopup', preserve sorting
85     NSMutableString *ms;
86     NSArray  *qp;
87     unsigned i, count;
88     
89     qp    = [[u substringFromIndex:(r.location + r.length)] 
90                 componentsSeparatedByString:@"&"];
91     count = [qp count];
92     ms    = [NSMutableString stringWithCapacity:count * 12];
93     
94     for (i = 0; i < count; i++) {
95       NSString *s;
96       
97       s = [qp objectAtIndex:i];
98       
99       /* filter out */
100       if ([s hasPrefix:@"search="]) continue;
101       if ([s hasPrefix:@"filterpopup="]) continue;
102       
103       if ([ms length] > 0) [ms appendString:@"&"];
104       [ms appendString:s];
105     }
106     
107     if ([ms length] == 0) {
108       /* no other query params */
109       u = [u substringToIndex:r.location];
110     }
111     else {
112       u = [u substringToIndex:r.location + r.length];
113       u = [u stringByAppendingString:ms];
114     }
115     return u;
116   }
117   return [u hasSuffix:@"/"] ? @"view" : @"#";
118 }
119
120 - (BOOL)showLinkBanner {
121   return YES;
122 }
123
124 - (NSString *)bannerToolbarStyle {
125   return nil;
126 }
127
128 - (NSString *)bannerConsumeStyle {
129   return nil;
130 }
131
132 /* URL generation */
133 // TODO: I think all this should be done by the clientObject?!
134 // TODO: is the stuff below necessary at all in the mailer frame?
135
136 - (NSString *)rootURL {
137   WOContext *ctx;
138   NSArray   *traversalObjects;
139
140   if (self->rootURL != nil)
141     return self->rootURL;
142
143   ctx = [self context];
144   traversalObjects = [ctx objectTraversalStack];
145   self->rootURL = [[[traversalObjects objectAtIndex:0]
146                                       rootURLInContext:ctx]
147                                       copy];
148   return self->rootURL;
149 }
150
151 - (NSString *)userRootURL {
152   WOContext *ctx;
153   NSArray   *traversalObjects;
154
155   if (self->userRootURL)
156     return self->userRootURL;
157
158   ctx = [self context];
159   traversalObjects = [ctx objectTraversalStack];
160   self->userRootURL = [[[[traversalObjects objectAtIndex:1]
161                                            baseURLInContext:ctx]
162                                            stringByAppendingString:@"/"]
163                                            retain];
164   return self->userRootURL;
165 }
166
167 - (NSString *)calendarRootURL {
168   return [[self userRootURL] stringByAppendingString:@"Calendar/"];
169 }
170
171 - (NSString *)contactsRootURL {
172   return [[self userRootURL] stringByAppendingString:@"Contacts/"];
173 }
174
175 /* error handling */
176
177 - (BOOL)hasErrorText {
178   return [[[[self context] request] formValueForKey:@"error"] length] > 0
179     ? YES : NO;
180 }
181 - (NSString *)errorText {
182   return [[[self context] request] formValueForKey:@"error"];
183 }
184
185 - (NSString *)errorAlertJavaScript {
186   NSString *errorText;
187   
188   if ([(errorText = [self errorText]) length] == 0)
189     return nil;
190   
191   // TODO: proper JavaScript escaping
192   errorText = [errorText stringByEscapingHTMLString];
193   errorText = [errorText stringByReplacingString:@"\"" withString:@"'"];
194   
195   return [NSString stringWithFormat:
196                      @"<script language=\"JavaScript\">"
197                      @"alert(\"%@\");"
198                      @"</script>", errorText];
199 }
200
201 /* FIXME: migrated methods which might not work yet... */
202 // #warning check this
203 // - (NSString *) mailFolderName
204 // {
205 //   NSMutableArray *mailboxes;
206 //   SOGoMailObject *currentObject;
207
208 //   mailboxes = [NSMutableArray new];
209 //   [mailboxes autorelease];
210
211 //   currentObject = [self clientObject];
212 //   while (![currentObject isKindOfClass: [SOGoMailAccounts class]])
213 //     {
214 //       [mailboxes insertObject: [currentObject nameInContainer] atIndex: 0];
215 //       currentObject = [currentObject container];
216 //     }
217
218 //   return [NSString stringWithFormat: @"/%@",
219 //                    [mailboxes componentsJoinedByString: @"/"]];
220 // }
221
222 - (id) composeAction
223 {
224   NSArray *c;
225   NSString *inbox, *url, *parameter;
226   NSMutableDictionary *urlParams;
227   id actionResult;
228
229   c = [[self clientObject] toManyRelationshipKeys];
230   if ([c count] > 0)
231     {
232       urlParams = [NSMutableDictionary new];
233       [urlParams autorelease];
234
235       parameter = [self queryParameterForKey: @"mailto"];
236       if (parameter)
237         [urlParams setObject: parameter
238                    forKey: @"mailto"];
239       inbox = [NSString stringWithFormat: @"%@/folderINBOX",
240                         [c objectAtIndex: 0]];
241       url = [inbox composeURLWithAction: @"compose"
242                    parameters: urlParams
243                    andHash: NO];
244       actionResult = [self redirectToLocation: url];
245     }
246   else
247     actionResult = self;
248
249   return actionResult;
250 }
251
252 @end /* UIxMailMainFrame */