Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit 892ea17

Browse files
committed
Browser: Added Mac OS as a platform
Also restored ability to open “.touchdb” database files.
1 parent 4b41b19 commit 892ea17

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed

Source/AppList.m

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ - (NSImage*) findAppIcon {
7676
NSString* bundlePath = [self.path stringByAppendingPathComponent: getAppBundleName(self.path)];
7777
NSString* infoPath = [bundlePath stringByAppendingPathComponent: @"Info.plist"];
7878
NSDictionary* plist = [NSDictionary dictionaryWithContentsOfFile: infoPath];
79-
// NSLog(@"plist = %@", plist);
8079
NSString* iconFileName = plist[@"CFBundleIcons"][@"CFBundlePrimaryIcon"][@"CFBundleIconFiles"][0];
8180
if (!iconFileName)
8281
return nil;
@@ -90,7 +89,8 @@ - (NSImage*) findAppIcon {
9089

9190

9291
#define kSimulatorPath @"Library/Application Support/iPhone Simulator/"
93-
#define kDbDirPath @"Library/Application Support/CouchbaseLite"
92+
#define kDbDirName @"CouchbaseLite"
93+
#define kIOSDbDirPath @"Library/Application Support/CouchbaseLite"
9494
#define kDbPathExtensions @[@"cblite", @"touchdb"]
9595
#define kReplicatorDbName @"_replicator"
9696

@@ -115,7 +115,7 @@ - (NSImage*) findAppIcon {
115115
}
116116

117117

118-
static NSDictionary* findOSDirs(NSError** error) {
118+
static NSDictionary* findIOSSimulatorDirs(NSError** error) {
119119
NSString* dir = [NSHomeDirectory() stringByAppendingPathComponent: kSimulatorPath];
120120
return iterateDir(dir, error, ^NSString *(NSString *dirname) {
121121
if (isdigit([dirname characterAtIndex: 0]) && [dirname doubleValue] >= 6.0)
@@ -137,7 +137,7 @@ - (NSImage*) findAppIcon {
137137
NSString* appsDir = [osDir stringByAppendingPathComponent: @"Applications"];
138138
return iterateDir(appsDir, error, ^NSString *(NSString *dirName) {
139139
NSString* appHomeDir = [appsDir stringByAppendingPathComponent: dirName];
140-
NSString* cblPath = [appHomeDir stringByAppendingPathComponent: kDbDirPath];
140+
NSString* cblPath = [appHomeDir stringByAppendingPathComponent: kIOSDbDirPath];
141141
if (![[NSFileManager defaultManager] fileExistsAtPath: cblPath])
142142
return nil;
143143
return [getAppBundleName(appHomeDir) stringByDeletingPathExtension];
@@ -146,14 +146,42 @@ - (NSImage*) findAppIcon {
146146

147147

148148
static NSDictionary* findAppDatabases(NSString* appHomeDir, NSError** error) {
149-
NSString* cblPath = [appHomeDir stringByAppendingPathComponent: kDbDirPath];
149+
NSString* cblPath = [appHomeDir stringByAppendingPathComponent: kIOSDbDirPath];
150150
return iterateDir(cblPath, error, ^NSString *(NSString *filename) {
151151
if (![kDbPathExtensions containsObject: filename.pathExtension])
152152
return nil;
153153
NSString* dbName = filename.stringByDeletingPathExtension;
154154
if ([dbName isEqualToString: kReplicatorDbName])
155155
return nil;
156-
return dbName;
156+
return [dbName stringByReplacingOccurrencesOfString: @":" withString: @"/"];
157+
});
158+
}
159+
160+
161+
static NSDictionary* findMacAppDirs(NSError** error) {
162+
NSString* dirName = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES)[0];
163+
return iterateDir(dirName, error, ^NSString *(NSString *appDirName) {
164+
NSString* appDirPath = [dirName stringByAppendingPathComponent: appDirName];
165+
NSString* cblPath = [appDirPath stringByAppendingPathComponent: kDbDirName];
166+
BOOL isDir;
167+
if (![[NSFileManager defaultManager] fileExistsAtPath: cblPath isDirectory: &isDir]
168+
|| !isDir)
169+
return nil;
170+
return [[appDirName componentsSeparatedByString: @"."] lastObject];
171+
});
172+
}
173+
174+
175+
static NSDictionary* findMacAppDatabases(NSString* appSupportDir, NSError** error) {
176+
NSLog(@"Looking in %@", appSupportDir);
177+
NSString* cblPath = [appSupportDir stringByAppendingPathComponent: kDbDirName];
178+
return iterateDir(cblPath, error, ^NSString *(NSString *filename) {
179+
if (![kDbPathExtensions containsObject: filename.pathExtension])
180+
return nil;
181+
NSString* dbName = filename.stringByDeletingPathExtension;
182+
if ([dbName isEqualToString: kReplicatorDbName])
183+
return nil;
184+
return [dbName stringByReplacingOccurrencesOfString: @":" withString: @"/"];
157185
});
158186
}
159187

@@ -165,7 +193,7 @@ - (NSImage*) findAppIcon {
165193

166194
AppListNode* BuildAppList(NSError** outError) {
167195
AppListNode* root = [[AppListNode alloc] initWithType: kOSNode path: nil displayName: nil];
168-
NSDictionary* versions = findOSDirs(outError);
196+
NSDictionary* versions = findIOSSimulatorDirs(outError);
169197
if (!versions)
170198
return nil;
171199
for (NSString* version in sortedKeys(versions)) {
@@ -190,13 +218,35 @@ - (NSImage*) findAppIcon {
190218
if (versNode.children.count > 0)
191219
[root.children addObject: versNode];
192220
}
221+
222+
// Now find Mac apps:
223+
AppListNode* versNode = [[AppListNode alloc] initWithType: kOSNode path: @"" displayName: @"Mac OS"];
224+
NSDictionary* apps = findMacAppDirs(outError);
225+
if (!apps)
226+
return nil;
227+
for (NSString* app in sortedKeys(apps)) {
228+
AppListNode* appNode = [[AppListNode alloc] initWithType: kAppNode path: apps[app] displayName: app];
229+
230+
NSDictionary* dbs = findMacAppDatabases(apps[app], outError);
231+
if (!dbs)
232+
return nil;
233+
for (NSString* db in sortedKeys(dbs)) {
234+
AppListNode* dbNode = [[AppListNode alloc] initWithType: kDbNode path: dbs[db] displayName: db];
235+
[appNode.children addObject: dbNode];
236+
}
237+
if (appNode.children.count > 0)
238+
[versNode.children addObject: appNode];
239+
}
240+
if (versNode.children.count > 0)
241+
[root.children addObject: versNode];
242+
193243
return root;
194244
}
195245

196246

197247
void TestAppList(void) {
198248
NSError* error;
199-
NSDictionary* versions = findOSDirs(&error);
249+
NSDictionary* versions = findIOSSimulatorDirs(&error);
200250
NSCAssert(versions, @"error %@", error);
201251
for (NSString* version in sortedKeys(versions)) {
202252
NSLog(@"%@:", version);

Source/Couchbase Lite Viewer-Info.plist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<key>CFBundleTypeExtensions</key>
1111
<array>
1212
<string>cblite</string>
13+
<string>touchdb</string>
1314
</array>
1415
<key>CFBundleTypeIconFile</key>
1516
<string>DBDoc</string>

Source/DBDocument.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ - (BOOL)readFromURL:(NSURL *)absoluteURL
3232
{
3333
NSParameterAssert(absoluteURL.isFileURL);
3434
_path = absoluteURL.path;
35-
if (![absoluteURL isFileURL] || ![_path.pathExtension isEqualToString: @"cblite"]) {
35+
if (![absoluteURL isFileURL] || ![@[@"cblite", @"touchdb"] containsObject: _path.pathExtension]) {
3636
if (outError)
3737
*outError = [NSError errorWithDomain: NSCocoaErrorDomain code: -1 userInfo: nil]; //TODO: Real error
3838
return NO;

0 commit comments

Comments
 (0)