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