Skip to content

Commit

Permalink
Merge branch 'release/1.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
pegli committed Oct 23, 2014
2 parents 1d22624 + fa6c5ee commit 9743409
Show file tree
Hide file tree
Showing 94 changed files with 14,711 additions and 101 deletions.
Binary file removed mobile/android/lib/cbl_collator_so-1.0.2.jar
Binary file not shown.
Binary file added mobile/android/lib/cbl_collator_so-1.0.3.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added mobile/android/lib/stateless4j-2.4.0.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion mobile/android/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 1.1.0
version: 1.2.0
apiversion: 2
description: TouchDB for Titanium
author: Paul Mietz Egli
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public String getContentType() {
@Kroll.getProperty(name = "contentURL")
public String getContentURL() {
BlobStore store = new BlobStore(attachment.getDocument().getDatabase().getAttachmentStorePath());
return "file:/" + store.pathForKey(BlobStore.keyForBlob(getContent().getBytes()));
return "file://" + store.pathForKey(BlobStore.keyForBlob(getContent().getBytes()));
}

@Kroll.getProperty(name = "document")
Expand Down
73 changes: 71 additions & 2 deletions mobile/android/src/com/obscure/titouchdb/DatabaseManagerProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
import com.couchbase.lite.Manager;
import com.couchbase.lite.Status;
import com.couchbase.lite.android.AndroidContext;
import com.couchbase.lite.listener.LiteListener;

@Kroll.proxy(parentModule = TitouchdbModule.class)
public class DatabaseManagerProxy extends KrollProxy {

public static final int DEFAULT_LISTENER_PORT = 5984;

private static final String[] EMPTY_STRING_ARRAY = new String[0];

public static final String LCAT = "DatabaseManagerProxy";
Expand All @@ -29,6 +32,8 @@ public class DatabaseManagerProxy extends KrollProxy {

private KrollDict lastError = null;

private LiteListener listener;

private Manager manager = null;

public DatabaseManagerProxy(Activity activity) {
Expand All @@ -40,7 +45,7 @@ public DatabaseManagerProxy(Activity activity) {
Log.e(LCAT, "Unable to create Manager", e);
}
}

@Kroll.method
public void close() {
lastError = null;
Expand All @@ -60,7 +65,7 @@ public String[] getAllDatabaseNames() {
List<String> names = manager.getAllDatabaseNames();
return names != null ? names.toArray(EMPTY_STRING_ARRAY) : EMPTY_STRING_ARRAY;
}

private DatabaseProxy getCachedDatabaseNamed(String name, boolean create) {
if (manager == null) return null;
lastError = null;
Expand Down Expand Up @@ -106,11 +111,24 @@ public DatabaseProxy getExistingDatabase(String name) {
return getCachedDatabaseNamed(name, false);
}

public String getInternalURL() {
if (listener == null) {
startListener(null);
}

// TODO make sure localhost is ok
return String.format("http://localhost:%d", listener.getListenPort());
}

@Kroll.getProperty(name = "error")
public KrollDict getLastError() {
return this.lastError;
}

protected Manager getManager() {
return manager;
}

@Kroll.method
public boolean installDatabase(String name, String pathToDatabase, String pathToAttachments) {
lastError = null;
Expand All @@ -121,4 +139,55 @@ public boolean installDatabase(String name, String pathToDatabase, String pathTo
public boolean isValidDatabaseName(String name) {
return Manager.isValidDatabaseName(name);
}

@Kroll.method
public KrollDict startListener(@Kroll.argument(optional = true) KrollDict options) {
if (listener != null) {
listener.stop();
listener = null;
}

int port = options != null && options.containsKeyAndNotNull("port") ? options.getInt("port") : DEFAULT_LISTENER_PORT;

listener = new LiteListener(manager, port, null);
listener.start();

int status = listener.serverStatus();
if (status == 0) {
return null;
}

KrollDict error = new KrollDict();
error.put("status", status);
switch (status) {
case 1:
error.put("message", "IO error during start");
break;
case 2:
error.put("message", "unexpected error");
break;
case 3:
error.put("message", "could not bind to port " + listener.getListenPort());
break;
case -1:
error.put("message", "error opening socket " + listener.getListenPort());
break;
case -2:
error.put("message", "listener is already running");
break;
case -3:
error.put("message", "listener did not start correctly");
break;
}

return error;
}

@Kroll.method
public void stopListener() {
if (listener != null) {
listener.stop();
listener = null;
}
}
}
34 changes: 25 additions & 9 deletions mobile/android/src/com/obscure/titouchdb/DatabaseProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;

import android.util.Log;

import com.couchbase.lite.AsyncTask;
import com.couchbase.lite.CouchbaseLiteException;
import com.couchbase.lite.Database;
import com.couchbase.lite.Database.ChangeEvent;
import com.couchbase.lite.Database.ChangeListener;
import com.couchbase.lite.Document;
import com.couchbase.lite.DocumentChange;
import com.couchbase.lite.Status;
import com.couchbase.lite.TransactionalTask;
import com.couchbase.lite.View;
Expand Down Expand Up @@ -58,15 +57,20 @@ public DatabaseProxy(DatabaseManagerProxy managerProxy, Database database) {
database.addChangeListener(this);
}

@Kroll.method
public void addChangeListener(KrollEventCallback cb) {
lastError = null;
// TODO
}

@Override
public void changed(ChangeEvent e) {
// TODO Auto-generated method stub
KrollDict dict = new KrollDict();
dict.put("database", this);
dict.put("isExternal", e.isExternal());

List<DocumentChangeProxy> changes = new ArrayList<DocumentChangeProxy>();
for (DocumentChange change : e.getChanges()) {
changes.add(new DocumentChangeProxy(change));
}

dict.put("changes", changes.toArray(new DocumentChangeProxy[0]));

fireEvent("change", dict);
}

@Kroll.method
Expand All @@ -81,6 +85,12 @@ public boolean compact() {
}
return true;
}

@Kroll.method
public boolean close() {
lastError = null;
return database.close();
}

@Kroll.method
public QueryProxy createAllDocumentsQuery() {
Expand Down Expand Up @@ -274,6 +284,12 @@ public void run(Database db) {

});
}

@Kroll.getProperty(name = "internalURL")
public String getInternalURL() {
// TODO URL encode name?
return String.format("%s/%s", managerProxy.getInternalURL(), database.getName());
}

@Kroll.method
public void runInTransaction(final KrollFunction f) {
Expand Down
54 changes: 54 additions & 0 deletions mobile/android/src/com/obscure/titouchdb/DocumentChangeProxy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.obscure.titouchdb;

import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;

import com.couchbase.lite.DocumentChange;

@Kroll.proxy(parentModule = TitouchdbModule.class)
public class DocumentChangeProxy extends KrollProxy {

private String documentId;

private boolean isConflict;

private boolean isCurrentRevision;

private String revisionId;

private String sourceUrl;

public DocumentChangeProxy(DocumentChange change) {
assert change != null;

this.isCurrentRevision = change.isCurrentRevision();
this.isConflict = change.isConflict();
this.sourceUrl = change.getSourceUrl() != null ? change.getSourceUrl().toString() : "";
}

@Kroll.getProperty(name = "documentId")
public String getDocumentId() {
return documentId;
}

@Kroll.getProperty(name = "revisionId")
public String getRevisionId() {
return revisionId;
}

@Kroll.getProperty(name = "sourceUrl")
public String getSourceUrl() {
return sourceUrl;
}

@Kroll.getProperty(name = "isConflict")
public boolean isConflict() {
return isConflict;
}

@Kroll.getProperty(name = "isCurrentRevision")
public boolean isCurrentRevision() {
return isCurrentRevision;
}

}
24 changes: 21 additions & 3 deletions mobile/android/src/com/obscure/titouchdb/DocumentProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

import com.couchbase.lite.CouchbaseLiteException;
import com.couchbase.lite.Document;
import com.couchbase.lite.Document.ChangeEvent;
import com.couchbase.lite.SavedRevision;

@Kroll.proxy(parentModule = TitouchdbModule.class)
public class DocumentProxy extends KrollProxy {
public class DocumentProxy extends KrollProxy implements Document.ChangeListener {

private static final String LCAT = "DocumentProxy";

Expand All @@ -27,6 +28,17 @@ public DocumentProxy(DatabaseProxy databaseProxy, Document document) {

this.databaseProxy = databaseProxy;
this.document = document;

document.addChangeListener(this);
}

@Override
public void changed(ChangeEvent change) {
KrollDict dict = new KrollDict();
dict.put("document", this);
dict.put("change", new DocumentChangeProxy(change.getChange()));

fireEvent("change", dict);
}

@Kroll.method
Expand Down Expand Up @@ -109,12 +121,18 @@ public SavedRevisionProxy[] getLeafRevisions() {

@Kroll.getProperty(name = "properties")
public KrollDict getProperties() {
return TypePreprocessor.toKrollDict(document.getProperties());
if (document.getCurrentRevision() != null) {
return TypePreprocessor.toKrollDict(document.getProperties());
}
else return null;
}

@Kroll.method
public Object getProperty(String key) {
return TypePreprocessor.preprocess(document.getProperty(key));
if (document.getCurrentRevision() != null) {
return TypePreprocessor.preprocess(document.getProperty(key));
}
else return null;
}

@Kroll.method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public int getCompletedChangesCount() {

@Kroll.getProperty(name = "docIds")
public String[] getDocIds() {
List<String> docids = replicator.getDocIds();
// TODO fix in 1.0.4+ ?
// https://github.com/couchbase/couchbase-lite-java-core/issues/307
List<String> docids = null; // replicator.getDocIds();
return docids != null ? docids.toArray(EMPTY_STRING_ARRAY) : EMPTY_STRING_ARRAY;
}

Expand Down
34 changes: 0 additions & 34 deletions mobile/ios/Classes/ComObscureTitouchdbModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

@interface ComObscureTitouchdbModule ()
@property (nonatomic, strong) TDDatabaseManagerProxy * databaseManagerProxy;
@property (nonatomic, strong) CBLListener * listener;
@end

@implementation ComObscureTitouchdbModule
Expand Down Expand Up @@ -90,39 +89,6 @@ - (void)enableLogging:(id)args {
[CBLManager enableLogging:category];
}

#pragma mark CBLListener

/** start an HTTP listener for the database. Options and defaults are:
port: 5984,
readOnly: false
*/
#define DEFAULT_LISTENER_PORT 5984
- (id)startListener:(id)args {
NSDictionary * opts;
ENSURE_ARG_OR_NIL_AT_INDEX(opts, args, 0, NSDictionary)

if (self.listener) {
[self.listener stop];
self.listener = nil;
}

__block NSError * error = nil;
NSUInteger port = [opts objectForKey:@"port"] ? [[opts objectForKey:@"port"] unsignedIntegerValue] : DEFAULT_LISTENER_PORT;

self.listener = [[CBLListener alloc] initWithManager:[CBLManager sharedInstance] port:port];
self.listener.readOnly = [[opts objectForKey:@"readOnly"] boolValue];

// TODO maybe add auth and Bonjour name?

[self.listener start:&error];

return [self errorDict:error];
}

- (id)stopListener:(id)args {
[self.listener stop];
}

#pragma mark -
#pragma mark CBLAuthenticator

Expand Down
Loading

0 comments on commit 9743409

Please sign in to comment.