]> err.no Git - scalable-opengroupware.org/blob - UI/MailerUI/UIxMailMainFrame.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1302 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/WOContext.h>
24 #import <NGObjWeb/WORequest.h>
25 #import <NGObjWeb/SoComponent.h>
26 #import <NGExtensions/NSString+misc.h>
27
28 #import <SoObjects/Mailer/SOGoMailObject.h>
29 #import <SoObjects/Mailer/SOGoMailAccounts.h>
30 #import <SoObjects/SOGo/NSDictionary+URL.h>
31 #import <SoObjects/SOGo/NSArray+Utilities.h>
32 #import <SoObjects/SOGo/SOGoUser.h>
33 #import <SOGoUI/UIxComponent.h>
34
35 #import "UIxMailMainFrame.h"
36
37 @implementation UIxMailMainFrame
38
39 - (void) _setupContext
40 {
41   SOGoUser *activeUser;
42   NSString *login, *module;
43   SOGoMailAccounts *clientObject;
44
45   activeUser = [context activeUser];
46   login = [activeUser login];
47   clientObject = [self clientObject];
48
49   module = [clientObject nameInContainer];
50
51   ud = [activeUser userSettings];
52   moduleSettings = [ud objectForKey: module];
53   if (!moduleSettings)
54     {
55       moduleSettings = [NSMutableDictionary new];
56       [moduleSettings autorelease];
57     }
58   [ud setObject: moduleSettings forKey: module];
59 }
60
61 /* accessors */
62 - (NSString *) mailAccounts
63 {
64   NSArray *accounts, *accountNames;
65
66   accounts = [[context activeUser] mailAccounts];
67   accountNames = [accounts objectsForKey: @"name"];
68
69   return [accountNames jsonRepresentation];
70 }
71
72 - (NSString *) pageFormURL
73 {
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 - (id <WOActionResults>) composeAction
118 {
119   NSArray *accounts;
120   NSString *firstAccount, *newLocation;
121   SOGoMailAccounts *co;
122   NSDictionary *formValues;
123
124   co = [self clientObject];
125   accounts = [[context activeUser] mailAccounts];
126   firstAccount = [[accounts objectsForKey: @"name"] objectAtIndex: 0];
127   formValues = [[context request] formValues];
128   newLocation = [NSString stringWithFormat: @"%@/%@/compose%@",
129                           [co baseURLInContext: context],
130                           firstAccount,
131                           [formValues asURLParameters]];
132
133   return [self redirectToLocation: newLocation];
134 }
135
136 - (WOResponse *) getFoldersStateAction
137 {
138   NSString *expandedFolders;
139
140   [self _setupContext];
141   expandedFolders = [moduleSettings objectForKey: @"ExpandedFolders"];
142
143   return [self responseWithStatus: 200 andString: expandedFolders];
144 }
145
146 - (WOResponse *) saveFoldersStateAction
147 {
148   WORequest *request;
149   NSString *expandedFolders;
150   
151   [self _setupContext];
152   request = [context request];
153   expandedFolders = [request formValueForKey: @"expandedFolders"];
154
155   [moduleSettings setObject: expandedFolders
156                   forKey: @"ExpandedFolders"];
157
158   [ud synchronize];
159
160   return [self responseWithStatus: 204];
161 }
162
163 @end /* UIxMailMainFrame */