File: CollectionController.m

package info (click to toggle)
mpdcon.app 1.5.1-0.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 820 kB
  • sloc: objc: 4,941; makefile: 25
file content (550 lines) | stat: -rw-r--r-- 14,668 bytes parent folder | download
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/* 
   Project: MPDCon

   Copyright (C) 2004

   Author: Daniel Luederwald

   Created: 2004-05-12 17:59:14 +0200 by flip
   
   Collection Controller

   This application is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
 
   This application is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.
 
   You should have received a copy of the GNU General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
*/

#include <AppKit/AppKit.h>
#include "CollectionController.h"

/* ---------------------
   - Private Interface -
   ---------------------*/
@interface CollectionController(Private)

- (void) _refreshViews: (id)sender;
- (void) _filterCollectionByString: (NSString *)filterString;
- (void) _getAllAlbumsForArtistAt: (NSInteger)row;
- (void) _getAllTracksForArtistAt: (NSInteger)artistRow albumAt: (NSInteger)albumRow;


int _artistSort(id artist1, id artist2, void *context);
int _albumSort(id album1, id album2, void *context);
int _trackSort(id track1, id track2, void *context);
int _titleSort(id title1, id title2, void *context);
int _aSort(id string1, id string2, void *context);
@end

@implementation CollectionController

/* --------------------------
   - Initialization Methods -
   --------------------------*/

+ (id) sharedCollectionController
{
  static CollectionController *_sharedCollectionController = nil;

  if (! _sharedCollectionController) 
    {
      _sharedCollectionController = [[CollectionController 
				       allocWithZone: [self zone]] init];
    }
  
  return _sharedCollectionController;
}

- (id) init
{
  self = [self initWithWindowNibName: @"Collection"];
  
  if (self)
    {
      [self setWindowFrameAutosaveName: @"Collection"];
    }

  return self;
}

- (void) dealloc
{
  [allSongs release];
  [allArtists release];
  [allAlbums release];
  [filteredTracks release];
  [super dealloc];
}

/* --------------------
   - Playlist Methods -
   --------------------*/

- (void) addSelected: (id)sender
{
  NSEnumerator *songEnum = [trackView selectedRowEnumerator];
  NSNumber *songNumber;
  
  while ((songNumber = [songEnum nextObject]) != nil) 
    {
      [[MPDController sharedMPDController] 
	addTrack: [[allSongs objectAtIndex: [songNumber intValue]] getPath]];
    }
}

/* ---------------
   - Gui Methods -
   ---------------*/

- (void) awakeFromNib
{
  NSNotificationCenter *defCenter;

  [trackView setAutosaveName: @"CollectionTrackTable"];
  [trackView setAutosaveTableColumns: YES];
  
  [trackView setTarget: self];
  [trackView setDoubleAction: @selector(doubleClicked:)];

  defCenter = [NSNotificationCenter defaultCenter];

  [defCenter addObserver: self
		selector: @selector(didNotConnect:)
		name: DidNotConnectNotification
		object: nil];

  [defCenter addObserver: self
                selector: @selector(_refreshViews:)
                    name: ShownCollectionChangedNotification
                  object: nil];

 [self _refreshViews:nil];
}

- (void) updateCollection: (id)sender
{
  [[MPDController sharedMPDController] updateCollection];
}

- (void) doubleClicked: (id)sender
{
  if ([trackView clickedRow] >= 0) 
    {
	  [self addSelected: self];
	}
  else 
    {
      NSString *identifier;
  
      identifier = [(NSTableColumn *)[[trackView tableColumns] 
	    	  objectAtIndex: [trackView clickedColumn]] 
		     identifier];
	  
      if ([identifier isEqual: @"artist"]) 
        {
          NSArray *tmpArray = [[[allSongs autorelease] 
		    	     sortedArrayUsingFunction: _artistSort 
			         context: NULL] retain];
          allSongs = tmpArray;
        } 
      else if ([identifier isEqual: @"album"]) 
        {
          NSArray *tmpArray = [[[allSongs autorelease]
		    	     sortedArrayUsingFunction: _albumSort 
			         context: NULL] retain];
          allSongs = tmpArray;
        } 
      else if ([identifier isEqual: @"title"]) 
        {
          NSArray *tmpArray = [[[allSongs autorelease]
		    	     sortedArrayUsingFunction: _titleSort 
			         context: NULL] retain];
          allSongs = tmpArray;
        } 
      else if ([identifier isEqual: @"trackNr"]) 
        {
          NSArray *tmpArray = [[[allSongs autorelease]
		    	     sortedArrayUsingFunction: _trackSort 
			         context: NULL] retain];
          allSongs = tmpArray;
        } 
  
      [trackView reloadData];
    }
}

- (void) filterCollection: (id) sender
{
  [self _refreshViews:nil];
}

- (void) clearFilter: (id)sender
{
  if ([[filterField stringValue] compare: @""] == NSOrderedSame)
    {
      return;
    }
    
  [filterField setStringValue: @""];
  [self _refreshViews:nil];
}

/* --------------------------------
   - TableView dataSource Methods -
   --------------------------------*/

- (NSInteger) numberOfRowsInTableView: (NSTableView *)tableView
{
  return [allSongs count];

}

- (id) tableView: (NSTableView *)tableView 
objectValueForTableColumn: (NSTableColumn *)tableColumn 
	     row:(NSInteger) row
{
  NSString *identifier = [tableColumn identifier];
  
  return [[allSongs objectAtIndex: row] valueForKey: identifier];
}


/* ----------------------------
   - Browser delegate Methods -
   ----------------------------*/
   
- (NSInteger)browser:(NSBrowser *)sender numberOfRowsInColumn:(NSInteger)column
{
  if (column == 0)
    {
      return [allArtists count] + 1;
    }
    
  else
    {
      [self _getAllAlbumsForArtistAt: [sender selectedRowInColumn: 0]];
      return [allAlbums count]+1;
    }
}

- (void)browser:(NSBrowser *)sender willDisplayCell:(id)cell
          atRow:(NSInteger)row
         column:(NSInteger)column
{
#warning FIXME the formatters are not working
  if (column == 0) {
      [cell setLeaf: NO];
      
      if (row == 0) {
          //[cell setFormatter: [[[BoldFormatter alloc] init] autorelease]];
          [cell setStringValue: [NSString stringWithFormat: @"All (%"PRIuPTR" Artists)", [allArtists count]]];
      } else {
          //[cell setFormatter: [[[NormalFormatter alloc] init] autorelease]];
          [cell setStringValue: [allArtists objectAtIndex: row-1]];
      }
  } else {
      [cell setLeaf: YES];
      
      if (row == 0) {
          //[cell setFormatter: [[[BoldFormatter alloc] init] autorelease]];
          [cell setStringValue: [NSString stringWithFormat: @"All (%"PRIuPTR" Albums)", [allAlbums count]]];
      } else {
          //[cell setFormatter: [[[NormalFormatter alloc] init] autorelease]];
          [cell setStringValue: [allAlbums objectAtIndex: row-1]];
      }
  }
}

- (NSString *)browser:(NSBrowser *)sender
        titleOfColumn:(NSInteger)column
{
  if (column == 0) {
      return @"Artists";
  } else if (column == 1) {
      return @"Albums";
  } else {
      return @"";
  }
}
  

- (void) selectionChanged: (id) sender
{
  [self _getAllTracksForArtistAt: [sender selectedRowInColumn: 0] albumAt: [sender selectedRowInColumn: 1]];
  
  [trackView reloadData];
  [trackView deselectAll: sender];
  
  if ([trackView numberOfRows] > 0)
    {
      [trackView scrollRowToVisible: 0];
    }
}
 

/* ------------------------------
   - TableView dragging Methods -
   ------------------------------*/
   
- (BOOL) tableView: (NSTableView *)tv
	 writeRows: (NSArray *)rows
      toPasteboard: (NSPasteboard*)pboard
{
  NSArray *typeArray;
  NSMutableArray *songArray;
  
  BOOL accept;
  NSUInteger count;
  
  count = [rows count];
  
  if (count > 0)
    {
      NSUInteger i;
      songArray = [[NSMutableArray alloc] init];
      
      for (i = 0; i < count; i++)
        {
          [songArray addObject: [[allSongs objectAtIndex: [[rows objectAtIndex: i] integerValue]] getPath]];
        }
      
      accept = YES;
      typeArray = [NSArray arrayWithObjects: CollectionDragType, nil];
      [pboard declareTypes: typeArray owner: self];
      [pboard setPropertyList: [songArray autorelease] forType: CollectionDragType];
    }
  else
    {
      accept = NO;
    }
  
  return accept;
}
/* ------------------------
   - Notification Methods -
   ------------------------*/

- (void) didNotConnect: (NSNotification *)aNotif
{
  [[self window] performClose: self];
}

- (NSArray *) getAllTracks
{
  return filteredTracks;
}

@end

/* -------------------
   - Private Methods -
   -------------------*/
@implementation CollectionController(Private)

- (void) _refreshViews:(id) sender
{
  [self _filterCollectionByString: [filterField stringValue]];
  
  [collectionView reloadColumn: 0];

  [collectionView selectRow: 0 inColumn: 0];
  
  [self selectionChanged: collectionView];
}

- (void) _getAllAlbumsForArtistAt: (NSInteger)row
{
  [allAlbums release];
  
  NSMutableSet *tmpAlbums = [[NSMutableSet alloc] init];
     
  NSUInteger i;
     
  for (i = 0; i < [filteredTracks count]; i++)
    {
      if (row <= 0)
        {
          [tmpAlbums addObject: [[filteredTracks objectAtIndex: i] getAlbum]];
        }
      else
        {
          if ([[[filteredTracks objectAtIndex: i] getArtist] compare: [allArtists objectAtIndex: (row-1)]] == NSOrderedSame)
            {
              [tmpAlbums addObject: [[filteredTracks objectAtIndex: i] getAlbum]];
            }
        }
    }
      
  allAlbums = [[[[tmpAlbums autorelease] allObjects] sortedArrayUsingFunction: _aSort context: NULL] retain];
}


- (void) _getAllTracksForArtistAt: (NSInteger)artistRow albumAt: (NSInteger)albumRow
{
  [allSongs release];
  
  if (artistRow < 1)
    {
      if (albumRow < 1)
        {
          allSongs = [[NSArray arrayWithArray: filteredTracks] retain];
        }
      else
        {
          NSMutableArray *tmpArray = [[NSMutableArray alloc] init];
              
          NSUInteger i;
              
          for (i = 0; i < [filteredTracks count]; i++)
            {
              if ([[[filteredTracks objectAtIndex: i] getAlbum] compare: [allAlbums objectAtIndex: (albumRow -1)]] == NSOrderedSame)
                {
                  [tmpArray addObject: [filteredTracks objectAtIndex: i]];
                }
            }
                
          allSongs = [[NSArray arrayWithArray: [tmpArray autorelease]] retain];
        }
    }
  else
    {
      if (albumRow < 1)
        {
         NSMutableArray *tmpArray = [[NSMutableArray alloc] init];
            
         NSUInteger i;
              
         for (i = 0; i < [filteredTracks count]; i++)
           {
             if ([[[filteredTracks objectAtIndex: i] getArtist] compare: [allArtists objectAtIndex: (artistRow -1)]] == NSOrderedSame)
               {
                 [tmpArray addObject: [filteredTracks objectAtIndex: i]];
               }
           }
                
         allSongs = [[NSArray arrayWithArray: [tmpArray autorelease]] retain];
        }
      else
        {
          NSMutableArray *tmpArray = [[NSMutableArray alloc] init];
              
          NSUInteger i;
              
          for (i = 0; i < [filteredTracks count]; i++)
            {
              if (([[[filteredTracks objectAtIndex: i] getArtist] compare: [allArtists objectAtIndex: (artistRow -1)]] == NSOrderedSame) &&
                ([[[filteredTracks objectAtIndex: i] getAlbum] compare: [allAlbums objectAtIndex: (albumRow -1)]] == NSOrderedSame))
                {
                  [tmpArray addObject: [filteredTracks objectAtIndex: i]];
                }
            }
                
          allSongs = [[NSArray arrayWithArray: [tmpArray autorelease]] retain];
        }
    }
    
}
    
- (void) _filterCollectionByString: (NSString *) fString
{  
  [allArtists release];
  [filteredTracks release];

  if ([fString compare: @""] == NSOrderedSame)
    {
      allArtists = [[[MPDController sharedMPDController] getAllArtists] retain];
      filteredTracks = [[[MPDController sharedMPDController] getAllTracksWithMetadata: YES] retain];
    }
  else
    {
      NSArray *allTracks = [[[MPDController sharedMPDController] getAllTracksWithMetadata: YES] retain];
      NSMutableArray *tmpArray = [[NSMutableArray alloc] init];
      NSMutableSet *tmpArtists = [[NSMutableSet alloc] init];
      
      NSUInteger i;
      
      for (i = 0; i < [allTracks count]; i++)
        {
          if ([[[allTracks objectAtIndex: i] getArtist] rangeOfString: fString options: NSCaseInsensitiveSearch].location != NSNotFound)  
            {
              [tmpArray addObject: [allTracks objectAtIndex: i]];
            }
          else if ([[[allTracks objectAtIndex: i] getTitle] rangeOfString: fString options: NSCaseInsensitiveSearch].location != NSNotFound)  
            {
              [tmpArray addObject: [allTracks objectAtIndex: i]];
            }
          else if ([[[allTracks objectAtIndex: i] getAlbum] rangeOfString: fString options: NSCaseInsensitiveSearch].location != NSNotFound)  
            {
              [tmpArray addObject: [allTracks objectAtIndex: i]];
            }
        }
        
      [allTracks release]; 
      filteredTracks = [[NSArray arrayWithArray: [tmpArray autorelease]] retain];
 
      for (i = 0; i < [filteredTracks count]; i++)
        {
          [tmpArtists addObject: [[filteredTracks objectAtIndex: i] getArtist]];
        }
        
      allArtists = [[[[tmpArtists autorelease] allObjects] sortedArrayUsingFunction: _aSort context: NULL] retain];
    }
}


int _artistSort(id artist1, id artist2, void *context)
{
  PlaylistItem *pl1, *pl2;

  pl1 = (PlaylistItem *)artist1;
  pl2 = (PlaylistItem *)artist2;

  return [[pl1 getArtist] caseInsensitiveCompare: [pl2 getArtist]];
}

int _albumSort(id album1, id album2, void *context)
{
  PlaylistItem *pl1, *pl2;

  pl1 = (PlaylistItem *)album1;
  pl2 = (PlaylistItem *)album2;

  return [[pl1 getAlbum] caseInsensitiveCompare: [pl2 getAlbum]];
}

int _titleSort(id track1, id track2, void *context)
{
  PlaylistItem *pl1, *pl2;

  pl1 = (PlaylistItem *)track1;
  pl2 = (PlaylistItem *)track2;

  return [[pl1 getTitle] caseInsensitiveCompare: [pl2 getTitle]];
}

int _trackSort(id track1, id track2, void *context)
{
  PlaylistItem *pl1, *pl2;

  pl1 = (PlaylistItem *)track1;
  pl2 = (PlaylistItem *)track2;

  return [[pl1 getTrackNr] caseInsensitiveCompare: [pl2 getTrackNr]];
}

int _aSort(id string1, id string2, void *context)
{
  return [string1 caseInsensitiveCompare: string2];
}

@end