]> err.no Git - scalable-opengroupware.org/blob - UI/MailerUI/UIxMailEditorAttach.m
moved SOGo files up
[scalable-opengroupware.org] / UI / MailerUI / UIxMailEditorAttach.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 #include <SOGoUI/UIxComponent.h>
23
24 /*
25   UIxMailEditorAction
26   
27   An mail editor component which works on SOGoDraftObject's. This component
28   manages the attachments of a draft object.
29 */
30
31 @class NSArray, NSString, NSData;
32
33 @interface UIxMailEditorAttach : UIxComponent
34 {
35   NSString *filePath1;
36   NSString *filePath2;
37   NSString *filePath3;
38   NSData   *fileData1;
39   NSData   *fileData2;
40   NSData   *fileData3;
41   NSString *attachmentName;
42
43   NSArray *attachmentNames;
44 }
45
46 @end
47
48 #include <SoObjects/Mailer/SOGoDraftObject.h>
49 #include "common.h"
50
51 @implementation UIxMailEditorAttach
52
53 - (void)dealloc {
54   [self->attachmentNames release];
55   [self->attachmentName release];
56   [self->filePath1 release];
57   [self->filePath2 release];
58   [self->filePath3 release];
59   [self->fileData1 release];
60   [self->fileData2 release];
61   [self->fileData3 release];
62   [super dealloc];
63 }
64
65 /* accessors */
66
67 - (void)setAttachmentName:(NSString *)_value {
68   ASSIGNCOPY(self->attachmentName, _value);
69 }
70 - (NSString *)attachmentName {
71   return self->attachmentName;
72 }
73
74 - (void)setFilePath1:(NSString *)_value {
75   ASSIGNCOPY(self->filePath1, _value);
76 }
77 - (NSString *)filePath1 {
78   return self->filePath1;
79 }
80
81 - (void)setFilePath2:(NSString *)_value {
82   ASSIGNCOPY(self->filePath2, _value);
83 }
84 - (NSString *)filePath2 {
85   return self->filePath2;
86 }
87
88 - (void)setFilePath3:(NSString *)_value {
89   ASSIGNCOPY(self->filePath3, _value);
90 }
91 - (NSString *)filePath3 {
92   return self->filePath3;
93 }
94
95 - (void)setFileData1:(NSData *)_data {
96   ASSIGN(self->fileData1, _data);
97 }
98 - (NSData *)fileData1 {
99   return self->fileData1;
100 }
101
102 - (void)setFileData2:(NSData *)_data {
103   ASSIGN(self->fileData2, _data);
104 }
105 - (NSData *)fileData2 {
106   return self->fileData2;
107 }
108
109 - (void)setFileData3:(NSData *)_data {
110   ASSIGN(self->fileData3, _data);
111 }
112 - (NSData *)fileData3 {
113   return self->fileData3;
114 }
115
116 - (NSArray *)attachmentNames {
117   NSArray *a;
118
119   if (self->attachmentNames != nil)
120     return self->attachmentNames;
121   
122   a = [[self clientObject] fetchAttachmentNames];
123   a = [a sortedArrayUsingSelector:@selector(compare:)];
124   self->attachmentNames = [a copy];
125   return self->attachmentNames;
126 }
127 - (BOOL)hasAttachments {
128   return [[self attachmentNames] count] > 0 ? YES : NO;
129 }
130
131 /* requests */
132
133 - (BOOL)shouldTakeValuesFromRequest:(WORequest *)_rq inContext:(WOContext*)_c{
134   return YES;
135 }
136
137 /* operations */
138
139 - (NSString *)defaultPathExtension {
140   return @"txt";
141 }
142
143 - (NSString *)newAttachmentName {
144   NSArray  *usedNames;
145   unsigned i;
146   
147   usedNames = [[self clientObject] fetchAttachmentNames];
148   for (i = [usedNames count]; i < 100; i++) {
149     NSString *name;
150     
151     name = [NSString stringWithFormat:@"attachment%d", i];
152     if (![usedNames containsObject:name])
153       return name;
154   }
155   [self errorWithFormat:@"too many attachments?!"];
156   return nil;
157 }
158
159 - (NSString *)fixupAttachmentName:(NSString *)_name {
160   NSString *pe;
161   NSRange r;
162
163   if (_name == nil)
164     return  nil;
165   
166   pe = [_name pathExtension];
167   if ([pe length] == 0)
168     /* would be better to check the content-type, but well */
169     pe = [self defaultPathExtension];
170   
171   r = [_name rangeOfString:@"/"];
172   if (r.length > 0) _name = [_name lastPathComponent];
173   
174   r = [_name rangeOfString:@" "];
175   if (r.length > 0) 
176     _name = [_name stringByReplacingString:@" " withString:@"_"];
177   
178   if ([_name hasPrefix:@"."]) {
179     _name = [@"dotfile-" stringByAppendingString:
180                 [_name substringFromIndex:1]];
181   }
182   
183   // TODO: should we need to check for umlauts?
184   
185   if ([_name length] == 0)
186     return [[self newAttachmentName] stringByAppendingPathExtension:pe];
187   
188   return _name;
189 }
190
191 - (BOOL)saveFileData:(NSData *)_data name:(NSString *)_name {
192   NSException *error;
193   
194   if (_data == nil)
195     return NO;
196   if ([_name length] == 0) {
197     _name = [self newAttachmentName];
198     _name = [_name stringByAppendingPathExtension:[self defaultPathExtension]];
199   }
200   
201   if ((_name = [self fixupAttachmentName:_name]) == nil)
202     return NO;
203   
204   // TODO: add size limit?
205   error = [[self clientObject] saveAttachment:_data withName:_name];
206   if (error != nil) {
207     [self logWithFormat:@"ERROR: could not save: %@", error];
208     return NO;
209   }
210   return YES;
211 }
212
213 /* actions */
214
215 - (id)viewAttachmentsAction {
216   [self debugWithFormat:@"view attachments ..."];
217   return self;
218 }
219
220 - (id)attachAction {
221   BOOL ok;
222   
223   ok = YES;
224   if ([self->fileData1 length] > 0)
225     ok = [self saveFileData:self->fileData1 name:[self filePath1]];
226   if (ok && [self->fileData2 length] > 0)
227     ok = [self saveFileData:self->fileData2 name:[self filePath2]];
228   if (ok && [self->fileData3 length] > 0)
229     [self saveFileData:self->fileData3 name:[self filePath3]];
230   
231   if (!ok) {
232     // TODO: improve error handling
233     return [NSException exceptionWithHTTPStatus:500 /* server error */
234                         reason:@"failed to save attachment ..."];
235   }
236   
237   return [self redirectToLocation:@"viewAttachments"];
238 }
239
240 - (id)deleteAttachmentAction {
241   NSException *error;
242
243   error = [[self clientObject] deleteAttachmentWithName:[self attachmentName]];
244   
245   if (error != nil)
246     return error;
247   
248   return [self redirectToLocation:@"viewAttachments"];
249 }
250
251 @end /* UIxMailEditorAttach */