]> err.no Git - scalable-opengroupware.org/blob - UI/MailerUI/UIxMailForwardAction.m
29daff75f9717da929f96b6d05870ec44cc41beb
[scalable-opengroupware.org] / UI / MailerUI / UIxMailForwardAction.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 #import <Foundation/NSDictionary.h>
23 #import <Foundation/NSString.h>
24
25 #import <NGExtensions/NSNull+misc.h>
26
27 #import <SoObjects/Mailer/SOGoMailObject.h>
28 #import <SoObjects/Mailer/SOGoDraftObject.h>
29
30 #import "UIxMailEditorAction.h"
31
32 @interface UIxMailForwardAction : UIxMailEditorAction
33 @end
34
35
36 @implementation UIxMailForwardAction
37
38 - (NSString *)getAttachmentNameForSubject:(NSString *)_subject {
39   /* SOGoDraftObject disallows some strings - anything else required? */
40   static NSString *sescape[] = { 
41     @"/", @"..", @"~", @"\"", @"'", @" ", @".", nil 
42   };
43   static int maxFilenameLength = 64;
44   NSString *s;
45   unsigned i;
46   
47   if (![_subject isNotNull] || [_subject length] == 0)
48     return _subject;
49   s = _subject;
50   
51   if ([s length] > maxFilenameLength)
52     s = [s substringToIndex:maxFilenameLength];
53   
54   for (i = 0; sescape[i] != nil; i++)
55     s = [s stringByReplacingString:sescape[i] withString:@"_"];
56   
57   return [s stringByAppendingString:@".mail"];
58 }
59
60 - (NSString *)forwardSubject:(NSString *)_subject {
61   if (![_subject isNotNull] || [_subject length] == 0)
62     return _subject;
63   
64   /* Note: this is how Thunderbird 1.0 creates the subject */
65   _subject = [@"[Fwd: " stringByAppendingString:_subject];
66   _subject = [_subject stringByAppendingString:@"]"];
67   return _subject;
68 }
69
70 - (id)forwardAction {
71   NSException  *error;
72   NSData       *content;
73   NSDictionary *info, *attachment;
74   id result;
75
76   /* fetch message */
77   
78   if ((content = [[self clientObject] content]) == nil)
79     return [self didNotFindMailError];
80   if ([content isKindOfClass:[NSException class]])
81     return content;
82   
83   /* setup draft */
84   
85   if ((error = [self _setupNewDraft]) != nil)
86     return error;
87   
88   /* set subject (do we need to set anything else?) */
89   
90   info = [NSDictionary dictionaryWithObjectsAndKeys:
91                          [self forwardSubject:[[self clientObject] subject]],
92                          @"subject",
93                        nil];
94   if ((error = [newDraft storeInfo:info]) != nil)
95     return error;
96   
97   /* attach message */
98   
99   // TODO: use subject for filename?
100   error = [newDraft saveAttachment:content withName:@"forward.mail"];
101   if (error != nil)
102     return error;
103   
104   // TODO: we might want to pass the original URL to the editor for a final
105   //       redirect back to the message?
106   result = [self redirectToEditNewDraft];
107   [self reset];
108   return result;
109 }
110
111 @end /* UIxMailForwardAction */