-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyApp.m
599 lines (449 loc) · 17.7 KB
/
MyApp.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//----------------------------------------------------------------------------------------
// MyApp.m - NSApplication's delegate
//
// Copyright Dominique Peretti, 2004 - 2007.
// Copyright © Chris, 2007 - 2010. All rights reserved.
//----------------------------------------------------------------------------------------
#import <CoreServices/CoreServices.h>
#import "MyApp.h"
#import "MySVN.h"
#import "MyWorkingCopy.h"
#import "GetEthernetAddrSample.h"
#import "FavoriteWorkingCopies.h"
#import "RepositoriesController.h"
#import "SvnFileStatusToColourTransformer.h"
#import "SvnDateTransformer.h"
#import "ArrayCountTransformer.h"
#import "FilePathCleanUpTransformer.h"
#import "TrimNewLinesTransformer.h"
#import "Tasks.h"
#import "CommonUtils.h"
#import "IconUtils.h"
#import "ViewUtils.h"
#import "SvnInterface.h"
// TO_DO: Add file "TaskStatusToColorTransformer.h"
@interface TaskStatusToColorTransformer : NSObject @end
@interface ActionToColor : NSValueTransformer @end
//----------------------------------------------------------------------------------------
static void
addTransform (Class itsClass, NSString* itsName)
{
[NSValueTransformer setValueTransformer: [[itsClass alloc] init] forName: itsName];
}
//----------------------------------------------------------------------------------------
#pragma mark -
//----------------------------------------------------------------------------------------
@implementation MyApp
//----------------------------------------------------------------------------------------
+ (MyApp*) myApp
{
static id controller = nil;
if (!controller)
{
controller = [NSApp delegate];
}
return controller;
}
//----------------------------------------------------------------------------------------
+ (void) initialize
{
InitViewUtils();
NSMutableDictionary* const dictionary = [NSMutableDictionary dictionary];
[SvnFileStatusToColourTransformer initialize: dictionary];
NSFileManager* const fm = [NSFileManager defaultManager];
NSString* svnBin = @"/usr/local/bin";
if ([fm isExecutableFileAtPath: @"/opt/subversion/bin/svn"])
svnBin = @"/opt/subversion/bin";
else if ([fm isExecutableFileAtPath: @"/usr/bin/svn"])
svnBin = @"/usr/bin";
[dictionary setObject: svnBin forKey: @"svnBinariesFolder"];
[dictionary setObject: kNSTrue forKey: @"cacheSvnQueries"];
[dictionary setObject: [NSNumber numberWithInt:0] forKey: @"defaultDiffApplication"];
[dictionary setObject: @"%y-%d-%m %H:%M:%S" forKey: @"dateformat"];
[dictionary setObject: kNSFalse forKey: @"includeRevisionInName"];
[dictionary setObject: kNSTrue forKey: @"installSvnxTool"];
[dictionary setObject: kNSTrue forKey: @"wcEditShown"];
[dictionary setObject: kNSTrue forKey: @"repEditShown"];
// Working Copy
[dictionary setObject: kNSTrue forKey: @"addWorkingCopyOnCheckout"];
[dictionary setObject: kNSFalse forKey: kUseOldParsingMethod];
[dictionary setObject: kNSTrue forKey: @"abbrevWCFilePaths"];
[dictionary setObject: kNSTrue forKey: @"expandWCTree"];
[dictionary setObject: kNSFalse forKey: @"autoRefreshWC"];
[dictionary setObject: kNSFalse forKey: @"compactWCColumns"];
[dictionary setObject: kNSFalse forKey: @"showWCExternals"];
// Review & Commit
[dictionary setObject: [NSArray arrayWithContentsOfFile:
[[NSBundle mainBundle] pathForResource: @"Templates" ofType: @"plist"]]
forKey: @"msgTemplates"];
[dictionary setObject: @"0" forKey: @"diffDefaultTab"]; // 0=>side by side, 1=>inline, 2=>unified
[dictionary setObject: @"5" forKey: @"diffContextLines"];
[dictionary setObject: kNSTrue forKey: @"diffShowFunction"];
[dictionary setObject: kNSTrue forKey: @"diffShowCharacters"];
[dictionary setObject: [NSNumber numberWithInt: 99] forKey: @"loggingLevel"];
[Preferences() registerDefaults: dictionary];
[[NSUserDefaultsController sharedUserDefaultsController] setInitialValues: dictionary];
[SvnFileStatusToColourTransformer update];
InitWCPreferences();
// Transformers // Used by:
addTransform([SvnFileStatusToColourTransformer class], @"FileStatusToColor"); // MyWorkingCopy
addTransform([SvnDateTransformer class], @"SvnDateTransformer"); // MySvnLogView
addTransform([ArrayCountTransformer class], @"ArrayCountTransformer"); // MySvnLogView
addTransform([FilePathCleanUpTransformer class], @"FilePathCleanUp"); // FavoriteWorkingCopies (path field)
addTransform([FilePathWorkingCopy class], @"FilePathWorkingCopy"); // FavoriteWorkingCopies
addTransform([TrimNewLinesTransformer class], @"TrimNewLines"); // MySvnLogView (author column)
addTransform([TaskStatusToColorTransformer class], @"TaskStatusToColor"); // Activity Window in svnX.nib
addTransform([ActionToColor class], @"ActionToColor"); // MySvnLogView
addTransform([OneLineTransformer class], @"ForceOneLine"); // preferencesWindow
}
//----------------------------------------------------------------------------------------
- (BOOL) checkSVNExistence: (BOOL) warn
{
NSFileManager* fm = [NSFileManager defaultManager];
BOOL isDir = NO,
exists = [fm fileExistsAtPath: SvnPath() isDirectory: &isDir] && isDir &&
[fm isExecutableFileAtPath: SvnCmdPath()];
// dprintf("cmdPath='%@', exists=%d isDir=%d", SvnCmdPath(), exists, isDir);
NSData* version = nil;
if (exists)
{
Task* task = [Task task];
NSPipe* pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
[task launch: SvnCmdPath() arguments: [NSArray arrayWithObjects: @"--version", @"-q", nil]];
version = [[pipe fileHandleForReading] readDataToEndOfFile];
// dprintf("version=[%@], pipe=%@", version, pipe);
}
[self setSvnVersion: version];
if (!exists && warn)
{
NSBeep();
id text = isDir ? @"Make sure the svn binary is present in the folder:\n%C%@%C.\n\n"
"Is a Subversion client installed?"
" If so, make sure the path is correctly set in the preferences."
: @"The 'Path to svn binaries folder' preference is\n%C%@%C.\n\n"
"This folder was not found.";
NSAlert* alert = [NSAlert alertWithMessageText: @"Error: Unable to locate svn binary."
defaultButton: @"Open Preferences"
alternateButton: @"Quit"
otherButton: nil
informativeTextWithFormat: text, 0x201C, SvnPath(), 0x201D];
[alert setAlertStyle: NSCriticalAlertStyle];
if ([alert runModal] == NSOKButton)
[self performSelector: @selector(openPreferences:) withObject: nil afterDelay: 0];
else
[NSApp terminate: nil];
}
return exists;
}
//----------------------------------------------------------------------------------------
- (BOOL) checkSVNExistence
{
return [self checkSVNExistence: TRUE];
}
//----------------------------------------------------------------------------------------
- (void) applicationWillFinishLaunching: (NSNotification*) note
{
#pragma unused(note)
InitIconCache();
if (GetPreferenceBool(@"installSvnxTool"))
{
NSString* target = [NSHomeDirectory() stringByAppendingPathComponent: @"bin/svnx"];
if (![[NSFileManager defaultManager] fileExistsAtPath: target])
{
[[NSFileManager defaultManager]
createSymbolicLinkAtPath: target
pathContent: [[NSBundle mainBundle] pathForResource: @"svnx" ofType: nil]];
}
}
[self checkSVNExistence: TRUE];
[repositoriesController showWindow];
[favoriteWorkingCopies showWindow];
}
//----------------------------------------------------------------------------------------
#if qDebug
- (IBAction) test: (id) sender
{
#pragma unused(sender)
dprintf("sender=%@", sender);
}
#endif
//----------------------------------------------------------------------------------------
#pragma mark -
#pragma mark AppleScript Support
//----------------------------------------------------------------------------------------
// Compare a single file in a svnX window. Invoked from Applescript.
- (void) displayHistory: (NSString*) path
{
[[NSApplication sharedApplication] activateIgnoringOtherApps: YES];
[favoriteWorkingCopies fileHistoryOpenSheetForItem: path];
}
//----------------------------------------------------------------------------------------
// Open a working copy window.
- (void) openWorkingCopy: (NSString*) path
{
[favoriteWorkingCopies openWorkingCopy: path];
}
//----------------------------------------------------------------------------------------
// Open a repository window.
- (void) openRepository: (NSString*) url
{
[repositoriesController openRepository: url];
}
//----------------------------------------------------------------------------------------
// Open one or more files in appropriate applications.
- (void) openFiles: (id) fileOrFiles
{
OpenFiles(fileOrFiles);
}
//----------------------------------------------------------------------------------------
// Compare one or more files with their base revisions.
- (void) diffFiles: (id) fileOrFiles
{
[favoriteWorkingCopies diffFiles: fileOrFiles];
}
//----------------------------------------------------------------------------------------
// Interactively resolve one or more conflicted files.
- (void) resolveFiles: (id) fileOrFiles
{
[favoriteWorkingCopies resolveFiles: fileOrFiles];
}
//----------------------------------------------------------------------------------------
#pragma mark -
//----------------------------------------------------------------------------------------
- (IBAction) openPreferences: (id) sender
{
#pragma unused(sender)
[preferencesWindow setDelegate: self];
[preferencesWindow makeKeyAndOrderFront: self];
}
//----------------------------------------------------------------------------------------
- (IBAction) closePreferences: (id) sender
{
#pragma unused(sender)
[preferencesWindow close];
[self checkSVNExistence: TRUE];
}
//----------------------------------------------------------------------------------------
- (BOOL) windowShouldClose: (id) sender
{
if (sender == preferencesWindow)
{
[sender makeFirstResponder: sender];
[self performSelector: @selector(checkSVNExistence) withObject: nil afterDelay: 0];
}
return YES;
}
//----------------------------------------------------------------------------------------
- (BOOL) useOldParsingMethod
{
return GetPreferenceBool(kUseOldParsingMethod) || ![self svnHasLibs];
}
//----------------------------------------------------------------------------------------
- (void) setUseOldParsingMethod: (BOOL) on
{
SetPreferenceBool(kUseOldParsingMethod, on);
}
//----------------------------------------------------------------------------------------
- (BOOL) svnHasLibs
{
return (fSvnVersion != 0) && SvnInitialize();
}
//----------------------------------------------------------------------------------------
- (void) setSvnHasLibs: (id) ignore
{
#pragma unused(ignore)
[self setUseOldParsingMethod: GetPreferenceBool(kUseOldParsingMethod)];
}
//----------------------------------------------------------------------------------------
- (UInt32) svnVersionNum
{
return fSvnVersion;
}
//----------------------------------------------------------------------------------------
- (NSString*) svnVersion
{
return fSvnVersion ? [NSString stringWithFormat: @"%u.%u.%u",
fSvnVersion / 1000000, (fSvnVersion / 1000) % 1000, fSvnVersion % 1000]
: nil;
}
//----------------------------------------------------------------------------------------
- (void) setSvnVersion: (NSData*) data
{
char buf[64];
[data getBytes: buf length: sizeof(buf) - 1];
buf[([data length] < sizeof(buf)) ? [data length] : sizeof(buf) - 1] = 0;
// Parse \w[0-9]+.[0-9]+.[0-9]+
UInt32 version = 0;
int i = 0, n, digit;
while (buf[i] && buf[i] <= ' ')
++i;
for (n = 0; (digit = buf[i] - '0') >= 0 && digit <= 9; ++i)
n = n * 10 + digit;
version = n * 1000000;
if (buf[i++] == '.')
{
for (n = 0; (digit = buf[i] - '0') >= 0 && digit <= 9; ++i)
n = n * 10 + digit;
version += n * 1000;
if (buf[i++] == '.')
{
for (n = 0; (digit = buf[i] - '0') >= 0 && digit <= 9; ++i)
n = n * 10 + digit;
version += n;
}
}
extern UInt32 gSvnVersion;
gSvnVersion =
fSvnVersion = version;
// dprintf("version=%u => '%@' %@", version, [self svnVersion], data);
[self setSvnHasLibs: nil]; // Force bindings to update
// Fix-up /opt/subversion/lib if svn >= 1.6.9
NSFileManager* const fm = [NSFileManager defaultManager];
BOOL isDirectory;
if (version >= 1006009 &&
[GetPreference(@"svnBinariesFolder") isEqualToString: @"/opt/subversion/bin"] &&
[fm fileExistsAtPath: @"/opt/subversion/lib" isDirectory: &isDirectory] && isDirectory)
{
[fm createSymbolicLinkAtPath: @"/opt/subversion/lib/libapr-1.0.dylib"
pathContent: @"/usr/lib/libapr-1.0.dylib"];
[fm createSymbolicLinkAtPath: @"/opt/subversion/lib/libaprutil-1.0.dylib"
pathContent: @"/usr/lib/libaprutil-1.0.dylib"];
}
}
//----------------------------------------------------------------------------------------
static BOOL gCanFocus = YES;
- (void) svnCmdPathFocus: (id) sender
{
[[sender window] makeFirstResponder: sender];
gCanFocus = YES;
NSBeep();
}
//----------------------------------------------------------------------------------------
- (IBAction) svnCmdPathChanged: (id) sender
{
if (![self checkSVNExistence: FALSE] && gCanFocus)
{
gCanFocus = NO;
[self performSelector: @selector(svnCmdPathFocus:) withObject: sender afterDelay: 0.125];
}
}
//----------------------------------------------------------------------------------------
#pragma mark -
//----------------------------------------------------------------------------------------
- (BOOL) applicationShouldHandleReopen: (NSApplication*) theApplication
hasVisibleWindows: (BOOL) visibleWindows
{
#pragma unused(theApplication)
if (!visibleWindows)
{
[favoriteWorkingCopies showWindow];
}
return YES;
}
//----------------------------------------------------------------------------------------
- (BOOL) applicationShouldOpenUntitledFile: (NSApplication*) sender
{
#pragma unused(sender)
return NO;
}
//----------------------------------------------------------------------------------------
- (BOOL) application: (NSApplication*) theApplication
openFile: (NSString*) filename
{
#pragma unused(theApplication)
[self displayHistory: filename];
return YES;
}
//----------------------------------------------------------------------------------------
- (void) openRepository: (NSURL*) url user: (NSString*) user pass: (NSString*) pass
{
[repositoriesController openRepositoryBrowser: [url absoluteString] title: [url absoluteString] user: user pass: pass];
}
//----------------------------------------------------------------------------------------
// svnX -> Quit sends this message. Alert user if any R&C messages are uncommitted.
- (IBAction) quitApp: (id) sender
{
#pragma unused(sender)
int unsavedCount = 0;
for_each_obj(en, it, [[NSDocumentController sharedDocumentController] documents])
{
if (ISA(it, MyWorkingCopy))
unsavedCount += [it countUnsavedSubControllers];
}
if (unsavedCount != 0)
{
NSBeep();
NSAlert* alert = [NSAlert alertWithMessageText: @"Quit svnX."
defaultButton: @"Quit"
alternateButton: @"Cancel"
otherButton: nil
informativeTextWithFormat: @"Quitting svnX will close %u uncommitted message%@.",
unsavedCount, (unsavedCount > 1) ? @"s" : @""];
if ([alert runModal] != NSAlertDefaultReturn)
return;
}
[NSApp terminate: nil];
}
//----------------------------------------------------------------------------------------
#pragma mark -
#pragma mark Sparkle Plus delegate methods
//----------------------------------------------------------------------------------------
- (NSMutableArray*) updaterCustomizeProfileInfo: (NSMutableArray*) profileInfo
{
NSString *MACAddress = [self getMACAddress];
NSArray *profileDictObjs = [NSArray arrayWithObjects: @"MACAddr",@"MAC Address", MACAddress, MACAddress, nil];
NSArray *profileDictKeys = [NSArray arrayWithObjects: @"key", @"visibleKey", @"value", @"visibleValue", nil];
[profileInfo addObject: [NSDictionary dictionaryWithObjects: profileDictObjs forKeys: profileDictKeys]];
//NSLog(@"%@", profileInfo);
return profileInfo;
}
//----------------------------------------------------------------------------------------
- (NSString*) getMACAddress
{
EnetData data[10];
UInt32 entryCount = 10;
MACAddress mac;
int err = GetEthernetAddressInfo((EnetData*) &data, &entryCount);
if (err == noErr)
{
NSValue* value = [NSValue valueWithBytes: &data[0].macAddress objCType: @encode(MACAddress)];
[value getValue: &mac];
NSMutableString* s = [NSMutableString string];
for (int i = 0; i < kIOEthernetAddressSize; ++i)
{
[s appendFormat: @"%02X", mac[i]];
if (i < kIOEthernetAddressSize - 1)
[s appendString: @":"];
}
return s;
}
return @"";
}
@end // MyApp
//----------------------------------------------------------------------------------------
#pragma mark -
//----------------------------------------------------------------------------------------
// The text view of the help documentation window
@interface HelpDocView : NSTextView @end
@implementation HelpDocView
- (void) awakeFromNib
{
[[self window] setDelegate: self];
}
//----------------------------------------------------------------------------------------
- (void) windowDidBecomeMain: (NSNotification*) notification
{
#pragma unused(notification)
if ([[self string] length] == 0)
[self readRTFDFromFile: [[NSBundle mainBundle] pathForResource: @"Documentation" ofType: @"rtf"]];
}
//----------------------------------------------------------------------------------------
- (void) windowWillClose: (NSNotification*) notification
{
#pragma unused(notification)
[self setString: @""];
}
@end // HelpDocView
//----------------------------------------------------------------------------------------
// End of MyApp.m