2 Copyright (C) 2004-2005 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
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
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.
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
22 #include <SOGoUI/UIxComponent.h>
27 An mail editor component which works on SOGoDraftObject's. This component
28 manages the attachments of a draft object.
31 @class NSArray, NSString, NSData;
33 @interface UIxMailEditorAttach : UIxComponent
41 NSString *attachmentName;
43 NSArray *attachmentNames;
48 #include <SoObjects/Mailer/SOGoDraftObject.h>
51 @implementation UIxMailEditorAttach
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];
67 - (void)setAttachmentName:(NSString *)_value {
68 ASSIGNCOPY(self->attachmentName, _value);
70 - (NSString *)attachmentName {
71 return self->attachmentName;
74 - (void)setFilePath1:(NSString *)_value {
75 ASSIGNCOPY(self->filePath1, _value);
77 - (NSString *)filePath1 {
78 return self->filePath1;
81 - (void)setFilePath2:(NSString *)_value {
82 ASSIGNCOPY(self->filePath2, _value);
84 - (NSString *)filePath2 {
85 return self->filePath2;
88 - (void)setFilePath3:(NSString *)_value {
89 ASSIGNCOPY(self->filePath3, _value);
91 - (NSString *)filePath3 {
92 return self->filePath3;
95 - (void)setFileData1:(NSData *)_data {
96 ASSIGN(self->fileData1, _data);
98 - (NSData *)fileData1 {
99 return self->fileData1;
102 - (void)setFileData2:(NSData *)_data {
103 ASSIGN(self->fileData2, _data);
105 - (NSData *)fileData2 {
106 return self->fileData2;
109 - (void)setFileData3:(NSData *)_data {
110 ASSIGN(self->fileData3, _data);
112 - (NSData *)fileData3 {
113 return self->fileData3;
116 - (NSArray *)attachmentNames {
119 if (self->attachmentNames != nil)
120 return self->attachmentNames;
122 a = [[self clientObject] fetchAttachmentNames];
123 a = [a sortedArrayUsingSelector:@selector(compare:)];
124 self->attachmentNames = [a copy];
125 return self->attachmentNames;
127 - (BOOL)hasAttachments {
128 return [[self attachmentNames] count] > 0 ? YES : NO;
133 - (BOOL)shouldTakeValuesFromRequest:(WORequest *)_rq inContext:(WOContext*)_c{
139 - (NSString *)defaultPathExtension {
143 - (NSString *)newAttachmentName {
147 usedNames = [[self clientObject] fetchAttachmentNames];
148 for (i = [usedNames count]; i < 100; i++) {
151 name = [NSString stringWithFormat:@"attachment%d", i];
152 if (![usedNames containsObject:name])
155 [self errorWithFormat:@"too many attachments?!"];
159 - (NSString *)fixupAttachmentName:(NSString *)_name {
166 pe = [_name pathExtension];
167 if ([pe length] == 0)
168 /* would be better to check the content-type, but well */
169 pe = [self defaultPathExtension];
171 r = [_name rangeOfString:@"/"];
172 if (r.length > 0) _name = [_name lastPathComponent];
174 r = [_name rangeOfString:@" "];
176 _name = [_name stringByReplacingString:@" " withString:@"_"];
178 if ([_name hasPrefix:@"."]) {
179 _name = [@"dotfile-" stringByAppendingString:
180 [_name substringFromIndex:1]];
183 // TODO: should we need to check for umlauts?
185 if ([_name length] == 0)
186 return [[self newAttachmentName] stringByAppendingPathExtension:pe];
191 - (BOOL)saveFileData:(NSData *)_data name:(NSString *)_name {
196 if ([_name length] == 0) {
197 _name = [self newAttachmentName];
198 _name = [_name stringByAppendingPathExtension:[self defaultPathExtension]];
201 if ((_name = [self fixupAttachmentName:_name]) == nil)
204 // TODO: add size limit?
205 error = [[self clientObject] saveAttachment:_data withName:_name];
207 [self logWithFormat:@"ERROR: could not save: %@", error];
215 - (id)viewAttachmentsAction {
216 [self debugWithFormat:@"view attachments ..."];
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]];
232 // TODO: improve error handling
233 return [NSException exceptionWithHTTPStatus:500 /* server error */
234 reason:@"failed to save attachment ..."];
237 return [self redirectToLocation:@"viewAttachments"];
240 - (id)deleteAttachmentAction {
243 error = [[self clientObject] deleteAttachmentWithName:[self attachmentName]];
248 return [self redirectToLocation:@"viewAttachments"];
251 @end /* UIxMailEditorAttach */