]> err.no Git - scalable-opengroupware.org/blob - UI/MailerUI/UIxMailAccountActions.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1206 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/NSObject+Utilities.h>
35 #import <SoObjects/SOGo/NSString+Utilities.h>
36
37 #import "../Common/WODirectAction+SOGo.h"
38
39 #import "UIxMailAccountActions.h"
40
41 @implementation UIxMailAccountActions
42
43 - (NSString *) _folderType: (NSString *) baseName
44 {
45   NSString *folderType;
46
47   if ([baseName isEqualToString: @"INBOX"])
48     folderType = @"inbox";
49   else if ([baseName isEqualToString: draftFolderName])
50     folderType = @"draft";
51   else if ([baseName isEqualToString: sentFolderName])
52     folderType = @"sent";
53   else if ([baseName isEqualToString: trashFolderName])
54     folderType = @"trash";
55   else
56     folderType = @"folder";
57
58   return folderType;
59 }
60
61 - (NSDictionary *) _lineForFolder: (NSString *) folder
62 {
63   NSArray *parts;
64   NSMutableDictionary *folderData;
65   NSString *baseName;
66
67   folderData = [NSMutableDictionary dictionary];
68   parts = [folder componentsSeparatedByString: @"/"];
69   baseName = [parts lastObject];
70   [folderData setObject: folder forKey: @"path"];
71   [folderData setObject: [self _folderType: baseName]
72               forKey: @"type"];
73
74   return folderData;
75 }
76
77 - (NSArray *) _jsonFolders: (NSEnumerator *) rawFolders
78 {
79   NSMutableArray *folders;
80   NSString *currentFolder;
81
82   folders = [NSMutableArray array];
83
84   currentFolder = [rawFolders nextObject];
85   while (currentFolder)
86     {
87       [folders addObject: [self _lineForFolder: currentFolder]];
88       currentFolder = [rawFolders nextObject];
89     }
90
91   return folders;
92 }
93
94 - (WOResponse *) listMailboxesAction
95 {
96   SOGoMailAccount *co;
97   NSArray *rawFolders, *folders;
98   WOResponse *response;
99
100   co = [self clientObject];
101   draftFolderName = [[co draftsFolderNameInContext: context]
102                       substringFromIndex: 6];
103   sentFolderName = [[co sentFolderNameInContext: context]
104                      substringFromIndex: 6];
105   trashFolderName = [[co trashFolderNameInContext: context]
106                       substringFromIndex: 6];
107
108   rawFolders = [co allFolderPaths];
109   folders = [self _jsonFolders: [rawFolders objectEnumerator]];
110
111   response = [self responseWithStatus: 200];
112   [response setHeader: @"text/plain; charset=utf-8"
113             forKey: @"content-type"];
114   [response appendContentString: [folders jsonRepresentation]];
115
116   return response;
117 }
118
119 /* compose */
120
121 - (WOResponse *) composeAction
122 {
123   SOGoDraftsFolder *drafts;
124   SOGoDraftObject *newDraftMessage;
125   NSString *urlBase, *url, *value;
126   NSArray *mailTo;
127   
128
129   drafts = [[self clientObject] draftsFolderInContext: context];
130   newDraftMessage = [drafts newDraft];
131
132   value = [[self request] formValueForKey: @"mailto"];
133   if ([value length] > 0)
134     {
135       mailTo = [NSArray arrayWithObject: value];
136       [newDraftMessage setHeaders: [NSDictionary dictionaryWithObject: mailTo
137                                                  forKey: @"to"]];
138       [newDraftMessage storeInfo];
139     }
140
141   urlBase = [newDraftMessage baseURLInContext: context];
142   url = [urlBase composeURLWithAction: @"edit"
143                  parameters: nil
144                  andHash: NO];
145
146   return [self redirectToLocation: url];  
147 }
148
149 @end