-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLSProfileManager.m
More file actions
417 lines (309 loc) · 12.2 KB
/
LSProfileManager.m
File metadata and controls
417 lines (309 loc) · 12.2 KB
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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//
// LSProfileManager.m
// LSProfileManager
//
// Created by StephenChen on 14/12/19.
// Copyright (c) 2014年 Lansion. All rights reserved.
//
#import "LSProfileManager.h"
#import "LSFileManager.h"
#import "CommonFunc.h"
static NSString* KEY_PROFILENAME = @"PROFILENAME";
static NSString* KEY_LASTLOGIN_PROFILE = @"LASTLOGIN_PROFILE";
static NSString* KEY_LASTLOGIN_PSW = @"LASTLOGIN_PSW";
static NSString* profileHome = @"profiles";
static NSString* profileList = @"profiles/profiles.plist";
static NSString* lastLogin = @"profiles/lastLogin.plist";
static NSString* imageFolder = @"profiles/%@/images";
static NSString* audioFolder = @"profiles/%@/audios";
static NSString* profileInfo = @"profiles/%@/info.plist";
static NSString* extraFolder = @"profiles/%@/%@";
static NSMutableDictionary *curProfile;
@implementation LSProfileManager
+ (BOOL) setUp {
BOOL re = [LSFileManager setUp];
if (!re)
return re;
re = [LSFileManager isExisted:profileList under:DOC_DIR];
if (!re) {
[LSFileManager createFolder:profileHome under:DOC_DIR];//创建程序文件夹
[LSFileManager writeNSDictionary:[NSDictionary new] toFile:profileList under:DOC_DIR];//创建账户管理列表
[LSFileManager writeNSDictionary:[NSDictionary new] toFile:lastLogin under:DOC_DIR];//创建上次账户记录文件
}
return YES;
}
//加载当前账户文件,并记录当前账户
+ (BOOL) loadProfile:(NSString *)name password:(NSString*)password{
NSMutableDictionary* profiles = [LSProfileManager listProfilesForUpdate];
if ([profiles objectForKey:name] == nil) {//用户不存在
[LSProfileManager deleteHomeFolder:name];
//在用户集列表中添加用户
[profiles setValue:@"NO_ASSIGNED" forKey:name];
[LSFileManager writeNSDictionary:profiles toFile:profileList under:DOC_DIR];
//创建用户文件包
[LSProfileManager createHomeFolder:name];
//用户图片包
NSString* imageFolderPath = [NSString stringWithFormat:imageFolder, name];
[LSFileManager createFolder:imageFolderPath under:DOC_DIR];
//用户语音包
NSString* audioFolderPath = [NSString stringWithFormat:audioFolder, name];
[LSFileManager createFolder:audioFolderPath under:DOC_DIR];
//用户配置信息列表
NSString* profileInfoPath = [NSString stringWithFormat:profileInfo, name];
NSMutableDictionary *infoDict = [NSMutableDictionary new];
[infoDict setObject:name forKey:KEY_PROFILENAME];
[LSFileManager writeNSDictionary:infoDict toFile:profileInfoPath under:DOC_DIR];
}
NSString* profileInfoPath = [NSString stringWithFormat:profileInfo, name];
curProfile = [LSFileManager readNSMutableDictionaryFile:profileInfoPath under:DOC_DIR];
//上次登录信息
NSMutableDictionary *lastLoginData = [NSMutableDictionary new];
[lastLoginData setObject:name forKey:KEY_LASTLOGIN_PROFILE];
[lastLoginData setObject:password forKeyedSubscript:KEY_LASTLOGIN_PSW];
[LSFileManager writeNSDictionary:lastLoginData toFile:lastLogin under:DOC_DIR];
return YES;
}
+ (BOOL) deleteProfile:(NSString*)name {
NSMutableDictionary* profiles = [LSProfileManager listProfilesForUpdate];
// delete before create
[profiles removeObjectForKey:name];
[LSProfileManager deleteHomeFolder:name];
[LSFileManager writeNSDictionary:profiles toFile:profileList under:DOC_DIR]; // create a plist file store profile and username;
return YES;
}
+ (BOOL) createFolder:(NSString*)folderName {
NSString* extraFolderPath = [LSProfileManager getFolderPath:folderName];
if (extraFolderPath == nil) {
return NO;
}
if ([LSFileManager isExisted:extraFolderPath under:DOC_DIR]) {
return YES;
}
return [LSFileManager createFolder:extraFolderPath under:DOC_DIR];
}
+ (BOOL) deleteFolder:(NSString*)folderName {
NSString* extraFolderPath = [LSProfileManager getFolderPath:folderName];
if (extraFolderPath == nil) {
return NO;
}
return [LSFileManager deleteFile:extraFolderPath under:DOC_DIR];
}
+ (BOOL) resetLastLoginPsw{
NSMutableDictionary *dic = [[self lastLogin]mutableCopy];
[dic setObject:@"" forKey:KEY_LASTLOGIN_PSW];
[LSFileManager writeNSDictionary:[dic copy] toFile:lastLogin under:DOC_DIR];
return YES;
}
+ (NSString*) getFolderPath:(NSString*)folderName {
if (curProfile == nil) {
return nil;
}
NSString *profile = [curProfile valueForKey:KEY_PROFILENAME];
if (profile == nil) {
return nil;
}
return [NSString stringWithFormat:extraFolder,profile,folderName];
}
+ (NSString*) saveImage:(UIImage*)image{
if (curProfile == nil) {
return nil;
}
NSString *profile = [curProfile valueForKey:KEY_PROFILENAME];
if (profile == nil) {
return nil;
}
if (image == nil) {
return nil;
}
NSData *imageData = nil;
// get png format nsdata
imageData = UIImagePNGRepresentation(image);
if (imageData == nil || [imageData length] <= 0) {
return nil;
}
NSString* imageName;
NSDate* currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYYMMddhhmmssSSS"];
NSString* tempName = [CommonFunc base64StringFromText:[dateFormatter stringFromDate:currentDate]];
// NSString* tempName = @"1";
imageName = [tempName stringByAppendingPathExtension:@"png"];
//将图片写入指定路径
NSString* imagePath = [NSString stringWithFormat: imageFolder,profile];
NSString* filePath = [imagePath stringByAppendingPathComponent:imageName];
NSString* path = [LSFileManager writeNSData:imageData toFile:filePath under:DOC_DIR];
if (path == nil) {
return nil;
}
return imageName;
}
+ (UIImage*) getImage:(NSString *)imageName{
if (curProfile == nil) {
return nil;
}
NSString *profile = [curProfile valueForKey:KEY_PROFILENAME];
if (profile == nil) {
return nil;
}
if (imageName == nil) {
return nil;
}
NSString* imagePath = [NSString stringWithFormat: imageFolder,profile];
NSString* filePath = [imagePath stringByAppendingPathComponent:imageName];
BOOL isExist = [LSFileManager isExisted:filePath under:DOC_DIR];
if (!isExist) {
// NSLog(@"can not find image of %@.", imageName);
return nil;
}
NSData* imageData = [LSFileManager readNSDataFile:filePath under:DOC_DIR];
UIImage *image = [UIImage imageWithData:imageData];
if (image == nil) {
// NSLog(@"Error when read the image");
return nil;
}
return image;
}
+ (NSString*) saveAudio:(NSData*)audioDate{
if (curProfile == nil) {
return nil;
}
NSString *profile = [curProfile valueForKey:KEY_PROFILENAME];
if (profile == nil) {
return nil;
}
if (audioDate == nil||[audioDate length]<=0) {
// NSLog(@"Can not create audio file.");
return nil;
}
NSString* audioName;
NSDate* currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYYMMddhhmmssSSS"];
NSString* tempName = [CommonFunc base64StringFromText:[dateFormatter stringFromDate:currentDate]];
audioName = [tempName stringByAppendingPathExtension:@"caf"];
//将图片写入指定路径
NSString* audioPath = [NSString stringWithFormat: audioFolder,profile];
NSString* filePath = [audioPath stringByAppendingPathComponent:audioName];
NSString* path = [LSFileManager writeNSData:audioDate toFile:filePath under:DOC_DIR];
if (path == nil) {
return nil;
}
return audioName;
}
+ (NSData*) getAudio:(NSString*)audioName{
if (curProfile == nil) {
return nil;
}
NSString *profile = [curProfile valueForKey:KEY_PROFILENAME];
if (profile == nil) {
return nil;
}
if (audioName == nil) {
return nil;
}
NSString* audioPath = [NSString stringWithFormat: audioFolder,profile];
NSString* filePath = [audioPath stringByAppendingPathComponent:audioName];
BOOL isExist = [LSFileManager isExisted:filePath under:DOC_DIR];
if (!isExist) {
// NSLog(@"can not find audio of %@.", audioName);
return nil;
}
NSData* audioData = [LSFileManager readNSDataFile:filePath under:DOC_DIR];
if (audioData == nil) {
// NSLog(@"Error when read the audio");
return nil;
}
return audioData;
}
// basic information management for profiles
+ (BOOL) setKey:(NSString*)key withValue:(NSObject*)value{
if (curProfile == nil) {
return NO;
}
NSString *profile = [curProfile valueForKey:KEY_PROFILENAME];
if (profile == nil) {
return NO;
}
if (key == nil||key.length<=0) {
return NO;
}
if (key == nil||value==nil||key.length<=0) {
return NO;
}
NSString* profileInfoPath = [NSString stringWithFormat:profileInfo, profile];
NSMutableDictionary* infoDictionary = [LSProfileManager listInfoFileForUpdate:profileInfoPath];
if (infoDictionary == nil) {
// NSLog(@"Can not fine info file of profileInfo");
return NO;
}
[infoDictionary setObject:value forKey:key];
[LSFileManager writeNSDictionary:infoDictionary toFile:profileInfoPath under:DOC_DIR];
return YES;
}
+ (NSObject*) getValueOfKey:(NSString*)key {
if (curProfile == nil) {
return nil;
}
NSString *profile = [curProfile valueForKey:KEY_PROFILENAME];
if (profile == nil) {
return nil;
}
if (key == nil||key.length<=0) {
return nil;
}
NSString* profileInfoPath = [NSString stringWithFormat:profileInfo, profile];
NSDictionary* infoDictionary = [LSProfileManager listInfoFile:profileInfoPath];
if (infoDictionary != nil) {
return [infoDictionary objectForKey:key];
}
return nil;
}
// private
+ (BOOL) checkInfoFile:(NSString*) infoFile {
return [LSFileManager isExisted:infoFile under:DOC_DIR];
}
+ (NSDictionary*) listInfoFile:(NSString*)infoFile {
if ([LSProfileManager checkInfoFile:infoFile]) {
return [LSFileManager readNSDictionaryFile:infoFile under:DOC_DIR];
}
return nil;
}
+ (NSMutableDictionary*) listInfoFileForUpdate:(NSString*)infoFile {
if ([LSProfileManager checkInfoFile:infoFile]) {
return [LSFileManager readNSMutableDictionaryFile:infoFile under:DOC_DIR];
}
return nil;
}
+ (NSDictionary*) listProfiles {
NSDictionary* profiles = [LSFileManager readNSDictionaryFile:profileList under:DOC_DIR];
return profiles;
}
+ (NSDictionary*) lastLogin{
return [LSFileManager readNSMutableDictionaryFile:lastLogin under:DOC_DIR];
}
+ (NSMutableDictionary*) listProfilesForUpdate {
NSMutableDictionary* profiles = [LSFileManager readNSMutableDictionaryFile:profileList under:DOC_DIR];
return profiles;
}
+ (BOOL) checkPlist:(NSString*)name {
NSDictionary *profiles = [LSProfileManager listProfiles];
return [[profiles allKeys] containsObject:name];
}
+ (BOOL) checkHomeFolder:(NSString*)name {
NSString *homeFolder = [profileHome stringByAppendingPathComponent:name];
return [LSFileManager isExisted:homeFolder under:DOC_DIR];
}
+ (BOOL) createHomeFolder:(NSString*) name {
NSString *homeFolder = [profileHome stringByAppendingPathComponent:name];
if ([LSFileManager createFolder:homeFolder under:DOC_DIR]) {
NSString* infoFile = [NSString stringWithFormat:profileInfo, name];
infoFile = [LSFileManager writeNSDictionary:[NSDictionary new] toFile:infoFile under:DOC_DIR]; // create a plist file store profile and username;
return infoFile == nil;
};
return NO;
}
+ (BOOL) deleteHomeFolder:(NSString*) name {
NSString *homeFolder = [profileHome stringByAppendingPathComponent:name];
return [LSFileManager deleteFile:homeFolder under:DOC_DIR];
}
@end