]> git.sesse.net Git - vlc/blob - projects/macosx/frontrow_plugin/VLCMediaListController.m
Merge branch 'master' of git://git.videolan.org/vlc
[vlc] / projects / macosx / frontrow_plugin / VLCMediaListController.m
1 /*****************************************************************************
2  * VLCMediaListController.m: Front Row plugin
3  *****************************************************************************
4  * Copyright (C) 2007 - 2008 the VideoLAN Team
5  * $Id$
6  *
7  * Authors: Pierre d'Herbemont <pdherbemont at videolan dot org>
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 "VLCMediaListController.h"
25 #import "VLCPlayerController.h"
26
27 #import <BackRow/BRListControl.h>
28 #import <BackRow/BRTextMenuItemLayer.h>
29 #import <BackRow/BRControllerStack.h>
30 #import <BackRow/BRHeaderControl.h>
31
32 @interface VLCMediaListController ()
33
34 @property(retain, nonatomic) VLCMediaListAspect * mediaListAspect;
35
36 @end
37
38 @implementation VLCMediaListController
39
40 @synthesize mediaListAspect;
41
42 - initWithMediaListAspect:(VLCMediaListAspect *)aMediaListAspect
43 {
44     return [self initWithMediaListAspect:aMediaListAspect andTitle:nil];
45 }
46
47 - initWithMediaListAspect:(VLCMediaListAspect *)aMediaListAspect andTitle:(NSString *)title
48 {
49     if( self = [super init] )
50     {
51         self.mediaListAspect = aMediaListAspect;
52         [self.mediaListAspect addObserver:self forKeyPath:@"media" options:NSKeyValueChangeRemoval|NSKeyValueChangeInsertion|NSKeyValueChangeSetting context:nil];
53         [[self list] setDatasource:self];
54         isReloading = NO;
55         if(title)
56         {
57             [[self header] setTitle: title];
58         }
59     }
60     return self;
61 }
62
63 - (void)dealloc
64 {
65     [self.mediaListAspect removeObserver:self forKeyPath:@"media"];
66     [mediaListAspect release];
67     [super dealloc];
68 }
69
70 - (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
71 {
72     if ([keyPath isEqualToString:@"media"]) {
73         if(!isReloading)
74         {
75             isReloading = YES;
76             [self performSelector:@selector(reload) withObject:nil afterDelay: [[self list] itemCount] > 10 ? 2. : [[self list] itemCount] ? 0.3 : 0.0];
77         }
78     }
79     else {
80         [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
81     }
82 }
83
84 - (void)willBePushed
85 {
86     PRINT_ME();
87 }
88
89 - (void)willBePopped
90 {
91     PRINT_ME();    
92 }
93
94 #pragma mark -
95 #pragma mark Reload hack
96
97 - (void) reload
98 {
99     [[self list] reload];
100     isReloading = NO;
101 }
102
103 #pragma mark -
104 #pragma mark Data source
105
106 - (NSInteger)itemCount
107 {
108     return [mediaListAspect count];
109 }
110
111 - (CGFloat)heightForRow:(NSInteger)row
112 {
113     return 64.0;
114 }
115
116 - (BOOL)rowSelectable:(NSInteger)row
117 {
118     return YES;
119 }
120
121 - (NSString*)titleForRow:(NSInteger)row
122 {
123     return [[mediaListAspect mediaAtIndex:row] valueForKeyPath:@"metaDictionary.title"];
124 }
125
126 - (id)itemForRow:(NSInteger)row
127 {
128     BOOL isDirectory = ![[mediaListAspect nodeAtIndex:row] isLeaf];
129     
130     BRTextMenuItemLayer * item = nil;
131
132     if(isDirectory) {
133         item = [BRTextMenuItemLayer folderMenuItem];
134     }
135     else {
136         item = [BRTextMenuItemLayer menuItem];
137     }
138
139     [item setTitle:[self titleForRow:row]];
140     
141     return item;
142 }
143
144 - (void)itemSelected:(NSInteger)row
145 {
146     VLCMediaListAspectNode * node = [mediaListAspect nodeAtIndex:row];
147     BOOL isDirectory = ![node isLeaf];
148     
149     BRController * controller = nil;
150     
151     if(isDirectory) {
152         controller = [[[VLCMediaListController alloc] initWithMediaListAspect:[node children] andTitle:[[node media] valueForKeyPath:@"metaDictionary.title"]] autorelease];
153     }
154     else {
155         static VLCPlayerController * playerController = nil;
156         if(playerController == nil) {
157             playerController = [[VLCPlayerController alloc] init];
158         }
159         
160         playerController.media = [mediaListAspect mediaAtIndex:row];
161         controller = playerController;
162     }
163     
164     if(controller != nil) {
165         [[self stack] pushController:controller];
166     }
167 }
168
169 @end