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