From 93305ec8079b991c90d73699464ed554b1d14e4a Mon Sep 17 00:00:00 2001 From: helge Date: Tue, 26 Oct 2004 15:12:38 +0000 Subject: [PATCH] implemented attachment delete git-svn-id: http://svn.opengroupware.org/SOGo/trunk@426 d1b88da0-ebda-0310-925b-ed51d893ca5b --- SOGo/SoObjects/Mailer/ChangeLog | 4 + SOGo/SoObjects/Mailer/SOGoDraftObject.h | 1 + SOGo/SoObjects/Mailer/SOGoDraftObject.m | 15 +++ SOGo/SoObjects/Mailer/Version | 2 +- SOGo/UI/Mailer/ChangeLog | 2 + SOGo/UI/Mailer/UIxMailEditorAttach.m | 117 +++++++++++++++++++++--- SOGo/UI/Mailer/UIxMailEditorAttach.wox | 56 ++++++------ SOGo/UI/Mailer/Version | 2 +- 8 files changed, 155 insertions(+), 44 deletions(-) diff --git a/SOGo/SoObjects/Mailer/ChangeLog b/SOGo/SoObjects/Mailer/ChangeLog index 867e9aa4..04f91206 100644 --- a/SOGo/SoObjects/Mailer/ChangeLog +++ b/SOGo/SoObjects/Mailer/ChangeLog @@ -1,3 +1,7 @@ +2004-10-26 Helge Hess + + * SOGoDraftObject.[hm]: added method to delete attachments (v0.9.39) + 2004-10-26 Marcus Mueller * SOGoDraftObject.h: lF compile fix (v0.9.38) diff --git a/SOGo/SoObjects/Mailer/SOGoDraftObject.h b/SOGo/SoObjects/Mailer/SOGoDraftObject.h index 1ec8bcc4..9a0aedc4 100644 --- a/SOGo/SoObjects/Mailer/SOGoDraftObject.h +++ b/SOGo/SoObjects/Mailer/SOGoDraftObject.h @@ -49,6 +49,7 @@ - (NSArray *)fetchAttachmentNames; - (BOOL)isValidAttachmentName:(NSString *)_name; - (BOOL)saveAttachment:(NSData *)_attachment withName:(NSString *)_name; +- (BOOL)deleteAttachmentWithName:(NSString *)_name; @end diff --git a/SOGo/SoObjects/Mailer/SOGoDraftObject.m b/SOGo/SoObjects/Mailer/SOGoDraftObject.m index 8b7f3365..722ceaa5 100644 --- a/SOGo/SoObjects/Mailer/SOGoDraftObject.m +++ b/SOGo/SoObjects/Mailer/SOGoDraftObject.m @@ -161,4 +161,19 @@ return [_attachment writeToFile:p atomically:YES]; } +- (BOOL)deleteAttachmentWithName:(NSString *)_name { + NSFileManager *fm; + NSString *p; + + if (![self isValidAttachmentName:_name]) + return NO; + + fm = [self spoolFileManager]; + p = [[self draftFolderPath] stringByAppendingPathComponent:_name]; + if (![fm fileExistsAtPath:p]) + return YES; /* well, doesn't exist, so its deleted ;-) */ + + return [fm removeFileAtPath:p handler:nil]; +} + @end /* SOGoDraftObject */ diff --git a/SOGo/SoObjects/Mailer/Version b/SOGo/SoObjects/Mailer/Version index e89334b8..5c7b2848 100644 --- a/SOGo/SoObjects/Mailer/Version +++ b/SOGo/SoObjects/Mailer/Version @@ -1,6 +1,6 @@ # Version file -SUBMINOR_VERSION:=38 +SUBMINOR_VERSION:=39 # v0.9.35 requires SOGoLogic v0.9.24 # v0.9.34 requires SOGoLogic v0.9.22 diff --git a/SOGo/UI/Mailer/ChangeLog b/SOGo/UI/Mailer/ChangeLog index 7152649e..acf01f5e 100644 --- a/SOGo/UI/Mailer/ChangeLog +++ b/SOGo/UI/Mailer/ChangeLog @@ -1,5 +1,7 @@ 2004-10-26 Helge Hess + * UIxMailEditorAttach.m: added attachment delete (v0.9.48) + * UIxMailEditorAttach.wox, mailer.css: work on layout, added delete action (v0.9.47) diff --git a/SOGo/UI/Mailer/UIxMailEditorAttach.m b/SOGo/UI/Mailer/UIxMailEditorAttach.m index efae6dfa..d20c2719 100644 --- a/SOGo/UI/Mailer/UIxMailEditorAttach.m +++ b/SOGo/UI/Mailer/UIxMailEditorAttach.m @@ -40,6 +40,8 @@ NSData *fileData2; NSData *fileData3; NSString *attachmentName; + + NSArray *attachmentNames; } @end @@ -50,6 +52,7 @@ @implementation UIxMailEditorAttach - (void)dealloc { + [self->attachmentNames release]; [self->attachmentName release]; [self->filePath1 release]; [self->filePath2 release]; @@ -111,6 +114,21 @@ return self->fileData3; } +- (NSArray *)attachmentNames { + NSArray *a; + + if (self->attachmentNames != nil) + return self->attachmentNames; + + a = [[self clientObject] fetchAttachmentNames]; + a = [a sortedArrayUsingSelector:@selector(compare:)]; + self->attachmentNames = [a copy]; + return self->attachmentNames; +} +- (BOOL)hasAttachments { + return [[self attachmentNames] count] > 0 ? YES : NO; +} + /* requests */ - (BOOL)shouldTakeValuesFromRequest:(WORequest *)_rq inContext:(WOContext*)_c{ @@ -119,37 +137,108 @@ /* operations */ +- (NSString *)defaultPathExtension { + return @"txt"; +} + +- (NSString *)newAttachmentName { + NSArray *usedNames; + unsigned i; + + usedNames = [[self clientObject] fetchAttachmentNames]; + for (i = [usedNames count]; i < 100; i++) { + NSString *name; + + name = [NSString stringWithFormat:@"attachment%d", i]; + if (![usedNames containsObject:name]) + return name; + } + [self logWithFormat:@"ERROR: too many attachments?!"]; + return nil; +} + +- (NSString *)fixupAttachmentName:(NSString *)_name { + NSString *pe; + NSRange r; + + if (_name == nil) + return nil; + + pe = [_name pathExtension]; + if ([pe length] == 0) + /* would be better to check the content-type, but well */ + pe = [self defaultPathExtension]; + + r = [_name rangeOfString:@"/"]; + if (r.length > 0) _name = [_name lastPathComponent]; + + r = [_name rangeOfString:@" "]; + if (r.length > 0) + _name = [_name stringByReplacingString:@" " withString:@"_"]; + + if ([_name hasPrefix:@"."]) { + _name = [@"dotfile-" stringByAppendingString: + [_name substringFromIndex:1]]; + } + + // TODO: should we need to check for umlauts? + + if ([_name length] == 0) + return [[self newAttachmentName] stringByAppendingPathExtension:pe]; + + return _name; +} + - (BOOL)saveFileData:(NSData *)_data name:(NSString *)_name { - [self logWithFormat:@"TODO: save attachment %@, length %d", - _name, [_data length]]; - return NO; + if (_data == nil) + return NO; + if ([_name length] == 0) { + _name = [self newAttachmentName]; + _name = [_name stringByAppendingPathExtension:[self defaultPathExtension]]; + } + + if ((_name = [self fixupAttachmentName:_name]) == nil) + return nil; + + // TODO: add size limit? + return [[self clientObject] saveAttachment:_data withName:_name]; } /* actions */ - (id)viewAttachmentsAction { - [self logWithFormat:@"view attachments ..."]; + [self debugWithFormat:@"view attachments ..."]; return self; } - (id)attachAction { - [self logWithFormat:@"should save ..."]; - - // TODO: error handling! + BOOL ok; + ok = YES; if ([self->fileData1 length] > 0) - [self saveFileData:self->fileData1 name:[self filePath1]]; - if ([self->fileData2 length] > 0) - [self saveFileData:self->fileData2 name:[self filePath2]]; - if ([self->fileData3 length] > 0) + ok = [self saveFileData:self->fileData1 name:[self filePath1]]; + if (ok && [self->fileData2 length] > 0) + ok = [self saveFileData:self->fileData2 name:[self filePath2]]; + if (ok && [self->fileData3 length] > 0) [self saveFileData:self->fileData3 name:[self filePath3]]; - return self; + if (!ok) { + // TODO: improve error handling + return [NSException exceptionWithHTTPStatus:500 /* server error */ + reason:@"failed to save attachment ..."]; + } + + return [self redirectToLocation:@"viewAttachments"]; } - (id)deleteAttachmentAction { - [self logWithFormat:@"delete attachment: %@", [self attachmentName]]; - return self; + if (![[self clientObject] deleteAttachmentWithName:[self attachmentName]]) { + // TODO: improve error handling + return [NSException exceptionWithHTTPStatus:500 /* server error */ + reason:@"failed to delete attachment ..."]; + } + + return [self redirectToLocation:@"viewAttachments"]; } @end /* UIxMailEditorAttach */ diff --git a/SOGo/UI/Mailer/UIxMailEditorAttach.wox b/SOGo/UI/Mailer/UIxMailEditorAttach.wox index d0c4aa5f..fb002ce2 100644 --- a/SOGo/UI/Mailer/UIxMailEditorAttach.wox +++ b/SOGo/UI/Mailer/UIxMailEditorAttach.wox @@ -55,35 +55,35 @@
-
- -
-
- - - - - - - - - - - - -
- Attachments - - -
- delete -
-
+
+ +
+
+ + + + + + + + + + + + +
+ Attachments + + +
+ delete +
+
+
+
-
diff --git a/SOGo/UI/Mailer/Version b/SOGo/UI/Mailer/Version index ea2cd5d9..98a3496a 100644 --- a/SOGo/UI/Mailer/Version +++ b/SOGo/UI/Mailer/Version @@ -1,6 +1,6 @@ # $Id$ -SUBMINOR_VERSION:=47 +SUBMINOR_VERSION:=48 # v0.9.43 requires NGObjWeb v4.3.73 # v0.9.42 requires NGObjWeb v4.3.72 -- 2.39.5