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
|
/*
Preferences.m
Zipper
Copyright (C) 2012 Free Software Foundation, Inc
Authors: Dirk Olmes <dirk@xanthippe.ping.de>
Riccardo Mottola <rm@gnu.org>
This application 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
*/
#import <Foundation/Foundation.h>
#import "Preferences.h"
#import "NSFileManager+Custom.h"
#define X_MISSING_PREF @"MissingPreferenceException"
#define X_WRONG_PREF @"WrongPreferenceException"
@interface Preferences (PrivateAPI)
+ (NSString *)stringForKey:(NSString *)key;
+ (BOOL)boolForKey:(NSString *)key;
+ (void)checkExecutable:(NSString *)executable withName:(NSString *)name;
@end
/**
* This class encapsulates the access to the app's preferences. It faciliates providing a
* Dictionary that will be used instead of NSUserDefaults and searching the PATH environment
* variable.
*/
@implementation Preferences : NSObject
/**
* To faciliate unit testing it's possible to provide the Preferences class with an NSDictionary
* that makes up the preferences.
*/
static NSDictionary *_replacementPrefs = nil;
/**
* Additional Preferences loaded from PropertyList file
*/
static NSDictionary *_plistPrefs;
/**
* This is the mapping between file extensions and tar's extract option. This option differs
* from platform to platform. In order to encapsulate this, Preferences manages this mapping
* and clients can ask for a compression argument with <code>compressionArgumentForFile:</code>
*/
static NSMutableDictionary *_extensionMapping = nil;
+ (void)initialize
{
NSString *path;
if (_extensionMapping == nil)
{
_extensionMapping = [[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"", @"tar",
@"-z", @"gz",
@"-z", @"tgz",
@"-j", @"bz2",
nil] retain];
}
// see if there's a property list containing preferences to use
path = [[NSBundle bundleForClass:self] pathForResource:@"DefaultPreferences" ofType:@"plist"];
if (path != nil)
{
_plistPrefs = [[NSDictionary dictionaryWithContentsOfFile:path] retain];
}
}
+ (void)usePreferences:(NSDictionary *)newPrefs;
{
[_replacementPrefs release];
_replacementPrefs = newPrefs;
[_replacementPrefs retain];
}
+ (NSString *)tarExecutable;
{
NSString *tar = [self stringForKey:PREF_KEY_TAR];
if (tar == nil)
{
// search the PATH
tar = [[NSFileManager defaultManager] locateExecutable:@"tar"];
}
return tar;
}
+ (void)setTarExecutable:(NSString *)newTar
{
if (newTar != nil)
{
[[NSUserDefaults standardUserDefaults] setObject:newTar forKey:PREF_KEY_TAR];
}
}
+ (BOOL)isBsdTar;
{
return [self boolForKey:PREF_KEY_BSD_TAR];
}
+ (void)setIsBsdTar:(BOOL)flag
{
[[NSUserDefaults standardUserDefaults] setBool:flag forKey:PREF_KEY_BSD_TAR];
if (flag == YES)
{
// on BSD tar also uses -z for extracting .bz archives
[_extensionMapping setObject:@"-z" forKey:@"bz2"];
}
else
{
[_extensionMapping setObject:@"-j" forKey:@"bz2"];
}
}
+ (NSString *)zipExecutable;
{
NSString *zip = [self stringForKey:PREF_KEY_ZIP];
if (zip == nil)
{
zip = [[NSFileManager defaultManager] locateExecutable:@"zip"];
}
return zip;
}
+ (void)setZipExecutable:(NSString *)newZip
{
if (newZip != nil)
{
[[NSUserDefaults standardUserDefaults] setObject:newZip forKey:PREF_KEY_ZIP];
}
}
+ (NSString *)unzipExecutable;
{
NSString *unzip = [self stringForKey:PREF_KEY_UNZIP];
if (unzip == nil)
{
unzip = [[NSFileManager defaultManager] locateExecutable:@"unzip"];
}
return unzip;
}
+ (void)setUnzipExecutable:(NSString *)newUnzip
{
if (newUnzip != nil)
{
[[NSUserDefaults standardUserDefaults] setObject:newUnzip forKey:PREF_KEY_UNZIP];
}
}
+ (NSString *)sevenZipExecutable;
{
NSString *zip = [self stringForKey:PREF_KEY_SEVEN_ZIP];
if (zip == nil)
{
zip = [[NSFileManager defaultManager] locateExecutable:@"7z"];
// corner case: only 7za may be available on the system
if (zip == nil)
{
zip = [[NSFileManager defaultManager] locateExecutable:@"7za"];
}
}
return zip;
}
+ (void)setSevenZipExecutable:(NSString *)new7zip
{
if (new7zip != nil)
{
[[NSUserDefaults standardUserDefaults] setObject:new7zip forKey:PREF_KEY_SEVEN_ZIP];
}
}
+ (NSString *)rarExecutable;
{
NSString *rar = [self stringForKey:PREF_KEY_RAR];
if (rar == nil)
{
rar = [[NSFileManager defaultManager] locateExecutable:@"unrar"];
}
return rar;
}
+ (void)setRarExecutable:(NSString *)newRar;
{
if (newRar != nil)
{
[[NSUserDefaults standardUserDefaults] setObject:newRar forKey:PREF_KEY_RAR];
}
}
+ (NSString *)lhaExecutable
{
NSString *lha = [self stringForKey:PREF_KEY_LHA];
if (lha == nil)
{
lha = [[NSFileManager defaultManager] locateExecutable:@"lha"];
}
return lha;
}
+ (void)setLhaExecutable:(NSString *)newLha;
{
if (newLha != nil)
{
[[NSUserDefaults standardUserDefaults] setObject:newLha forKey:PREF_KEY_LHA];
}
}
+ (NSString *)lzxExecutable
{
NSString *lzx = [self stringForKey:PREF_KEY_LZX];
if (lzx == nil)
{
lzx = [[NSFileManager defaultManager] locateExecutable:@"unlzx"];
}
return lzx;
}
+ (void)setLzxExecutable:(NSString *)newLzx;
{
if (newLzx != nil)
{
[[NSUserDefaults standardUserDefaults] setObject:newLzx forKey:PREF_KEY_LZX];
}
}
+ (NSString *)gzipExecutable
{
NSString *gzip = [self stringForKey:PREF_KEY_GZIP];
if (gzip == nil)
{
gzip = [[NSFileManager defaultManager] locateExecutable:@"gzip"];
}
return gzip;
}
+ (void)setGzipExecutable:(NSString *)newGzip
{
if (newGzip != nil)
{
[[NSUserDefaults standardUserDefaults] setObject:newGzip forKey:PREF_KEY_GZIP];
}
}
+ (NSString *)gunzipExecutable
{
NSString *gunzip = [self stringForKey:PREF_KEY_GUNZIP];
if (gunzip == nil)
{
gunzip = [[NSFileManager defaultManager] locateExecutable:@"gunzip"];
}
return gunzip;
}
+ (void)setGunzipExecutable:(NSString *)newGunzip
{
if (newGunzip != nil)
{
[[NSUserDefaults standardUserDefaults] setObject:newGunzip forKey:PREF_KEY_GUNZIP];
}
}
+ (NSString *)bzip2Executable
{
NSString *bzip2 = [self stringForKey:PREF_KEY_BZIP2];
if (bzip2 == nil)
{
bzip2 = [[NSFileManager defaultManager] locateExecutable:@"bzip2"];
}
return bzip2;
}
+ (void)setBzip2Executable:(NSString *)newBzip2
{
if (newBzip2 != nil)
{
[[NSUserDefaults standardUserDefaults] setObject:newBzip2 forKey:PREF_KEY_BZIP2];
}
}
+ (NSString *)bunzip2Executable
{
NSString *bunzip2 = [self stringForKey:PREF_KEY_BUNZIP2];
if (bunzip2 == nil)
{
bunzip2 = [[NSFileManager defaultManager] locateExecutable:@"bunzip2"];
}
return bunzip2;
}
+ (void)setBunzip2Executable:(NSString *)newBunzip2
{
if (newBunzip2 != nil)
{
[[NSUserDefaults standardUserDefaults] setObject:newBunzip2 forKey:PREF_KEY_BUNZIP2];
}
}
+ (NSString *)unarjExecutable
{
NSString *unarj = [self stringForKey:PREF_KEY_UNARJ];
if (unarj == nil)
{
unarj = [[NSFileManager defaultManager] locateExecutable:@"unarj"];
}
return unarj;
}
+ (void)setUnarjExecutable:(NSString *)newUnarj
{
if (newUnarj != nil)
{
[[NSUserDefaults standardUserDefaults] setObject:newUnarj forKey:PREF_KEY_UNARJ];
}
}
+ (NSString *)unaceExecutable
{
NSString *unace = [self stringForKey:PREF_KEY_UNACE];
if (unace == nil)
{
unace = [[NSFileManager defaultManager] locateExecutable:@"unace"];
}
return unace;
}
+ (void)setUnaceExecutable:(NSString *)newUnace
{
if (newUnace != nil)
{
[[NSUserDefaults standardUserDefaults] setObject:newUnace forKey:PREF_KEY_UNACE];
}
}
+ (NSString *)zooExecutable
{
NSString *zoo = [self stringForKey:PREF_KEY_ZOO];
if (zoo == nil)
{
zoo = [[NSFileManager defaultManager] locateExecutable:@"zoo"];
}
return zoo;
}
+ (void)setZooExecutable:(NSString *)newZoo
{
if (newZoo != nil)
{
[[NSUserDefaults standardUserDefaults] setObject:newZoo forKey:PREF_KEY_ZOO];
}
}
+ (NSString *)xzExecutable
{
NSString *xz = [self stringForKey:PREF_KEY_XZ];
if (xz == nil)
{
xz = [[NSFileManager defaultManager] locateExecutable:@"xz"];
}
return xz;
}
+ (void)setXzExecutable:(NSString *)newXz
{
if (newXz != nil)
{
[[NSUserDefaults standardUserDefaults] setObject:newXz forKey:PREF_KEY_XZ];
}
}
+ (NSString *)lastOpenDirectory
{
return [self stringForKey:PREF_KEY_OPEN_DIR];
}
+ (void)setLastOpenDirectory:(NSString *)path;
{
[[NSUserDefaults standardUserDefaults] setObject:path forKey:PREF_KEY_OPEN_DIR];
}
+ (NSString *)lastExtractDirectory;
{
return [self stringForKey:PREF_KEY_EXTRACT_DIR];
}
+ (void)setLastExtractDirectory:(NSString *)path;
{
[[NSUserDefaults standardUserDefaults] setObject:path forKey:PREF_KEY_EXTRACT_DIR];
}
+ (NSString *)compressionArgumentForFile:(NSString *)fileName
{
if (fileName != nil)
{
return [_extensionMapping objectForKey:[fileName pathExtension]];
}
return nil;
}
/**
* Returns the name of the app that will be used to open files that don't have a
* pathExtension.
*/
+ (NSString *)defaultOpenApp;
{
return [self stringForKey:PREF_KEY_DEFAULT_OPEN_APP];
}
+ (void)setDefaultOpenApp:(NSString *)path;
{
[[NSUserDefaults standardUserDefaults] setObject:path forKey:PREF_KEY_DEFAULT_OPEN_APP];
}
+ (void)save
{
[[NSUserDefaults standardUserDefaults] synchronize];
}
//------------------------------------------------------------------------------
// private API
//------------------------------------------------------------------------------
+ (NSString *)stringForKey:(NSString *)key;
{
NSString *value;
if (_replacementPrefs != nil)
{
return [_replacementPrefs objectForKey:key];
}
value = [[NSUserDefaults standardUserDefaults] stringForKey:key];
if ((value == nil) && (_plistPrefs != nil))
{
value = [_plistPrefs objectForKey:key];
}
return value;
}
+ (BOOL)boolForKey:(NSString *)key
{
if (_replacementPrefs != nil)
{
NSString *value = [_replacementPrefs objectForKey:key];
return [value isEqual:@"YES"];
}
return [[NSUserDefaults standardUserDefaults] boolForKey:key];
}
@end
|