Created
November 27, 2012 05:58
-
-
Save rossmartin/4152618 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********* PhoneGapDropbox.m Cordova Plugin Implementation *******/ | |
#import "PhoneGapDropbox.h" | |
#import <Cordova/CDV.h> | |
#import <DropboxSDK/DropboxSDK.h> // import the DropboxSDK | |
#import "AppDelegate.h" | |
@implementation PhoneGapDropbox | |
@synthesize restoreJavaScript; | |
- (void) link:(CDVInvokedUrlCommand*)command | |
{ | |
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; | |
UIViewController *topView = appDelegate.detailViewController; // appDelegate.detailViewController is the web view in this case | |
CDVPluginResult* pluginResult = nil; | |
NSString* javaScript = nil; | |
NSLog(@"Dropbox link method is executing"); | |
if (![[DBSession sharedSession] isLinked]) { | |
[[DBSession sharedSession] linkFromController:topView]; | |
} | |
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; | |
javaScript = [pluginResult toSuccessCallbackString:command.callbackId]; | |
[self writeJavascript:javaScript]; | |
} | |
- (void) checkAuth:(CDVInvokedUrlCommand*)command | |
{ | |
CDVPluginResult* pluginResult = nil; | |
NSString* javaScript = nil; | |
if ([[DBSession sharedSession] isLinked]){ | |
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; | |
javaScript = [pluginResult toSuccessCallbackString:command.callbackId]; | |
NSLog(@"this user has previously linked with Dropbox"); | |
} else { | |
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR]; | |
javaScript = [pluginResult toErrorCallbackString:command.callbackId]; | |
NSLog(@"this user is not linked with Dropbox"); | |
} | |
[self writeJavascript:javaScript]; | |
} | |
- (void) unlink:(CDVInvokedUrlCommand*)command | |
{ | |
CDVPluginResult* pluginResult = nil; | |
NSString* javaScript = nil; | |
NSLog(@"Dropbox unlink method is executing"); | |
[[DBSession sharedSession] unlinkAll]; | |
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; | |
javaScript = [pluginResult toSuccessCallbackString:command.callbackId]; | |
[self writeJavascript:javaScript]; | |
} | |
- (void) upload:(CDVInvokedUrlCommand*)command | |
{ | |
CDVPluginResult* pluginResult = nil; | |
NSString* javaScript = nil; | |
NSLog(@"Dropbox backup method is executing"); | |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory | |
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"somefile.txt"]; | |
//[self.restClient uploadFile:@"somefile.txt" toPath:@"/" withParentRev:nil fromPath:filePath]; // this will create new backup files, ex. some-file(1).txt, etc. | |
[self.restClient uploadFile:@"somefile.txt" toPath:@"/" fromPath:filePath]; // it says this is deprecated but this is the only way to overwrite/update the file | |
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; | |
javaScript = [pluginResult toSuccessCallbackString:command.callbackId]; | |
[self writeJavascript:javaScript]; | |
} | |
- (void) download:(CDVInvokedUrlCommand*)command | |
{ | |
CDVPluginResult* pluginResult = nil; | |
NSLog(@"Dropbox restore method is executing"); | |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory | |
NSString *localPath = [documentsDirectory stringByAppendingPathComponent:@"somefile.txt"]; | |
NSString *dropBoxFile = @"/somefile.txt"; | |
[[self restClient] loadFile:dropBoxFile intoPath:localPath]; // v1.1.1 RDM loadedFile method will execute after this is done | |
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; | |
self.restoreJavaScript = [pluginResult toSuccessCallbackString:command.callbackId]; // add the callback to the restoreJavaScript instance var | |
} | |
// this below is needed for the REST client | |
- (DBRestClient *)restClient { | |
if (!restClient) { | |
restClient = | |
[[DBRestClient alloc] initWithSession:[DBSession sharedSession]]; | |
restClient.delegate = (id)self; | |
} | |
return restClient; | |
} | |
- (void)restClient:(DBRestClient*)client uploadedFile:(NSString*)destPath | |
from:(NSString*)srcPath metadata:(DBMetadata*)metadata { | |
NSLog(@"File uploaded successfully to path: %@", metadata.path); | |
} | |
- (void)restClient:(DBRestClient*)client uploadFileFailedWithError:(NSError*)error { | |
NSLog(@"File upload failed with error - %@", error); | |
} | |
- (void) restClient:(DBRestClient*)client loadedFile:(NSString*)localPath { | |
NSLog(@"File downloaded into path: %@", localPath); | |
[self writeJavascript:restoreJavaScript]; // return code to execute JavaScript function after file is downloaded | |
} | |
- (void)restClient:(DBRestClient*)client loadFileFailedWithError:(NSError*)error { | |
NSLog(@"There was an error loading the file - %@", error); | |
} | |
- (void)restClient:(DBRestClient *)client loadedMetadata:(DBMetadata *)metadata { | |
if (metadata.isDirectory) { | |
NSLog(@"Folder '%@' contains:", metadata.path); | |
for (DBMetadata *file in metadata.contents) { | |
NSLog(@"\t%@", file.filename); | |
} | |
} | |
} | |
- (void)restClient:(DBRestClient *)client | |
loadMetadataFailedWithError:(NSError *)error { | |
NSLog(@"Error loading metadata: %@", error); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment