~ubuntuone-client-engineering/ubuntuone-ios-client/trunk

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
//
//  PlaylistEditViewController.m
//  iSub
//
//  Created by Zachery Bir on 4/5/11.
//  Copyright 2011 Canonical Ltd.
//	
//  This program is free software: you can redistribute it and/or modify it
//  under the terms of the GNU Affero General Public License version 3,
//  as published by the Free Software Foundation.
//	
//  This program is distributed in the hope that it will be useful, but
//  WITHOUT ANY WARRANTY; without even the implied warranties of
//  MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
//  PURPOSE.  See the GNU Affero General Public License for more details.
//
//  You should have received a copy of the GNU Affero General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.

#import "PlaylistEditViewController.h"
#import "SubsonicTableViewController.h"
#import "Playlist.h"
#import "Song.h"
#import "DataCache.h"
#import "SongListParser.h"
#import "NSMutableSet+Extras.h"

@implementation PlaylistEditViewController
@synthesize playlist, originatorViewController;

+ (UINavigationController *)navigableViewControllerWithPlaylist:(Playlist *)playlist originator:(SubsonicTableViewController *)originator
{
    PlaylistEditViewController *playlistEditViewController = [[[PlaylistEditViewController alloc] initWithPlaylist:playlist] autorelease];
    playlistEditViewController.originatorViewController = originator;
    UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:playlistEditViewController] autorelease];
    return nav;
}

- (id)initWithPlaylist:(Playlist *)aPlaylist
{
	if ((self = [super initWithTitle:aPlaylist.name]))
    {
        self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel", @"") style:UIBarButtonItemStylePlain target:self action:@selector(cancel:)] autorelease];
        self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save", @"") style:UIBarButtonItemStylePlain target:self action:@selector(save:)] autorelease];

		self.playlist = aPlaylist;
        self.originatorViewController = nil;
        self.navigationItem.prompt = NSLocalizedString(@"Add songs to playlist", @"");
    }
    return self;
}

#pragma mark - Data Loading

- (void)beginLoadingData:(NSString*)force
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	
	NSDictionary *cachedArtists = [[DataCache sharedDataCache] objectForKey:@"SongsList"];
	
	if (cachedArtists && ![force isEqual:@"YES"])
	{
		self.tableData = [cachedArtists objectForKey:@"songs"];
		self.indexes = [cachedArtists objectForKey:@"indexes"];
		self.groupedTableData = [cachedArtists objectForKey:@"groupedSongs"];
	}
	else
	{
		NSURL *url = [[Subsonic sharedSubsonic] getBaseUrl:@"getSongs.view"];
		NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
		
		SongListParser *parser = [[SongListParser alloc] init];
		[xmlParser setDelegate:parser];
		
		if (![xmlParser parse])
		{
			[self performSelectorOnMainThread:@selector(failedLoadingData:) withObject:[xmlParser parserError] waitUntilDone:NO];
			[xmlParser release];
			[parser release];
			[pool release];
			return;
		}
		
		self.tableData = parser.songs;
        if ([self.tableData count] <= 8)
        {
            indexed = NO;
        }
		self.indexes = parser.indexes;
		self.groupedTableData = parser.groupedSongs;
				
		[xmlParser release];
		[parser release];
		
		NSDictionary *cacheData = [NSDictionary dictionaryWithObjectsAndKeys:self.tableData, @"songs",
								   self.indexes, @"indexes", self.groupedTableData, @"groupedSongs", nil];
		
		[[DataCache sharedDataCache] setObject:cacheData forKey:@"SongsList"];
	}
	
	[self performSelectorOnMainThread:@selector(finishLoadingData) withObject:nil waitUntilDone:NO];
	[pool release];
}

#pragma mark - UITableView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
	static NSString *CellIdentifier = @"Cell";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (nil == cell)
	{
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
	}
	
    cell.accessoryType = UITableViewCellAccessoryNone;

    Song *s = nil;
    
	if (self.searchDisplayController.searchResultsTableView == tableView)
	{
		s = [self.searchResults objectAtIndex:indexPath.row];
		cell.textLabel.text = s.title;
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@", s.album, s.artist];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	}
	else
	{
        if (indexed)
        {
            NSString *index = [self.indexes objectAtIndex:indexPath.section];
            s = [[self.groupedTableData objectForKey:index] objectAtIndex:indexPath.row]; 
        }
        else
        {
            s = [self.tableData objectAtIndex:indexPath.row];
        }
		cell.textLabel.text = s.title;
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@", s.album, s.artist];
		cell.accessoryType = UITableViewCellAccessoryNone;
	}

    if ([self.playlist.contents containsObject:s.songId])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
	
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
	Song *selection = nil;
		
	if (self.searchDisplayController.searchResultsTableView == tableView)
	{
		selection = [self.searchResults objectAtIndex:indexPath.row];
	}
	else if(indexed) 
	{	
		NSString *index = [self.indexes objectAtIndex:indexPath.section];
		selection = [[self.groupedTableData objectForKey:index] objectAtIndex:indexPath.row];
	}
    else 
    {
        selection = [self.tableData objectAtIndex:indexPath.row];
    }
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        [self.playlist.contents addObject:selection.songId];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    } else {
        [self.playlist.contents removeObject:selection.songId];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)cancel:(id)sender
{
    self.playlist = nil;
    [self dismissModalViewControllerAnimated:YES];
}

- (void)dealloc
{
	RELEASE_SAFELY(playlist);
	[super dealloc];
}

- (void)save:(id)sender
{
    NSMutableSet *extraParameters = [NSMutableSet set];
    if (nil != self.playlist.playlistId)
    {
        [extraParameters addKeyValueObjectFromArray:[NSArray arrayWithObjects:@"playlistId", self.playlist.playlistId, nil]];
    }
    [extraParameters addKeyValueObjectFromArray:[NSArray arrayWithObjects:@"name", self.playlist.name, nil]];
    for (NSString *songId in self.playlist.contents)
    {
        [extraParameters addKeyValueObjectFromArray:[NSArray arrayWithObjects:@"songId", songId, nil]];
    }
	NSURL *url = [[Subsonic sharedSubsonic] getBaseUrl:@"createPlaylist.view" parameters:extraParameters];
    NSURLRequest *request = [[[NSURLRequest alloc] initWithURL:url] autorelease];
    NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
    [connection start];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [self.originatorViewController performSelector:@selector(reload:) withObject:nil afterDelay:0.5];
    [self cancel:nil];
}
@end