cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From i..@apache.org
Subject [1/6] git commit: file-extras: Update iOS code to work better with new version of File
Date Thu, 13 Feb 2014 03:41:11 GMT
Updated Branches:
  refs/heads/file-extras-rewrite [created] b7d0b60fa


file-extras: Update iOS code to work better with new version of File


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugins/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugins/commit/84881b9a
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugins/tree/84881b9a
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugins/diff/84881b9a

Branch: refs/heads/file-extras-rewrite
Commit: 84881b9a3ac875f9da8dba4786b418700f8d1414
Parents: a8debe9
Author: Ian Clelland <iclelland@chromium.org>
Authored: Wed Feb 12 14:17:02 2014 -0500
Committer: Ian Clelland <iclelland@chromium.org>
Committed: Wed Feb 12 22:33:39 2014 -0500

----------------------------------------------------------------------
 file-extras/fileextras.js    |   5 +-
 file-extras/ios/FileExtras.m | 111 +++++++++++++++++++++++++++++---------
 2 files changed, 87 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugins/blob/84881b9a/file-extras/fileextras.js
----------------------------------------------------------------------
diff --git a/file-extras/fileextras.js b/file-extras/fileextras.js
index a5eb426..459888c 100644
--- a/file-extras/fileextras.js
+++ b/file-extras/fileextras.js
@@ -52,10 +52,7 @@ var Purpose = {
 fileextras.getDirectoryForPurpose = function(purpose, options, successCallback, failureCallback)
{
     argscheck.checkArgs('sOfF', 'fileextras.getDirectoryForPurpose', arguments);
     var augmentedSuccessCallback = successCallback && function(fullPath) {
-        var lastSegment = fullPath.replace(/.*\//, '');
-        // Note: the fileSystem property is null
-        var directoryEntry = new DirectoryEntry(lastSegment, fullPath);
-        successCallback(directoryEntry);
+        resolveLocalFileSystemURL(fullPath, successCallback, failureCallback);
     };
 
     var purposeInt = Purpose[purpose];

http://git-wip-us.apache.org/repos/asf/cordova-plugins/blob/84881b9a/file-extras/ios/FileExtras.m
----------------------------------------------------------------------
diff --git a/file-extras/ios/FileExtras.m b/file-extras/ios/FileExtras.m
index 827126c..80378cb 100644
--- a/file-extras/ios/FileExtras.m
+++ b/file-extras/ios/FileExtras.m
@@ -19,6 +19,8 @@
 */
 
 #import <Cordova/CDVPlugin.h>
+#import "CDVFile.h"
+#import "CDVLocalFilesystem.h"
 
 enum FileSystemPurpose {
     DATA = 0,
@@ -29,11 +31,71 @@ enum FileSystemPurpose {
 };
 typedef int FileSystemPurpose;
 
-@interface FileExtras : CDVPlugin
+@interface FileExtras : CDVPlugin {
+    NSDictionary *availableFilesystems;
+    NSMutableSet *installedFilesystems;
+}
 @end
 
 @implementation FileExtras
 
+- (id)initWithWebView:(UIWebView*)theWebView
+{
+    self = [super initWithWebView:theWebView];
+    if (self) {
+        NSString *libPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask,
YES) objectAtIndex:0];
+        NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,
YES) objectAtIndex:0];
+        availableFilesystems = @{
+            @"library": libPath,
+            @"library-nosync": [libPath stringByAppendingPathComponent:@"NoCloud"],
+            @"documents": docPath,
+            @"documents-nosync": [docPath stringByAppendingPathComponent:@"NoCloud"],
+            @"cache": [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,
YES) objectAtIndex:0],
+            @"bundle": [[NSBundle mainBundle] bundlePath],
+            @"root": @"/"
+        };
+        installedFilesystems = [[NSMutableSet alloc] initWithCapacity:7];
+    }
+    return self;
+}
+
+- (void)pluginInitialize
+{
+    id vc = self.viewController;
+    
+    NSDictionary *settings = [vc settings];
+    NSString *filesystemsStr = [[settings objectForKey:@"iosextrafilesystems"] lowercaseString];
+    if (!filesystemsStr) {
+        filesystemsStr = @"library,library-nosync,documents,documents-nosync,cache,bundle";
+    }
+    NSArray *filesystems = [filesystemsStr componentsSeparatedByString:@","];
+    
+    /* Build non-syncable directories as necessary */
+    for (NSString *nonSyncFS in @[@"library-nosync", @"documents-nosync"]) {
+        if ([filesystems containsObject:nonSyncFS]) {
+            [self makeNonSyncable:[availableFilesystems objectForKey:nonSyncFS]];
+        }
+    }
+    
+    CDVFile *filePlugin = [[vc commandDelegate] getCommandInstance:@"File"];
+    if (filePlugin) {
+        /* Register filesystems in order */
+        for (NSString *fsName in filesystems) {
+            if (![installedFilesystems containsObject:fsName]) {
+                NSString *fsRoot = [availableFilesystems objectForKey:fsName];
+                if (fsRoot) {
+                    [filePlugin registerFilesystem:[[CDVLocalFilesystem alloc] initWithName:fsName
root:fsRoot]];
+                    [installedFilesystems addObject:fsName];
+                } else {
+                    NSLog(@"Unrecognized extra filesystem identifier: %@", fsName);
+                }
+            }
+        }
+    } else {
+        NSLog(@"File plugin not found; cannot initialize file-extras plugin");
+    }
+}
+
 - (void)makeNonSyncable:(NSString*)path {
     [[NSFileManager defaultManager] createDirectoryAtPath:path
               withIntermediateDirectories:YES
@@ -53,29 +115,33 @@ typedef int FileSystemPurpose;
     NSString *path = nil;
 
     switch (purpose) {
-        case DATA:
-            path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask,
YES) objectAtIndex:0];
-            if (!syncable) {
-                path = [path stringByAppendingPathComponent:@"NoCloud"];
-                [self makeNonSyncable:path];
-            }
-            break;
+      case DATA:
+        if (syncable && [installedFilesystems containsObject:@"library"]) {
+            path = @"cdvfile://localhost/library/";
+        } else if ([installedFilesystems containsObject:@"library-nosync"]) {
+            path = @"cdvfile://localhost/library-nosync/";
+        }
+        break;
       case DOCUMENTS:
-            path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,
YES) objectAtIndex:0];
-            if (!syncable) {
-                path = [path stringByAppendingPathComponent:@"NoCloud"];
-                [self makeNonSyncable:path];
-            }
-            break;
+        if (syncable && [installedFilesystems containsObject:@"documents"]) {
+            path = @"cdvfile://localhost/documents/";
+        } else if ([installedFilesystems containsObject:@"documents-nosync"]) {
+            path = @"cdvfile://localhost/documents-nosync/";
+        }
+        break;
       case CACHE:
-          path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,
YES) objectAtIndex:0];
-          break;
+        if ([installedFilesystems containsObject:@"cache"]) {
+            path = @"cdvfile://localhost/cache/";
+        }
+        break;
       case TEMP:
-          path = NSTemporaryDirectory();
-          break;
+        path = @"cdvfile://localhost/temporary/";
+        break;
       case IOS_BUNDLE:
-          path = [[NSBundle mainBundle] bundlePath];
-          break;
+        if ([installedFilesystems containsObject:@"bundle"]) {
+            path = @"cdvfile://localhost/bundle/";
+        }
+        break;
     }
 
     CDVPluginResult *pluginResult = nil;
@@ -83,11 +149,6 @@ typedef int FileSystemPurpose;
     if (!path) {
         pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
     } else {
-        // Remove the trailing slash if it's there.
-        if ([path hasSuffix:@"/"]) {
-            path = [path substringToIndex:[path length] - 1];
-        }
-
         pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:path];
     }
 


Mime
View raw message