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