]> err.no Git - scalable-opengroupware.org/blob - SOGo/UI/MailerUI/UIxMailView.m
implemented trashing
[scalable-opengroupware.org] / SOGo / UI / MailerUI / UIxMailView.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 @interface UIxMailView : UIxComponent
25 {
26   id currentAddress;
27 }
28
29 - (BOOL)isDeletableClientObject;
30
31 @end
32
33 #include <UI/MailPartViewers/UIxMailRenderingContext.h> // cyclic
34 #include "WOContext+UIxMailer.h"
35 #include <SoObjects/Mailer/SOGoMailObject.h>
36 #include <NGImap4/NGImap4Envelope.h>
37 #include <NGImap4/NGImap4EnvelopeAddress.h>
38 #include "common.h"
39
40 @implementation UIxMailView
41
42 - (void)dealloc {
43   [self->currentAddress release];
44   [super dealloc];
45 }
46
47 /* notifications */
48
49 - (void)sleep {
50   [self->currentAddress release]; self->currentAddress = nil;
51   [super sleep];
52 }
53
54 /* accessors */
55
56 - (void)setCurrentAddress:(id)_addr {
57   ASSIGN(self->currentAddress, _addr);
58 }
59 - (id)currentAddress {
60   return self->currentAddress;
61 }
62
63 - (NSString *)objectTitle {
64   return [[self clientObject] subject];
65 }
66 - (NSString *)panelTitle {
67   NSString *s;
68   
69   s = [self labelForKey:@"View Mail"];
70   s = [s stringByAppendingString:@": "];
71   s = [s stringByAppendingString:[self objectTitle]];
72   return s;
73 }
74
75 /* expunge / delete setup and permissions */
76
77 - (BOOL)showMarkDeletedButton {
78   if (![[self clientObject] isDeletionAllowed])
79     return NO;
80   
81   return NO; // TODO: make configurable in profile
82 }
83
84 - (BOOL)showTrashButton {
85   if (![[self clientObject] isDeletionAllowed])
86     return NO;
87   // TODO: should also check for Trash write access (or add -isTrashingAllowed)
88   
89   return YES; // TODO: make configurable in profile
90 }
91
92 /* links (DUP to UIxMailPartViewer!) */
93
94 - (NSString *)linkToEnvelopeAddress:(NGImap4EnvelopeAddress *)_address {
95   // TODO: make some web-link, eg open a new compose panel?
96   return [@"mailto:" stringByAppendingString:[_address baseEMail]];
97 }
98
99 - (NSString *)currentAddressLink {
100   return [self linkToEnvelopeAddress:[self currentAddress]];
101 }
102
103 /* fetching */
104
105 - (id)message {
106   return [[self clientObject] fetchCoreInfos];
107 }
108
109 - (BOOL)hasCC {
110   return [[[self clientObject] ccEnvelopeAddresses] count] > 0 ? YES : NO;
111 }
112
113 /* viewers */
114
115 - (id)contentViewerComponent {
116   // TODO: I would prefer to flatten the body structure prior rendering,
117   //       using some delegate to decide which parts to select for alternative.
118   id info;
119   
120   info = [[self clientObject] bodyStructure];
121   return [[[self context] mailRenderingContext] viewerForBodyInfo:info];
122 }
123
124 /* actions */
125
126 - (id)defaultAction {
127   if ([self message] == nil) {
128     return [NSException exceptionWithHTTPStatus:404 /* Not Found */
129                         reason:@"did not find specified message!"];
130   }
131   return self;
132 }
133
134 - (BOOL)isDeletableClientObject {
135   return [[self clientObject] respondsToSelector:@selector(delete)];
136 }
137 - (BOOL)isInlineViewer {
138   return NO;
139 }
140
141 - (id)redirectToParentFolder {
142   id url;
143   
144   url = [[[self clientObject] container] baseURLInContext:[self context]];
145   return [self redirectToLocation:url];
146 }
147
148 - (id)deleteAction {
149   NSException *ex;
150   
151   if (![self isDeletableClientObject]) {
152     return [NSException exceptionWithHTTPStatus:400 /* Bad Request */
153                         reason:@"method cannot be invoked on "
154                                @"the specified object"];
155   }
156   
157   if ([self isInvokedBySafeMethod]) {
158     // TODO: fix UI to use POST for unsafe actions
159     [self logWithFormat:@"WARNING: method is invoked using safe HTTP method!"];
160   }
161   
162   if ((ex = [[self clientObject] delete]) != nil) {
163     id url;
164     
165     url = [[ex reason] stringByEscapingURL];
166     url = [@"view?error=" stringByAppendingString:url];
167     return [self redirectToLocation:url];
168     //return ex;
169   }
170   
171   if (![self isInlineViewer]) {
172     // if everything is ok, close the window (send a JS closing the Window)
173     id page;
174     
175     page = [self pageWithName:@"UIxMailWindowCloser"];
176     [page takeValue:@"YES" forKey:@"refreshOpener"];
177     return page;
178   }
179   
180   return [self redirectToParentFolder];
181 }
182
183 - (id)trashAction {
184   NSException *ex;
185   
186   if ([self isInvokedBySafeMethod]) {
187     // TODO: fix UI to use POST for unsafe actions
188     [self logWithFormat:@"WARNING: method is invoked using safe HTTP method!"];
189   }
190   
191   if ((ex = [[self clientObject] trashInContext:[self context]]) != nil) {
192     id url;
193     
194     url = [[ex reason] stringByEscapingURL];
195     url = [@"view?error=" stringByAppendingString:url];
196     return [self redirectToLocation:url];
197   }
198   
199   if (![self isInlineViewer]) {
200     // if everything is ok, close the window (send a JS closing the Window)
201     id page;
202     
203     page = [self pageWithName:@"UIxMailWindowCloser"];
204     [page takeValue:@"YES" forKey:@"refreshOpener"];
205     return page;
206   }
207   
208   return [self redirectToParentFolder];
209 }
210
211 - (id)getMailAction {
212   // TODO: we might want to flush the caches?
213   return [self redirectToLocation:@"view"];
214 }
215
216 /* generating response */
217
218 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
219   UIxMailRenderingContext *mctx;
220
221   mctx = [[NSClassFromString(@"UIxMailRenderingContext")
222                             alloc] initWithViewer:self context:_ctx];
223   [_ctx pushMailRenderingContext:mctx];
224   [mctx release];
225   
226   [super appendToResponse:_response inContext:_ctx];
227   
228   [[_ctx popMailRenderingContext] reset];
229 }
230
231 @end /* UIxMailView */