"Fossies" - the Fresh Open Source Software Archive 
Member "opengroupware-5.5rc3/WebUI/Project/OGoProject/SkyProject4NewLink.m" (5 Dec 2015, 8848 Bytes) of package /linux/privat/opengroupware-5.5rc3.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Matlab source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "SkyProject4NewLink.m" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
5.5rc2_vs_5.5rc3.
1 /*
2 Copyright (C) 2000-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 <OGoFoundation/LSWContentPage.h>
23
24 @class NSString, NSArray;
25 @class EOGlobalID;
26
27 @interface SkyProject4NewLink : LSWContentPage
28 {
29 id fileManager;
30 EOGlobalID *folderGID;
31
32 NSString *fileName;
33 NSString *link;
34 NSString *subject;
35
36 /* cache */
37 NSArray *favorites;
38 id favorite;
39
40 /* transient */
41 id item;
42 }
43
44 @end
45
46 #include <OGoFoundation/OGoClipboard.h>
47 #include <NGExtensions/NSString+Ext.h>
48 #include "common.h"
49
50 @implementation SkyProject4NewLink
51
52 static BOOL debugOn = NO;
53
54 - (void)dealloc {
55 [self->item release];
56 [self->favorites release];
57 [self->favorite release];
58 [self->link release];
59 [self->fileName release];
60 [self->fileManager release];
61 [self->folderGID release];
62 [self->subject release];
63 [super dealloc];
64 }
65
66 /* notifications */
67
68 - (void)sleep {
69 [self->item release]; self->item = nil;
70 [super sleep];
71 }
72
73 /* accessors */
74
75 - (void)setFileManager:(id)_fm {
76 ASSIGN(self->fileManager, _fm);
77 }
78 - (id)fileManager {
79 return self->fileManager;
80 }
81
82 - (void)setFolderId:(EOGlobalID *)_gid {
83 ASSIGNCOPY(self->folderGID, _gid);
84 }
85 - (id)folderId {
86 return self->folderGID;
87 }
88
89 - (void)setFileName:(NSString *)_fileName {
90 ASSIGNCOPY(self->fileName, _fileName);
91 }
92 - (NSString *)fileName {
93 return self->fileName;
94 }
95
96 - (void)setLink:(NSString *)_link {
97 ASSIGNCOPY(self->link, _link);
98 }
99 - (NSString *)link {
100 return self->link;
101 }
102
103 - (void)setSubject:(NSString *)_subject {
104 ASSIGNCOPY(self->subject, _subject);
105 }
106 - (NSString *)subject {
107 return self->subject;
108 }
109
110 - (NSString *)folderPath {
111 EOGlobalID *fgid;
112
113 fgid = [self folderId];
114 return [[self fileManager] pathForGlobalID:fgid];
115 }
116 - (NSString *)filePath {
117 EOGlobalID *fgid;
118 NSString *path;
119
120 fgid = [self folderId];
121 path = [[self fileManager] pathForGlobalID:fgid];
122 return [path stringByAppendingPathComponent:[self fileName]];
123 }
124
125 - (NSString *)windowTitle {
126 NSString *path;
127 NSString *lnk;
128
129 lnk = [[self labels] valueForKey:@"CreateLinkAtPath"];
130
131 lnk = (lnk != nil)
132 ? lnk
133 : (NSString *)@"create link at path ";
134
135 path = [[self fileManager] pathForGlobalID:[self folderId]];
136 path = [lnk stringByAppendingString:path];
137
138 return path;
139 }
140
141 - (BOOL)showTitle {
142 return [[self fileManager]
143 isKindOfClass:NSClassFromString(@"SkyProjectFileManager")];
144 }
145
146 - (BOOL)isEditorPage {
147 return YES;
148 }
149
150 - (NSArray *)favorites {
151 NSMutableArray *t;
152 NSEnumerator *of;
153 id f;
154
155 if (self->favorites)
156 return self->favorites;
157
158 of = [[(OGoSession *)[self session] favorites] objectEnumerator];
159 t = [NSMutableArray arrayWithCapacity:8];
160 while ((f = [of nextObject])) {
161 id gid;
162
163 if ([f respondsToSelector:@selector(entityName)])
164 [t addObject:f];
165 else if ((gid = [f valueForKey:@"globalID"]))
166 [t addObject:gid];
167 }
168 self->favorites = [t copy];
169
170 return self->favorites;
171 }
172
173 - (void)setFavorite:(id)_fav {
174 ASSIGN(self->favorite, _fav);
175 }
176 - (id)favorite {
177 return self->favorite;
178 }
179
180 - (void)setItem:(id)_item {
181 ASSIGN(self->item, _item);
182 }
183 - (id)item {
184 return self->item;
185 }
186
187 - (NSString *)itemLabel {
188 NSString *olabel;
189 NSString *elabel;
190 NSString *tmp;
191
192 elabel = [[self item] entityName];
193 tmp = [[self labels] valueForKey:elabel];
194 if (tmp) elabel = tmp;
195
196 olabel = [[self session] labelForObject:[self item]];
197
198 if ([elabel length] > 0)
199 olabel = [NSString stringWithFormat:@"%@: %@", elabel, olabel];
200
201 return olabel;
202 }
203
204 - (NSString *)favoriteType {
205 NSString *ename;
206 NSString *ext;
207 id fav;
208
209 fav = [self favorite];
210 ename = nil;
211
212 if ([fav respondsToSelector:@selector(entityName)])
213 ename = [fav entityName];
214
215 if ([ename length] == 0) {
216 EOGlobalID *gid;
217
218 if ((gid = [fav valueForKey:@"globalID"]))
219 if ([gid isKindOfClass:[EOKeyGlobalID class]])
220 ename = [gid entityName];
221 }
222
223 if ([ename length] == 0) {
224 #if DEBUG
225 [self debugWithFormat:@"cannot determine entity of favorite: %@", fav];
226 #endif
227 ename = nil;
228 }
229
230 if ([ename isEqualToString:@"Date"])
231 ext = @"appointment";
232 else if ([ename isEqualToString:@"Doc"])
233 ext = @"document";
234 else
235 ext = [ename lowercaseString];
236
237 return ext;
238 }
239
240 /* favorites */
241
242 - (BOOL)shouldUseFavoriteInsteadOfTarget:(NSString *)_target {
243 if (self->favorite == nil)
244 return NO;
245 if ([_target length] > 0) // we prefer a typed target
246 return NO;
247
248 return YES;
249 }
250
251 - (EOKeyGlobalID *)getKeyGlobalIDOfFavorite:(id)_object {
252 EOKeyGlobalID *gid;
253
254 gid = ([_object isKindOfClass:[EOKeyGlobalID class]])
255 ? _object
256 : [_object valueForKey:@"globalID"];
257
258 if (![gid isKindOfClass:[EOKeyGlobalID class]])
259 return nil;
260 if ([gid keyCount] != 1)
261 return nil;
262 return gid;
263 }
264
265 - (NSString *)getTargetOfKeyGlobalID:(EOKeyGlobalID *)_gid {
266 NSString *s;
267
268 if (_gid == nil)
269 return nil;
270
271 s = [[_gid keyValues][0] stringValue];
272 if ([s length] == 0)
273 return nil;
274
275 return s;
276 }
277
278 /* actions */
279
280 - (id)save {
281 id fm;
282 NSString *fname;
283 NSString *folderPath;
284 NSString *target;
285
286 fname = [self fileName];
287 if ([fname length] == 0) {
288 [self setErrorString:@"missing filename for link!"];
289 return nil;
290 }
291 fm = [self fileManager];
292 target = [self link];
293
294 if ([self shouldUseFavoriteInsteadOfTarget:target]) {
295 EOKeyGlobalID *gid;
296
297 if ((gid = [self getKeyGlobalIDOfFavorite:self->favorite]) == nil) {
298 [self setErrorString:
299 @"could not determine usable object id of favorite-object!"];
300 return nil;
301 }
302
303 if ((target = [self getTargetOfKeyGlobalID:gid]) == nil) {
304 [self setErrorString:@"cannot link the specified favorite!"];
305 return nil;
306 }
307 if (debugOn) {
308 [self debugWithFormat:@"link to favorite: %@ to %@",
309 self->favorite, target];
310 }
311
312 if ([[fname pathExtension] length] == 0) {
313 NSString *ttype;
314
315 ttype = [self favoriteType];
316 if ([ttype length] > 0)
317 fname = [fname stringByAppendingPathExtension:ttype];
318 }
319 else if (debugOn) {
320 [self debugWithFormat:@"using path extension: %@",
321 [fname pathExtension]];
322 }
323 }
324 else if ([target length] == 0) {
325 [self setErrorString:@"specify a link target!"];
326 return nil;
327 }
328 else if ([target isAbsoluteURL]) {
329 // do not try to check existence of URLs
330 }
331 else if ([fm fileExistsAtPath:target]) {
332 /* make absolute fn */
333 target = [[fm fileAttributesAtPath:target traverseLink:NO]
334 objectForKey:NSFilePath];
335 }
336
337 /* create new file */
338
339 // Note: this is only required in database projects if you try to set
340 // a title
341 // TODO: should be replaced with a feature check:
342 // [fm requiresPathExtensionAtPath:xx];
343 if ([fm isKindOfClass:NSClassFromString(@"SkyProjectFileManager")]) {
344 if ([[fname pathExtension] length] == 0)
345 fname = [fname stringByAppendingPathExtension:@"link"];
346 }
347
348 folderPath = [fm pathForGlobalID:[self folderId]];
349 fname = [folderPath stringByAppendingPathComponent:fname];
350
351 if ([fm fileExistsAtPath:fname]) {
352 [self setErrorString:@"a file already exists at the specified path !"];
353 return nil;
354 }
355
356 if (![fm createSymbolicLinkAtPath:fname pathContent:target]) {
357 NSString *fmt;
358
359 fmt = [NSString stringWithFormat:@"could not create link to '%@' at '%@'.",
360 target, fname];
361
362 [self setErrorString:fmt];
363 return nil;
364 }
365
366 if ([self->subject length] > 0) {
367 NSDictionary *attrs;
368
369 attrs = [NSDictionary dictionaryWithObjectsAndKeys:
370 self->subject, @"NSFileSubject", nil];
371
372 if (![fm changeFileAttributes:attrs atPath:fname]) {
373 NSString *fmt;
374
375 fmt = [NSString stringWithFormat:
376 @"could not set title for '%@'.", fname];
377 [self setErrorString:fmt];
378 return nil;
379 }
380 }
381
382 return [[(OGoSession *)[self session] navigation] leavePage];
383 }
384
385 - (id)cancel {
386 return [[(OGoSession *)[self session] navigation] leavePage];
387 }
388
389 @end /* SkyProject4NewLink */