William Bowling is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this again1 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 | /*
* Adium is the legal property of its developers, whose names are listed in the copyright file included
* with this source distribution.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program; if not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#import "XtrasInstaller.h"
#import <AIUtilities/AIApplicationAdditions.h>
#import <AIUtilities/AIBundleAdditions.h>
#import <AIUtilities/AIStringAdditions.h>
//Should only be YES for testing
#define ALLOW_UNTRUSTED_XTRAS NO
@interface XtrasInstaller ()
- (void)closeInstaller;
- (void)updateInfoText;
@end
/*!
* @class XtrasInstaller
* @brief Class which displays a progress window and downloads an AdiumXtra, decompresses it, and installs it.
*/
@implementation XtrasInstaller
//XtrasInstaller does not autorelease because it will release itself when closed
+ (XtrasInstaller *)installer
{
return [[XtrasInstaller alloc] init];
}
- (id)init
{
if ((self = [super init])) {
download = nil;
window = nil;
}
return self;
}
- (void)dealloc
{
[download release];
[super dealloc];
}
- (IBAction)cancel:(id)sender;
{
if (download) [download cancel];
[self closeInstaller];
}
- (void)sheetDidDismiss:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
[self cancel:nil];
}
- (void)closeInstaller
{
if (window) [window close];
[self autorelease];
}
- (void)installXtraAtURL:(NSURL *)url
{
if ([[url host] isEqualToString:@"www.adiumxtras.com"] || ALLOW_UNTRUSTED_XTRAS) {
NSURL *urlToDownload;
[NSBundle loadNibNamed:@"XtraProgressWindow" owner:self];
[progressBar setUsesThreadedAnimation:YES];
xtraName = nil;
amountDownloaded = 0;
downloadSize = 0;
[progressBar setDoubleValue:0];
[cancelButton setLocalizedString:AILocalizedString(@"Cancel",nil)];
[window setTitle:AILocalizedString(@"Xtra Download",nil)];
[self updateInfoText];
[window makeKeyAndOrderFront:self];
urlToDownload = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@://%@/%@%@%@", @"http", [url host], [url path],
([url query] ? @"?" : @""),
([url query] ? [url query] : @"")]];
// dest = [NSTemporaryDirectory() stringByAppendingPathComponent:[[urlToDownload path] lastPathComponent]];
AILogWithSignature(@"Downloading %@", urlToDownload);
download = [[NSURLDownload alloc] initWithRequest:[NSURLRequest requestWithURL:urlToDownload] delegate:self];
// [download setDestination:dest allowOverwrite:YES];
[urlToDownload release];
} else {
NSRunAlertPanel(AILocalizedString(@"Nontrusted Xtra", nil),
AILocalizedString(@"This Xtra is not hosted by adiumxtras.com. Automatic installation is not allowed.", nil),
AILocalizedString(@"Cancel", nil),
nil, nil);
[self closeInstaller];
}
}
- (void)updateInfoText
{
NSInteger percentComplete = (downloadSize > 0 ? (NSUInteger)(((double)amountDownloaded / (double)downloadSize) * 100.0) : 0);
NSString *installText = [NSString stringWithFormat:AILocalizedString(@"Downloading %@", @"Install an Xtra; %@ is the name of the Xtra."), (xtraName ? xtraName : @"")];
[infoText setStringValue:[NSString stringWithFormat:@"%@ (%lu%%)", installText, percentComplete]];
}
- (void)download:(NSURLDownload *)connection didReceiveResponse:(NSHTTPURLResponse *)response
{
xtraName = [[response allHeaderFields] objectForKey:@"X-Xtraname"];
amountDownloaded = 0;
downloadSize = [response expectedContentLength];
[progressBar setMaxValue:(long long)downloadSize];
[progressBar setDoubleValue:0.0];
AILogWithSignature(@"Beginning download of %@, which has size %ll", [response allHeaderFields], downloadSize);
[self updateInfoText];
}
- (void)download:(NSURLDownload *)connection decideDestinationWithSuggestedFilename:(NSString *)filename
{
NSString * downloadDir = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString uuid]];
[[NSFileManager defaultManager] createDirectoryAtPath:downloadDir withIntermediateDirectories:YES attributes:nil error:NULL];
dest = [downloadDir stringByAppendingPathComponent:filename];
AILogWithSignature(@"Downloading to is %@", dest);
[download setDestination:dest allowOverwrite:YES];
}
- (void)download:(NSURLDownload *)download didReceiveDataOfLength:(NSUInteger)length
{
amountDownloaded += (long long)length;
if (downloadSize != NSURLResponseUnknownLength) {
[progressBar setDoubleValue:(double)amountDownloaded];
[self updateInfoText];
}
else
[progressBar setIndeterminate:YES];
}
- (BOOL)download:(NSURLDownload *)download shouldDecodeSourceDataOfMIMEType:(NSString *)encodingType {
return NO;
}
- (void)download:(NSURLDownload *)inDownload didFailWithError:(NSError *)error {
NSString *errorMsg;
errorMsg = [NSString stringWithFormat:AILocalizedString(@"An error occurred while downloading this Xtra: %@.",nil),[error localizedDescription]];
NSBeginAlertSheet(AILocalizedString(@"Xtra Downloading Error",nil), AILocalizedString(@"Cancel",nil), nil, nil, window, self,
NULL, @selector(sheetDidDismiss:returnCode:contextInfo:), nil, errorMsg);
}
- (void)downloadDidFinish:(NSURLDownload *)download {
NSString *lastPathComponent = [[dest lowercaseString] lastPathComponent];
NSString *pathExtension = [lastPathComponent pathExtension];
BOOL decompressionSuccess = YES, success = NO;
if ([pathExtension isEqualToString:@"tgz"] || [lastPathComponent hasSuffix:@".tar.gz"]) {
NSTask *uncompress, *untar;
uncompress = [[NSTask alloc] init];
[uncompress setLaunchPath:@"/usr/bin/gunzip"];
[uncompress setArguments:[NSArray arrayWithObjects:@"-df" , [dest lastPathComponent] , nil]];
[uncompress setCurrentDirectoryPath:[dest stringByDeletingLastPathComponent]];
@try
{
[uncompress launch];
[uncompress waitUntilExit];
}
@catch(id exc)
{
decompressionSuccess = NO;
}
[uncompress release];
if (decompressionSuccess) {
if ([pathExtension isEqualToString:@"tgz"]) {
dest = [[dest stringByDeletingPathExtension] stringByAppendingPathExtension:@"tar"];
} else {
//hasSuffix .tar.gz
dest = [dest substringToIndex:[dest length] - 3];//remove the .gz, leaving us with .tar
}
untar = [[NSTask alloc] init];
[untar setLaunchPath:@"/usr/bin/tar"];
[untar setArguments:[NSArray arrayWithObjects:@"-xvf", [dest lastPathComponent], nil]];
[untar setCurrentDirectoryPath:[dest stringByDeletingLastPathComponent]];
@try
{
[untar launch];
[untar waitUntilExit];
}
@catch(id exc)
{
decompressionSuccess = NO;
}
[untar release];
}
} else if ([pathExtension isEqualToString:@"zip"]) {
NSTask *unzip;
//First, perform the actual unzipping
unzip = [[NSTask alloc] init];
[unzip setLaunchPath:@"/usr/bin/unzip"];
[unzip setArguments:[NSArray arrayWithObjects:
@"-o", /* overwrite */
@"-q", /* quiet! */
dest, /* source zip file */
@"-d", [dest stringByDeletingLastPathComponent], /*destination folder*/
nil]];
[unzip setCurrentDirectoryPath:[dest stringByDeletingLastPathComponent]];
@try
{
[unzip launch];
[unzip waitUntilExit];
}
@catch(id exc)
{
decompressionSuccess = NO;
}
[unzip release];
} else {
decompressionSuccess = NO;
}
NSFileManager *fileManager = [NSFileManager defaultManager];
NSEnumerator *fileEnumerator;
//Delete the compressed xtra, now that we've decompressed it
#ifdef DEBUG_BUILD
if (decompressionSuccess)
[fileManager removeItemAtPath:dest error:NULL];
#else
[fileManager removeItemAtPath:dest error:NULL];
#endif
dest = [dest stringByDeletingLastPathComponent];
//the remaining files in the directory should be the contents of the xtra
fileEnumerator = [fileManager enumeratorAtPath:dest];
if (decompressionSuccess && fileEnumerator) {
NSSet *supportedDocumentExtensions = [[NSBundle mainBundle] supportedDocumentExtensions];
for (NSString *nextFile in fileEnumerator) {
/* Ignore hidden files and the __MACOSX folder which some compression engines stick into the archive but
* /usr/bin/unzip doesn't handle properly.
*/
if ((![[nextFile lastPathComponent] hasPrefix:@"."]) &&
(![[nextFile pathComponents] containsObject:@"__MACOSX"])) {
NSString *fileExtension = [nextFile pathExtension];
NSEnumerator *supportedDocumentExtensionsEnumerator;
NSString *extension;
BOOL isSupported = NO;
//We want to do a case-insensitive path extension comparison
supportedDocumentExtensionsEnumerator = [supportedDocumentExtensions objectEnumerator];
while (!isSupported &&
(extension = [supportedDocumentExtensionsEnumerator nextObject])) {
isSupported = ([fileExtension caseInsensitiveCompare:extension] == NSOrderedSame);
}
if (isSupported) {
NSString *xtraPath = [dest stringByAppendingPathComponent:nextFile];
//Open the file directly
AILogWithSignature(@"Installing %@",xtraPath);
success = [[NSApp delegate] application:NSApp
openTempFile:xtraPath];
if (!success) {
NSLog(@"Installation Error: %@",xtraPath);
}
}
}
}
} else {
NSLog(@"Installation Error: %@ (%@)",dest, (decompressionSuccess ? @"Decompressed succesfully" : @"Failed to decompress"));
}
//delete our temporary directory, and any files remaining in it
#ifdef DEBUG_BUILD
if (success)
[fileManager removeItemAtPath:dest error:NULL];
#else
[fileManager removeItemAtPath:dest error:NULL];
#endif
[self closeInstaller];
}
@end
|