forked from tomcool420/SMFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMFFolderBrowser.m
346 lines (327 loc) · 11.2 KB
/
SMFFolderBrowser.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//
// SMFolderBrowser.m
// SoftwareMenuFramework
//
// Created by Thomas on 4/19/09.
// Copyright 2010 Thomas Cool. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "SMFFolderBrowser.h"
#import "SMFBaseAsset.h"
#import "SMFMediaPreview.h"
#import "SMFMenuItem.h"
#import "SMFThemeInfo.h"
#import "SMFImageAsset.h"
#import "SMFDebAsset.h"
@implementation SMFFolderBrowser
@synthesize separate;
@synthesize showHidden;
@synthesize showOnlyFolders;
@synthesize delegate;
@synthesize fpath;
-(id)previewControlForItem:(long)row
{
if (separate && row <[_folders count]) {
int count = [SMFPhotoMethods imagesCountForPath:[_folders objectAtIndex:row]];
if (count>1) {
NSArray *proxies = [SMFPhotoMethods imageProxiesForPath:[_folders objectAtIndex:row]];
BRMediaParadeControl *ctrl = [[BRMediaParadeControl alloc] init];
[ctrl setImageProxies:proxies];
return [ctrl autorelease];
}
BRImage *img = [SMFPhotoMethods firstPhotoForPath:[_folders objectAtIndex:row]];
BRImageAndSyncingPreviewController *p = [[BRImageAndSyncingPreviewController alloc] init];
[p setImage:img];
return [p autorelease];
}
if (separate && (row-[_folders count])<([_files count])) {
NSString *f = [_files objectAtIndex:(row-[_folders count])];
if ([SMFPhotoMethods isImageAtPath:f]) {
SMFImageAsset *a = [[SMFImageAsset alloc] initWithPath:f];
SMFMediaPreview *p = [[SMFMediaPreview alloc]init];
[p setAsset:a];
[a release];
return [p autorelease];
// BRImage *img = [BRImage imageWithPath:[_files objectAtIndex:(row-[_folders count])]];
// BRImageAndSyncingPreviewController *p = [[BRImageAndSyncingPreviewController alloc] init];
// [p setImage:img];
// return [p autorelease];
}
if ([[f pathExtension] localizedCaseInsensitiveCompare:@"deb"]==NSOrderedSame) {
NSLog(@"***###***");
SMFDebAsset *asset = [[SMFDebAsset alloc] initWithPath:f];
if (asset!=nil) {
SMFMediaPreview *p = [[SMFMediaPreview alloc]init];
[p setAsset:asset];
[asset release];
return [p autorelease];
}
}
}
return [super previewControlForItem:row];
}
+ (void)setString:(NSString *)inputString forKey:(NSString *)theKey inDomain:(NSString *)theDomain
{
CFPreferencesSetAppValue((CFStringRef)theKey, (CFStringRef)inputString, (CFStringRef)theDomain);
CFPreferencesAppSynchronize((CFStringRef)theDomain);
//CFRelease(inputString);
}
+ (NSString *)stringForKey:(NSString *)theKey inDomain:(NSString *)theDomain
{
CFPreferencesAppSynchronize((CFStringRef)theDomain);
NSString * myString = (NSString *)CFPreferencesCopyAppValue((CFStringRef)theKey, (CFStringRef)theDomain);
return [(NSString *)myString autorelease];
}
-(id)init{
self = [super init];
[self addLabel:@"org.tomcool.Software.SMF"];
separate = TRUE;
showHidden = FALSE;
_items = [[NSMutableArray alloc] initWithObjects:nil];
_paths = [[NSMutableArray alloc] initWithObjects:nil];
_man = [[NSFileManager defaultManager] retain];
_files = [[NSMutableArray alloc]init];
_folders = [[NSMutableArray alloc]init];
[[self list] setDatasource:self];
return self;
}
- (id)initWithPath:(NSString *)thePath
{
self = [self init];
if (self)
[self setPath:thePath];
return self;
}
- (void)setPath:(NSString *)thePath
{
[path release];
path = thePath;
[path retain];
[self setListTitle: [path lastPathComponent]];
[self reloadFiles];
}
-(void)reloadFiles
{
[_paths removeAllObjects];
[_items removeAllObjects];
[_folders removeAllObjects];
[[self list] removeDividers];
NSFileManager *man = [NSFileManager defaultManager];
BOOL isDir;
NSArray *files= [man contentsOfDirectoryAtPath:path error:nil];
for (NSString *file in files)
{
//NSLog(@"file: %@",file);
if (![file hasPrefix:@":"]) {
if (![file hasPrefix:@"."] || showHidden) {
NSString *tf = [path stringByAppendingPathComponent:file];
if ([man fileExistsAtPath:tf isDirectory:&isDir])
{
if (!(!isDir && showOnlyFolders))
{
if (separate)
{
if (isDir)
[_folders addObject:tf];
else
[_files addObject:tf];
}
else
[_files addObject:tf];
}
}
}
}
}
[[self list] addDividerAtIndex:[_folders count] withLabel:@"Files"];
//[[self list] reload];
}
-(NSString *)pathAtRow:(long)row
{
NSString *p=nil;
if (separate && row<[_folders count])
{
p=[_folders objectAtIndex:row];
}
else if(separate && (row-[_folders count])<[_files count])
{
p=[_files objectAtIndex:row];
}
else if(!separate && row<[_files count])
{
p=[_files objectAtIndex:row];
}
return p;
}
-(void)playPauseActionForRow:(long)row
{
NSString *p=[self pathAtRow:row];
if (p) {
if (delegate!=nil &&
[delegate conformsToProtocol:@protocol(SMFFolderBrowserDelegate)])
{
if ([delegate hasActionForFile:p])
{
if ([delegate respondsToSelector:@selector(browser:executePlayPauseActionForFile:)]) {
[delegate browser:self executePlayPauseActionForFile:p];
}
else if ([delegate respondsToSelector:@selector(executePlayPauseActionForFile:)]) {
[delegate executePlayPauseActionForFile:p];
}
else if ([delegate respondsToSelector:@selector(browser:executeActionForFile:)])
[delegate browser:self executeActionForFile:p];
else if ([delegate respondsToSelector:@selector(executeActionForFile:)])
[delegate executeActionForFile:p];
}
}
}
[self reloadFiles];
[[self list] reload];
}
-(void)leftActionForRow:(long)row
{
NSString *p=[self pathAtRow:row];
if (p)
{
if (delegate!=nil && [delegate conformsToProtocol:@protocol(SMFFolderBrowserDelegate)]) {
if ([delegate hasActionForFile:p]) {
if ([delegate respondsToSelector:@selector(browser:executeLeftActionForFile:)]) {
[delegate browser:self executeRightActionForFile:p];
}
else if ([delegate respondsToSelector:@selector(executeLeftActionForFile:)]) {
[delegate executeRightActionForFile:p];
}
else if ([delegate respondsToSelector:@selector(browser:executeActionForFile:)])
[delegate browser:self executeActionForFile:p];
else if ([delegate respondsToSelector:@selector(executeActionForFile:)])
[delegate executeActionForFile:p];
}
}
}
[self reloadFiles];
[[self list] reload];
}
-(void)rightActionForRow:(long)row
{
NSString *p=[self pathAtRow:row];
if (p)
{
if (delegate!=nil && [delegate conformsToProtocol:@protocol(SMFFolderBrowserDelegate)]) {
if ([delegate hasActionForFile:p])
{
if ([delegate respondsToSelector:@selector(browser:executeRightActionForFile:)]) {
[delegate browser:self executeRightActionForFile:p];
}
else if ([delegate respondsToSelector:@selector(executeRightActionForFile:)]) {
[delegate executeRightActionForFile:p];
}
else if ([delegate respondsToSelector:@selector(browser:executeActionForFile:)])
[delegate browser:self executeActionForFile:p];
else if ([delegate respondsToSelector:@selector(executeActionForFile:)])
[delegate executeActionForFile:p];
}
}
}
[self reloadFiles];
[[self list] reload];
}
-(void)itemSelected:(long)row
{
NSFileManager *man = [NSFileManager defaultManager];
if (separate)
{
if (row<[_folders count])
{
NSString *newPath = [_folders objectAtIndex:row];
SMFFolderBrowser *p = [[SMFFolderBrowser alloc]initWithPath:newPath];
p.delegate=self.delegate;
[[self stack]pushController:p];
[p release];
}
}
else
{
NSString *newPath = [_files objectAtIndex:row];//[path stringByAppendingPathComponent:[_files objectAtIndex:row]];
BOOL isDir;
if([man fileExistsAtPath:newPath isDirectory:&isDir] && isDir)
{
SMFFolderBrowser *p = [[SMFFolderBrowser alloc]initWithPath:[_files objectAtIndex:row]];
[[self stack]pushController:p];
[p release];
}
}
}
- (id)itemForRow:(long)row
{
if (separate)
{
if (row<[_folders count])
{
SMFMenuItem *it = [SMFMenuItem folderMenuItem];
[it setText:[[_folders objectAtIndex:row] lastPathComponent]
withAttributes:[[BRThemeInfo sharedTheme]menuItemTextAttributes]];
if (fpath!=nil&&
[[_folders objectAtIndex:row] localizedCaseInsensitiveCompare:fpath]==NSOrderedSame) {
[it setImage:[[SMFThemeInfo sharedTheme] selectedImage]];
}
return it;
}
else if(row<([_folders count]+[_files count]))
{
SMFMenuItem *it = [SMFMenuItem menuItem];
[it setText:[[_files objectAtIndex:(row-[_folders count])] lastPathComponent]
withAttributes:[[BRThemeInfo sharedTheme]menuItemTextAttributes]];
return it;
}
}
else
{
BRMenuItem *it = [[BRMenuItem alloc] init];
//NSLog(@"it: %@, %@",it,[[_files objectAtIndex:row] lastPathComponent]);
[it setText:[[_files objectAtIndex:row] lastPathComponent]
withAttributes:[[BRThemeInfo sharedTheme]menuItemTextAttributes]];
if (fpath && [[_files objectAtIndex:row] localizedCaseInsensitiveCompare:fpath]==NSOrderedSame) {
[it setImage:[[SMFThemeInfo sharedTheme] selectedImage]];
}
return [it autorelease];
}
return nil;
}
-(id)titleForRow:(long)row
{
if (separate)
{
if (row<[_folders count])
{
return [[_folders objectAtIndex:row] lastPathComponent];
}
else if(row<([_folders count]+[_files count]))
{
return [[_files objectAtIndex:(row-[_folders count])] lastPathComponent];
}
}
else
{
//NSLog(@"title: %@",[[_files objectAtIndex:row] lastPathComponent]);
return [[_files objectAtIndex:row] lastPathComponent];
}
return nil;
}
-(long)itemCount
{
return ([_folders count]+[_files count]);
}
-(void)dealloc
{
[_man release];
[_folders release];
[_files release];
self.delegate=nil;
[super dealloc];
}
-(void)wasExhumed
{
[self reloadFiles];
[[self list] reload];
}
@end