forked from bps/scrobblepod
-
Notifications
You must be signed in to change notification settings - Fork 3
/
PreferencesController.m
299 lines (234 loc) · 8.85 KB
/
PreferencesController.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
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
#import "PreferencesController.h"
#import "SFHFKeychainUtils.h"
#import "Defines.h"
#import "iPodWatcher.h"
#import <Security/Security.h>
#import <QuartzCore/CoreAnimation.h>
#import "HubStrings.h"
#import "HubNotifications.h"
#define maxItems 10 // Cutoff for history items
static void loginItemsChanged(LSSharedFileListRef listRef, void *context);
@implementation PreferencesController
@synthesize launchOnLogin;
#pragma mark WindowController Methods
- (id) init {
if (!(self = [super initWithWindowNibName:@"Preferences"]))
return nil;
loginItemsListRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
seedValue = LSSharedFileListGetSeedValue(loginItemsListRef);
if (loginItemsListRef)
LSSharedFileListAddObserver(loginItemsListRef, CFRunLoopGetMain(), kCFRunLoopCommonModes,loginItemsChanged, self);
return self;
}
- (void) dealloc
{
if (loginItemsListRef)
CFRelease(loginItemsListRef);
[super dealloc];
}
- (void)windowDidLoad {
// [self.window setLevel:NSModalPanelWindowLevel]; // Disabled since 0.51 preview 3
[self.window center];
[self.window setShowsToolbarButton:NO];
NSImageCell *theCell = [[NSImageCell alloc] init];
[historyIconTableColumn setDataCell:theCell];
[theCell release];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:@selector(loginProcessing) name:APIHUB_WebServiceAuthorizationProcessing object:nil];
[notificationCenter addObserver:self selector:@selector(loginComplete) name:APIHUB_WebServiceAuthorizationCompleted object:nil];
NSString *username = [[NSUserDefaults standardUserDefaults] stringForKey:@"Username"];
if (!username || username.length==0) [currentLoginContainer setHidden:YES];
self.window.contentView = generalPrefsView;
// [self setPreferencesView:generalPrefsView];
}
- (NSString *)windowNibName {
return @"Preferences";
}
- (IBAction)showWindow:(id)sender {
[self.window setCollectionBehavior: NSWindowCollectionBehaviorCanJoinAllSpaces];
if (![self.window isVisible]) {
// [prefToolbar setVisible:NO];
[self changeView:generalPrefsToolbarItem];
}
[super showWindow:self];
[NSApp activateIgnoringOtherApps:YES];
[self.window makeKeyAndOrderFront:self];
[prefToolbar setVisible:YES];
}
#pragma mark Toolbar
-(BOOL)validateToolbarItem:(NSToolbarItem *)theItem {
return YES;
}
- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar {
NSMutableArray *theArray = [NSMutableArray new];
NSToolbarItem *currentItem;
for (currentItem in [toolbar items]) {
[theArray addObject:currentItem.itemIdentifier];
}
[theArray autorelease];
return theArray;
}
#pragma mark Changing Views
-(IBAction)changeView:(NSToolbarItem *)sender {
[prefToolbar setSelectedItemIdentifier:sender.itemIdentifier];
if ([sender tag]==1) {
[self setPreferencesView:generalPrefsView];
} else if ([sender tag]==3) {
[self setPreferencesView:lastfmPrefsView];
} else if ([sender tag]==4) {
[self setPreferencesView:exclusionsView];
} else if ([sender tag]==6) {
[self setPreferencesView:historyView];
}
}
-(void)setPreferencesView:(NSView *)inputView {
if (self.window.contentView != inputView) {
NSRect windowRect = self.window.frame;
float newHeight = inputView.frame.size.height;
int difference = newHeight - [self.window.contentView frame].size.height;
windowRect.origin.y -= difference;
windowRect.size.height += difference;
difference = inputView.frame.size.width - [self.window.contentView frame].size.width;
windowRect.origin.x -= difference/2;
windowRect.size.width += difference;
[inputView setHidden:YES];
[self.window.contentView setHidden:YES];
[self.window setContentView:inputView];
[self.window setFrame:windowRect display:YES animate:YES];
[inputView setHidden: NO];
}
}
#pragma mark Pane:General Methods
-(IBAction)startChooseXML:(id)sender {
NSOpenPanel * panel = [NSOpenPanel openPanel];
[panel setAllowsMultipleSelection:NO];
[panel setCanChooseDirectories:NO];
[panel beginSheetForDirectory:[@"~/Music/iTunes/" stringByExpandingTildeInPath]
file:@"iTunes Music Library.xml"
types:[NSArray arrayWithObject:@"xml"]
modalForWindow:self.window
modalDelegate:self
didEndSelector:@selector(filePanelDidEnd: returnCode: contextInfo:)
contextInfo:nil];
}
-(void)filePanelDidEnd:(NSOpenPanel *)panel returnCode:(int)returnCode contextInfo:(void *)contextInfo {
[panel orderOut:nil];
if (returnCode == NSOKButton) {
[[NSUserDefaults standardUserDefaults] setObject:[panel filename] forKey:BGPrefXmlLocation];
[[NSNotificationCenter defaultCenter] postNotificationName:BGXmlLocationChangedNotification object:nil];
}
}
-(IBAction)updateAutoDecision:(id)sender {
[[NSNotificationCenter defaultCenter] postNotificationName:BGNotificationPodMounted object:nil];
}
#pragma mark Pane:LastFm Methods
-(IBAction)openLastFmWebsite:(id)sender {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.last.fm/join/"]];
}
-(IBAction)openAuthWebsite:(id)sender {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.last.fm/api/auth?api_key=%@",API_KEY]]];
}
-(void)loginProcessing {
[NSThread detachNewThreadSelector:@selector(startAuthSpinner) toTarget:self withObject:nil];
}
-(void)startAuthSpinner {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[currentLoginContainer setHidden:NO];
[authSpinner startAnimation:self];
[pool drain];
}
-(void)loginComplete {
[authSpinner stopAnimation:self];
}
#pragma mark Pane:History Methods
- (BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(NSInteger)rowIndex {
return NO;
}
-(void)addHistoryWithSuccess:(BOOL)wasSuccess andDate:(NSDate *)aDate andDescription:(NSString *)aDescription {
NSDictionary *theDict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:wasSuccess],@"success",aDate,@"date",aDescription,@"comment",nil];
NSMutableArray *newArray;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
newArray = [[defaults arrayForKey:@"ActivityHistory"] mutableCopy];
if (!newArray) newArray = [NSMutableArray new];
[newArray insertObject:theDict atIndex:0];
int numberOfItems = newArray.count;
if (numberOfItems>maxItems && maxItems<numberOfItems) {
NSRange removeRange = NSMakeRange(maxItems,numberOfItems-maxItems);
[newArray removeObjectsAtIndexes: [NSIndexSet indexSetWithIndexesInRange:removeRange] ];
}
[defaults setObject:newArray forKey:@"ActivityHistory"];
[newArray release];
}
#pragma mark LoginItems
- (NSArray *)loginItems
{
CFArrayRef snapshotRef = LSSharedFileListCopySnapshot(loginItemsListRef, &seedValue);
return [NSMakeCollectable(snapshotRef) autorelease];
}
- (LSSharedFileListItemRef)mainBundleLoginItemCopy
{
NSArray *loginItems = [self loginItems];
NSURL *bundleURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
for (id item in loginItems) {
LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)item;
CFURLRef itemURLRef;
if (LSSharedFileListItemResolve(itemRef, 0, &itemURLRef, NULL) == noErr) {
NSURL *itemURL = (NSURL *)[NSMakeCollectable(itemURLRef) autorelease];
if ([itemURL isEqual:bundleURL]) {
CFRetain(item);
return (LSSharedFileListItemRef)item;
}
}
}
return NULL;
}
- (void)addMainBundleToLoginItems;
{
NSURL *bundleURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSDictionary *properties = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:@"com.apple.loginitem.HideOnLaunch"];
LSSharedFileListItemRef itemRef;
itemRef = LSSharedFileListInsertItemURL(loginItemsListRef, kLSSharedFileListItemLast, NULL, NULL, (CFURLRef)bundleURL, (CFDictionaryRef)properties, NULL);
if (itemRef)
CFRelease(itemRef);
}
- (void)removeMainBundleFromLoginItems;
{
LSSharedFileListItemRef itemRef = [self mainBundleLoginItemCopy];
if (!itemRef)
return;
LSSharedFileListItemRemove(loginItemsListRef, itemRef);
CFRelease(itemRef);
}
- (BOOL)launchOnLogin;
{
if (!loginItemsListRef)
return NO;
LSSharedFileListItemRef itemRef = [self mainBundleLoginItemCopy];
if (!itemRef)
return NO;
CFRelease(itemRef);
return YES;
}
- (void)setLaunchOnLogin:(BOOL)value;
{
if (!loginItemsListRef)
return;
if (!value) {
[self removeMainBundleFromLoginItems];
} else {
[self addMainBundleToLoginItems];
}
}
- (UInt32)launchOnLoginSeedValue;
{
return seedValue;
}
@end
static void loginItemsChanged(LSSharedFileListRef listRef, void *context)
{
PreferencesController *controller = context;
if ([controller launchOnLoginSeedValue] == LSSharedFileListGetSeedValue(listRef))
return;
[controller willChangeValueForKey:@"launchOnLogin"];
[controller didChangeValueForKey:@"launchOnLogin"];
}