├── .gitignore ├── .gitmodules ├── LICENSE ├── Makefile ├── PreferenceLoader.plist ├── README.md ├── Tweak.xm ├── control ├── debug.h ├── prefs.h └── prefs.xm /.gitignore: -------------------------------------------------------------------------------- 1 | obj 2 | _ 3 | .theos 4 | debs 5 | *.deb 6 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "framework"] 2 | path = framework 3 | url = git://github.com/DHowett/theos.git 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | export TARGET=iphone:2.0:2.0 2 | include framework/makefiles/common.mk 3 | 4 | LIBRARY_NAME = libprefs 5 | libprefs_LOGOSFLAGS = -c generator=internal 6 | libprefs_FILES = prefs.xm 7 | libprefs_FRAMEWORKS = UIKit 8 | libprefs_PRIVATE_FRAMEWORKS = Preferences 9 | libprefs_CFLAGS = -I. 10 | libprefs_COMPATIBILITY_VERSION = 2.2.0 11 | libprefs_LIBRARY_VERSION = $(shell echo "$(THEOS_PACKAGE_BASE_VERSION)" | cut -d'~' -f1) 12 | libprefs_LDFLAGS = -compatibility_version $($(THEOS_CURRENT_INSTANCE)_COMPATIBILITY_VERSION) 13 | libprefs_LDFLAGS += -current_version $($(THEOS_CURRENT_INSTANCE)_LIBRARY_VERSION) 14 | 15 | TWEAK_NAME = PreferenceLoader 16 | PreferenceLoader_FILES = Tweak.xm 17 | PreferenceLoader_FRAMEWORKS = UIKit 18 | PreferenceLoader_PRIVATE_FRAMEWORKS = Preferences 19 | PreferenceLoader_LIBRARIES = prefs 20 | PreferenceLoader_CFLAGS = -I. 21 | PreferenceLoader_LDFLAGS = -L$(THEOS_OBJ_DIR) 22 | 23 | include $(THEOS_MAKE_PATH)/library.mk 24 | include $(THEOS_MAKE_PATH)/tweak.mk 25 | 26 | after-libprefs-stage:: 27 | $(ECHO_NOTHING)mkdir -p $(THEOS_STAGING_DIR)/usr/include/libprefs$(ECHO_END) 28 | $(ECHO_NOTHING)cp prefs.h $(THEOS_STAGING_DIR)/usr/include/libprefs/prefs.h$(ECHO_END) 29 | 30 | after-stage:: 31 | find $(THEOS_STAGING_DIR) -iname '*.plist' -exec plutil -convert binary1 {} \; 32 | $(FAKEROOT) chown -R 0:80 $(THEOS_STAGING_DIR) 33 | mkdir -p $(THEOS_STAGING_DIR)/Library/PreferenceBundles $(THEOS_STAGING_DIR)/Library/PreferenceLoader/Preferences 34 | 35 | after-install:: 36 | install.exec "killall -9 Preferences" 37 | -------------------------------------------------------------------------------- /PreferenceLoader.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Filter 6 | 7 | Bundles 8 | 9 | com.apple.Preferences 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This is a drop-in replacement for Thomas Moore's PreferenceLoader project. 2 | I personally found this necessary, as there were various things about the existing PreferenceLoader I did not like. 3 | 4 | ### Complaints about the Original ### 5 | * unnecessary hooking 6 | * +[NSData initWithContentsOfFile:] 7 | * -[NSBundle pathForResource:ofType:] 8 | * -[PSSpecifier setupIconImageWithPath:] 9 | * Due to the way PreferenceLoader was implemented (intercepting the toplevel settings plists as they were read from disk and inserting our data directly into them (!)) it had certain filenames hardcoded, such as Settings-(iPhone|iPod).plist, and wouldn't work for other devices. 10 | * Due to the broad hooks above, all sorts of things that don't need to be intercepted are. Every resource path calculated from a bundle gets extra checks tacked onto it, and every NSData-read-from-file gets a filename check. 11 | * Its current lack of compatibility with iPhoneOS 3.2 and the iPad (relies on a removed ivar, and Settings-Wildcat.plist is not processed.) 12 | 13 | ### What this Replacement offers ### 14 | * source availability 15 | * error recovery (loading a bad bundle results in a simple "There was an error loading ..." instead of a blank preferences panel) 16 | * cleaner design 17 | * iPhoneOS 3.2 and iPad compatibility. 18 | 19 | ### License ### 20 | PreferenceLoader is made available under the provisions of the GNU Lesser General Public License (LGPL), version 3. 21 | -------------------------------------------------------------------------------- /Tweak.xm: -------------------------------------------------------------------------------- 1 | #import 2 | #import 3 | 4 | #import "prefs.h" 5 | 6 | #define DEBUG_TAG "PreferenceLoader" 7 | #import "debug.h" 8 | 9 | /* {{{ Imports (Preferences.framework) */ 10 | // Weak (3.2+, dlsym) 11 | static NSString **pPSTableCellUseEtchedAppearanceKey = NULL; 12 | /* }}} */ 13 | 14 | /* {{{ UIDevice 3.2 Additions */ 15 | @interface UIDevice (iPad) 16 | - (BOOL)isWildcat; 17 | @end 18 | /* }}} */ 19 | 20 | /* {{{ Locals */ 21 | static BOOL _Firmware_lt_60 = NO; 22 | /* }}} */ 23 | 24 | %hook PrefsListController 25 | static NSMutableArray *_loadedSpecifiers = nil; 26 | static int _extraPrefsGroupSectionID = 0; 27 | 28 | /* {{{ iPad Hooks */ 29 | %group iPad 30 | - (NSString *)tableView:(id)view titleForHeaderInSection:(int)section { 31 | if([_loadedSpecifiers count] == 0) return %orig; 32 | if(section == _extraPrefsGroupSectionID) return _Firmware_lt_60 ? @"Extensions" : NULL; 33 | return %orig; 34 | } 35 | 36 | - (float)tableView:(id)view heightForHeaderInSection:(int)section { 37 | if([_loadedSpecifiers count] == 0) return %orig; 38 | if(section == _extraPrefsGroupSectionID) return _Firmware_lt_60 ? 22.0f : 10.f; 39 | return %orig; 40 | } 41 | %end 42 | /* }}} */ 43 | 44 | static NSInteger PSSpecifierSort(PSSpecifier *a1, PSSpecifier *a2, void *context) { 45 | NSString *string1 = [a1 name]; 46 | NSString *string2 = [a2 name]; 47 | return [string1 localizedCaseInsensitiveCompare:string2]; 48 | } 49 | 50 | - (id)specifiers { 51 | bool first = (MSHookIvar(self, "_specifiers") == nil); 52 | if(first) { 53 | PLLog(@"initial invocation for -specifiers"); 54 | %orig; 55 | [_loadedSpecifiers release]; 56 | _loadedSpecifiers = [[NSMutableArray alloc] init]; 57 | NSArray *subpaths = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:@"/Library/PreferenceLoader/Preferences" error:NULL]; 58 | for(NSString *item in subpaths) { 59 | if(![[item pathExtension] isEqualToString:@"plist"]) continue; 60 | PLLog(@"processing %@", item); 61 | NSString *fullPath = [NSString stringWithFormat:@"/Library/PreferenceLoader/Preferences/%@", item]; 62 | NSDictionary *plPlist = [NSDictionary dictionaryWithContentsOfFile:fullPath]; 63 | if(![PSSpecifier environmentPassesPreferenceLoaderFilter:[plPlist objectForKey:@"filter"] ?: [plPlist objectForKey:PLFilterKey]]) continue; 64 | 65 | NSDictionary *entry = [plPlist objectForKey:@"entry"]; 66 | if(!entry) continue; 67 | PLLog(@"found an entry key for %@!", item); 68 | 69 | if(![PSSpecifier environmentPassesPreferenceLoaderFilter:[entry objectForKey:PLFilterKey]]) continue; 70 | 71 | NSArray *specs = [self specifiersFromEntry:entry sourcePreferenceLoaderBundlePath:[fullPath stringByDeletingLastPathComponent] title:[[item lastPathComponent] stringByDeletingPathExtension]]; 72 | if(!specs) continue; 73 | 74 | // But it's possible for there to be more than one with an isController == 0 (PSBundleController) bundle. 75 | // so, set all the specifiers to etched mode (if necessary). 76 | if(pPSTableCellUseEtchedAppearanceKey && [UIDevice instancesRespondToSelector:@selector(isWildcat)] && [[UIDevice currentDevice] isWildcat]) 77 | for(PSSpecifier *specifier in specs) { 78 | [specifier setProperty:[NSNumber numberWithBool:1] forKey:*pPSTableCellUseEtchedAppearanceKey]; 79 | } 80 | 81 | PLLog(@"appending to the array!"); 82 | [_loadedSpecifiers addObjectsFromArray:specs]; 83 | } 84 | 85 | [_loadedSpecifiers sortUsingFunction:&PSSpecifierSort context:NULL]; 86 | 87 | if([_loadedSpecifiers count] > 0) { 88 | PLLog(@"so we gots us some specifiers! that's awesome! let's add them to the list..."); 89 | PSSpecifier *groupSpecifier = [PSSpecifier groupSpecifierWithName:_Firmware_lt_60 ? @"Extensions" : nil]; 90 | [_loadedSpecifiers insertObject:groupSpecifier atIndex:0]; 91 | NSMutableArray *_specifiers = MSHookIvar(self, "_specifiers"); 92 | int group, row; 93 | int firstindex; 94 | if ([self getGroup:&group row:&row ofSpecifierID:_Firmware_lt_60 ? @"General" : @"TWITTER"]) { 95 | firstindex = [self indexOfGroup:group] + [[self specifiersInGroup:group] count]; 96 | PLLog(@"Adding to the end of group %d at index %d", group, firstindex); 97 | } else { 98 | firstindex = [_specifiers count]; 99 | PLLog(@"Adding to the end of entire list"); 100 | } 101 | NSIndexSet *indices = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(firstindex, [_loadedSpecifiers count])]; 102 | [_specifiers insertObjects:_loadedSpecifiers atIndexes:indices]; 103 | PLLog(@"getting group index"); 104 | NSUInteger groupIndex = 0; 105 | for(PSSpecifier *spec in _specifiers) { 106 | if(MSHookIvar(spec, "cellType") != PSGroupCell) continue; 107 | if(spec == groupSpecifier) break; 108 | ++groupIndex; 109 | } 110 | _extraPrefsGroupSectionID = groupIndex; 111 | PLLog(@"group index is %d", _extraPrefsGroupSectionID); 112 | } 113 | } 114 | return MSHookIvar(self, "_specifiers"); 115 | } 116 | %end 117 | 118 | %ctor { 119 | %init(); 120 | 121 | _Firmware_lt_60 = kCFCoreFoundationVersionNumber < 793.00; 122 | if([UIDevice instancesRespondToSelector:@selector(isWildcat)] && [[UIDevice currentDevice] isWildcat]) 123 | %init(iPad); 124 | 125 | void *preferencesHandle = dlopen("/System/Library/PrivateFrameworks/Preferences.framework/Preferences", RTLD_LAZY | RTLD_NOLOAD); 126 | if(preferencesHandle) { 127 | pPSTableCellUseEtchedAppearanceKey = (NSString **)dlsym(preferencesHandle, "PSTableCellUseEtchedAppearanceKey"); 128 | dlclose(preferencesHandle); 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /control: -------------------------------------------------------------------------------- 1 | Package: preferenceloader 2 | Name: PreferenceLoader 3 | Depends: mobilesubstrate 4 | Version: 2.2.0 5 | Architecture: iphoneos-arm 6 | Description: load preferences in style 7 | Maintainer: Dustin Howett 8 | Author: Dustin Howett 9 | Section: System 10 | dev: dustinhowett 11 | Depiction: http://moreinfo.thebigboss.org/moreinfo/depiction.php?file=preferenceloader2Data 12 | Homepage: http://moreinfo.thebigboss.org/moreinfo/depiction.php?file=preferenceloader2Data 13 | Tag: role::hacker, purpose::library 14 | -------------------------------------------------------------------------------- /debug.h: -------------------------------------------------------------------------------- 1 | #ifndef __DEBUG_H 2 | #define __DEBUG_H 3 | 4 | #ifndef DEBUG_TAG 5 | #define DEBUG_TAG "PreferenceLoader" 6 | #endif 7 | 8 | #if DEBUG 9 | # define PLLog(...) NSLog(@ DEBUG_TAG "! %s:%d: %@", __FILE__, __LINE__, [NSString stringWithFormat:__VA_ARGS__]) 10 | #else 11 | # define PLLog(...) 12 | #endif 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /prefs.h: -------------------------------------------------------------------------------- 1 | #import 2 | 3 | @interface PSListController (libprefs) 4 | - (NSArray *)specifiersFromEntry:(NSDictionary *)entry sourcePreferenceLoaderBundlePath:(NSString *)sourceBundlePath title:(NSString *)title; 5 | @end 6 | 7 | extern NSString *const PLFilterKey; 8 | 9 | @interface PSSpecifier (libprefs) 10 | + (BOOL)environmentPassesPreferenceLoaderFilter:(NSDictionary *)filter; 11 | @property (nonatomic, retain, readonly) NSBundle *preferenceLoaderBundle; 12 | @end 13 | 14 | @interface PLCustomListController: PSListController { } 15 | @end 16 | 17 | @interface PLLocalizedListController: PLCustomListController { } 18 | @end 19 | -------------------------------------------------------------------------------- /prefs.xm: -------------------------------------------------------------------------------- 1 | #import 2 | #import 3 | #import 4 | #import 5 | #import 6 | 7 | #import "prefs.h" 8 | 9 | #define DEBUG_TAG "libprefs" 10 | #import "debug.h" 11 | 12 | /* {{{ Imports (Preferences.framework) */ 13 | extern "C" NSArray* SpecifiersFromPlist(NSDictionary* plist, 14 | PSSpecifier* prevSpec, 15 | id target, 16 | NSString* plistName, 17 | NSBundle* curBundle, 18 | NSString** pTitle, 19 | NSString** pSpecifierID, 20 | PSListController* callerList, 21 | NSMutableArray** pBundleControllers); 22 | 23 | 24 | extern NSString *const PSBundlePathKey; 25 | extern NSString *const PSLazilyLoadedBundleKey; 26 | extern NSString *const PSBundleIsControllerKey; 27 | extern NSString *const PSActionKey; 28 | extern NSString *const PSTitleKey; 29 | 30 | // Weak (3.2+, dlsym) 31 | static NSString **pPSFooterTextGroupKey = NULL; 32 | static NSString **pPSStaticTextGroupKey = NULL; 33 | /* }}} */ 34 | 35 | /* {{{ PSSpecifier 3.2 Additions */ 36 | @interface PSSpecifier (OS32) 37 | - (Class)detailControllerClass; 38 | @end 39 | /* }}} */ 40 | 41 | /* {{{ PSViewController 3.2 Additions */ 42 | @interface PSViewController (OS32) 43 | - (void)setSpecifier:(PSSpecifier *)specifier; 44 | @end 45 | /* }}} */ 46 | 47 | /* {{{ Prototypes */ 48 | static NSArray *generateErrorSpecifiersWithText(NSString *errorText); 49 | /* }}} */ 50 | 51 | /* {{{ Constants */ 52 | static NSString *const PLBundleKey = @"pl_bundle"; 53 | NSString *const PLFilterKey = @"pl_filter"; 54 | static NSString *const PLAlternatePlistNameKey = @"pl_alt_plist_name"; 55 | /* }}} */ 56 | 57 | /* {{{ Locals */ 58 | static BOOL _Firmware_lt_60 = NO; 59 | /* }}} */ 60 | 61 | /* {{{ Preferences Controllers */ 62 | @implementation PLCustomListController 63 | - (id)bundle { 64 | return [[self specifier] preferenceLoaderBundle]; 65 | } 66 | 67 | - (id)specifiers { 68 | if(!_specifiers) { 69 | PLLog(@"loading specifiers for a custom bundle."); 70 | PSSpecifier *specifier = [self specifier]; 71 | if(!specifier) { 72 | NSString *errorText = @"There appears to have been an error restoring these preferences!"; 73 | return _specifiers = [[NSArray alloc] initWithArray:generateErrorSpecifiersWithText(errorText)]; 74 | } 75 | NSString *alternatePlistName = [specifier propertyForKey:PLAlternatePlistNameKey]; 76 | if(alternatePlistName) 77 | _specifiers = [[self loadSpecifiersFromPlistName:alternatePlistName target:self] retain]; 78 | else 79 | _specifiers = [super specifiers]; 80 | if(!_specifiers || [_specifiers count] == 0) { 81 | [_specifiers release]; 82 | NSString *errorText = @"There appears to be an error with these preferences!"; 83 | _specifiers = [[NSArray alloc] initWithArray:generateErrorSpecifiersWithText(errorText)]; 84 | } else { 85 | if([self respondsToSelector:@selector(setTitle:)]) { 86 | [self setTitle:specifier.name]; 87 | } 88 | NSMutableArray *removals = [NSMutableArray array]; 89 | for(PSSpecifier *spec in _specifiers) { 90 | if(MSHookIvar(spec, "cellType") == PSLinkCell && ![spec propertyForKey:PSBundlePathKey]) { 91 | MSHookIvar(spec, "detailControllerClass") = [self class]; 92 | [spec setProperty:[[self specifier] propertyForKey:PLBundleKey] forKey:PLBundleKey]; 93 | } 94 | 95 | if(![PSSpecifier environmentPassesPreferenceLoaderFilter:[spec propertyForKey:PLFilterKey]]) 96 | [removals addObject:spec]; 97 | 98 | if(removals.count > 0) { 99 | NSMutableArray *newSpecifiers = [_specifiers mutableCopy]; 100 | [_specifiers release]; 101 | [newSpecifiers removeObjectsInArray:removals]; 102 | _specifiers = newSpecifiers; 103 | } 104 | } 105 | } 106 | } 107 | return _specifiers; 108 | } 109 | 110 | - (id)navigationTitle { 111 | return self.specifier.name; 112 | } 113 | 114 | @end 115 | 116 | @implementation PLLocalizedListController 117 | - (id)navigationTitle { 118 | NSString *original = [super navigationTitle]; 119 | return [[self bundle] localizedStringForKey:original value:original table:nil]; 120 | } 121 | 122 | - (id)specifiers { 123 | if(!_specifiers) { 124 | PLLog(@"Localizing specifiers for a localized bundle."); 125 | _specifiers = [super specifiers]; 126 | for(PSSpecifier *spec in _specifiers) { 127 | if([spec name]) [spec setName:[[self bundle] localizedStringForKey:[spec name] value:[spec name] table:nil]]; 128 | if([spec titleDictionary]) { 129 | NSMutableDictionary *newTitles = [NSMutableDictionary dictionary]; 130 | for(NSString *key in [spec titleDictionary]) { 131 | NSString *value = [[spec titleDictionary] objectForKey:key]; 132 | [newTitles setObject:[[self bundle] localizedStringForKey:value value:value table:nil] forKey:key]; 133 | } 134 | [spec setTitleDictionary:newTitles]; 135 | } 136 | if([spec shortTitleDictionary]) { 137 | NSMutableDictionary *newTitles = [NSMutableDictionary dictionary]; 138 | for(NSString *key in [spec shortTitleDictionary]) { 139 | NSString *value = [[spec shortTitleDictionary] objectForKey:key]; 140 | [newTitles setObject:[[self bundle] localizedStringForKey:value value:value table:nil] forKey:key]; 141 | } 142 | [spec setShortTitleDictionary:newTitles]; 143 | } 144 | static NSString *localizableKeys[] = { @"headerDetailText", @"placeholder", @"staticTextMessage" }; 145 | for (size_t i = 0; i < sizeof(localizableKeys) / sizeof(NSString *); i++) { 146 | NSString *value = [spec propertyForKey:localizableKeys[i]]; 147 | if(value) 148 | [spec setProperty:[[self bundle] localizedStringForKey:value value:value table:nil] forKey:localizableKeys[i]]; 149 | } 150 | if(pPSFooterTextGroupKey) { 151 | NSString *value = [spec propertyForKey:*pPSFooterTextGroupKey]; 152 | if(value) 153 | [spec setProperty:[[self bundle] localizedStringForKey:value value:value table:nil] forKey:*pPSFooterTextGroupKey]; 154 | } 155 | } 156 | } 157 | return _specifiers; 158 | } 159 | @end 160 | 161 | @interface PLFailedBundleListController: PSListController { } 162 | @end 163 | @implementation PLFailedBundleListController 164 | - (id)navigationTitle { 165 | return @"Error"; 166 | } 167 | 168 | - (id)specifiers { 169 | if(!_specifiers) { 170 | PLLog(@"Generating error specifiers for a failed bundle :("); 171 | NSString *const errorText = [NSString stringWithFormat:@"There was an error loading the preference bundle for %@.", [[self specifier] name]]; 172 | _specifiers = [[NSArray alloc] initWithArray:generateErrorSpecifiersWithText(errorText)]; 173 | } 174 | return _specifiers; 175 | } 176 | @end 177 | /* }}} */ 178 | 179 | /* {{{ Helper Functions */ 180 | static NSArray *generateErrorSpecifiersWithText(NSString *errorText) { 181 | NSMutableArray *errorSpecifiers = [NSMutableArray array]; 182 | if(pPSFooterTextGroupKey) { 183 | PSSpecifier *spec = [PSSpecifier emptyGroupSpecifier]; 184 | [spec setProperty:errorText forKey:*pPSFooterTextGroupKey]; 185 | [errorSpecifiers addObject:spec]; 186 | } else { 187 | if(pPSStaticTextGroupKey) { 188 | PSSpecifier *spec = [PSSpecifier emptyGroupSpecifier]; 189 | [spec setProperty:[NSNumber numberWithBool:YES] forKey:*pPSStaticTextGroupKey]; 190 | [errorSpecifiers addObject:spec]; 191 | spec = [PSSpecifier preferenceSpecifierNamed:errorText target:nil set:nil get:nil detail:nil cell:[PSTableCell cellTypeFromString:@"PSTitleValueCell"] edit:nil]; 192 | [errorSpecifiers addObject:spec]; 193 | } 194 | } 195 | return errorSpecifiers; 196 | } 197 | 198 | @implementation PSSpecifier (libprefs) 199 | + (BOOL)environmentPassesPreferenceLoaderFilter:(NSDictionary *)filter { 200 | PLLog(@"Checking filter %@", filter); 201 | 202 | if(!filter) return YES; 203 | bool valid = YES; 204 | 205 | NSArray *coreFoundationVersion = [filter objectForKey:@"CoreFoundationVersion"]; 206 | if(coreFoundationVersion && coreFoundationVersion.count > 0) { 207 | NSNumber *lowerBound = [coreFoundationVersion objectAtIndex:0]; 208 | NSNumber *upperBound = coreFoundationVersion.count > 1 ? [coreFoundationVersion objectAtIndex:1] : nil; 209 | PLLog(@"%@ <= CF Version (%f) < %@", lowerBound, kCFCoreFoundationVersionNumber, upperBound); 210 | valid = valid && (kCFCoreFoundationVersionNumber >= lowerBound.floatValue); 211 | 212 | if(upperBound) 213 | valid = valid && (kCFCoreFoundationVersionNumber < upperBound.floatValue); 214 | } 215 | PLLog(valid ? @"Filter matched" : @"Filter did not match"); 216 | return valid; 217 | } 218 | 219 | - (NSBundle *)preferenceLoaderBundle { 220 | return [self propertyForKey:PLBundleKey]; 221 | } 222 | 223 | @end 224 | /* }}} */ 225 | 226 | /* {{{ Hooks */ 227 | static void pl_loadFailedBundle(NSString *bundlePath, PSSpecifier *specifier) { 228 | PLLog(@"lazyLoadBundle:%@ (bundle path %@) failed.", specifier, bundlePath); 229 | NSLog(@"Failed to load PreferenceBundle at %@.", bundlePath); 230 | MSHookIvar(specifier, "detailControllerClass") = [PLFailedBundleListController class]; 231 | [specifier removePropertyForKey:PSBundleIsControllerKey]; 232 | [specifier removePropertyForKey:PSActionKey]; 233 | [specifier removePropertyForKey:PSBundlePathKey]; 234 | [specifier removePropertyForKey:PSLazilyLoadedBundleKey]; 235 | } 236 | 237 | static void pl_lazyLoadBundleCore(id self, SEL _cmd, PSSpecifier *specifier, void(*_orig)(id, SEL, PSSpecifier *)) { 238 | NSString *bundlePath = [[specifier propertyForKey:PSLazilyLoadedBundleKey] retain]; 239 | PLLog(@"In pl_lazyLoadBundleCore for %@ (%s), specifier %@", self, _cmd, specifier); 240 | PLLog(@"%%orig is %p.", _orig); 241 | 242 | _orig(self, _cmd, specifier); // NB: This removes the PSLazilyLoadedBundleKey property. 243 | if(![[NSBundle bundleWithPath:bundlePath] isLoaded]) { 244 | pl_loadFailedBundle(bundlePath, specifier); 245 | } 246 | [bundlePath release]; 247 | } 248 | 249 | %group Firmware_lt_60 250 | %hook PrefsRootController 251 | - (void)lazyLoadBundle:(PSSpecifier *)specifier { 252 | pl_lazyLoadBundleCore(self, _cmd, specifier, &%orig); 253 | 254 | } 255 | %end 256 | %end 257 | 258 | %hook PSListController 259 | %group Firmware_ge_60 260 | - (void)lazyLoadBundle:(PSSpecifier *)specifier { 261 | pl_lazyLoadBundleCore(self, _cmd, specifier, &%orig); 262 | } 263 | %end 264 | 265 | %new 266 | - (PSViewController *)controllerForSpecifier:(PSSpecifier *)specifier 267 | { 268 | %log(); 269 | Class detailClass = [specifier respondsToSelector:@selector(detailControllerClass)] ? [specifier detailControllerClass] : MSHookIvar(specifier, "detailControllerClass"); 270 | if (!detailClass) 271 | detailClass = [PLCustomListController class]; 272 | if (![detailClass isSubclassOfClass:[PSViewController class]]) 273 | return nil; 274 | id result = [detailClass alloc]; 275 | if ([result respondsToSelector:@selector(initForContentSize:)]) 276 | result = [result initForContentSize:[[self view] bounds].size]; 277 | else 278 | result = [result init]; 279 | [result setRootController:self.rootController]; 280 | [result setParentController:self]; 281 | if ([result respondsToSelector:@selector(setSpecifier:)]) 282 | [result setSpecifier:specifier]; 283 | else if ([result isKindOfClass:[PSListController class]]) { 284 | NSArray *&_specifierIvar = MSHookIvar(result, "_specifier"); 285 | [_specifierIvar release]; 286 | _specifierIvar = [specifier retain]; 287 | } 288 | return [result autorelease]; 289 | } 290 | 291 | - (NSArray *)loadSpecifiersFromPlistName:(NSString *)plistName target:(id)target { 292 | NSArray *result = %orig(); 293 | if([result count] > 0) 294 | return result; 295 | 296 | NSDictionary *properties = self.specifier.properties; 297 | if(!properties) 298 | return nil; 299 | 300 | PLLog(@"Loading specifiers from PSListController's specifier's properties."); 301 | NSArray *&bundleControllers = MSHookIvar(self, "_bundleControllers"); 302 | NSString *title = nil; 303 | NSString *specifierID = nil; 304 | result = SpecifiersFromPlist(properties, [self specifier], target, plistName, [self bundle], &title, &specifierID, self, &bundleControllers); 305 | 306 | if(title) 307 | [self setTitle:title]; 308 | 309 | if(specifierID) 310 | [self setSpecifierID:specifierID]; 311 | 312 | return result; 313 | } 314 | %end 315 | 316 | %hook NSBundle 317 | + (NSBundle *)bundleWithPath:(NSString *)path { 318 | NSString *newPath = nil; 319 | NSRange sysRange = [path rangeOfString:@"/System/Library/PreferenceBundles" options:0]; 320 | if(sysRange.location != NSNotFound) { 321 | newPath = [path stringByReplacingCharactersInRange:sysRange withString:@"/Library/PreferenceBundles"]; 322 | } 323 | if(newPath && [[NSFileManager defaultManager] fileExistsAtPath:newPath]) { 324 | // /Library/PreferenceBundles will override /System/Library/PreferenceBundles. 325 | path = newPath; 326 | } 327 | return %orig; 328 | } 329 | %end 330 | /* }}} */ 331 | 332 | @implementation PSListController (libprefs) 333 | 334 | - (NSArray *)specifiersFromEntry:(NSDictionary *)entry sourcePreferenceLoaderBundlePath:(NSString *)sourceBundlePath title:(NSString *)title { 335 | NSDictionary *specifierPlist = [NSDictionary dictionaryWithObjectsAndKeys:[NSArray arrayWithObjects:entry, nil], @"items", nil]; 336 | 337 | BOOL isBundle = [entry objectForKey:@"bundle"] != nil; 338 | BOOL isLocalizedBundle = ![[sourceBundlePath lastPathComponent] isEqualToString:@"Preferences"]; 339 | 340 | NSBundle *prefBundle; 341 | NSString *bundleName = [entry objectForKey:@"bundle"]; 342 | NSString *bundlePath = [entry objectForKey:@"bundlePath"]; 343 | 344 | if(isBundle) { 345 | // Second Try (bundlePath key failed) 346 | if(![[NSFileManager defaultManager] fileExistsAtPath:bundlePath]) 347 | bundlePath = [NSString stringWithFormat:@"/Library/PreferenceBundles/%@.bundle", bundleName]; 348 | 349 | // Third Try (/Library failed) 350 | if(![[NSFileManager defaultManager] fileExistsAtPath:bundlePath]) 351 | bundlePath = [NSString stringWithFormat:@"/System/Library/PreferenceBundles/%@.bundle", bundleName]; 352 | 353 | // Really? (/System/Library failed...) 354 | if(![[NSFileManager defaultManager] fileExistsAtPath:bundlePath]) { 355 | NSLog(@"Discarding specifier for missing isBundle bundle %@.", bundleName); 356 | return nil; 357 | } 358 | prefBundle = [NSBundle bundleWithPath:bundlePath]; 359 | PLLog(@"is a bundle: %@!", prefBundle); 360 | } else { 361 | prefBundle = [NSBundle bundleWithPath:sourceBundlePath]; 362 | PLLog(@"is NOT a bundle, so we're giving it %@!", prefBundle); 363 | } 364 | 365 | PLLog(@"loading specifiers!"); 366 | NSArray *&bundleControllers = MSHookIvar(self, "_bundleControllers"); 367 | NSArray *specs = SpecifiersFromPlist(specifierPlist, nil, _Firmware_lt_60 ? [self rootController] : self, title, prefBundle, NULL, NULL, (PSListController*)self, &bundleControllers); 368 | PLLog(@"loaded specifiers!"); 369 | 370 | if([specs count] == 0) return nil; 371 | PLLog(@"It's confirmed! There are Specifiers here, Captain!"); 372 | 373 | if(isBundle) { 374 | // Only set lazy-bundle for isController specifiers. 375 | if([[entry objectForKey:@"isController"] boolValue]) { 376 | for(PSSpecifier *specifier in specs) { 377 | [specifier setProperty:bundlePath forKey:PSLazilyLoadedBundleKey]; 378 | [specifier setProperty:[NSBundle bundleWithPath:sourceBundlePath] forKey:PLBundleKey]; 379 | if(!specifier.name) { 380 | specifier.name = title; 381 | } 382 | } 383 | } 384 | } else { 385 | // There really should only be one specifier. 386 | PSSpecifier *specifier = [specs objectAtIndex:0]; 387 | MSHookIvar(specifier, "detailControllerClass") = isLocalizedBundle ? [PLLocalizedListController class] : [PLCustomListController class]; 388 | [specifier setProperty:prefBundle forKey:PLBundleKey]; 389 | 390 | if(![[specifier propertyForKey:PSTitleKey] isEqualToString:title]) { 391 | [specifier setProperty:title forKey:PLAlternatePlistNameKey]; 392 | if(!specifier.name) { 393 | specifier.name = title; 394 | } 395 | } 396 | } 397 | 398 | return specs; 399 | } 400 | 401 | @end 402 | 403 | %ctor { 404 | _Firmware_lt_60 = kCFCoreFoundationVersionNumber < 793.00; 405 | %init; 406 | 407 | if(_Firmware_lt_60) { 408 | %init(Firmware_lt_60); 409 | } else { 410 | %init(Firmware_ge_60); 411 | } 412 | 413 | void *preferencesHandle = dlopen("/System/Library/PrivateFrameworks/Preferences.framework/Preferences", RTLD_LAZY | RTLD_NOLOAD); 414 | if(preferencesHandle) { 415 | pPSFooterTextGroupKey = (NSString **)dlsym(preferencesHandle, "PSFooterTextGroupKey"); 416 | pPSStaticTextGroupKey = (NSString **)dlsym(preferencesHandle, "PSStaticTextGroupKey"); 417 | dlclose(preferencesHandle); 418 | } 419 | } 420 | --------------------------------------------------------------------------------