bfa9963162a34f633d9174a95af06765b13c4352
[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.io.File;
22 import java.util.Vector;
23
24 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
25 import android.accounts.Account;
26 import android.content.ContentProviderClient;
27 import android.content.ContentResolver;
28 import android.content.ContentValues;
29 import android.database.Cursor;
30 import android.net.Uri;
31 import android.os.RemoteException;
32 import android.util.Log;
33
34 public class OCFile {
35 private static String TAG = "OCFile";
36
37 private long id_;
38 private long parent_id_;
39 private long length_;
40 private long creation_timestamp_;
41 private long modified_timestamp_;
42 private String path_;
43 private String storage_path_;
44 private String mimetype_;
45
46 private ContentResolver cr_;
47 private ContentProviderClient cp_;
48 private Account account_;
49
50 public static OCFile createNewFile(ContentProviderClient cr, Account account,
51 String path, long length, long creation_timestamp,
52 long modified_timestamp, String mimetype, long parent_id) {
53 OCFile new_file = new OCFile(cr, account);
54
55 try {
56 Cursor c = new_file.cp_.query(ProviderTableMeta.CONTENT_URI_FILE, null,
57 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND "
58 + ProviderTableMeta.FILE_PATH + "=?", new String[]{new_file.account_.name,
59 path}, null);
60 if (c.moveToFirst())
61 new_file.setFileData(c);
62 } catch (RemoteException e) {
63 Log.e(TAG, e.getMessage());
64 }
65
66 new_file.path_ = path;
67 new_file.length_ = length;
68 new_file.creation_timestamp_ = creation_timestamp;
69 new_file.modified_timestamp_ = modified_timestamp;
70 new_file.mimetype_ = mimetype;
71 new_file.parent_id_ = parent_id;
72 Log.e(TAG, parent_id+"");
73
74 return new_file;
75 }
76
77 public OCFile(ContentResolver cr, Account account, long id) {
78 cr_ = cr;
79 account_ = account;
80 Cursor c = cr_.query(ProviderTableMeta.CONTENT_URI_FILE, null,
81 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND "
82 + ProviderTableMeta._ID + "=?",
83 new String[]{account_.name, String.valueOf(id)}, null);
84 if (c.moveToFirst())
85 setFileData(c);
86 }
87
88 public OCFile(ContentResolver cr, Account account, String path) {
89 cr_ = cr;
90 account_ = account;
91
92 Cursor c = cr_.query(ProviderTableMeta.CONTENT_URI_FILE, null,
93 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND "
94 + ProviderTableMeta.FILE_PATH + "=?", new String[]{account_.name,
95 path}, null);
96 if (c.moveToFirst()) {
97 setFileData(c);
98 if (path_ != null)
99 path_ = path;
100 }
101 }
102
103 public long getFileId() {
104 return id_;
105 }
106
107 public String getPath() {
108 return path_;
109 }
110
111 public boolean fileExtist() {
112 return id_ != -1;
113 }
114
115 public boolean isDirectory() {
116 return mimetype_ != null && mimetype_.equals("DIR");
117 }
118
119 public boolean isDownloaded() {
120 return storage_path_ != null;
121 }
122
123 public String getStoragePath() {
124 return storage_path_;
125 }
126 public void setStoragePath(String storage_path) {
127 storage_path_ = storage_path;
128 }
129
130 public long getCreationTimestamp() {
131 return creation_timestamp_;
132 }
133 public void setCreationTimestamp(long creation_timestamp) {
134 creation_timestamp_ = creation_timestamp;
135 }
136
137 public long getModificationTimestamp() {
138 return modified_timestamp_;
139 }
140 public void setModificationTimestamp(long modification_timestamp) {
141 modified_timestamp_ = modification_timestamp;
142 }
143
144 public String getFileName() {
145 if (path_ != null) {
146 File f = new File(path_);
147 return f.getName().equals("") ? "/" : f.getName();
148 }
149 return null;
150 }
151
152 public String getMimetype() {
153 return mimetype_;
154 }
155
156 public void save() {
157 ContentValues cv = new ContentValues();
158 cv.put(ProviderTableMeta.FILE_MODIFIED, modified_timestamp_);
159 cv.put(ProviderTableMeta.FILE_CREATION, creation_timestamp_);
160 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, length_);
161 cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, mimetype_);
162 cv.put(ProviderTableMeta.FILE_NAME, getFileName());
163 if (parent_id_ != 0)
164 cv.put(ProviderTableMeta.FILE_PARENT, parent_id_);
165 cv.put(ProviderTableMeta.FILE_PATH, path_);
166 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, storage_path_);
167 cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account_.name);
168
169 if (fileExtist()) {
170 if (cp_ != null) {
171 try {
172 cp_.update(ProviderTableMeta.CONTENT_URI, cv, ProviderTableMeta._ID
173 + "=?", new String[]{String.valueOf(id_)});
174 } catch (RemoteException e) {
175 Log.e(TAG, e.getMessage());
176 return;
177 }
178 } else {
179 cr_.update(ProviderTableMeta.CONTENT_URI, cv, ProviderTableMeta._ID
180 + "=?", new String[]{String.valueOf(id_)});
181 }
182 } else {
183 Uri new_entry = null;
184 if (cp_ != null) {
185 try {
186 new_entry = cp_.insert(ProviderTableMeta.CONTENT_URI_FILE, cv);
187 } catch (RemoteException e) {
188 Log.e(TAG, e.getMessage());
189 id_ = -1;
190 return;
191 }
192 } else {
193 new_entry = cr_.insert(ProviderTableMeta.CONTENT_URI_FILE, cv);
194 }
195 try {
196 String p = new_entry.getEncodedPath();
197 id_ = Integer.parseInt(p.substring(p.lastIndexOf('/')+1));
198 } catch (NumberFormatException e) {
199 Log.e(TAG, "Can't retrieve file id from uri: " + new_entry.toString()
200 + ", reason: " + e.getMessage());
201 id_ = -1;
202 }
203 }
204 }
205
206 public Vector<OCFile> getDirectoryContent() {
207 if (isDirectory() && id_ != -1) {
208 Vector<OCFile> ret = new Vector<OCFile>();
209
210 Uri req_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR,
211 String.valueOf(id_));
212 Cursor c = null;
213 if (cp_ != null) {
214 try {
215 c = cp_.query(req_uri, null, null, null, null);
216 } catch (RemoteException e) {
217 Log.e(TAG, e.getMessage());
218 return ret;
219 }
220 } else {
221 c = cr_.query(req_uri, null, null, null, null);
222 }
223
224 if (c.moveToFirst())
225 do {
226 OCFile child = new OCFile(cp_, account_);
227 child.setFileData(c);
228 ret.add(child);
229 } while (c.moveToNext());
230
231 return ret;
232 }
233 return null;
234 }
235
236 public void addFile(OCFile file) {
237 file.parent_id_ = id_;
238 file.save();
239 }
240
241 private OCFile(ContentProviderClient cp, Account account) {
242 account_ = account;
243 cp_ = cp;
244 resetData();
245 }
246
247 private OCFile(ContentResolver cr, Account account) {
248 account_ = account;
249 cr_ = cr;
250 resetData();
251 }
252
253 private void resetData() {
254 id_ = -1;
255 path_ = null;
256 parent_id_ = 0;
257 storage_path_ = null;
258 mimetype_ = null;
259 length_ = 0;
260 creation_timestamp_ = 0;
261 modified_timestamp_ = 0;
262 }
263
264 private void setFileData(Cursor c) {
265 resetData();
266 if (c != null) {
267 id_ = c.getLong(c.getColumnIndex(ProviderTableMeta._ID));
268 path_ = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH));
269 parent_id_ = c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_PARENT));
270 storage_path_ = c.getString(c
271 .getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH));
272 mimetype_ = c.getString(c
273 .getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE));
274 length_ = c.getLong(c
275 .getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH));
276 creation_timestamp_ = c.getLong(c
277 .getColumnIndex(ProviderTableMeta.FILE_CREATION));
278 modified_timestamp_ = c.getLong(c
279 .getColumnIndex(ProviderTableMeta.FILE_MODIFIED));
280 }
281 }
282 }