Skip to content
This repository has been archived by the owner on Dec 12, 2020. It is now read-only.

Commit

Permalink
Added archive function and syncing of done.txt file.
Browse files Browse the repository at this point in the history
  • Loading branch information
chuckbjones committed Feb 26, 2012
1 parent 3507fef commit 64ec46f
Show file tree
Hide file tree
Showing 43 changed files with 3,001 additions and 201 deletions.
79 changes: 79 additions & 0 deletions Classes/DropboxFile.h
@@ -0,0 +1,79 @@
/**
* This file is part of Todo.txt Touch, an iOS app for managing your todo.txt file.
*
* @author Todo.txt contributors <todotxt@yahoogroups.com>
* @copyright 2011 Todo.txt contributors (http://todotxt.com)
*
* Dual-licensed under the GNU General Public License and the MIT License
*
* @license GNU General Public License http://www.gnu.org/licenses/gpl.html
*
* Todo.txt Touch is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
* later version.
*
* Todo.txt Touch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with Todo.txt Touch. If not, see
* <http://www.gnu.org/licenses/>.
*
*
* @license The MIT License http://www.opensource.org/licenses/mit-license.php
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#import <Foundation/Foundation.h>
#import <DropboxSDK/DropboxSDK.h>

typedef enum {
dbInitialized = 0,
dbStarted,
dbFound,
dbNotFound,
dbNotChanged,
dbConflict,
dbSuccess,
dbError
} DropboxFileStatus;

@interface DropboxFile : NSObject {
NSString *remoteFile;
NSString *localFile;
NSString *originalRev;
DBMetadata *loadedMetadata;
DropboxFileStatus status;
NSError *error;
}

@property (nonatomic, readonly) NSString *remoteFile;
@property (nonatomic, readonly) NSString *localFile;
@property (nonatomic, readonly) NSString *originalRev;
@property (nonatomic, retain) DBMetadata *loadedMetadata;
@property (nonatomic, assign) DropboxFileStatus status;
@property (nonatomic, retain) NSError *error;

- (id) initWithRemoteFile:(NSString*)aRemoteFile
localFile:(NSString*)aLocalFile
originalRev:(NSString*)rev;

@end
74 changes: 74 additions & 0 deletions Classes/DropboxFile.m
@@ -0,0 +1,74 @@
/**
* This file is part of Todo.txt Touch, an iOS app for managing your todo.txt file.
*
* @author Todo.txt contributors <todotxt@yahoogroups.com>
* @copyright 2011 Todo.txt contributors (http://todotxt.com)
*
* Dual-licensed under the GNU General Public License and the MIT License
*
* @license GNU General Public License http://www.gnu.org/licenses/gpl.html
*
* Todo.txt Touch is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
* later version.
*
* Todo.txt Touch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with Todo.txt Touch. If not, see
* <http://www.gnu.org/licenses/>.
*
*
* @license The MIT License http://www.opensource.org/licenses/mit-license.php
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#import "DropboxFile.h"

@implementation DropboxFile

@synthesize remoteFile, localFile, originalRev, loadedMetadata, status, error;

- (id) initWithRemoteFile:(NSString*)aRemoteFile
localFile:(NSString*)aLocalFile
originalRev:(NSString*)rev {
self = [super init];
if (self) {
remoteFile = [aRemoteFile copy];
localFile = [aLocalFile copy];
originalRev = [rev copy];
status = dbInitialized;
}
return self;

}

- (void) dealloc {
[error release];
[loadedMetadata release];
[localFile release];
[remoteFile release];
[originalRev release];
[super dealloc];
}

@end
25 changes: 14 additions & 11 deletions Classes/DropboxTodoUploader.h → Classes/DropboxFileDownloader.h
Expand Up @@ -42,23 +42,26 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/


#import <Foundation/Foundation.h>
#import "RemoteClient.h"
#import <DropboxSDK/DropboxSDK.h>
#import "DropboxFile.h"

@interface DropboxTodoUploader : NSObject {
id<RemoteClient> remoteClient;
@interface DropboxFileDownloader : NSObject {
DBRestClient *restClient;
NSString *rev;
NSString *localFile;
BOOL overwrite;
id target;
SEL onComplete;
DropboxFileStatus status;
NSError *error;
NSArray *files;
NSInteger curFile;
}

@property (nonatomic, assign) id<RemoteClient> remoteClient;
@property (nonatomic, readonly) NSString *rev;
@property (nonatomic, retain) NSString *localFile;
@property (nonatomic, assign) BOOL overwrite;
@property (nonatomic, readonly) NSArray *files;
@property (nonatomic, readonly) DropboxFileStatus status;
@property (nonatomic, readonly) NSError *error;

- (void) pushTodo;
- (id) initWithTarget:(id)aTarget onComplete:(SEL)selector;
- (void) pullFiles:(NSArray*)dropboxFiles;

@end
125 changes: 91 additions & 34 deletions Classes/DropboxTodoDownloader.m → Classes/DropboxFileDownloader.m
Expand Up @@ -42,15 +42,25 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#import "DropboxTodoDownloader.h"
#import "DropboxRemoteClient.h"
#import "DropboxFileDownloader.h"

@interface DropboxTodoDownloader () <DBRestClientDelegate>
@interface DropboxFileDownloader () <DBRestClientDelegate>
@end

@implementation DropboxTodoDownloader
@implementation DropboxFileDownloader

@synthesize remoteClient, rev;
@synthesize files, status, error;

- (id) initWithTarget:(id)aTarget onComplete:(SEL)selector {
self = [super init];
if (self) {
target = aTarget;
onComplete = selector;
status = dbInitialized;
curFile = -1;
}
return self;
}

- (DBRestClient*)restClient {
if (restClient == nil) {
Expand All @@ -59,55 +69,102 @@ - (DBRestClient*)restClient {
}
return restClient;
}

- (void) pullTodo {

- (void) loadNextFile {
if (++curFile < files.count) {
DropboxFile *file = [files objectAtIndex:curFile];
if (file.status == dbFound) {
[self.restClient loadFile:file.remoteFile
atRev:file.loadedMetadata.rev
intoPath:file.localFile];
} else {
[self loadNextFile];
}
} else {
// we're done!
status = dbSuccess;
[target performSelector:onComplete];
}
}

- (void) loadNextMetadata {
if (++curFile < files.count) {
DropboxFile *file = [files objectAtIndex:curFile];
file.status = dbStarted;
[self.restClient loadMetadata:file.remoteFile];
} else {
// we got all of the metadata, now get the files
curFile = -1;
[self loadNextFile];
}
}

- (void) pullFiles:(NSArray*)dropboxFiles {
[files release];
files = [dropboxFiles retain];
curFile = -1;
status = dbStarted;

// First call loadMetadata to get the current rev
// then, call loadFile with that rev
// don't save rev until we successfully loaded the file
[self.restClient loadMetadata:[DropboxRemoteClient todoTxtRemoteFile]];
// first check metadata of each file, starting with the first
[self loadNextMetadata];
}

#pragma mark -
#pragma mark DBRestClientDelegate methods

- (void)restClient:(DBRestClient*)client loadedMetadata:(DBMetadata*)metadata {
// we loaded the latest metadata for the todo file. Now we can pull it
rev = [metadata.rev retain];

[self.restClient loadFile:[DropboxRemoteClient todoTxtRemoteFile]
atRev:rev
intoPath:[DropboxRemoteClient todoTxtTmpFile]];
DropboxFile *file = [files objectAtIndex:curFile];

// save off the returned metadata
file.loadedMetadata = metadata;

if ([metadata.rev isEqualToString:file.originalRev]) {
// don't bother downloading if the rev is the same
file.status = dbNotChanged;
} else {
file.status = dbFound;
}

// get the next metadata
[self loadNextMetadata];
}

- (void)restClient:(DBRestClient*)client loadMetadataFailedWithError:(NSError*)error {
DropboxFile *file = [files objectAtIndex:curFile];

// there was no metadata for the todo file, meaning it does not exist
// so there is nothing to load
// call RemoteClientDelegate method
if (self.remoteClient.delegate && [self.remoteClient.delegate respondsToSelector:@selector(remoteClient:loadedFile:)]) {
[self.remoteClient.delegate remoteClient:self.remoteClient loadedFile:nil];
}
file.status = dbNotFound;

// get the next metadata
[self loadNextMetadata];
}

- (void)restClient:(DBRestClient*)client loadedFile:(NSString*)destPath {
// save off the downloaded file's rev
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:rev forKey:@"dropbox_last_rev"];

// call RemoteClientDelegate method
if (self.remoteClient.delegate && [self.remoteClient.delegate respondsToSelector:@selector(remoteClient:loadedFile:)]) {
[self.remoteClient.delegate remoteClient:self.remoteClient loadedFile:destPath];
}
DropboxFile *file = [files objectAtIndex:curFile];

file.status = dbSuccess;

[self loadNextFile];
}

- (void)restClient:(DBRestClient*)client loadFileFailedWithError:(NSError*)error {
if (self.remoteClient.delegate && [self.remoteClient.delegate respondsToSelector:@selector(remoteClient:loadFileFailedWithError:)]) {
[self.remoteClient.delegate remoteClient:self.remoteClient loadFileFailedWithError:error];
}
- (void)restClient:(DBRestClient*)client loadFileFailedWithError:(NSError*)theError {
DropboxFile *file = [files objectAtIndex:curFile];

file.status = dbError;
file.error = theError;

status = dbError;
[error release];
error = [theError retain];

// don't bother downloading any more files after the first error
[target performSelector:onComplete];
}

- (void) dealloc {
[rev release];
[error release];
[files release];
[restClient release];
[super dealloc];
}
Expand Down

0 comments on commit 64ec46f

Please sign in to comment.