736a5de3a25da5b8f503eab436e79483b8465f3f
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / datamodel / OCFile.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package eu.alefzero.owncloud.datamodel;
20
21 import java.util.Vector;
22
23 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
24 import android.accounts.Account;
25 import android.content.ContentProvider;
26 import android.content.ContentValues;
27 import android.database.Cursor;
28 import android.net.Uri;
29 import android.util.Log;
30
31 public class OCFile {
32 private static String TAG = "OCFile";
33
34 private long id_;
35 private long length_;
36 private long creation_timestamp_;
37 private long modified_timestamp_;
38 private String path_;
39 private String storage_path_;
40 private String mimetype_;
41
42 private ContentProvider cp_;
43 private Account account_;
44
45 public OCFile(ContentProvider cp, Account account, long id) {
46 cp_ = cp;
47 account_ = account;
48 Cursor c = cp_.query(ProviderTableMeta.CONTENT_URI_FILE,
49 null,
50 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " +
51 ProviderTableMeta._ID + "=?",
52 new String[]{account_.name, String.valueOf(id)},
53 null);
54 if (c.moveToFirst())
55 setFileData(c);
56 }
57
58 public OCFile(ContentProvider cp, Account account, String path) {
59 cp_ = cp;
60 account_ = account;
61 Cursor c = cp_.query(ProviderTableMeta.CONTENT_URI_FILE,
62 null,
63 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " +
64 ProviderTableMeta.FILE_PATH + "=?",
65 new String[]{account_.name, path},
66 null);
67 if (c.moveToFirst()) {
68 setFileData(c);
69 if (path_ != null) path_ = path;
70 }
71 }
72
73 public long getFileId() { return id_; }
74
75 public String getPath() { return path_; }
76
77 public boolean fileExtist() { return id_ != -1; }
78
79 public boolean isDirectory() { return mimetype_ != null && mimetype_.equals("dir"); }
80
81 public boolean isDownloaded() { return storage_path_ != null; }
82
83 public String getStoragePath() { return storage_path_; }
84 public void setStoragePath(String storage_path) { storage_path_ = storage_path; }
85
86 public long getCreationTimestamp() { return creation_timestamp_; }
87 public void setCreationTimestamp(long creation_timestamp) { creation_timestamp_ = creation_timestamp; }
88
89 public long getModificationTimestamp() { return modified_timestamp_; }
90 public void setModificationTimestamp(long modification_timestamp) { modified_timestamp_ = modification_timestamp; }
91
92 public void save() {
93 ContentValues cv = new ContentValues();
94 cv.put(ProviderTableMeta.FILE_MODIFIED, modified_timestamp_);
95 cv.put(ProviderTableMeta.FILE_CREATION, creation_timestamp_);
96 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, length_);
97 cv.put(ProviderTableMeta.CONTENT_TYPE, mimetype_);
98
99 Uri new_entry = cp_.insert(ProviderTableMeta.CONTENT_URI, cv);
100 try {
101 id_ = Integer.parseInt(new_entry.getEncodedPath());
102 } catch (NumberFormatException e) {
103 Log.e(TAG, "Can't retrieve file id from uri: " + new_entry.toString() +
104 ", reason: " + e.getMessage());
105 id_ = -1;
106 }
107 }
108
109 public Vector<OCFile> getDirectoryContent() {
110 if (isDirectory() && id_ != -1) {
111 Vector<OCFile> ret = new Vector<OCFile>();
112
113 Uri req_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, String.valueOf(id_));
114 Cursor c = cp_.query(req_uri, null, null, null, null);
115
116 if (c.moveToFirst())
117 do {
118 OCFile child = new OCFile(cp_, account_);
119 child.setFileData(c);
120 ret.add(child);
121 } while (c.moveToNext());
122
123 return ret;
124 }
125 return null;
126 }
127
128 private OCFile(ContentProvider cp, Account account) {
129 account_ = account;
130 cp_ = cp;
131 }
132
133 private void setFileData(Cursor c) {
134 id_ = -1;
135 path_ = null;
136 storage_path_ = null;
137 mimetype_ = null;
138 length_ = 0;
139 creation_timestamp_ = 0;
140 modified_timestamp_ = 0;
141 if (c != null) {
142 id_ = c.getLong(c.getColumnIndex(ProviderTableMeta._ID));
143 path_ = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH));
144 storage_path_ = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH));
145 mimetype_ = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE));
146 length_ = c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH));
147 creation_timestamp_ = c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_CREATION));
148 modified_timestamp_ = c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_MODIFIED));
149 }
150 }
151 }