Skip to content

Commit

Permalink
Fixed a bug where import fails if Ringtone folder does not exist. Ugl…
Browse files Browse the repository at this point in the history
…y code, but im so tired...
  • Loading branch information
Jesperflodin1 committed Aug 20, 2018
1 parent cd996d0 commit b598472
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 29 deletions.
22 changes: 14 additions & 8 deletions JFTHRingtoneDataController.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ - (instancetype)init {
if (self = [super init]) {
[self loadTweakPlist];
self.shouldWriteITunesRingtonePlist = NO;
DLog(@"Initialized");
ALog(@"Initialized");
}
return self;
}
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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];
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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
Expand Down
48 changes: 37 additions & 11 deletions JFTHRingtoneImporter.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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];

Expand Down Expand Up @@ -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];
}
Expand All @@ -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
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions Log.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 6 additions & 5 deletions Tweak.xm
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ HBPreferencesValueChangeCallback updateRingtonePlist = ^(NSString *key, id<NSCop
// Test with both Audiko Lite and Pro
%group ToneHelper

%hook TLToneManager
%hook PSUISoundsPrefController

//Gets called once when opening the ringtone settings
-(void)_loadITunesRingtoneInfoPlistAtPath:(id)arg1 {
-(id)init {
if ([[[NSBundle mainBundle] bundleIdentifier] isEqualToString:@"com.apple.Preferences"]) {
DLog(@"In preferences");
if (!kEnabled) {
DLog(@"Disabled");
return %orig;
}
DLog(@"Enabled");
ALog(@"Enabled");
//We're in preferences app, lets look for new ringtones to import
JFTHRingtoneImporter *importer = [[JFTHRingtoneImporter alloc] init];

Expand All @@ -64,7 +64,8 @@ HBPreferencesValueChangeCallback updateRingtonePlist = ^(NSString *key, id<NSCop
}
return %orig;
}

%end
%hook TLToneManager

-(NSMutableArray *)_tonesFromManifestPath:(NSPathStore2 *)arg1 mediaDirectoryPath:(NSPathStore2 *)arg2 {
if (!kEnabled || kWriteITunesRingtonePlist) {
Expand Down Expand Up @@ -120,7 +121,7 @@ HBPreferencesValueChangeCallback updateRingtonePlist = ^(NSString *key, id<NSCop
[bundleID isEqualToString:@"com.apple.MobileAddressBook"] ||
[bundleID isEqualToString:@"com.apple.mobilemail"] ||
[bundleID isEqualToString:@"com.apple.mobiletimer"]) {
DLog(@"Initializing ToneHelper");
ALog(@"Initializing ToneHelper");
preferences = [[HBPreferences alloc] initWithIdentifier:@"fi.flodin.tonehelper"];

[preferences registerBool:&kEnabled default:NO forKey:@"kEnabled"];
Expand Down
2 changes: 1 addition & 1 deletion layout/DEBIAN/control
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: fi.flodin.tonehelper
Name: ToneHelper
Depends: mobilesubstrate, ws.hbang.common (>= 1.12)
Version: 0.2.8
Version: 0.2.10
Architecture: iphoneos-arm
Description: Automatically adds ringtones from Audiko to device
Maintainer: Jesper Flodin <[email protected]>
Expand Down
2 changes: 1 addition & 1 deletion thprefsbundle/JFTHRingtoneListController.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ - (id)specifiers {
}

- (instancetype)init {
DLog(@"Initializing ringtone list");
ALog(@"Initializing ringtone list");
self = [super init];
_toneData = [[JFTHRingtoneDataController alloc] init];

Expand Down
1 change: 1 addition & 0 deletions thprefsbundle/JFTHRootListController.x
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion thprefsbundle/Resources/Root.plist
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ Requires Respring!</string>
<key>cell</key>
<string>PSButtonCell</string>
<key>action</key>
<string>respring</string>
<string>respring:</string>
<key>label</key>
<string>Respring</string>
</dict>
Expand Down

0 comments on commit b598472

Please sign in to comment.