William Bowling is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this again1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | /*
* Adium is the legal property of its developers, whose names are listed in the copyright file included
* with this source distribution.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program; if not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#import "AIAppearancePreferencesPlugin.h"
#import "AIAppearancePreferences.h"
#import "AIDockController.h"
#import <Adium/AIMenuControllerProtocol.h>
#import <Adium/AIAbstractListController.h>
#import <Adium/AIStatusIcons.h>
#import <Adium/AIServiceIcons.h>
#import "AIMenuBarIcons.h"
#import <AIUtilities/AIStringAdditions.h>
#import <AIUtilities/AIDictionaryAdditions.h>
#import <AIUtilities/AIMenuAdditions.h>
#import "AIXtrasManager.h"
#define APPEARANCE_DEFAULT_PREFS @"AppearanceDefaults"
@implementation AIAppearancePreferencesPlugin
- (void)installPlugin
{
id<AIPreferenceController> preferenceController = adium.preferenceController;
[adium createResourcePathForName:LIST_LAYOUT_FOLDER];
[adium createResourcePathForName:LIST_THEME_FOLDER];
//Prepare our preferences
[preferenceController registerDefaults:[NSDictionary dictionaryNamed:APPEARANCE_DEFAULT_PREFS
forClass:[self class]]
forGroup:PREF_GROUP_APPEARANCE];
preferences = [[AIAppearancePreferences preferencePaneForPlugin:self] retain];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(invalidStatusSetActivated:)
name:AIStatusIconSetInvalidSetNotification
object:nil];
[preferenceController registerPreferenceObserver:self forGroup:PREF_GROUP_APPEARANCE];
}
- (void)uninstallPlugin
{
[adium.preferenceController unregisterPreferenceObserver:self];
}
/*!
* @brief Apply changed preferences
*/
- (void)preferencesChangedForGroup:(NSString *)group key:(NSString *)key object:(AIListObject *)object
preferenceDict:(NSDictionary *)prefDict firstTime:(BOOL)firstTime
{
//Status icons
if ([group isEqualToString:PREF_GROUP_APPEARANCE]) {
if (firstTime || [key isEqualToString:KEY_STATUS_ICON_PACK]) {
NSString *path = [adium pathOfPackWithName:[prefDict objectForKey:KEY_STATUS_ICON_PACK]
extension:@"AdiumStatusIcons"
resourceFolderName:@"Status Icons"];
BOOL success = NO;
if (path) {
success = [AIStatusIcons setActiveStatusIconsFromPath:path];
}
//If the preferred pack isn't found (it was probably deleted while active), use the default one
if (!success) {
NSString *name = [adium.preferenceController defaultPreferenceForKey:KEY_STATUS_ICON_PACK
group:PREF_GROUP_APPEARANCE
object:nil];
path = [adium pathOfPackWithName:name
extension:@"AdiumStatusIcons"
resourceFolderName:@"Status Icons"];
[AIStatusIcons setActiveStatusIconsFromPath:path];
}
}
//Service icons
if (firstTime || [key isEqualToString:KEY_SERVICE_ICON_PACK]) {
NSString *path = [adium pathOfPackWithName:[prefDict objectForKey:KEY_SERVICE_ICON_PACK]
extension:@"AdiumServiceIcons"
resourceFolderName:@"Service Icons"];
BOOL success = NO;
if (path) {
success = [AIServiceIcons setActiveServiceIconsFromPath:path];
}
//If the preferred pack isn't found (it was probably deleted while active), use the default one
if (!success) {
NSString *name = [adium.preferenceController defaultPreferenceForKey:KEY_SERVICE_ICON_PACK
group:PREF_GROUP_APPEARANCE
object:nil];
path = [adium pathOfPackWithName:name
extension:@"AdiumServiceIcons"
resourceFolderName:@"Service Icons"];
[AIServiceIcons setActiveServiceIconsFromPath:path];
}
}
// Menu Bar Icons
if (firstTime || [key isEqualToString:KEY_MENU_BAR_ICONS]) {
// Post a notification to update the menu bar icons.
[[NSNotificationCenter defaultCenter] postNotificationName:AIMenuBarIconsDidChangeNotification
object:nil];
}
//Theme
if (firstTime || [key isEqualToString:KEY_LIST_THEME_NAME]) {
[self applySetWithName:[prefDict objectForKey:KEY_LIST_THEME_NAME]
extension:LIST_THEME_EXTENSION
inFolder:LIST_THEME_FOLDER
toPreferenceGroup:PREF_GROUP_LIST_THEME];
}
if (firstTime || [key isEqualToString:KEY_LIST_LAYOUT_NAME]) {
[self applySetWithName:[prefDict objectForKey:KEY_LIST_LAYOUT_NAME]
extension:LIST_LAYOUT_EXTENSION
inFolder:LIST_LAYOUT_FOLDER
toPreferenceGroup:PREF_GROUP_LIST_LAYOUT];
}
}
}
/*!
* @brief An invalid status set was activated
*
* Reset to the default by clearing our preference
*/
- (void)invalidStatusSetActivated:(NSNotification *)inNotification
{
[adium.preferenceController setPreference:nil
forKey:KEY_STATUS_ICON_PACK
group:PREF_GROUP_APPEARANCE];
//Tell the preferences to update
[preferences xtrasChanged:nil];
}
#pragma mark ListLayout and ListTheme preference management
//Apply a set of preferences
- (void)applySetWithName:(NSString *)setName extension:(NSString *)extension inFolder:(NSString *)folder toPreferenceGroup:(NSString *)preferenceGroup
{
NSFileManager *defaultManager = [NSFileManager defaultManager];
NSString *fileName;
NSDictionary *setDictionary = nil;
//Look in each resource location until we find it
fileName = [setName stringByAppendingPathExtension:extension];
for (NSString *resourcePath in [adium resourcePathsForName:folder]) {
NSString *filePath = [resourcePath stringByAppendingPathComponent:fileName];
if ([defaultManager fileExistsAtPath:filePath]) {
NSBundle *xtraBundle;
if((xtraBundle = [NSBundle bundleWithPath:filePath]) &&
([[xtraBundle objectForInfoDictionaryKey:@"XtraBundleVersion"] integerValue] == 1)) {
filePath = [[xtraBundle resourcePath] stringByAppendingPathComponent:@"Data.plist"];
}
setDictionary = [NSDictionary dictionaryWithContentsOfFile:filePath];
if (setDictionary) break;
}
}
//Apply its values
if (setDictionary) {
[adium.preferenceController setPreferences:setDictionary
inGroup:preferenceGroup];
}
}
//Create a layout or theme set
- (BOOL)createSetFromPreferenceGroup:(NSString *)preferenceGroup withName:(NSString *)setName extension:(NSString *)extension inFolder:(NSString *)folder
{
NSString *path;
NSString *fileName = [[setName safeFilenameString] stringByAppendingPathExtension:extension];
//If we don't find one, create a path to a bundle in the application support directory
path = [[[adium applicationSupportDirectory] stringByAppendingPathComponent:folder] stringByAppendingPathComponent:fileName];
if ([AIXtrasManager createXtraBundleAtPath:path])
path = [path stringByAppendingPathComponent:@"Contents/Resources/Data.plist"];
if ([[adium.preferenceController preferencesForGroup:preferenceGroup] writeToFile:path atomically:NO]) {
[[NSNotificationCenter defaultCenter] postNotificationName:AIXtrasDidChangeNotification object:extension];
return YES;
} else {
NSRunAlertPanel(AILocalizedString(@"Error Saving Theme",nil),
AILocalizedString(@"Unable to write file %@ to %@",nil),
AILocalizedString(@"OK",nil),
nil,
nil,
fileName,
path);
return NO;
}
}
//Delete a layout or theme set
- (BOOL)deleteSetWithName:(NSString *)setName extension:(NSString *)extension inFolder:(NSString *)folder
{
BOOL success;
success = [[NSFileManager defaultManager] removeItemAtPath:[adium pathOfPackWithName:setName
extension:extension
resourceFolderName:folder]
error:NULL];
//The availability of an xtras just changed, since we deleted it... post a notification so we can update
[[NSNotificationCenter defaultCenter] postNotificationName:AIXtrasDidChangeNotification object:extension];
return success;
}
- (BOOL)renameSetWithName:(NSString *)setName extension:(NSString *)extension inFolder:(NSString *)folder toName:(NSString *)newName
{
BOOL success;
NSString *destFolder = [[adium applicationSupportDirectory] stringByAppendingPathComponent:folder];
NSString *newFileName = [newName stringByAppendingPathExtension:extension];
success = [[NSFileManager defaultManager] moveItemAtPath:[adium pathOfPackWithName:setName
extension:extension
resourceFolderName:folder]
toPath:[destFolder stringByAppendingPathComponent:newFileName]
error:NULL];
//The availability of an xtras just changed, since we deleted it... post a notification so we can update
[[NSNotificationCenter defaultCenter] postNotificationName:AIXtrasDidChangeNotification object:extension];
return success;
}
- (BOOL)duplicateSetWithName:(NSString *)setName extension:(NSString *)extension inFolder:(NSString *)folder newName:(NSString *)newName
{
BOOL success;
//Duplicate the set
NSString *destFolder = [[adium applicationSupportDirectory] stringByAppendingPathComponent:folder];
NSString *newFileName = [newName stringByAppendingPathExtension:extension];
success = [[NSFileManager defaultManager] copyItemAtPath:[adium pathOfPackWithName:setName
extension:extension
resourceFolderName:folder]
toPath:[destFolder stringByAppendingPathComponent:newFileName]
error:NULL];
//The availability of an xtras just changed, since we deleted it... post a notification so we can update
[[NSNotificationCenter defaultCenter] postNotificationName:AIXtrasDidChangeNotification object:extension];
return success;
}
//Sort sets
NSInteger availableSetSort(NSDictionary *objectA, NSDictionary *objectB, void *context) {
return [[objectA objectForKey:@"name"] caseInsensitiveCompare:[objectB objectForKey:@"name"]];
}
- (NSArray *)availableSetsWithExtension:(NSString *)extension fromFolder:(NSString *)folder
{
NSMutableArray *setArray = [NSMutableArray array];
NSMutableSet *alreadyAddedArray = [NSMutableSet set];
for (NSString *filePath in [adium allResourcesForName:folder withExtensions:extension]) {
NSString *name;
NSBundle *xtraBundle;
NSDictionary *themeDict;
name = [[filePath lastPathComponent] stringByDeletingPathExtension];
if((xtraBundle = [NSBundle bundleWithPath:filePath]) &&
([[xtraBundle objectForInfoDictionaryKey:@"XtraBundleVersion"] integerValue] == 1)) {
filePath = [[xtraBundle resourcePath] stringByAppendingPathComponent:@"Data.plist"];
}
if ((themeDict = [NSDictionary dictionaryWithContentsOfFile:filePath])) {
//The Adium resource path is last in our resourcePaths array; by only adding sets we haven't
//already added, we allow precedence to occur rather than conflict.
if (![alreadyAddedArray containsObject:name]) {
[setArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:
name, @"name",
filePath, @"path",
themeDict, @"preferences",
nil]];
[alreadyAddedArray addObject:name];
}
}
}
return [setArray sortedArrayUsingFunction:availableSetSort context:nil];
}
- (NSArray *)availableLayoutSets
{
return [self availableSetsWithExtension:LIST_LAYOUT_EXTENSION
fromFolder:LIST_LAYOUT_FOLDER];
}
- (NSArray *)availableThemeSets
{
return [self availableSetsWithExtension:LIST_THEME_EXTENSION
fromFolder:LIST_THEME_FOLDER];
}
@end
|