1/*
2   Project: MPDCon
3
4   Copyright (C) 2004
5
6   Author: Daniel Luederwald
7
8   Created: 2004-05-12 17:59:14 +0200 by flip
9
10   PlaylistsManager Controller
11
12   This application is free software; you can redistribute it and/or
13   modify it under the terms of the GNU General Public
14   License as published by the Free Software Foundation; either
15   version 2 of the License, or (at your option) any later version.
16
17   This application is distributed in the hope that it will be useful,
18   but WITHOUT ANY WARRANTY; without even the implied warranty of
19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20   Library General Public License for more details.
21
22   You should have received a copy of the GNU General Public
23   License along with this library; if not, write to the Free
24   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
25*/
26
27#include <AppKit/AppKit.h>
28#include "PlaylistsManagerController.h"
29
30@implementation PlaylistsManagerController
31
32/* --------------------------
33   - Initialization Methods -
34   --------------------------*/
35
36+ (id) sharedPLManagerController
37{
38  static PlaylistsManagerController *_sharedPLManagerController = nil;
39
40  if (! _sharedPLManagerController) {
41      _sharedPLManagerController = [[PlaylistsManagerController allocWithZone: [self zone]] init];
42  }
43
44  return _sharedPLManagerController;
45}
46
47- (id) init
48{
49  if ((self = [self initWithWindowNibName: @"PlaylistsManager"]) != nil)
50    {
51      [self setWindowFrameAutosaveName: @"PlaylistsManager"];
52      mpdController = [MPDController sharedMPDController];
53    }
54  return self;
55}
56
57- (void) dealloc
58{
59  [playlists release];
60
61  [super dealloc];
62}
63
64/* ---------------
65   - Gui Methods -
66   ---------------*/
67
68- (void) awakeFromNib
69{
70  [listView setTarget: self];
71  [listView setDoubleAction: @selector(loadList:)];
72
73  [[NSNotificationCenter defaultCenter] addObserver: self
74					   selector: @selector(didNotConnect:)
75					       name: DidNotConnectNotification
76					     object: nil];
77
78  [self updateLists: self];
79}
80
81- (void) updateLists: (id)sender
82{
83  [playlists release];
84
85  playlists = [[mpdController getAllPlaylists] retain];
86
87  [listView deselectAll: self];
88  [listView reloadData];
89}
90
91
92- (void) loadList: (id)sender
93{
94  if ([listView selectedRow] >= 0) {
95      [mpdController loadPlaylist: [playlists objectAtIndex: [listView selectedRow]]];
96  }
97}
98
99
100- (void) saveList: (id)sender
101{
102  NSString *name;
103
104  name = [saveField stringValue];
105
106  if ([name isEqual: @""]) {
107      return;
108  }
109
110  if ([playlists containsObject: name]) {
111      NSInteger answer;
112
113      answer = NSRunAlertPanel(_(@"Playlist exists"), _(@"Overwrite it?"),
114			       _(@"Ok"), _(@"Cancel"), nil);
115
116      if (answer != NSAlertDefaultReturn) {
117	  return;
118      } else {
119	[mpdController removePlaylist: name];
120      }
121  }
122
123  [mpdController savePlaylist: name];
124
125  [saveField setStringValue: @""];
126
127  [self updateLists: self];
128
129}
130
131- (void) removeList: (id)sender
132{
133  if ([listView selectedRow] >= 0) {
134      [mpdController removePlaylist: [playlists objectAtIndex: [listView selectedRow]]];
135  }
136
137  [self updateLists: self];
138}
139
140/* --------------------------------
141   - TableView dataSource Methods -
142   --------------------------------*/
143
144- (NSInteger) numberOfRowsInTableView: (NSTableView *)tableView
145{
146  return [playlists count];
147}
148
149- (id) tableView: (NSTableView *)tableView objectValueForTableColumn: (NSTableColumn *)tableColumn
150	     row:(NSInteger)row
151{
152  return [playlists objectAtIndex: row];
153}
154
155/* ------------------------
156   - Notification Methods -
157   ------------------------*/
158
159- (void) tableViewSelectionDidChange: (NSNotification *)aNotif
160{
161  if ([listView selectedRow] == -1) {
162      [saveField setStringValue: @""];
163  } else {
164      [saveField setStringValue: [playlists objectAtIndex: [listView selectedRow]]];
165  }
166}
167
168- (void) didNotConnect: (NSNotification *)aNotif
169{
170  [[self window] performClose: self];
171}
172@end
173