]> err.no Git - scalable-opengroupware.org/blob - UI/MailerUI/UIxMailAccountActions.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1238 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.h>
28 #import <NGObjWeb/WORequest.h>
29 #import <NGObjWeb/WOResponse.h>
30 #import <NGImap4/NGImap4Connection.h>
31 #import <SoObjects/Mailer/SOGoMailAccount.h>
32 #import <SoObjects/Mailer/SOGoDraftObject.h>
33 #import <SoObjects/Mailer/SOGoDraftsFolder.h>
34 #import <SoObjects/SOGo/NSArray+Utilities.h>
35 #import <SoObjects/SOGo/NSObject+Utilities.h>
36 #import <SoObjects/SOGo/NSString+Utilities.h>
37
38 #import "../Common/WODirectAction+SOGo.h"
39
40 #import "UIxMailAccountActions.h"
41
42 @implementation UIxMailAccountActions
43
44 - (id) init
45 {
46   if ((self = [super init]))
47     {
48       inboxFolderName = nil;
49       draftsFolderName = nil;
50       sentFolderName = nil;
51       trashFolderName = nil;
52     }
53
54   return self;
55 }
56
57 - (void) dealloc
58 {
59   [inboxFolderName release];
60   [draftsFolderName release];
61   [sentFolderName release];
62   [trashFolderName release];
63   [super dealloc];
64 }
65
66 - (NSString *) _folderType: (NSString *) folderName
67 {
68   NSString *folderType;
69   SOGoMailAccount *co;
70   NSArray *specialFolders;
71
72   if (!inboxFolderName)
73     {
74       co = [self clientObject];
75       specialFolders = [[NSArray arrayWithObjects:
76                                    [co inboxFolderNameInContext: context],
77                                  [co draftsFolderNameInContext: context],
78                                  [co sentFolderNameInContext: context],
79                                  [co trashFolderNameInContext: context],
80                                  nil] stringsWithFormat: @"/%@"];
81       ASSIGN (inboxFolderName, [specialFolders objectAtIndex: 0]);
82       ASSIGN (draftsFolderName, [specialFolders objectAtIndex: 1]);
83       ASSIGN (sentFolderName, [specialFolders objectAtIndex: 2]);
84       ASSIGN (trashFolderName, [specialFolders objectAtIndex: 3]);
85     }
86
87   if ([folderName isEqualToString: inboxFolderName])
88     folderType = @"inbox";
89   else if ([folderName isEqualToString: draftsFolderName])
90     folderType = @"draft";
91   else if ([folderName isEqualToString: sentFolderName])
92     folderType = @"sent";
93   else if ([folderName isEqualToString: trashFolderName])
94     folderType = @"trash";
95   else
96     folderType = @"folder";
97
98   return folderType;
99 }
100
101 - (NSArray *) _jsonFolders: (NSEnumerator *) rawFolders
102 {
103   NSMutableArray *folders;
104   NSString *currentFolder;
105   NSDictionary *folderData;
106
107   folders = [NSMutableArray array];
108   while ((currentFolder = [rawFolders nextObject]))
109     {
110       folderData = [NSDictionary dictionaryWithObjectsAndKeys:
111                                    currentFolder, @"path",
112                                  [self _folderType: currentFolder], @"type",
113                                  nil];
114       [folders addObject: folderData];
115     }
116
117   return folders;
118 }
119
120 - (WOResponse *) listMailboxesAction
121 {
122   SOGoMailAccount *co;
123   NSEnumerator *rawFolders;
124   NSArray *folders;
125   WOResponse *response;
126
127   co = [self clientObject];
128
129   rawFolders = [[co allFolderPaths] objectEnumerator];
130   folders = [self _jsonFolders: rawFolders];
131
132   response = [self responseWithStatus: 200];
133   [response setHeader: @"text/plain; charset=utf-8"
134             forKey: @"content-type"];
135   [response appendContentString: [folders jsonRepresentation]];
136
137   return response;
138 }
139
140 /* compose */
141
142 - (WOResponse *) composeAction
143 {
144   SOGoDraftsFolder *drafts;
145   SOGoDraftObject *newDraftMessage;
146   NSString *urlBase, *url, *value;
147   NSArray *mailTo;
148   
149
150   drafts = [[self clientObject] draftsFolderInContext: context];
151   newDraftMessage = [drafts newDraft];
152
153   value = [[self request] formValueForKey: @"mailto"];
154   if ([value length] > 0)
155     {
156       mailTo = [NSArray arrayWithObject: value];
157       [newDraftMessage setHeaders: [NSDictionary dictionaryWithObject: mailTo
158                                                  forKey: @"to"]];
159       [newDraftMessage storeInfo];
160     }
161
162   urlBase = [newDraftMessage baseURLInContext: context];
163   url = [urlBase composeURLWithAction: @"edit"
164                  parameters: nil
165                  andHash: NO];
166
167   return [self redirectToLocation: url];  
168 }
169
170 @end