/
CalTalkController.m
executable file
·296 lines (250 loc) · 10.4 KB
/
CalTalkController.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
#import "AutoLaunch.h"
#import "CalTalk.h"
#import "CalTalkController.h"
#import "CalTalkData.h"
#import "CalTalkServer.h"
#import "NetworkUserData.h"
#import "SharedCalendarData.h"
#import "DataIsEmptyTransformer.h"
#import "EnabledColorTransformer.h"
CalTalkController *g_calTalkController = nil;
@interface CalTalkController (Private)
- (void)observeValueForKeyPath: (NSString*)keyPath ofObject: (id)object change: (NSDictionary*)change context: (void*)context;
@end
@implementation CalTalkController
+ (void)initialize
{
NSUserDefaultsController *sharedController;
DataIsEmptyTransformer *transformer;
// Set initial values for user defaults
sharedController = [NSUserDefaultsController sharedUserDefaultsController];
[sharedController setInitialValues:
[NSDictionary dictionaryWithObjectsAndKeys:
NSFullUserName(), @"shareName",
[NSNumber numberWithUnsignedShort: 24080], @"sharePort",
[NSNumber numberWithBool: NO], @"showInStatusArea",
[NSNumber numberWithBool: YES], @"confirmQuit",
[NSNumber numberWithBool: YES], @"showInDock",
[NSNumber numberWithBool: NO], @"startOnLogin",
[NSNumber numberWithBool: NO], @"shareCalendars",
[NSString stringWithFormat: @"(%@)", NSUserName()], @"addToNameText",
[NSNumber numberWithBool: YES], @"addToName",
NSUserName(), @"secUserName",
[NSData data], @"secPasswordHash",
[NSNumber numberWithBool: (NSAppKitVersionNumber >= 800.0? YES : NO)],
@"secDigestAuth",
[NSNumber numberWithBool: YES], @"secStoreInKeychainByDefault",
nil]];
// Register custom value transformers
transformer = [[[DataIsEmptyTransformer alloc] init] autorelease];
[NSValueTransformer setValueTransformer: transformer forName: @"DataIsEmpty"];
transformer = [[[EnabledColorTransformer alloc] init] autorelease];
[NSValueTransformer setValueTransformer: transformer forName: @"EnabledColor"];
}
- (void)awakeFromNib
{
id userDefaultsController;
if(g_calTalkController)
NSLog(@"Warning! More than one CalTalkController instance!\n");
g_calTalkController = self;
// Set the double-click action for the network calendars view
[o_networkCalendarsTableView setTarget: self];
[o_networkCalendarsTableView setDoubleAction: @selector(subscribeToCalendar:)];
// Create the server object
m_server = [[CalTalkServer alloc] initWithController: self];
// If we're set to share, start the server
userDefaultsController = [NSUserDefaultsController sharedUserDefaultsController];
// Watch the shareCalendars pref so we know when to start and stop the server
[userDefaultsController addObserver: self forKeyPath: @"values.shareCalendars" options: NSKeyValueObservingOptionNew context: NULL];
//NSLog(@"shareCalendars: %@\n", [userDefaultsController valueForKeyPath: @"values.shareCalendars"]); // XXX
if([[userDefaultsController valueForKeyPath: @"values.shareCalendars"] boolValue])
[m_server start];
// If we're in the autolaunch list, set the startOnLogin pref accordingly
// XXX this causes a 72-byte leakÉ
[userDefaultsController
setValue: [NSNumber numberWithBool: [AutoLaunch isFileInAutoLaunch: [[NSBundle mainBundle] bundlePath]]]
forKeyPath: @"values.startOnLogin"];
// Watch the startOnLogin pref so we can add/remove ourselves from the list
[userDefaultsController addObserver: self forKeyPath: @"values.startOnLogin" options: NSKeyValueObservingOptionNew context: NULL];
}
- (IBAction)refreshUsersList:(id)sender
{
// XXX This would be better named refreshUserCalendarsList:, but I'm too lazy right now to change the
// nib connections
// Get the selected user
unsigned selectedUserIndex = [o_networkUsersController selectionIndex];
if(selectedUserIndex != NSNotFound)
{
NetworkUserData *selectedUserData = [[_content networkUsers] objectAtIndex: selectedUserIndex];
[selectedUserData refreshSharedCalendars];
}
}
- (IBAction)showMyCalendarsTab:(id)sender
{
// Bring the calendars window to front, then activate the first tab
[o_calendarsTabView selectTabViewItemWithIdentifier: @"My Calendars"];
[o_calendarsWindow makeKeyAndOrderFront: self];
}
- (IBAction)showNetworkTab:(id)sender
{
// Bring the calendars window to front, then activate the second tab
[o_calendarsTabView selectTabViewItemWithIdentifier: @"Network"];
[o_calendarsWindow makeKeyAndOrderFront: self];
}
- (IBAction)showSecurityPreferencesTab:(id)sender
{
// Open the preferences window and switch to the Security tab
[o_preferencesTabView selectTabViewItemWithIdentifier: @"Security"];
[o_preferencesPanel makeKeyAndOrderFront: self];
}
- (IBAction)subscribeToCalendar:(id)sender
{
unsigned selectedUserIndex = [o_networkUsersController selectionIndex],
selectedCalendarIndex = [o_networkCalendarsController selectionIndex];
if(selectedUserIndex != NSNotFound && selectedCalendarIndex != NSNotFound)
{
NetworkUserData *selectedUserData = [[_content networkUsers] objectAtIndex: selectedUserIndex];
NSString *selectedCalendarName = [[selectedUserData sharedCalendars] objectAtIndex: selectedCalendarIndex];
NSURL *calendarURL = [selectedUserData URLForCalendar: selectedCalendarName];
NSMutableString *calendarURLString = [NSMutableString stringWithString: [calendarURL absoluteString]];
// iCal 1.x ignores GetURL requests for http:// URLs, so change the scheme to webcal://
[calendarURLString replaceOccurrencesOfString: @"http:" withString: @"webcal:" options: NSCaseInsensitiveSearch
range: NSMakeRange(0, [calendarURLString length])];
// Open the calendar in iCal
NSString *iCalSubscribeScriptString = [NSString stringWithFormat:
@"tell app \"iCal\"\n"
" launch\n"
" activate\n"
" ignoring application responses\n"
" GetURL \"%@\"\n"
" end ignoring\n"
"end tell\n",
calendarURLString];
NSAppleScript *iCalSubscribeScript = [[NSAppleScript alloc] initWithSource: iCalSubscribeScriptString];
NSDictionary *scriptErrorInfo;
NSString *progressString = [NSString stringWithFormat: NSLocalizedString(@"Subscribing to %@'s %@", @""),
[selectedUserData userName],
selectedCalendarName];
[[self content] addJobToProgress: progressString];
if(![iCalSubscribeScript executeAndReturnError: &scriptErrorInfo])
{
[[NSAlert alertWithMessageText: NSLocalizedString(@"Subscribe Error", @"")
defaultButton: nil
alternateButton: nil
otherButton: nil
informativeTextWithFormat:
NSLocalizedString(@"There was an error subscribing to the calendar \"%@\". iCal returned the following "
"error message:\n\n"
"%@", @""), selectedCalendarName, [scriptErrorInfo objectForKey: NSAppleScriptErrorMessage]]
beginSheetModalForWindow: o_calendarsWindow modalDelegate: self didEndSelector: @selector(throwAwayAlertDidEnd:returnCode:contextInfo:)
contextInfo: NULL];
}
[[self content] removeJobFromProgress: progressString];
[iCalSubscribeScript release];
}
}
- (IBAction)goToICal: (id)sender
{
NSAppleScript *iCalActivateScript = [[NSAppleScript alloc] initWithSource:
@"tell app \"iCal\"\n"
" launch\n"
" activate\n"
"end tell\n"];
NSDictionary *scriptErrorInfo;
[[self content] addJobToProgress: NSLocalizedString(@"Switching to iCal", @"")];
if(![iCalActivateScript executeAndReturnError: &scriptErrorInfo])
{
[[NSAlert alertWithMessageText: NSLocalizedString(@"Error Switching to iCal", @"")
defaultButton: nil
alternateButton: nil
otherButton: nil
informativeTextWithFormat:
NSLocalizedString(@"iCal returned the following error message while trying to switch to it:\n\n"
"%@", @""), [scriptErrorInfo objectForKey: NSAppleScriptErrorMessage]]
beginSheetModalForWindow: o_calendarsWindow modalDelegate: self didEndSelector: @selector(throwAwayAlertDidEnd:returnCode:contextInfo:)
contextInfo: NULL];
}
[[self content] removeJobFromProgress: NSLocalizedString(@"Switching to iCal", @"")];
}
- (IBAction)confirmQuit:(id)sender
{
NSUserDefaultsController *sharedController = [NSUserDefaultsController sharedUserDefaultsController];
if([[[sharedController values] valueForKey: @"confirmQuit"] boolValue]
&& [[[sharedController values] valueForKey: @"shareCalendars"] boolValue])
[o_quitConfirmPanel makeKeyAndOrderFront: self];
else
[NSApp terminate: self];
}
- (IBAction)dismissPasswordPanel:(id)sender
{
[NSApp endSheet: o_passwordSheet returnCode: [sender tag]];
}
- (CalTalkServer*)server
{
return m_server;
}
- (NSWindow*)calendarsWindow
{
return o_calendarsWindow;
}
- (NSPanel*)passwordPanel
{
return o_passwordSheet;
}
- (NSTextField*)passwordPanelUsernameTextField
{
return o_passwordSheetUsernameTextField;
}
- (NSTextField*)passwordPanelPasswordTextField
{
return o_passwordSheetPasswordTextField;
}
- (NSButton*)passwordPanelStoreInKeychainButton
{
return o_passwordSheetStoreInKeychainButton;
}
- (PasswordController*)passwordController
{
return o_passwordController;
}
- (void)applicationDidBecomeActive: (NSNotification*)notification
{
// Reveal the calendars window
[o_calendarsWindow makeKeyAndOrderFront: self];
}
- (BOOL)applicationShouldHandleReopen: (NSApplication*)app hasVisibleWindows: (BOOL)hasWindows
{
// Reveal the calendars window
[o_calendarsWindow makeKeyAndOrderFront: self];
return YES;
}
- (void)throwAwayAlertDidEnd: (NSAlert*)alert returnCode: (int)returnCode contextInfo: (void*)contextInfo
{
// do nothing
}
@end
#pragma mark -
@implementation CalTalkController (Private)
- (void)observeValueForKeyPath: (NSString*)keyPath ofObject: (id)object change: (NSDictionary*)change context: (void*)context
{
id userDefaultsController = [NSUserDefaultsController sharedUserDefaultsController];
if([object isEqual: userDefaultsController])
{
if([keyPath isEqualToString: @"values.shareCalendars"])
{
if([[userDefaultsController valueForKeyPath: @"values.shareCalendars"] boolValue])
[m_server start];
else
[m_server stop];
}
else if([keyPath isEqualToString: @"values.startOnLogin"])
{
if([[userDefaultsController valueForKeyPath: @"values.startOnLogin"] boolValue])
[AutoLaunch addFileToAutoLaunch: [[NSBundle mainBundle] bundlePath] hide: YES];
else
[AutoLaunch removeFileFromAutoLaunch: [[NSBundle mainBundle] bundlePath]];
}
}
[super observeValueForKeyPath: keyPath ofObject: object change: change context: context];
}
@end