]> err.no Git - scalable-opengroupware.org/blob - UI/MailerUI/UIxMailAccountActions.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1275 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / MailerUI / UIxMailAccountActions.m
1 /* UIxMailAccountActions.m - this file is part of SOGo
2  *
3  * Copyright (C) 2007 Inverse groupe conseil
4  *
5  * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6  *
7  * This file is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This file is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSDictionary.h>
25 #import <Foundation/NSEnumerator.h>
26
27 #import <NGObjWeb/WOContext+SoObjects.h>
28 #import <NGObjWeb/WORequest.h>
29 #import <NGObjWeb/WOResponse.h>
30 #import <NGImap4/NGImap4Connection.h>
31
32 #import <SoObjects/Mailer/SOGoMailAccount.h>
33 #import <SoObjects/Mailer/SOGoDraftObject.h>
34 #import <SoObjects/Mailer/SOGoDraftsFolder.h>
35 #import <SoObjects/SOGo/NSArray+Utilities.h>
36 #import <SoObjects/SOGo/NSObject+Utilities.h>
37 #import <SoObjects/SOGo/NSString+Utilities.h>
38 #import <SoObjects/SOGo/SOGoUser.h>
39
40 #import "../Common/WODirectAction+SOGo.h"
41
42 #import "UIxMailAccountActions.h"
43
44 @implementation UIxMailAccountActions
45
46 - (id) init
47 {
48   if ((self = [super init]))
49     {
50       inboxFolderName = nil;
51       draftsFolderName = nil;
52       sentFolderName = nil;
53       trashFolderName = nil;
54     }
55
56   return self;
57 }
58
59 - (void) dealloc
60 {
61   [inboxFolderName release];
62   [draftsFolderName release];
63   [sentFolderName release];
64   [trashFolderName release];
65   [super dealloc];
66 }
67
68 - (NSString *) _folderType: (NSString *) folderName
69 {
70   NSString *folderType;
71   SOGoMailAccount *co;
72   NSArray *specialFolders;
73
74   if (!inboxFolderName)
75     {
76       co = [self clientObject];
77       specialFolders = [[NSArray arrayWithObjects:
78                                    [co inboxFolderNameInContext: context],
79                                  [co draftsFolderNameInContext: context],
80                                  [co sentFolderNameInContext: context],
81                                  [co trashFolderNameInContext: context],
82                                  nil] stringsWithFormat: @"/%@"];
83       ASSIGN (inboxFolderName, [specialFolders objectAtIndex: 0]);
84       ASSIGN (draftsFolderName, [specialFolders objectAtIndex: 1]);
85       ASSIGN (sentFolderName, [specialFolders objectAtIndex: 2]);
86       ASSIGN (trashFolderName, [specialFolders objectAtIndex: 3]);
87     }
88
89   if ([folderName isEqualToString: inboxFolderName])
90     folderType = @"inbox";
91   else if ([folderName isEqualToString: draftsFolderName])
92     folderType = @"draft";
93   else if ([folderName isEqualToString: sentFolderName])
94     folderType = @"sent";
95   else if ([folderName isEqualToString: trashFolderName])
96     folderType = @"trash";
97   else
98     folderType = @"folder";
99
100   return folderType;
101 }
102
103 - (NSArray *) _jsonFolders: (NSEnumerator *) rawFolders
104 {
105   NSMutableArray *folders;
106   NSString *currentFolder;
107   NSDictionary *folderData;
108
109   folders = [NSMutableArray array];
110   while ((currentFolder = [rawFolders nextObject]))
111     {
112       folderData = [NSDictionary dictionaryWithObjectsAndKeys:
113                                    currentFolder, @"path",
114                                  [self _folderType: currentFolder], @"type",
115                                  nil];
116       [folders addObject: folderData];
117     }
118
119   return folders;
120 }
121
122 - (WOResponse *) listMailboxesAction
123 {
124   SOGoMailAccount *co;
125   NSEnumerator *rawFolders;
126   NSArray *folders;
127   WOResponse *response;
128
129   co = [self clientObject];
130
131   rawFolders = [[co allFolderPaths] objectEnumerator];
132   folders = [self _jsonFolders: rawFolders];
133
134   response = [self responseWithStatus: 200];
135   [response setHeader: @"text/plain; charset=utf-8"
136             forKey: @"content-type"];
137   [response appendContentString: [folders jsonRepresentation]];
138
139   return response;
140 }
141
142 /* compose */
143
144 - (WOResponse *) composeAction
145 {
146   SOGoDraftsFolder *drafts;
147   SOGoDraftObject *newDraftMessage;
148   NSString *urlBase, *url, *value, *signature;
149   NSArray *mailTo;
150   BOOL save;
151
152   drafts = [[self clientObject] draftsFolderInContext: context];
153   newDraftMessage = [drafts newDraft];
154
155   save = NO;
156
157   value = [[self request] formValueForKey: @"mailto"];
158   if ([value length] > 0)
159     {
160       mailTo = [NSArray arrayWithObject: value];
161       [newDraftMessage
162         setHeaders: [NSDictionary dictionaryWithObject: mailTo
163                                   forKey: @"to"]];
164       save = YES;
165     }
166
167   signature = [[context activeUser] signature];
168   if ([signature length])
169     {
170       [newDraftMessage
171         setText: [NSString stringWithFormat: @"\r\n--\r\n%@", signature]];
172       save = YES;
173     }
174   if (save)
175     [newDraftMessage storeInfo];
176
177   urlBase = [newDraftMessage baseURLInContext: context];
178   url = [urlBase composeURLWithAction: @"edit"
179                  parameters: nil
180                  andHash: NO];
181
182   return [self redirectToLocation: url];  
183 }
184
185 @end