Skip to content

Commit

Permalink
Code cleanup: Use standard method for getting application support dir…
Browse files Browse the repository at this point in the history
…ectory.
  • Loading branch information
skovatch committed Jan 19, 2019
1 parent 9e304e8 commit ba2fec0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 53 deletions.
59 changes: 19 additions & 40 deletions AppController.m
Expand Up @@ -134,26 +134,17 @@ - (instancetype)init {
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"ForceHLSBBCVideo"];

//Make sure Application Support folder exists
NSString *folder = @"~/Library/Application Support/Get iPlayer Automator/";
folder = folder.stringByExpandingTildeInPath;
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:folder])
{
[fileManager createDirectoryAtPath:folder withIntermediateDirectories:NO attributes:nil error:nil];
}
[fileManager changeCurrentDirectoryPath:folder];
NSString *appSupportDirectory = [[NSFileManager defaultManager] applicationSupportDirectory];
[[NSFileManager defaultManager] changeCurrentDirectoryPath:appSupportDirectory];

//Install Plugins If Needed
NSString *pluginPath = [folder stringByAppendingPathComponent:@"plugins"];
if (/*![fileManager fileExistsAtPath:pluginPath]*/TRUE)
{
[_logger addToLog:@"Installing/Updating Get_iPlayer Plugins..." :self];
NSString *providedPath = [NSBundle mainBundle].bundlePath;
if ([fileManager fileExistsAtPath:pluginPath]) [fileManager removeItemAtPath:pluginPath error:NULL];
providedPath = [providedPath stringByAppendingPathComponent:@"/Contents/Resources/plugins"];
[fileManager copyItemAtPath:providedPath toPath:pluginPath error:nil];
}

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *pluginPath = [appSupportDirectory stringByAppendingPathComponent:@"plugins"];
[_logger addToLog:@"Installing/Updating Get_iPlayer Plugins..." :self];
NSString *providedPath = [NSBundle mainBundle].bundlePath;
if ([fileManager fileExistsAtPath:pluginPath]) [fileManager removeItemAtPath:pluginPath error:NULL];
providedPath = [providedPath stringByAppendingPathComponent:@"/Contents/Resources/plugins"];
[fileManager copyItemAtPath:providedPath toPath:pluginPath error:nil];

//Initialize Arguments
_getiPlayerPath = [[NSString alloc] initWithString:[NSBundle mainBundle].bundlePath];
Expand Down Expand Up @@ -192,19 +183,14 @@ - (void)awakeFromNib
//Read Queue & Series-Link from File
NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *folder = @"~/Library/Application Support/Get iPlayer Automator/";
folder = folder.stringByExpandingTildeInPath;
if ([fileManager fileExistsAtPath: folder] == NO)
{
[fileManager createDirectoryAtPath:folder withIntermediateDirectories:NO attributes:nil error:nil];
}
NSString *appSupportFolder = [[NSFileManager defaultManager] applicationSupportDirectory];

// remove obsolete cache files
[fileManager removeItemAtPath:[folder stringByAppendingPathComponent:@"ch4.cache"] error:nil];
[fileManager removeItemAtPath:[folder stringByAppendingPathComponent:@"podcast.cache"] error:nil];
[fileManager removeItemAtPath:[appSupportFolder stringByAppendingPathComponent:@"ch4.cache"] error:nil];
[fileManager removeItemAtPath:[appSupportFolder stringByAppendingPathComponent:@"podcast.cache"] error:nil];

NSString *filename = @"Queue.automatorqueue";
NSString *filePath = [folder stringByAppendingPathComponent:filename];
NSString *filePath = [appSupportFolder stringByAppendingPathComponent:filename];

NSDictionary * rootObject;
@try
Expand All @@ -229,7 +215,7 @@ - (void)awakeFromNib
//Read Format Preferences

filename = @"Formats.automatorqueue";
filePath = [folder stringByAppendingPathComponent:filename];
filePath = [appSupportFolder stringByAppendingPathComponent:filename];

@try
{
Expand Down Expand Up @@ -261,7 +247,7 @@ - (void)awakeFromNib
}

filename = @"ITVFormats.automator";
filePath = [folder stringByAppendingPathComponent:filename];
filePath = [appSupportFolder stringByAppendingPathComponent:filename];
@try {
rootObject = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
[_itvFormatController addObjects:[rootObject valueForKey:@"itvFormats"]];
Expand Down Expand Up @@ -1656,16 +1642,9 @@ - (void)saveAppData
}

}
NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *folder = @"~/Library/Application Support/Get iPlayer Automator/";
folder = folder.stringByExpandingTildeInPath;
if ([fileManager fileExistsAtPath: folder] == NO)
{
[fileManager createDirectoryAtPath:folder withIntermediateDirectories:NO attributes:nil error:nil];
}
NSString *appSupportFolder = [[NSFileManager defaultManager] applicationSupportDirectory];
NSString *filename = @"Queue.automatorqueue";
NSString *filePath = [folder stringByAppendingPathComponent:filename];
NSString *filePath = [appSupportFolder stringByAppendingPathComponent:filename];

NSMutableDictionary * rootObject;
rootObject = [NSMutableDictionary dictionary];
Expand All @@ -1676,7 +1655,7 @@ - (void)saveAppData
[NSKeyedArchiver archiveRootObject: rootObject toFile: filePath];

filename = @"Formats.automatorqueue";
filePath = [folder stringByAppendingPathComponent:filename];
filePath = [appSupportFolder stringByAppendingPathComponent:filename];

rootObject = [NSMutableDictionary dictionary];

Expand All @@ -1685,7 +1664,7 @@ - (void)saveAppData
[NSKeyedArchiver archiveRootObject:rootObject toFile:filePath];

filename = @"ITVFormats.automator";
filePath = [folder stringByAppendingPathComponent:filename];
filePath = [appSupportFolder stringByAppendingPathComponent:filename];
rootObject = [NSMutableDictionary dictionary];
rootObject[@"itvFormats"] = _itvFormatController.arrangedObjects;
[NSKeyedArchiver archiveRootObject:rootObject toFile:filePath];
Expand Down
7 changes: 1 addition & 6 deletions BBCDownload.m
Expand Up @@ -95,12 +95,7 @@ - (instancetype)initWithProgramme:(Programme *)tempShow tvFormats:(NSArray *)tvF

//We don't want this to refresh now!
NSString *cacheExpiryArg = @"-e604800000000";
NSString *appSupportFolder = (@"~/Library/Application Support/Get iPlayer Automator/").stringByExpandingTildeInPath;
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: appSupportFolder])
{
[fileManager createDirectoryAtPath:appSupportFolder withIntermediateDirectories:NO attributes:nil error:nil];
}
NSString *appSupportFolder = [[NSFileManager defaultManager] applicationSupportDirectory];
_profileDirArg = [[NSString alloc] initWithFormat:@"--profile-dir=%@", appSupportFolder];

//Add Arguments that can't be NULL
Expand Down
8 changes: 4 additions & 4 deletions DownloadHistoryController.m
Expand Up @@ -9,6 +9,7 @@
#import "DownloadHistoryController.h"
#import "DownloadHistoryEntry.h"
#import "Programme.h"
#import "NSFileManager+DirectoryLocations.h"


@implementation DownloadHistoryController
Expand All @@ -25,8 +26,8 @@ - (void)readHistory:(id)sender
if ([historyArrayController.arrangedObjects count] > 0)
[historyArrayController removeObjectsAtArrangedObjectIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [historyArrayController.arrangedObjects count])]];

NSString *historyFilePath = @"~/Library/Application Support/Get iPlayer Automator/download_history";
NSFileHandle *historyFile = [NSFileHandle fileHandleForReadingAtPath:historyFilePath.stringByExpandingTildeInPath];
NSString *historyFilePath = [[[NSFileManager defaultManager] applicationSupportDirectory] stringByAppendingPathComponent:@"download_history"];
NSFileHandle *historyFile = [NSFileHandle fileHandleForReadingAtPath:historyFilePath];
NSData *historyFileData = [historyFile readDataToEndOfFile];
NSString *historyFileInfo = [[NSString alloc] initWithData:historyFileData encoding:NSUTF8StringEncoding];

Expand Down Expand Up @@ -87,8 +88,7 @@ - (IBAction)writeHistory:(id)sender
{
[historyString appendFormat:@"%@\n", [entry entryString]];
}
NSString *historyPath = @"~/Library/Application Support/Get iPlayer Automator/download_history";
historyPath = historyPath.stringByExpandingTildeInPath;
NSString *historyPath = [[[NSFileManager defaultManager] applicationSupportDirectory] stringByAppendingPathComponent:@"download_history"];
NSData *historyData = [historyString dataUsingEncoding:NSUTF8StringEncoding];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:historyPath])
Expand Down
5 changes: 3 additions & 2 deletions LogController.m
Expand Up @@ -7,6 +7,7 @@
//

#import "LogController.h"
#import "NSFileManager+DirectoryLocations.h"

@implementation LogController

Expand All @@ -19,8 +20,8 @@ - (instancetype)init
NSString *initialLog = [NSString stringWithFormat:@"Get iPlayer Automator %@ Initialized.", version];
[self addToLog:initialLog :nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addToLogNotification:) name:@"AddToLog" object:nil];

NSString *filePath = (@"~/Library/Application Support/Get iPlayer Automator/log.txt").stringByExpandingTildeInPath;
NSString *filePath = [[[NSFileManager defaultManager] applicationSupportDirectory] stringByAppendingPathComponent:@"log.txt"];

[[NSFileManager defaultManager] createFileAtPath:filePath
contents:nil
attributes:nil];
Expand Down
2 changes: 1 addition & 1 deletion Programme.m
Expand Up @@ -202,7 +202,7 @@ -(void)proxyRetrievalFinished:(id)sender proxyDict:(NSDictionary *)proxyDict
_pipe = [[NSPipe alloc] init];

_metadataTask.launchPath = @"/usr/bin/perl";
NSString *profileDirPath = [@"~/Library/Application Support/Get iPlayer Automator/" stringByExpandingTildeInPath];
NSString *profileDirPath = [[NSFileManager defaultManager] applicationSupportDirectory];
NSString *profileArg = [NSString stringWithFormat:@"--profile-dir=%@", profileDirPath];

NSMutableArray *args = [NSMutableArray arrayWithArray:@[[[NSBundle mainBundle] pathForResource:@"get_iplayer" ofType:nil],
Expand Down

0 comments on commit ba2fec0

Please sign in to comment.