Added our GPL copyright header to all files
[vlc.git] / projects / macosx / frontrow_plugin / VLCApplianceController.m
1 /*****************************************************************************
2  * VLCApplianceController.m: Front Row plugin
3  *****************************************************************************
4  * Copyright (C) 2007 - 2008 the VideoLAN Team
5  * $Id$
6  *
7  * Authors: hyei
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #import "VLCApplianceController.h"
25
26 #import <BackRow/BRListControl.h>
27 #import <BackRow/BRTextMenuItemLayer.h>
28 #import <BackRow/BRControllerStack.h>
29
30 #import "VLCPlayerController.h"
31
32 @interface VLCApplianceController ()
33
34 @property(retain, nonatomic) NSString * path;
35
36 @end
37
38 @implementation VLCApplianceController
39
40 @synthesize path=_path;
41
42 - initWithPath:(NSString*)path
43 {
44     self = [super init];
45     
46     _contents = [[NSMutableArray alloc] init];
47     
48     self.path = path;
49     
50     [[self header] setTitle:[[NSFileManager defaultManager] displayNameAtPath:self.path]];
51     [[self list] setDatasource:self];
52     
53     return self;
54 }
55
56 - (void)dealloc
57 {
58     [_path release];
59     [_contents release];
60     [super dealloc];
61 }
62
63 - (void)setPath:(NSString*)path
64 {
65     if(path != _path) {
66         [_path release];
67         _path = [path retain];
68         
69         [_contents removeAllObjects];
70         
71         NSArray * contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:_path error:NULL];
72         
73         for(NSString * name in contents) {
74             NSString * filepath = [path stringByAppendingPathComponent:name];
75             int ok = 0;
76             
77             if([name hasPrefix:@"."]) {
78                 ok = -1;
79             }
80             
81             if(ok == 0) {
82                 BOOL directory;
83                 if(![[NSFileManager defaultManager] fileExistsAtPath:filepath isDirectory:&directory]) {
84                     ok = -1;
85                 }
86                 else if(directory) {
87                     ok = 1;
88                 }
89             }
90             
91             if(ok == 0) {
92                 NSString * type = [[NSWorkspace sharedWorkspace] typeOfFile:filepath error:NULL];
93                 if([[NSWorkspace sharedWorkspace] type:type conformsToType:(NSString*)kUTTypeMovie]) {
94                     ok = 1;
95                 }
96             }
97                 
98             if(ok == 0) {
99                 static NSSet * additionalValidExtensions = nil;
100                 if(additionalValidExtensions == nil) {
101                     additionalValidExtensions = [[NSSet alloc] initWithObjects:
102                                                  @"mkv",
103                                                  nil];
104                 }
105                 
106                 NSString * extension = [[name pathExtension] lowercaseString];
107                 if([additionalValidExtensions containsObject:extension]) {
108                     ok = 1;
109                 }
110             }
111             
112             if(ok == 1) {
113                 [_contents addObject:name];
114             }
115         }
116     }
117 }
118
119 - (void)willBePushed
120 {
121     PRINT_ME();
122 }
123
124 - (void)willBePopped
125 {
126     PRINT_ME();    
127 }
128
129
130 #pragma mark -
131 #pragma mark Utilities
132
133 - (NSString*)pathForRow:(NSInteger)row
134 {
135     NSString * name = [_contents objectAtIndex:row];
136     return [self.path stringByAppendingPathComponent:name];
137 }
138
139 - (BOOL)isDirectoryAtPath:(NSString*)path
140 {
141     NSDictionary * attributes = [[NSFileManager defaultManager] fileAttributesAtPath:path traverseLink:YES];
142     NSString * type = [attributes objectForKey:NSFileType];
143     return [type isEqualToString:NSFileTypeDirectory];
144 }
145
146
147 #pragma mark -
148 #pragma mark Data source
149
150 - (NSInteger)itemCount
151 {
152     return [_contents count];
153 }
154
155 - (CGFloat)heightForRow:(NSInteger)row
156 {
157     return 64.0;
158 }
159
160 - (BOOL)rowSelectable:(NSInteger)row
161 {
162     return YES;
163 }
164
165 - (NSString*)titleForRow:(NSInteger)row
166 {
167     return [_contents objectAtIndex:row];
168 }
169
170 - (id)itemForRow:(NSInteger)row
171 {
172     NSString * path = [self pathForRow:row];
173     BOOL isDirectory = [self isDirectoryAtPath:path];
174     
175     BRTextMenuItemLayer * item = nil;
176     
177     if(isDirectory) {
178         item = [BRTextMenuItemLayer folderMenuItem];
179     }
180     else {
181         item = [BRTextMenuItemLayer menuItem];
182     }
183
184     [item setTitle:[self titleForRow:row]];
185     
186     return item;
187 }
188
189 - (NSInteger)rowForTitle:(NSString *)title
190 {
191     return [_contents indexOfObject:title];
192 }
193
194 - (void)itemSelected:(NSInteger)row
195 {
196     NSString * path = [self pathForRow:row];
197     BOOL isDirectory = [self isDirectoryAtPath:path];
198     
199     BRController * controller = nil;
200     
201     if(isDirectory) {
202         controller = [[[VLCApplianceController alloc] initWithPath:path] autorelease];
203     }
204     else {
205 #ifdef FAKE
206         controller = [[[VLCAppPlayerController alloc] initWithPath:path] autorelease];
207 #else
208         static VLCPlayerController * playerController = nil;
209         if(playerController == nil) {
210             playerController = [[VLCPlayerController alloc] init];
211         }
212         
213         playerController.media = [VLCMedia mediaWithPath:path];
214         controller = playerController;
215 #endif
216     }
217     
218     if(controller != nil) {
219         [[self stack] pushController:controller];
220     }
221 }
222
223 @end