]> err.no Git - scalable-opengroupware.org/blob - UI/MailerUI/UIxMailMainFrame.m
400e3280208d056f1058313365f14d8748187457
[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
24 #import <NGObjWeb/WOContext.h>
25 #import <NGObjWeb/WORequest.h>
26 #import <NGObjWeb/WOResponse.h>
27 #import <NGObjWeb/SoComponent.h>
28 #import <NGExtensions/NSString+misc.h>
29
30 #import <SoObjects/Mailer/SOGoMailObject.h>
31 #import <SoObjects/Mailer/SOGoMailAccounts.h>
32 #import <SoObjects/SOGo/NSDictionary+URL.h>
33 #import <SoObjects/SOGo/NSArray+Utilities.h>
34 #import <SoObjects/SOGo/SOGoUser.h>
35 #import <SOGoUI/UIxComponent.h>
36
37 #import "UIxMailMainFrame.h"
38
39 @implementation UIxMailMainFrame
40
41 - (void) _setupContext
42 {
43   SOGoUser *activeUser;
44   NSString *login, *module;
45   SOGoMailAccounts *clientObject;
46
47   activeUser = [context activeUser];
48   login = [activeUser login];
49   clientObject = [self clientObject];
50
51   module = [clientObject nameInContainer];
52
53   ud = [activeUser userSettings];
54   moduleSettings = [ud objectForKey: module];
55   if (!moduleSettings)
56     {
57       moduleSettings = [NSMutableDictionary new];
58       [moduleSettings autorelease];
59     }
60   [ud setObject: moduleSettings forKey: module];
61 }
62
63 /* accessors */
64 - (NSString *) mailAccounts
65 {
66   NSArray *accounts, *accountNames;
67
68   accounts = [[context activeUser] mailAccounts];
69   accountNames = [accounts objectsForKey: @"name"];
70
71   return [accountNames jsonRepresentation];
72 }
73
74 - (NSString *) pageFormURL
75 {
76   NSString *u;
77   NSRange  r;
78   
79   u = [[[self context] request] uri];
80   if ((r = [u rangeOfString:@"?"]).length > 0) {
81     /* has query parameters */
82     // TODO: this is ugly, create reusable link facility in SOPE
83     // TODO: remove 'search' and 'filterpopup', preserve sorting
84     NSMutableString *ms;
85     NSArray  *qp;
86     unsigned i, count;
87     
88     qp    = [[u substringFromIndex:(r.location + r.length)] 
89                 componentsSeparatedByString:@"&"];
90     count = [qp count];
91     ms    = [NSMutableString stringWithCapacity:count * 12];
92     
93     for (i = 0; i < count; i++) {
94       NSString *s;
95       
96       s = [qp objectAtIndex:i];
97       
98       /* filter out */
99       if ([s hasPrefix:@"search="]) continue;
100       if ([s hasPrefix:@"filterpopup="]) continue;
101       
102       if ([ms length] > 0) [ms appendString:@"&"];
103       [ms appendString:s];
104     }
105     
106     if ([ms length] == 0) {
107       /* no other query params */
108       u = [u substringToIndex:r.location];
109     }
110     else {
111       u = [u substringToIndex:r.location + r.length];
112       u = [u stringByAppendingString:ms];
113     }
114     return u;
115   }
116   return [u hasSuffix:@"/"] ? @"view" : @"#";
117 }
118
119 - (id <WOActionResults>) composeAction
120 {
121   NSArray *accounts;
122   NSString *firstAccount, *newLocation;
123   SOGoMailAccounts *co;
124   NSDictionary *formValues;
125
126   co = [self clientObject];
127   accounts = [[context activeUser] mailAccounts];
128   firstAccount = [[accounts objectsForKey: @"name"] objectAtIndex: 0];
129   formValues = [[context request] formValues];
130   newLocation = [NSString stringWithFormat: @"%@/%@/compose%@",
131                           [co baseURLInContext: context],
132                           firstAccount,
133                           [formValues asURLParameters]];
134
135   return [self redirectToLocation: newLocation];
136 }
137
138 - (WOResponse *) getFoldersStateAction
139 {
140   NSString *expandedFolders;
141
142   [self _setupContext];
143   expandedFolders = [moduleSettings objectForKey: @"ExpandedFolders"];
144
145   return [self responseWithStatus: 200 andString: expandedFolders];
146 }
147
148 - (WOResponse *) saveFoldersStateAction
149 {
150   WORequest *request;
151   NSString *expandedFolders;
152   
153   [self _setupContext];
154   request = [context request];
155   expandedFolders = [request formValueForKey: @"expandedFolders"];
156
157   [moduleSettings setObject: expandedFolders
158                   forKey: @"ExpandedFolders"];
159
160   [ud synchronize];
161
162   return [self responseWithStatus: 204];
163 }
164
165 @end /* UIxMailMainFrame */