-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSSYDropboxGuy.m
279 lines (236 loc) · 8.42 KB
/
SSYDropboxGuy.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
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
#import "SSYDropboxGuy.h"
//#import "NSError+InfoAccess.h"
//#import "NSArray+SafeGetters.h"
//#import "NSString+Base64.h"
//#import "NSString+PythonPickles.h"
//#import "NSString+MorePaths.h"
#import "SSYOtherApper.h"
//#import "SSYShellTasker.h"
#import "SSWebBrowsing.h"
#import "NSFileManager+SomeMore.h"
#import "NSDate+NiceFormats.h"
NSString* const SSYDropboxGuyErrorDomain = @"SSYDropboxGuyErrorDomain" ;
NSString* const constDropboxBundleIdentifier = @"com.getdropbox.dropbox" ;
@implementation SSYDropboxGuy
+ (NSImage*)dropboxIcon {
NSImage* image = [SSYOtherApper iconForAppWithBundleIdentifier:constDropboxBundleIdentifier] ;
if (!image) {
image = [NSImage imageNamed:NSImageNameNetwork] ;
}
return image ;
}
+ (void)getDropbox {
[SSWebBrowsing browseToURLString:@"http://getdropbox.com"
browserBundleIdentifier:nil
activate:YES] ;
}
+ (NSString*)defaultDropboxPath {
NSString* path = [NSHomeDirectory() stringByAppendingPathComponent:@"Dropbox"] ;
return path ;
}
+ (BOOL)dropboxIsAvailable {
#if 0
#warning Faking Dropbox not available
return NO ;
#else
return ([[NSFileManager defaultManager] fileIsPermanentAtPath:[[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier:constDropboxBundleIdentifier]]) ;
#endif
}
+ (NSInteger)isInDropboxPath:(NSString*)path {
NSFileManager* fm = [NSFileManager defaultManager] ;
do {
path = [path stringByDeletingLastPathComponent] ;
if ([path isEqualToString:NSHomeDirectory()]) {
return NSOffState ;
}
NSError* error = nil ;
NSArray* siblings = [fm contentsOfDirectoryAtPath:path
error:&error] ;
if (error) {
return NSMixedState ;
}
if ([siblings indexOfObject:@".dropbox.cache"] != NSNotFound) {
return NSOnState ;
}
} while ([path length] > 2) ;
return NSOffState ;
}
+ (NSInteger)isInDropboxTrashPath:(NSString*)path {
BOOL answer = NO ;
if (path) {
NSArray* components = [path pathComponents] ;
if ([components indexOfObject:@".dropbox.cache"] != NSNotFound) {
answer = YES ;
}
}
return answer ;
}
/*
The following methods no longer work if user has Dropbox 1.2 or later, because
Dropbox has encrypted their configuration database. Sorry!
NSString* const constConfigDir = @".dropbox" ;
NSString* const constOldFilename = @"dropbox" ;
NSString* const constNewFilename = @"config" ;
NSString* const constFileExtension = @"db" ;
+ (NSString*)databasePathNew:(BOOL)new {
NSString* filename = new ? constNewFilename : constOldFilename ;
return [[[NSHomeDirectory() stringByAppendingPathComponent:constConfigDir]
stringByAppendingPathComponent:filename]
stringByAppendingPathExtension:constFileExtension] ;
}
+ (NSString*)dropboxPathError_p:(NSError**)error_p {
if (error_p) {
*error_p = nil ;
}
NSString* databasePath = nil ;
databasePath = [self databasePathNew:YES] ;
BOOL hasNewDatabase = [[NSFileManager defaultManager] fileExistsAtPath:databasePath] ;
BOOL hasOldDatabase ;
if (!hasNewDatabase) {
databasePath = [self databasePathNew:NO] ;
hasOldDatabase = [[NSFileManager defaultManager] fileExistsAtPath:databasePath] ;
}
if (!hasNewDatabase && !hasOldDatabase) {
return nil ;
}
NSError* underlyingError = nil ;
SSYSqliter* sqliter = [[SSYSqliter alloc] initWithPath:databasePath
error_p:&underlyingError] ;
if (!sqliter) {
// User has a Dropbox but database cannot be read
if (error_p) {
*error_p = [NSError errorWithDomain:SSYDropboxGuyErrorDomain
code:651101
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
@"Dropbox database couldn't be opened", NSLocalizedDescriptionKey,
underlyingError, NSUnderlyingErrorKey, // may be nil
nil]] ;
}
return nil ;
}
NSArray* queryResults = [sqliter selectColumn:@"value"
from:@"config"
whereColumn:@"key"
is:@"dropbox_path"
error:&underlyingError] ;
// Memory leak (and sqlite database left open) fixed in BookMacster version 1.3.19…
[sqliter release] ;
if (underlyingError) {
if (error_p) {
*error_p = [NSError errorWithDomain:SSYDropboxGuyErrorDomain
code:651102
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
@"Error reading Dropbox configuration", NSLocalizedDescriptionKey,
underlyingError, NSUnderlyingErrorKey, // may be nil
nil]] ;
}
return nil ;
}
NSString* queryResult = [queryResults firstObjectSafely] ;
NSString* dropboxPath= nil ;
if (!queryResult) {
// There is no 'dropbox_path' in table 'config' of the user's dropbox
// database. This means that they are using the default dropbox
// location, which is ...
if (error_p) {
*error_p = nil ;
}
dropboxPath = [self defaultDropboxPath] ;
}
else if (hasNewDatabase) {
dropboxPath = queryResult ;
}
else if (hasOldDatabase) {
// In the old database, the dropbox path in the database was first
// encoded as a Python pickle and then base64 encoded. We must
// undo those two encodings. Example:
// queryResult = @"Vi9Vc2Vycy9qay9Ecm9wYm94Mi9Ecm9wYm94CnAxCi4="
// pythonPickle = @"V/Users/jk/Dropbox2/Dropbox\np1\n.\n"
// dropboxPath = @"/Users/jk/Dropbox2/Dropbox"
// Base64 Decoding
NSString* pythonPickle = [queryResult stringBase64Decoded] ;
if (!pythonPickle) {
if (error_p) {
*error_p = [NSError errorWithDomain:SSYDropboxGuyErrorDomain
code:651103
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
@"Error base64 decoding Dropbox path", NSLocalizedDescriptionKey,
queryResult, @"base64",
nil]] ;
}
return nil ;
}
// Unpickling
dropboxPath = [pythonPickle pythonUnpickledError_p:&underlyingError] ;
if (!dropboxPath && error_p) {
*error_p = [NSError errorWithDomain:SSYDropboxGuyErrorDomain
code:651104
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
@"Error unpickling Dropbox path", NSLocalizedDescriptionKey,
pythonPickle, @"pickle",
nil]] ;
}
}
// So now we've got the supposed path out of the database. Now, let's
// make sure that folder exists at the supposed path.
BOOL isDir ;
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:dropboxPath
isDirectory:&isDir] ;
if (!exists || !isDir) {
if (error_p) {
NSString* msg1 = [NSString stringWithFormat:@"Dropbox folder is missing. Expected at:\n\n%@",
dropboxPath] ;
NSString* msg2 = @"Launch Dropbox app and follow their instructions." ;
*error_p = [NSError errorWithDomain:SSYDropboxGuyErrorDomain
code:651105
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
msg1, NSLocalizedDescriptionKey,
msg2, NSLocalizedRecoverySuggestionErrorKey,
nil]] ;
}
dropboxPath = nil ;
}
return dropboxPath ;
}
+ (BOOL)userHasDropboxError_p:(NSError**)error_p {
NSError* error = nil ;
NSString* path = [self dropboxPathError_p:&error] ;
if (error && error_p) {
*error_p = error ;
}
return (path != nil) ;
}
+ (BOOL)pathIsInDropbox:(NSString*)path {
if (!path) {
return NO ;
}
NSString* dropboxPath = [SSYDropboxGuy dropboxPathError_p:NULL] ;
if (!dropboxPath) {
return NO ;
}
BOOL isIn = [path pathIsDescendantOf:dropboxPath] ;
if (!isIn) {
// There is some kind of bug in the system whereby if this document's
// file path is /Users/jk/Cloud/Dropbox/Dropbox/DeleteTest.bkmslf,
// on my old 32-bit Mac Mini, path will at this point be instead
// /Users/jk/Cloud/dropbox/dropbox/DeleteTest.bkmslf. The same
// thing happens in BookMacster-Worker, where the path is obtained
// from -[BkmxDocumentController pathOfDocumentWithUuid:::::], which
// gets it from a stored alias record.
// So if that didn't work, we morph and retest it.
NSMutableString* morphedPath = [path mutableCopy] ;
NSString* priorPath ;
do {
priorPath = [NSString stringWithString:morphedPath] ;
[morphedPath replaceOccurrencesOfString:@"/dropbox/"
withString:@"/Dropbox/"
options:0
range:NSMakeRange(0, [morphedPath length])] ;
isIn = [morphedPath pathIsDescendantOf:dropboxPath] ;
} while (!isIn && ![priorPath isEqualToString:morphedPath]) ;
[morphedPath release] ;
}
return isIn ;
}
*/
@end