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;
private static String TAG = "OCFile";
private long id_;
+ private long parent_id_;
private long length_;
private long creation_timestamp_;
private long modified_timestamp_;
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,
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,
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_; }
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;
+ }
}
}
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());
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;
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));