diff --git a/JFTHRingtoneDataController.m b/JFTHRingtoneDataController.m index 0dd242e..450948d 100644 --- a/JFTHRingtoneDataController.m +++ b/JFTHRingtoneDataController.m @@ -17,7 +17,7 @@ - (instancetype)init { if (self = [super init]) { [self loadTweakPlist]; self.shouldWriteITunesRingtonePlist = NO; - DLog(@"Initialized"); + ALog(@"Initialized"); } return self; } @@ -50,14 +50,10 @@ - (void)loadTweakPlist { } - (BOOL)loadRingtonesPlist { - DLog(@"Ringtone Importer: Loading Ringtones.plist"); + ALog(@"Ringtone Importer: Loading Ringtones.plist"); NSError *dataError; NSData *plistData = [NSData dataWithContentsOfFile:RINGTONE_PLIST_PATH options:0 error:&dataError]; - if (!plistData) { - DLog(@"Failed to read itunes plist file: %@",dataError); - return NO; - } if (plistData) { //if plist exists, read it _ringtonesPlist = [NSPropertyListSerialization propertyListWithData:plistData @@ -67,6 +63,9 @@ - (BOOL)loadRingtonesPlist { NSMutableDictionary *ringtones = [[NSMutableDictionary alloc] init]; _ringtonesPlist = [[NSMutableDictionary alloc] init]; [_ringtonesPlist setObject:ringtones forKey:@"Ringtones"]; + + DLog(@"Failed to read itunes plist file (creating new file): %@",dataError); + return NO; } DLog(@"Read itunes plist: %@",_ringtonesPlist); return YES; @@ -79,6 +78,12 @@ - (void)saveTweakPlist { [newData writeToFile:TONEHELPERDATA_PLIST_PATH atomically:YES]; } - (void)saveRingtonesPlist { + // Folder may not exist, try to create it + NSFileManager *localFileManager = [[NSFileManager alloc] init]; + [localFileManager createDirectoryAtPath:@"/var/mobile/Media/iTunes_Control/iTunes/" + withIntermediateDirectories:YES + attributes:nil + error:nil]; //Write plist NSData *newData = [NSPropertyListSerialization dataWithPropertyList: _ringtonesPlist format: NSPropertyListXMLFormat_v1_0 @@ -87,6 +92,7 @@ - (void)saveRingtonesPlist { [newData writeToFile:RINGTONE_PLIST_PATH atomically:YES]; } - (void)save { + ALog(@"Saving plists"); [self saveTweakPlist]; if (self.shouldWriteITunesRingtonePlist) [self saveRingtonesPlist]; @@ -134,7 +140,7 @@ - (void)deleteRingtoneWithGUID:(NSString *)guid { for (NSString *item in ringtones) { if ([[[ringtones objectForKey:item] objectForKey:@"GUID"] isEqualToString:guid]) { - DLog(@"Deleting ringtone: %@",item); + ALog(@"Deleting ringtone: %@",item); NSFileManager *localFileManager = [[NSFileManager alloc] init]; [localFileManager removeItemAtPath:[@"/var/mobile/Media/iTunes_Control/Ringtones" stringByAppendingPathComponent:item] error:nil]; @@ -225,7 +231,7 @@ - (void)addRingtoneToITunesPlist:(NSDictionary *)tone fileName:(NSString *)file + (void)syncPlists:(BOOL)currentITunesWriteStatus { @autoreleasepool { - DLog(@"Syncing plists with currentITunesWriteStatus = %d",currentITunesWriteStatus); + ALog(@"Syncing plists with currentITunesWriteStatus = %d",currentITunesWriteStatus); JFTHRingtoneDataController *toneData = [[JFTHRingtoneDataController alloc] init]; // Need write access to itunes plist diff --git a/JFTHRingtoneImporter.m b/JFTHRingtoneImporter.m index c002b64..32d2a45 100644 --- a/JFTHRingtoneImporter.m +++ b/JFTHRingtoneImporter.m @@ -11,7 +11,7 @@ @implementation JFTHRingtoneImporter - (instancetype)init { if (self = [super init]) { //[self showTextHUD:@"Looking for new ringtones"]; - DLog(@"Ringtone Importer: Init"); + ALog(@"Ringtone Importer: Init"); ringtonesToImport = [[NSMutableDictionary alloc] init]; shouldImportRingtones = NO; @@ -30,7 +30,7 @@ - (instancetype)init { - (void)getRingtoneFilesFromApp:(NSString *)bundleID { - DLog(@"Ringtone Importer: listing app folder for bundle: %@",bundleID); + ALog(@"Ringtone Importer: listing app folder for bundle: %@",bundleID); // TODO: Get apps from preferences. Check if app exist and if folder exists. FBApplicationInfo *appInfo = [LSApplicationProxy applicationProxyForIdentifier:bundleID]; @@ -64,10 +64,11 @@ - (void)getRingtoneFilesFromApp:(NSString *)bundleID { if ([m4rFiles count] > 0) { // Add files to dict - DLog(@"Ringtone Importer: Found ringtones"); + ALog(@"Ringtone Importer: Found ringtones"); [ringtonesToImport setObject:m4rFiles forKey:bundleID]; self.shouldImportRingtones = YES; } else { + ALog(@"Found 0 ringtones to import"); [self showTextHUD:@"No new ringtones to import"]; [_textHUD dismissAfterDelay:3.0 animated:YES]; } @@ -83,7 +84,7 @@ - (void)setShouldImportRingtones:(BOOL)b { - (void)importNewRingtones { - DLog(@"Ringtone Importer: Import called"); + ALog(@"Ringtone Importer: Import called"); [self showTextHUD:@"Importing ringtones..."]; // Loop through files @@ -105,31 +106,56 @@ - (void)importNewRingtones { // Calculate MD5 NSString *m4rFileMD5Hash = [FileHash md5HashOfFileAtPath:[oldDirectory stringByAppendingPathComponent:appDirFile]]; - + NSError *fileCopyError; if ([localFileManager copyItemAtPath:[ oldDirectory stringByAppendingPathComponent:appDirFile] toPath:[RINGTONE_DIRECTORY stringByAppendingPathComponent:newFile] - error:nil]) // Will import again at next run if moving. i dont want that. + error:&fileCopyError]) // Will import again at next run if moving. i dont want that. { //Plist data [_ringtoneData addRingtoneToPlist:baseName file:newFile oldFileName:appDirFile importedFrom:bundleID hash:m4rFileMD5Hash]; DLog(@"File copy success: %@",appDirFile); importedCount++; + } else if ([localFileManager fileExistsAtPath:[RINGTONE_DIRECTORY stringByAppendingPathComponent:newFile]]) { + DLog(@"File already exists, skipping file"); } else { - failedCount++; - DLog(@"File copy (%@) failed",appDirFile); + ALog(@"File copy (%@) failed and it does not exist in target folder: %@",appDirFile, fileCopyError); + // Directory may not exist, try to create it + NSError *dirError; + if ([localFileManager createDirectoryAtPath:RINGTONE_DIRECTORY + withIntermediateDirectories:YES + attributes:nil + error:&dirError]) { + ALog(@"Ringtone folder created"); + if ([localFileManager copyItemAtPath:[ // Lets try again + oldDirectory stringByAppendingPathComponent:appDirFile] + toPath:[RINGTONE_DIRECTORY stringByAppendingPathComponent:newFile] + error:nil]) // Will import again at next run if moving. i dont want that. + { + + //Plist data + [_ringtoneData addRingtoneToPlist:baseName file:newFile oldFileName:appDirFile importedFrom:bundleID hash:m4rFileMD5Hash]; + DLog(@"File copy success: %@",appDirFile); + importedCount++; + } + } else { + ALog(@"Failed to create directory: %@", dirError); + failedCount++; + } + } } } // for loop end [_ringtoneData save]; - if (failedCount == 0) { + if ((failedCount == 0) && (importedCount > 0)) { [self showSuccessHUDText:[NSString stringWithFormat:@"Imported %d tones", importedCount]]; - } else { + [_statusHUD dismissAfterDelay:1.5 animated:YES]; + } else if (failedCount > 0) { [self showErrorHUDText:@"Error when importing tones"]; + [_statusHUD dismissAfterDelay:1.5 animated:YES]; } [_textHUD dismissAfterDelay:2.5 animated:YES]; - [_statusHUD dismissAfterDelay:1.5 animated:YES]; } - (NSString *)createNameFromFile:(NSString *)file { diff --git a/Log.h b/Log.h index 8ae2f96..2cba27f 100644 --- a/Log.h +++ b/Log.h @@ -1,10 +1,10 @@ #import "UIAlertController+Window.m" #ifdef DEBUG -# define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); +# define DLog(fmt, ...) NSLog((@"%s [Line %d JFDEBUG] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); #else # define DLog(...) #endif -#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); +#define ALog(fmt, ...) NSLog((@"%s [Line %d JFDEBUG]" fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); #ifdef DEBUG # define ULog(fmt, ...) { UIAlertController *alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"%s\n [Line %d] ", __PRETTY_FUNCTION__, __LINE__] message:[NSString stringWithFormat:fmt, ##__VA_ARGS__] preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]]; [alert show]; } #else diff --git a/Tweak.xm b/Tweak.xm index 2ae355d..baa5213 100644 --- a/Tweak.xm +++ b/Tweak.xm @@ -31,17 +31,17 @@ HBPreferencesValueChangeCallback updateRingtonePlist = ^(NSString *key, id= 1.12) -Version: 0.2.8 +Version: 0.2.10 Architecture: iphoneos-arm Description: Automatically adds ringtones from Audiko to device Maintainer: Jesper Flodin diff --git a/thprefsbundle/JFTHRingtoneListController.m b/thprefsbundle/JFTHRingtoneListController.m index 2057e20..4660990 100644 --- a/thprefsbundle/JFTHRingtoneListController.m +++ b/thprefsbundle/JFTHRingtoneListController.m @@ -51,7 +51,7 @@ - (id)specifiers { } - (instancetype)init { - DLog(@"Initializing ringtone list"); + ALog(@"Initializing ringtone list"); self = [super init]; _toneData = [[JFTHRingtoneDataController alloc] init]; diff --git a/thprefsbundle/JFTHRootListController.x b/thprefsbundle/JFTHRootListController.x index f8bea27..4fd0038 100644 --- a/thprefsbundle/JFTHRootListController.x +++ b/thprefsbundle/JFTHRootListController.x @@ -88,6 +88,7 @@ } - (void)respring:(PSSpecifier *)specifier { + ALog(@"Respring tapped"); PSTableCell *cell = [self cachedCellForSpecifier:specifier]; // disable the cell, in case it takes a moment diff --git a/thprefsbundle/Resources/Root.plist b/thprefsbundle/Resources/Root.plist index 0145b2e..65d2311 100644 --- a/thprefsbundle/Resources/Root.plist +++ b/thprefsbundle/Resources/Root.plist @@ -142,7 +142,7 @@ Requires Respring! cell PSButtonCell action - respring + respring: label Respring