#define THEME_PLIST_PATH @"/private/var/mobile/Library/Preferences/com.saurik.WinterBoard.plist"
#define THEME_PATH @"/Library/Themes/"
#import <Foundation/Foundation.h>
NSMutableArray *getThemeInfo()
{
//获取winterBoard的plist信息
NSDictionary *saurikDict = [NSDictionary dictionaryWithContentsOfFile:THEME_PLIST_PATH];
NSMutableArray *themeArr = nil;
if (saurikDict)
{
//获取theme列表信息
themeArr = [NSMutableArray arrayWithArray: [saurikDict objectForKey:@"Themes"]];
}
return themeArr;
}
//设置主题信息
NSMutableArray* changeThemeInfo(NSString* themePath)
{
if ([themePath hasSuffix:@"/"])
{
themePath = [themePath substringToIndex:[themePath length]-1];
}
NSRange range = [themePath rangeOfString:@"/" options:NSBackwardsSearch];
NSString *themeName = themePath;
if (range.length > 0 )
themeName = [themeName substringFromIndex:range.location+range.length];
NSMutableArray *themes = getThemeInfo();
BOOL isSameTheme = FALSE;
//判断主题是否存在于列表中
for (NSMutableDictionary *dict in themes)
{
NSString *themeStr = [dict objectForKey:@"Name"];
if ([themeStr isEqualToString:themeName])
{
NSString *themeKey = [dict objectForKey:@"Active"];
if (themeKey)
{
[dict setObject:[NSNumber numberWithInt:![themeKey intValue]] forKey:@"Active"];
isSameTheme = YES;
break;
}
}
}
//如果是新主题
if (!isSameTheme)
{
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:TRUE], @"Active", themeName, @"Name", nil];
[themes addObject:dict];
}
return themes;
}
//主题应用
void applyTheme(NSString *themePath)
{
NSMutableArray *themes = changeThemeInfo(themePath);
NSMutableDictionary *saurikDict = [NSMutableDictionary dictionaryWithContentsOfFile:THEME_PLIST_PATH];
NSLog(@"themes == %@",themes);
if (themes)
{
[saurikDict removeObjectForKey:@"Themes"];
[saurikDict setObject:themes forKey:@"Themes"];
[saurikDict writeToFile:THEME_PLIST_PATH atomically:YES];
system("killall SpringBoard");
}
else
{
NSLog(@"the array is nil");
}
}
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if (argc < 2)
{
NSLog(@"you need a parameter");
}
NSString *themeName = nil;
if (argc == 2)
{
themeName = [NSString stringWithFormat:@"%s",argv[1]];
}
if (themeName == nil)
{
NSLog(@"your parameter is error such as: ./ThemeApply Black **** Black not '/'");
}
else
{
NSLog(@"themeName === %@",themeName);
applyTheme(themeName);
NSLog(@"apply themeName successfully");
}
[pool release];
return 0;
}