]> err.no Git - sope/blob - sopex/Samples/WOxExtTest/Browser.m
git-svn-id: http://svn.opengroupware.org/SOPE/trunk@1201 e4a50df8-12e2-0310-a44c...
[sope] / sopex / Samples / WOxExtTest / Browser.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
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 // $Id: Browser.m 1 2004-08-20 11:17:52Z znek $
22
23 #import <NGObjWeb/WOComponent.h>
24
25 @class NSFileManager, NSArray, NSString;
26
27 @interface Browser : WOComponent < NSCoding >
28 {
29   NSFileManager *fm;
30
31   NSArray  *currentPath;
32   NSString *currentPathString;
33 }
34 @end
35
36 #include "common.h"
37
38 @implementation Browser
39
40 - (id)init {
41   if ((self = [super init])) {
42     self->fm = [[NSFileManager defaultManager] retain];
43
44     //    [self setCurrentPath:nil];
45   }
46   return self;
47 }
48
49 - (void)dealloc {
50   RELEASE(self->currentPathString);
51   RELEASE(self->currentPath);
52   RELEASE(self->fm);
53   [super dealloc];
54 }
55
56 - (void)setCurrentPath:(NSArray *)_p {
57   if (_p == nil)
58     _p = [NSArray array];
59   
60   ASSIGN(self->currentPath, _p);
61   
62   RELEASE(self->currentPathString); self->currentPathString = nil;
63   self->currentPathString = [[_p componentsJoinedByString:@"/"] copy];
64 }
65 - (NSArray *)currentPath {
66   return self->currentPath;
67 }
68 - (NSString *)currentPathString {
69   return self->currentPathString;
70 }
71
72 - (NSFileManager *)fileManager {
73   return self->fm;
74 }
75
76 - (NSArray *)rootFolder {
77   return [[self fileManager] directoryContentsAtPath:@"."];
78 }
79
80 - (NSString *)bgColor {
81   NSString *cf;
82
83   cf = [self valueForKey:@"currentFolder"];
84   if ([cf isEqualToString:[self currentPathString]])
85     return @"AAAAAA";
86   if ([cf hasPrefix:[self currentPathString]])
87     return @"#FAE8B8";
88
89   return @"white"; // default bg
90 }
91
92 - (BOOL)currentIsDirectory {
93   BOOL isDir;
94
95   if (![self->fm fileExistsAtPath:[self currentPathString] isDirectory:&isDir])
96     return NO;
97   
98   return isDir;
99 }
100
101 - (NSString *)currentImage {
102   if ([self currentIsDirectory]) {
103     /* directory */
104     NSString *cf;
105     
106     cf = [self valueForKey:@"currentFolder"];
107
108     if ([cf hasPrefix:[self currentPathString]])
109       return @"folder_opened.gif";
110     else
111       return @"folder_closed.gif";
112   }
113   return nil;
114 }
115
116 - (NSArray *)dirContents {
117   if (![self currentIsDirectory])
118     return nil;
119   
120   return [[self fileManager] directoryContentsAtPath:[self currentPathString]];
121 }
122
123 - (id)clicked {
124   NSLog(@"clicked: path is %@", [self currentPathString]);
125   NSLog(@"clicked: item is %@", [self valueForKey:@"item"]);
126   [self takeValue:[self currentPathString] forKey:@"currentFolder"];
127   return self;
128 }
129
130 /* NSCoding */
131
132 - (void)encodeWithCoder:(NSCoder *)_coder {
133   [super encodeWithCoder:_coder];
134
135   [_coder encodeObject:self->currentPath];
136   [_coder encodeObject:self->currentPathString];
137 }
138 - (id)initWithCoder:(NSCoder *)_coder {
139   if ((self = [super initWithCoder:_coder])) {
140     self->fm   = [[NSFileManager defaultManager] retain];
141
142     self->currentPath       = [[_coder decodeObject] retain];
143     self->currentPathString = [[_coder decodeObject] retain];
144   }
145   return self;
146 }
147
148 @end /* TableView */