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