24029056bd863bb5a21b9c9141e4e1e47675d819
[vlc.git] / projects / macosx / frontrow_plugin / VLCApplianceController.m
1 //
2 //  VLCApplianceController.m
3 //  FRVLC
4 //
5 //  Created by hyei on 06/09/07.
6 //  Copyright 2007 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "VLCApplianceController.h"
10
11 #import <BackRow/BRListControl.h>
12 #import <BackRow/BRTextMenuItemLayer.h>
13 #import <BackRow/BRControllerStack.h>
14
15 #import "VLCPlayerController.h"
16
17 @interface VLCApplianceController ()
18
19 @property(retain, nonatomic) NSString * path;
20
21 @end
22
23 @implementation VLCApplianceController
24
25 @synthesize path=_path;
26
27 - initWithPath:(NSString*)path
28 {
29     self = [super init];
30     
31     _contents = [[NSMutableArray alloc] init];
32     
33     self.path = path;
34     
35     [[self header] setTitle:[[NSFileManager defaultManager] displayNameAtPath:self.path]];
36     [[self list] setDatasource:self];
37     
38     return self;
39 }
40
41 - (void)dealloc
42 {
43     [_path release];
44     [_contents release];
45     [super dealloc];
46 }
47
48 - (void)setPath:(NSString*)path
49 {
50     if(path != _path) {
51         [_path release];
52         _path = [path retain];
53         
54         [_contents removeAllObjects];
55         
56         NSArray * contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:_path error:NULL];
57         
58         for(NSString * name in contents) {
59             NSString * filepath = [path stringByAppendingPathComponent:name];
60             int ok = 0;
61             
62             if([name hasPrefix:@"."]) {
63                 ok = -1;
64             }
65             
66             if(ok == 0) {
67                 BOOL directory;
68                 if(![[NSFileManager defaultManager] fileExistsAtPath:filepath isDirectory:&directory]) {
69                     ok = -1;
70                 }
71                 else if(directory) {
72                     ok = 1;
73                 }
74             }
75             
76             if(ok == 0) {
77                 NSString * type = [[NSWorkspace sharedWorkspace] typeOfFile:filepath error:NULL];
78                 if([[NSWorkspace sharedWorkspace] type:type conformsToType:(NSString*)kUTTypeMovie]) {
79                     ok = 1;
80                 }
81             }
82                 
83             if(ok == 0) {
84                 static NSSet * additionalValidExtensions = nil;
85                 if(additionalValidExtensions == nil) {
86                     additionalValidExtensions = [[NSSet alloc] initWithObjects:
87                                                  @"mkv",
88                                                  nil];
89                 }
90                 
91                 NSString * extension = [[name pathExtension] lowercaseString];
92                 if([additionalValidExtensions containsObject:extension]) {
93                     ok = 1;
94                 }
95             }
96             
97             if(ok == 1) {
98                 [_contents addObject:name];
99             }
100         }
101     }
102 }
103
104 - (void)willBePushed
105 {
106     PRINT_ME();
107 }
108
109 - (void)willBePopped
110 {
111     PRINT_ME();    
112 }
113
114
115 #pragma mark -
116 #pragma mark Utilities
117
118 - (NSString*)pathForRow:(NSInteger)row
119 {
120     NSString * name = [_contents objectAtIndex:row];
121     return [self.path stringByAppendingPathComponent:name];
122 }
123
124 - (BOOL)isDirectoryAtPath:(NSString*)path
125 {
126     NSDictionary * attributes = [[NSFileManager defaultManager] fileAttributesAtPath:path traverseLink:YES];
127     NSString * type = [attributes objectForKey:NSFileType];
128     return [type isEqualToString:NSFileTypeDirectory];
129 }
130
131
132 #pragma mark -
133 #pragma mark Data source
134
135 - (NSInteger)itemCount
136 {
137     return [_contents count];
138 }
139
140 - (CGFloat)heightForRow:(NSInteger)row
141 {
142     return 64.0;
143 }
144
145 - (BOOL)rowSelectable:(NSInteger)row
146 {
147     return YES;
148 }
149
150 - (NSString*)titleForRow:(NSInteger)row
151 {
152     return [_contents objectAtIndex:row];
153 }
154
155 - (id)itemForRow:(NSInteger)row
156 {
157     NSString * path = [self pathForRow:row];
158     BOOL isDirectory = [self isDirectoryAtPath:path];
159     
160     BRTextMenuItemLayer * item = nil;
161     
162     if(isDirectory) {
163         item = [BRTextMenuItemLayer folderMenuItem];
164     }
165     else {
166         item = [BRTextMenuItemLayer menuItem];
167     }
168
169     [item setTitle:[self titleForRow:row]];
170     
171     return item;
172 }
173
174 - (NSInteger)rowForTitle:(NSString *)title
175 {
176     return [_contents indexOfObject:title];
177 }
178
179 - (void)itemSelected:(NSInteger)row
180 {
181     NSString * path = [self pathForRow:row];
182     BOOL isDirectory = [self isDirectoryAtPath:path];
183     
184     BRController * controller = nil;
185     
186     if(isDirectory) {
187         controller = [[[VLCApplianceController alloc] initWithPath:path] autorelease];
188     }
189     else {
190 #ifdef FAKE
191         controller = [[[VLCAppPlayerController alloc] initWithPath:path] autorelease];
192 #else
193         static VLCPlayerController * playerController = nil;
194         if(playerController == nil) {
195             playerController = [[VLCPlayerController alloc] init];
196         }
197         
198         playerController.media = [VLCMedia mediaWithPath:path];
199         controller = playerController;
200 #endif
201     }
202     
203     if(controller != nil) {
204         [[self stack] pushController:controller];
205     }
206 }
207
208 @end