-
Notifications
You must be signed in to change notification settings - Fork 3
/
SCAccountInfoManager.m
117 lines (96 loc) · 3.28 KB
/
SCAccountInfoManager.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
/**
Copyright (C) 2012 Alessandro Sangiuliano
Author: Alessandro Sangiuliano <[email protected]>
Date: January 2012
License: Modified BSD
*/
#import "SCAccountInfoManager.h"
@implementation SCAccountInfoManager
- (id)init
{
self = [super init];
NSError *error;
BOOL created = NO, isDir = YES;
NSArray *libraryDirs = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
gPath = [[[libraryDirs objectAtIndex: 0]
stringByAppendingPathComponent: @"Addresses"] mutableCopy];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL exist = [fileManager fileExistsAtPath:gPath isDirectory:&isDir];
if (!exist)
created = [[NSFileManager defaultManager] createDirectoryAtPath: gPath
withIntermediateDirectories: YES
attributes: nil
error: &error];
if (!created && !exist )
NSLog(@"Create directory error: %@", error);
/* This will create the correct path to save the waJID in the Addresses directory
* otherwise the path created will be: <parentsDirectories...>/AddresseswaJID,
* that's wrong
*/
[gPath appendString:@"/"];
fileName = @"waJID";
filePath = [[NSMutableString alloc] initWithString:gPath];
[filePath appendString:fileName];
return self;
}
@synthesize filePath;
-(NSString*)readJIDFromFileAtPath:(NSString*)aPath
{
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSData *contents;
if ([fileManager fileExistsAtPath:aPath] == YES)
{
NSFileHandle *fileHandler = [NSFileHandle fileHandleForReadingAtPath:aPath];
contents = [fileHandler readDataToEndOfFile];
[fileHandler closeFile];
}
else
{
[fileManager createFileAtPath:aPath contents:nil attributes:nil];
return @"N";
}
if ([contents length] == 0)
{
return @"N";
}
NSString *jid = [[NSString alloc] initWithData:contents encoding:NSUTF8StringEncoding];
return jid;
}
-(void)writeJIDToFile:(JID*)aJID atPath:(NSString*)aPath
{
NSFileManager *fileManager = [[NSFileManager alloc] init];
if ([fileManager fileExistsAtPath:aPath] == YES)
{
NSString *jid = [aJID jidString];
BOOL done = [jid writeToFile:aPath
atomically:NO
encoding:NSUTF8StringEncoding
error:NULL];
if (done == NO)
{
[NSAlert alertWithMessageText:@"Can't write to file"
defaultButton:@"OK"
alternateButton:nil
otherButton:nil
informativeTextWithFormat:@""];
}
}
else
{
[fileManager createFileAtPath:aPath contents:nil attributes:nil];
NSString *jid = [aJID jidString];
BOOL done = [jid writeToFile:aPath
atomically:NO
encoding:NSUTF8StringEncoding
error:NULL];
if (done == NO)
{
[NSAlert alertWithMessageText:@"Can't write to file"
defaultButton:@"OK"
alternateButton:nil
otherButton:nil
informativeTextWithFormat:@""];
}
}
}
@end