forked from TheStalwart/Theremin
-
Notifications
You must be signed in to change notification settings - Fork 6
/
FileBrowserController.m
265 lines (213 loc) · 8.51 KB
/
FileBrowserController.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
//
// FileBrowserController.m
// Theremin
//
// Created by kampfgnu on 17/12/2017.
//
#import "FileBrowserController.h"
#import "MusicServerClient.h"
#import "Directory.h"
#import "Song.h"
#import "WindowController.h"
#import "PlayListController.h"
#import "PreferencesController.h"
#import "WindowController.h"
@interface FileBrowserController ()
@property (nonatomic, strong) Directory *currentDirectory;
@end
@implementation FileBrowserController
- (id) init {
self = [super init];
if (self != nil) {
[NSBundle loadNibNamed:@"FileBrowser" owner:self];
mDirectories = [[NSMutableArray array] retain];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(clientConnectionStatusChanged:)
name:nMusicServerClientConnected
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(clientConnectionStatusChanged:)
name:nMusicServerClientDisconnected
object:nil];
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[mDirectories release];
[super dealloc];
}
- (void)show {
[mWindow makeKeyAndOrderFront:self];
}
- (void)clientConnectionStatusChanged:(NSNotification *)notification {
if (!([[[WindowController instance] currentLibraryDataSource] supportsDataSourceCapabilities] & eLibraryDataSourceSupportsImportingSongs))
return;
BOOL enableControls = NO;
self.currentDirectory = nil;
if ([[notification name] isEqualTo:nMusicServerClientConnected]) {
enableControls = YES;
self.currentDirectory = [[[WindowController instance] musicClient] rootDirectory];
[mTreeController setContent:self.currentDirectory];
[mTreeController setSelectionIndexPath:[NSIndexPath indexPathWithIndex:0]];
[mBrowser bind:@"content" toObject:mTreeController withKeyPath:@"arrangedObjects" options:nil];
[mBrowser bind:@"contentValues" toObject:mTreeController withKeyPath:@"arrangedObjects.lastPathComponent" options:nil];
[mBrowser bind:@"selectionIndexPaths" toObject:mTreeController withKeyPath:@"selectionIndexPaths" options:nil];
} else if ([[notification name] isEqualTo:nMusicServerClientDisconnected]) {
enableControls = NO;
[mBrowser unbind:@"content"];
[mBrowser unbind:@"contentValues"];
[mBrowser unbind:@"selectionIndexPaths"];
[mTreeController setContent:[[[Directory alloc] init] autorelease]];
} else {
return;
}
[mTableView reloadData];
[mBrowser setEnabled:enableControls];
}
- (NSArray *)selectedSongsFromTableView {
NSIndexSet *selection = [mTableView selectedRowIndexes];
if (self.currentDirectory == nil) {
return @[];
}
else {
NSMutableArray *songs = [NSMutableArray arrayWithCapacity:selection.count];
NSUInteger currentIndex = [selection firstIndex];
while (currentIndex != NSNotFound) {
[songs addObject:self.currentDirectory.songs[currentIndex]];
currentIndex = [selection indexGreaterThanIndex:currentIndex];
}
return songs;
}
}
////////////////////////////////////////////////////////////////////////
#pragma mark - NSBrowser
////////////////////////////////////////////////////////////////////////
- (IBAction)browserCellSelected:(id)sender {
NSArray *selectedObjects = [mTreeController selectedObjects];
if ([selectedObjects count] != 1) {
NSBeep();
return;
}
self.currentDirectory = [selectedObjects firstObject];
[mTableView reloadData];
}
- (NSArray *)selectedSongsUniqueIdentifiersInTable:(NSTableView *)tableView {
NSArray *songs = [self selectedSongsFromTableView];
NSMutableArray *uniqueIds = [NSMutableArray array];
for (int i = 0; i < [songs count]; i++) {
[uniqueIds addObject:[[songs objectAtIndex:i] uniqueIdentifier]];
}
return uniqueIds;
}
- (void)replacePlaylistWithSongIdentifiers:(NSArray *)uniqueIdentifiers {
[[[WindowController instance] musicClient] clearPlaylist];
[[[WindowController instance] musicClient] addSongsToPlaylistByUniqueIdentifier:uniqueIdentifiers];
[[[WindowController instance] musicClient] startPlayback];
[[[WindowController instance] playlistController] scheduleShowCurrentSongOnNextSongChange];
[[[WindowController instance] playlistController] scheduleSelectCurrentSongOnNextSongChange];
}
- (void)appendPlaylistWithSongIdentifiers:(NSArray *)uniqueIdentifiers {
[[[WindowController instance] musicClient] addSongsToPlaylistByUniqueIdentifier:uniqueIdentifiers];
}
- (NSArray *)uniqueSongIdentifiersOfCurrentBrowserSelection {
NSArray *selectedObjects = [mTreeController selectedObjects];
if ([selectedObjects count] != 1) {
NSBeep();
return @[];
}
Directory *firstDir = [selectedObjects firstObject];
NSMutableArray *songIds = [NSMutableArray array];
[self addSongIDsOfFolder:firstDir songList:&songIds];
return songIds;
}
- (void)addSongIDsOfFolder:(Directory *)directory songList:(NSMutableArray **)songIDs {
for (Song *song in directory.songs) {
[*songIDs addObject:[song uniqueIdentifier]];
}
for (Directory *subDir in directory.directoryEntries) {
[self addSongIDsOfFolder:subDir songList:songIDs];
}
}
- (IBAction)replacePlaylistWithCurrentDirectory:(id)sender {
[self replacePlaylistWithSongIdentifiers:[self uniqueSongIdentifiersOfCurrentBrowserSelection]];
}
- (IBAction)addPlaylistWithCurrentDirectory:(id)sender {
[self appendPlaylistWithSongIdentifiers:[self uniqueSongIdentifiersOfCurrentBrowserSelection]];
}
#pragma mark -
#pragma mark Actions
- (IBAction)replaceFilesInPlaylist:(id)sender {
if ([self executeActionForTableView:mTableView withSender:sender])
[self replacePlaylistWithSongIdentifiers:[self selectedSongsUniqueIdentifiersInTable:mTableView]];
}
- (IBAction)appendSongsToPlaylist:(id)sender {
if ([self executeActionForTableView:mTableView withSender:sender])
[self appendPlaylistWithSongIdentifiers:[self selectedSongsUniqueIdentifiersInTable:mTableView]];
}
- (void)modifyPlaylistWithSongIDs:(NSArray *)songIDs {
if (songIDs.count == 0) {
return;
}
switch ([[PreferencesController sharedInstance] libraryDoubleClickAction]) {
case eLibraryDoubleClickReplaces:
[self replacePlaylistWithSongIdentifiers:songIDs];
break;
case eLibraryDoubleClickAppends:
[self appendPlaylistWithSongIdentifiers:songIDs];
break;
}
}
- (IBAction)tableAction:(id)sender {
NSArray *songIDs = [self selectedSongsUniqueIdentifiersInTable:mTableView];
[self modifyPlaylistWithSongIDs:songIDs];
}
- (IBAction)doubleActionForBrowserDirectory:(id)sender {
NSArray *songIDs = [self uniqueSongIdentifiersOfCurrentBrowserSelection];
[self modifyPlaylistWithSongIDs:songIDs];
}
- (IBAction) getInfoOnSongs:(id)sender {
[[WindowController instance] getInfoOnSongs:[self selectedSongsFromTableView]];
}
- (BOOL) executeActionForTableView:(PWTableView *)aTableView withSender:(id)sender {
// if we were called using our PWTableView action character stuff, let it through
if ([aTableView characterActionInProgress] == NO)
// if clicked row is -1, the header could have been clicked - don't continue.
if ([aTableView clickedRow] == -1)
// if we are called from a context menu or toolbar item, also let it through
if ([sender isKindOfClass:[NSMenuItem class]] == NO && [sender isKindOfClass:[NSToolbarItem class]] == NO)
return NO;
return YES;
}
////////////////////////////////////////////////////////////////////////
#pragma mark - tableview stuff
////////////////////////////////////////////////////////////////////////
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return self.currentDirectory != nil ? self.currentDirectory.songs.count : 0;
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
if (self.currentDirectory == nil) {
return @"nothing";
}
Song *song = self.currentDirectory.songs[row];
if ([[tableColumn identifier] isEqualToString:@"length"]) {
int time = [[song valueForKey:@"time"] intValue];
return [NSString stringWithFormat:@"%d:%02d", time / 60, time % 60];
} else if ([[tableColumn identifier] isEqualToString:@"artist.name"]) {
return [song artist];
} else if ([[tableColumn identifier] isEqualToString:@"composer.name"]) {
return [song composer];
} else if ([[tableColumn identifier] isEqualToString:@"album.name"]) {
return [song album];
} else if ([[tableColumn identifier] isEqualToString:@"song.date"]) {
return [song date];
} else if ([[tableColumn identifier] isEqualToString:@"title"]) {
NSString *title = [song title];
if (title == nil || [title length] == 0)
return [[song file] lastPathComponent];
else
return title;
}
return [song valueForKeyPath:[tableColumn identifier]];
}
@end