-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBNPreferencesWindowController.m
180 lines (160 loc) · 6.17 KB
/
BNPreferencesWindowController.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
//
// BNPreferencesWindowController.m
// Basecamp Notifications
//
// Created by Nick Paulson on 6/24/10.
// Copyright 2010 Linebreak. All rights reserved.
//
#import "BNPreferencesWindowController.h"
#import "NSSound+NPSystemSounds.h"
#import "BNAccount.h"
#import "BNMenuController.h"
#import "NSDictionary+NPAdditions.h"
#import <Sparkle/Sparkle.h>
#import "BNURLPrefixValueTransformer.h"
#import "NSTabView+NPAdditions.h"
#import "BNOpenIDAccount.h"
@interface BNPreferencesWindowController ()
@property (retain, readwrite) NSArray *songNamesArray;
@property (retain, readwrite) NSArray *refreshStrings;
@end
@implementation BNPreferencesWindowController
@synthesize songNamesArray, refreshStrings;
- (id)initWithWindowNibName:(NSString *)windowNibName {
if (self = [super initWithWindowNibName:windowNibName]) {
self.songNamesArray = [[NSSound availableSystemSounds] retain];
self.refreshStrings = [NSArray arrayWithObjects:@"1 minute", @"5 minutes", @"15 minutes", @"30 minutes", nil];
}
return self;
}
- (void)awakeFromNib {
}
- (IBAction)soundPopUpTriggered:(id)sender {
[[NSUserDefaults standardUserDefaults] setInteger:NSOnState forKey:@"SoundNotificationsEnabled"];
[[NSSound soundNamed:[sender titleOfSelectedItem]] play];
}
- (IBAction)plusButtonPressed:(id)sender {
[NSApp beginSheet:addAccountSheet modalForWindow:self.window modalDelegate:self didEndSelector:NULL contextInfo:nil];
}
- (IBAction)minusButtonPressed:(id)sender {
if ([accountTableView selectedRow] >= 0) {
BNAccount *theAccount = [[BNActivityController sharedController] accountAtIndex:[accountTableView selectedRow]];
if (theAccount != nil) {
[[BNMenuController sharedController] removeProjectsForAccount:theAccount];
[[BNActivityController sharedController] removeAccount:theAccount];
[accountTableView reloadData];
}
}
}
- (IBAction)addAccountPressed:(id)sender {
BNURLPrefixValueTransformer *transformer = [[BNURLPrefixValueTransformer alloc] init];
BNAccount *theAccount = nil;
if ([tabView indexOfSelectedTabViewItem] == 0) {
if ([[userField stringValue] length] > 0 && [[passwordField stringValue] length] > 0) {
[[BNActivityController sharedController] getAccountsForUsername:[userField stringValue] password:[passwordField stringValue] delegate:self];
[loginSpinner setHidden:NO];
[loginSpinner startAnimation:self];
[accountInfoLabel setHidden:YES];
[addAccountButton setEnabled:NO];
[cancelButton setEnabled:NO];
}
} else if ([tabView indexOfSelectedTabViewItem] == 1) {
if ([[apiKeyField stringValue] length] > 0 && [[otherURLPrefixField stringValue] length] > 0) {
theAccount = [BNOpenIDAccount openIDAccountWithAPIToken:[apiKeyField stringValue] URL:[transformer transformedValue:[otherURLPrefixField stringValue]]];
}
if (theAccount != nil && ![[BNActivityController sharedController] hasAccount:theAccount]) {
[[BNActivityController sharedController] checkAccountCredentials:theAccount delegate:self];
[loginSpinner setHidden:NO];
[loginSpinner startAnimation:self];
[accountInfoLabel setHidden:YES];
[addAccountButton setEnabled:NO];
[cancelButton setEnabled:NO];
} else {
[accountInfoLabel setHidden:NO];
[accountInfoLabel setStringValue:@"Account already added"];
NSBeep();
}
}
[transformer release];
}
- (void)tabView:(NSTabView *)theTabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem {
NSInteger theIndex = [tabView indexOfTabViewItem:tabViewItem];
if (theIndex == 0) {
[addAccountSheet makeFirstResponder:userField];
} else if (theIndex == 1) {
[addAccountSheet makeFirstResponder:otherURLPrefixField];
}
}
- (void)foundAccounts:(NSArray *)accounts {
[loginSpinner setHidden:YES];
[loginSpinner stopAnimation:self];
for (BNAccount *currAccount in accounts) {
if (![[BNActivityController sharedController] hasAccount:currAccount])
[[BNActivityController sharedController] addAccount:currAccount];
}
[addAccountSheet orderOut:nil];
[NSApp endSheet:addAccountSheet];
[accountTableView reloadData];
[userField setStringValue:@""];
[passwordField setStringValue:@""];
[otherURLPrefixField setStringValue:@""];
[apiKeyField setStringValue:@""];
[tabView selectFirstTabViewItem:self];
[addAccountSheet makeFirstResponder:userField];
[addAccountButton setEnabled:YES];
[cancelButton setEnabled:YES];
}
- (void)findingAccountsFailedWithError:(NSError *)theError {
[loginSpinner setHidden:YES];
[loginSpinner stopAnimation:self];
[accountInfoLabel setStringValue:@"Login failed"];
[accountInfoLabel setHidden:NO];
NSBeep();
[addAccountButton setEnabled:YES];
[cancelButton setEnabled:YES];
}
- (void)checkedCredentialsForAccount:(BNAccount *)theAccount success:(BOOL)success {
[loginSpinner setHidden:YES];
[loginSpinner stopAnimation:self];
if (success) {
[[BNActivityController sharedController] addAccount:theAccount];
[addAccountSheet orderOut:nil];
[NSApp endSheet:addAccountSheet];
[accountTableView reloadData];
[userField setStringValue:@""];
[passwordField setStringValue:@""];
[otherURLPrefixField setStringValue:@""];
[apiKeyField setStringValue:@""];
[tabView selectFirstTabViewItem:self];
[addAccountSheet makeFirstResponder:userField];
} else {
[accountInfoLabel setStringValue:@"Login failed"];
[accountInfoLabel setHidden:NO];
NSBeep();
}
[addAccountButton setEnabled:YES];
[cancelButton setEnabled:YES];
}
- (IBAction)cancelButtonPressed:(id)sender {
[addAccountSheet orderOut:nil];
[NSApp endSheet:addAccountSheet];
}
#pragma mark NSTableView Methods
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView {
return [[BNActivityController sharedController] accountCount];
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
BNAccount *theAccount = [[BNActivityController sharedController] accountAtIndex:rowIndex];
if ([[aTableColumn identifier] isEqualToString:@"accountUserColumn"])
return theAccount.user;
else if ([[aTableColumn identifier] isEqualToString:@"basecampURLColumn"])
return [theAccount.URL absoluteString];
return nil;
}
- (void)dealloc {
self.songNamesArray = nil;
self.refreshStrings = nil;
[[SUUpdater sharedUpdater] removeObserver:self forKeyPath:@"lastUpdateCheckDate"];
[super dealloc];
}
@end