fix invalid list get in filelist
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / datamodel / OCFile.java
index f377a04..20e30fe 100644 (file)
 
 package eu.alefzero.owncloud.datamodel;
 
+import java.io.File;
 import java.util.Vector;
 
 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
 import android.accounts.Account;
-import android.content.ContentProvider;
+import android.content.ContentResolver;
 import android.content.ContentValues;
 import android.database.Cursor;
 import android.net.Uri;
@@ -32,6 +33,7 @@ public class OCFile {
   private static String TAG = "OCFile";
   
   private long id_;
+  private long parent_id_;
   private long length_;
   private long creation_timestamp_;
   private long modified_timestamp_;
@@ -39,10 +41,10 @@ public class OCFile {
   private String storage_path_;
   private String mimetype_;
   
-  private ContentProvider cp_;
+  private ContentResolver cp_;
   private Account account_;
   
-  public OCFile(ContentProvider cp, Account account, long id) {
+  public OCFile(ContentResolver cp, Account account, long id) {
     cp_ = cp;
     account_ = account;
     Cursor c = cp_.query(ProviderTableMeta.CONTENT_URI_FILE,
@@ -51,10 +53,11 @@ public class OCFile {
         ProviderTableMeta._ID + "=?",
         new String[]{account_.name, String.valueOf(id)},
         null);
-    setFileData(c);
+    if (c.moveToFirst())
+      setFileData(c);
   }
   
-  public OCFile(ContentProvider cp, Account account, String path) {
+  public OCFile(ContentResolver cp, Account account, String path) {
     cp_ = cp;
     account_ = account;
     Cursor c = cp_.query(ProviderTableMeta.CONTENT_URI_FILE,
@@ -63,8 +66,10 @@ public class OCFile {
         ProviderTableMeta.FILE_PATH + "=?",
         new String[]{account_.name, path},
         null);
-    setFileData(c);
-    if (path_ != null) path_ = path;
+    if (c.moveToFirst()) {
+      setFileData(c);
+      if (path_ != null) path_ = path;
+    }
   }
   
   public long getFileId() { return id_; }
@@ -86,20 +91,39 @@ public class OCFile {
   public long getModificationTimestamp() { return modified_timestamp_; }
   public void setModificationTimestamp(long modification_timestamp) { modified_timestamp_ = modification_timestamp; }
 
+  public String getFileName() {
+    if (path_ != null) {
+      File f = new File(path_);
+      return f.getName();
+    }
+    return null;
+  }
+
   public void save() {
     ContentValues cv = new ContentValues();
     cv.put(ProviderTableMeta.FILE_MODIFIED, modified_timestamp_);
     cv.put(ProviderTableMeta.FILE_CREATION, creation_timestamp_);
     cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, length_);
     cv.put(ProviderTableMeta.CONTENT_TYPE, mimetype_);
+    cv.put(ProviderTableMeta.FILE_NAME, getFileName());
+    cv.put(ProviderTableMeta.FILE_PARENT, parent_id_);
+    cv.put(ProviderTableMeta.FILE_PATH, path_);
+    cv.put(ProviderTableMeta.FILE_STORAGE_PATH, storage_path_);
+    cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account_.name);
     
-    Uri new_entry = cp_.insert(ProviderTableMeta.CONTENT_URI, cv);
-    try {
-      id_ = Integer.parseInt(new_entry.getEncodedPath());
-    } catch (NumberFormatException e) {
-      Log.e(TAG, "Can't retrieve file id from uri: " + new_entry.toString() +
-                ", reason: " + e.getMessage());
-      id_ = -1; 
+    if (fileExtist()) {
+      cp_.update(ProviderTableMeta.CONTENT_URI,
+                 cv,
+                 ProviderTableMeta._ID + "=?", new String[]{String.valueOf(id_)});
+    } else {
+      Uri new_entry = cp_.insert(ProviderTableMeta.CONTENT_URI, cv);
+      try {
+        id_ = Integer.parseInt(new_entry.getEncodedPath());
+      } catch (NumberFormatException e) {
+        Log.e(TAG, "Can't retrieve file id from uri: " + new_entry.toString() +
+                  ", reason: " + e.getMessage());
+        id_ = -1; 
+      }
     }
   }
   
@@ -112,8 +136,8 @@ public class OCFile {
 
       if (c.moveToFirst())
         do {
-          long id = c.getLong(c.getColumnIndex(ProviderTableMeta._ID));
-          OCFile child = new OCFile(cp_, account_, id);
+          OCFile child = new OCFile(cp_, account_);
+          child.setFileData(c);
           ret.add(child);
         } while (c.moveToNext());
 
@@ -122,6 +146,16 @@ public class OCFile {
     return null;
   }
   
+  public void addFile(OCFile file) {
+    file.parent_id_ = id_;
+    file.save();
+  }
+
+  private OCFile(ContentResolver cp, Account account) {
+    account_ = account;
+    cp_ = cp;
+  }
+  
   private void setFileData(Cursor c) {
     id_ = -1;
     path_ = null;
@@ -130,7 +164,7 @@ public class OCFile {
     length_ = 0;
     creation_timestamp_ = 0;
     modified_timestamp_ = 0;  
-    if (c != null && c.moveToFirst()) {
+    if (c != null) {
       id_ = c.getLong(c.getColumnIndex(ProviderTableMeta._ID));
       path_ = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH));
       storage_path_ = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH));