/
ViewTheme.m
157 lines (132 loc) · 3.96 KB
/
ViewTheme.m
1
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
// LimeChat is copyrighted free software by Satoshi Nakagawa <psychs AT limechat DOT net>.
// You can redistribute it and/or modify it under the terms of the GPL version 2 (see the file GPL.txt).
#import "ViewTheme.h"
#import "Preferences.h"
static NSString* resourceBasePath;
static NSString* userBasePath;
@implementation ViewTheme
{
NSString* name;
LogTheme* log;
OtherTheme* other;
CustomJSFile* js;
}
@synthesize name;
@synthesize log;
@synthesize other;
@synthesize js;
- (id)init
{
self = [super init];
if (self) {
log = [LogTheme new];
other = [OtherTheme new];
js = [CustomJSFile new];
}
return self;
}
- (void)dealloc
{
[name release];
[log release];
[other release];
[js release];
[super dealloc];
}
- (void)setName:(NSString *)value
{
if (name != value) {
[name release];
name = [value retain];
}
[self load];
}
- (void)load
{
if (name) {
NSArray* kindAndName = [ViewTheme extractFileName:[Preferences themeName]];
if (kindAndName) {
NSString* kind = kindAndName[0];
NSString* fname = kindAndName[1];
NSString* fullName = nil;
if ([kind isEqualToString:@"resource"]) {
fullName = [[ViewTheme resourceBasePath] stringByAppendingPathComponent:fname];
}
else {
fullName = [[ViewTheme userBasePath] stringByAppendingPathComponent:fname];
}
log.fileName = [fullName stringByAppendingString:@".css"];
other.fileName = [fullName stringByAppendingString:@".yaml"];
js.fileName = [fullName stringByAppendingString:@".js"];
return;
}
}
log.fileName = nil;
other.fileName = nil;
js.fileName = nil;
}
- (void)reload
{
[log reload];
[other reload];
[js reload];
}
+ (void)createUserDirectory
{
NSFileManager* fm = [NSFileManager defaultManager];
NSString* userBase = [self userBasePath];
BOOL isDir = NO;
BOOL res = [fm fileExistsAtPath:userBase isDirectory:&isDir];
if (res) {
return;
}
// create directory
[fm createDirectoryAtPath:userBase withIntermediateDirectories:YES attributes:nil error:NULL];
// copy themes from resource
NSString* resourceBase = [self resourceBasePath];
NSArray* resourceFiles = [fm contentsOfDirectoryAtPath:resourceBase error:NULL];
for (NSString* file in resourceFiles) {
if ([file hasPrefix:@"Sample."]) {
NSString* source = [resourceBase stringByAppendingPathComponent:file];
NSString* dest = [userBase stringByAppendingPathComponent:file];
[fm copyItemAtPath:source toPath:dest error:NULL];
}
}
}
+ (NSString*)buildResourceFileName:(NSString*)name
{
return [NSString stringWithFormat:@"resource:%@", name];
}
+ (NSString*)buildUserFileName:(NSString*)name
{
return [NSString stringWithFormat:@"user:%@", name];
}
+ (NSArray*)extractFileName:(NSString*)source
{
NSArray* ary = [source componentsSeparatedByString:@":"];
if (ary.count != 2) return nil;
return ary;
}
+ (NSString*)resourceBasePath
{
if (!resourceBasePath) {
resourceBasePath = [[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Themes"] retain];
}
return resourceBasePath;
}
+ (NSString*)userBasePath
{
if (!userBasePath) {
NSString* applicationSupportPath = NSHomeDirectory();
NSArray* ary = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask];
if (ary.count) {
applicationSupportPath = [ary[0] path];
}
NSString* path = applicationSupportPath;
path = [path stringByAppendingPathComponent:@"LimeChat"];
path = [path stringByAppendingPathComponent:@"Themes"];
userBasePath = [path retain];
}
return userBasePath;
}
@end