-
Notifications
You must be signed in to change notification settings - Fork 8
/
GolipseAppDelegate.m
221 lines (185 loc) · 7.54 KB
/
GolipseAppDelegate.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
//
// GolipseAppDelegate.m
// Golipse
//
// Created by David LeBer on 10-10-09.
// Copyright 2010 Align Software Inc. All rights reserved.
//
#import "GolipseAppDelegate.h"
#import "PreferenceWindowController.h"
#import "NSView-LayoutAdditions.h"
@implementation GolipseAppDelegate
@synthesize window;
@synthesize installLocationPicker;
@synthesize installLocationOtherItem;
@synthesize installLocationLabel;
@synthesize installNowButton;
@synthesize logScrollView;
@synthesize logTextView;
@synthesize installTask;
@synthesize logPipe;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[self.window makeKeyAndOrderFront:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(installTaskDidFinish:)
name:NSTaskDidTerminateNotification
object:self.installTask];
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication {
return YES;
}
- (void) awakeFromNib;
{
NSLog(@"Awake");
[self setupInstallLocationWithPath:kCurrentInstallLocation];
self.installLocationLabel.stringValue = NSLocalizedString(@"Install Location", @"Eclipse and WOLips install location");
self.installLocationOtherItem.title = NSLocalizedString(@"Other", "Choose a different install location");
self.installNowButton.title = NSLocalizedString(@"Go!", @"Install Eclipse and WOLips now");
[self.logTextView setEditable:NO];
self.logTextView.font = [NSFont fontWithName:@"Courier" size:0.0];
self.window.title = NSLocalizedString(@"Golipse", @"Golipse main window");
[self.installLocationLabel sizeToFit];
[self.installLocationPicker moveNextToView:self.installLocationLabel withSpace:5.0 stretch:NO];
userDidCancel = NO;
}
#pragma mark -
#pragma mark Install
- (IBAction) installWOLipsAction:(id)sender;
{
if ([self.installNowButton.title isEqualToString:NSLocalizedString(@"Cancel",@"")]) {
NSLog(@"cancel!");
userDidCancel = YES;
[self.installTask terminate];
return;
} else
userDidCancel = NO;
[self.installNowButton setTitle:NSLocalizedString(@"Cancel",@"Cancel the install")];
NSString *launchPath = [[NSBundle mainBundle] pathForResource:@"go_wolips" ofType:@"sh"];
NSLog(@"Script path: %@", launchPath);
NSString *installLocation = [kCurrentInstallLocation stringByAppendingPathComponent:kEclipseName];
NSString *eclipseURL = kEclipseDownloadURL;
NSLog(@"Launch Path: %@ %@ %@", launchPath, installLocation, eclipseURL);
self.installTask = [[NSTask alloc] init];
self.logPipe = [[NSPipe alloc] init];
NSFileHandle *handle = nil;
[self.installTask setLaunchPath:launchPath];
[self.installTask setArguments:[NSArray arrayWithObjects:installLocation, eclipseURL, nil]];
[self.installTask setStandardOutput:self.logPipe];
[self.installTask setStandardError:self.logPipe];
handle = [self.logPipe fileHandleForReading];
[self.installTask launch];
[self performSelectorInBackground:@selector(copyLogData:) withObject:handle];
}
- (void) installTaskDidFinish:(NSNotification *)note;
{
NSLog(@"Did finish: %@", note.userInfo);
[self.installNowButton setTitle:NSLocalizedString(@"Go!", @"Install Eclipse and WOLips now")];
[self.installNowButton setEnabled:YES];
self.installTask = nil;
if (userDidCancel)
{
NSString *logSnippet = NSLocalizedString(@"Install canceled by user.", @"");
[self appendStringToLog:logSnippet];
userDidCancel = NO;
}
}
- (void)copyLogData:(NSFileHandle*)handle {
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
NSData *data;
while([data=[handle availableData] length]) { // until EOF (check reference)
NSString *logSnippet=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[self performSelectorOnMainThread:@selector(appendStringToLog:) withObject:logSnippet waitUntilDone:YES];
[logSnippet release];
}
[pool release];
}
- (void) appendStringToLog:(NSString *)logSnippet;
{
NSRange theEnd=NSMakeRange([[self.logTextView string] length],0);
[self.logTextView replaceCharactersInRange:theEnd withString:logSnippet]; // append new string to the end
theEnd.location+=[logSnippet length]; // the end has moved
[self.logTextView scrollRangeToVisible:theEnd];
}
#pragma mark -
#pragma mark Preference Window
- (IBAction) showPreferenceWindow:(id)sender;
{
PreferenceWindowController *prefsController = [[PreferenceWindowController alloc] initWithWindowNibName:@"PreferenceWindow"];
[prefsController showWindow:[prefsController window]];
}
#pragma mark -
#pragma mark Install Location
- (IBAction)chooseInstallLocation:(id)sender;
{
NSOpenPanel* panel = [NSOpenPanel openPanel];
[panel setCanChooseFiles:NO];
[panel setCanChooseDirectories:YES];
[panel setAllowsMultipleSelection:NO];
if ([panel respondsToSelector:@selector(setCanCreateDirectories:)])
{
[panel setCanCreateDirectories:YES];
}
[panel setPrompt:NSLocalizedString(@"Choose", @"Choose a custom install location")];
[panel beginSheetForDirectory:kCurrentInstallLocation
file:nil
types:nil
modalForWindow:[self window]
modalDelegate:self
didEndSelector:@selector(chooseInstallLocationDidEnd:returnCode:contextInfo:)
contextInfo:nil];
}
- (void)chooseInstallLocationDidEnd: (NSOpenPanel *)panel returnCode: (int)returnCode contextInfo: (void *)contextInfo;
{
if (returnCode == NSFileHandlingPanelOKButton)
{
NSString* newInstallLocation = [[panel filenames] objectAtIndex:0];
[[NSUserDefaults standardUserDefaults] setValue:newInstallLocation forKey:kInstallLocationDefaultsKey];
[self setupInstallLocationWithPath:newInstallLocation];
} else {
[self.installLocationPicker selectItemAtIndex:0]; // Cancel pressed; reselect first item in menu, i.e., the directory
}
}
- (IBAction)chooseDefaultInstallLocation:(id)sender;
{
[[NSUserDefaults standardUserDefaults] setValue:kCurrentInstallLocation forKey:kInstallLocationDefaultsKey];
[self setupInstallLocationWithPath:kCurrentInstallLocation];
}
- (void)setupInstallLocationWithPath:(NSString*)inDLPath;
{
NSMenuItem* placeholder = [self.installLocationPicker itemAtIndex:0];
if (!placeholder)
return;
NSImage* icon = [[NSWorkspace sharedWorkspace] iconForFile:inDLPath];
[icon setScalesWhenResized:YES];
[icon setSize:NSMakeSize(16.0, 16.0)];
[placeholder setTitle:[[NSFileManager defaultManager] displayNameAtPath:inDLPath]];
[placeholder setImage:icon];
[self.installLocationPicker selectItemAtIndex:0];
[self.installLocationPicker sizeToFit];
}
#pragma mark -
+ (void)initialize;
{
NSLog(@"Initializing");
NSString *defaultEclipseURL = [[[NSBundle mainBundle] infoDictionary] objectForKey:kDefaultEclipseDownloadURLKey];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDesktopDirectory, NSUserDomainMask, YES);
NSString *desktopPath = [paths objectAtIndex:0];
[dictionary setObject:desktopPath forKey:kInstallLocationDefaultsKey];
[dictionary setObject:defaultEclipseURL forKey:kEclipseDownloadURLDefaultsKey];
[[NSUserDefaultsController sharedUserDefaultsController] setInitialValues:dictionary];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
}
- (void) dealloc;
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[logPipe release], logPipe = nil;
[installLocationPicker release], installLocationPicker = nil;
[installLocationLabel release], installLocationLabel = nil;
[installNowButton release], installNowButton = nil;
[logScrollView release], logScrollView = nil;
[logTextView release], logTextView = nil;
[installTask release], installTask = nil;
[super dealloc];
}
@end