This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
PreferencesController.m
198 lines (159 loc) · 6.3 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
#import "PreferencesController.h"
#import "SFHFKeychainUtils.h"
#import "Defines.h"
#import "iPodWatcher.h"
#import <Security/Security.h>
#import <QuartzCore/CoreAnimation.h>
#import "UKLoginItemRegistry.h"
#import "HubStrings.h"
#import "HubNotifications.h"
#define maxItems 10 // Cutoff for history items
@implementation PreferencesController
#pragma mark WindowController Methods
- (id) init {
self = [super initWithWindowNibName:@"Preferences"];
if (self != nil) {
}
return self;
}
- (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];
[startAtLogin setState:([UKLoginItemRegistry indexForLoginItemWithPath:[[NSBundle mainBundle] bundlePath]]+1)];
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];
[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)setLoginStart:(id)sender {
if ([sender state]==NSOnState) {
[UKLoginItemRegistry addLoginItemWithPath:[[NSBundle mainBundle] bundlePath] hideIt:NO];
} else {
[UKLoginItemRegistry removeLoginItemWithPath:[[NSBundle mainBundle] bundlePath]];
}
}
-(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 release];
}
-(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];
}
@end