1feb424bdaa2bfc2b2de3879db059753127d85bb
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / datamodel / FileDataStorageManager.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.Collections;
23 import java.util.Vector;
24
25 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
26 import android.accounts.Account;
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.Environment;
33 import android.os.RemoteException;
34 import android.util.Log;
35
36 public class FileDataStorageManager implements DataStorageManager {
37
38 private ContentResolver mContentResolver;
39 private ContentProviderClient mContentProvider;
40 private Account mAccount;
41
42 private static String TAG = "FileDataStorageManager";
43
44 public FileDataStorageManager(Account account, ContentResolver cr) {
45 mContentProvider = null;
46 mContentResolver = cr;
47 mAccount = account;
48 }
49
50 public FileDataStorageManager(Account account, ContentProviderClient cp) {
51 mContentProvider = cp;
52 mContentResolver = null;
53 mAccount = account;
54 }
55
56 @Override
57 public OCFile getFileByPath(String path) {
58 Cursor c = getCursorForValue(ProviderTableMeta.FILE_PATH, path);
59 OCFile file = null;
60 if (c.moveToFirst()) {
61 file = createFileInstance(c);
62 }
63 c.close();
64 return file;
65 }
66
67 @Override
68 public OCFile getFileById(long id) {
69 Cursor c = getCursorForValue(ProviderTableMeta._ID, String.valueOf(id));
70 OCFile file = null;
71 if (c.moveToFirst()) {
72 file = createFileInstance(c);
73 }
74 c.close();
75 return file;
76 }
77
78 @Override
79 public boolean fileExists(long id) {
80 return fileExists(ProviderTableMeta._ID, String.valueOf(id));
81 }
82
83 @Override
84 public boolean fileExists(String path) {
85 return fileExists(ProviderTableMeta.FILE_PATH, path);
86 }
87
88 @Override
89 public boolean saveFile(OCFile file) {
90 boolean overriden = false;
91 ContentValues cv = new ContentValues();
92 cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
93 cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
94 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
95 cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype());
96 cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
97 if (file.getParentId() != 0)
98 cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
99 cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
100 if (!file.isDirectory())
101 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
102 cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
103 cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDate());
104 cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.keepInSync() ? 1 : 0);
105
106 if (fileExists(file.getRemotePath())) {
107 OCFile tmpfile = getFileByPath(file.getRemotePath());
108 file.setStoragePath(tmpfile.getStoragePath());
109 if (!file.isDirectory());
110 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
111 file.setFileId(tmpfile.getFileId());
112
113 overriden = true;
114 if (getContentResolver() != null) {
115 getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv,
116 ProviderTableMeta._ID + "=?",
117 new String[] { String.valueOf(file.getFileId()) });
118 } else {
119 try {
120 getContentProvider().update(ProviderTableMeta.CONTENT_URI,
121 cv, ProviderTableMeta._ID + "=?",
122 new String[] { String.valueOf(file.getFileId()) });
123 } catch (RemoteException e) {
124 Log.e(TAG,
125 "Fail to insert insert file to database "
126 + e.getMessage());
127 }
128 }
129 } else {
130 Uri result_uri = null;
131 if (getContentResolver() != null) {
132 result_uri = getContentResolver().insert(
133 ProviderTableMeta.CONTENT_URI_FILE, cv);
134 } else {
135 try {
136 result_uri = getContentProvider().insert(
137 ProviderTableMeta.CONTENT_URI_FILE, cv);
138 } catch (RemoteException e) {
139 Log.e(TAG,
140 "Fail to insert insert file to database "
141 + e.getMessage());
142 }
143 }
144 if (result_uri != null) {
145 long new_id = Long.parseLong(result_uri.getPathSegments()
146 .get(1));
147 file.setFileId(new_id);
148 }
149 }
150
151 if (file.isDirectory() && file.needsUpdatingWhileSaving())
152 for (OCFile f : getDirectoryContent(file))
153 saveFile(f);
154
155 return overriden;
156 }
157
158 public void setAccount(Account account) {
159 mAccount = account;
160 }
161
162 public Account getAccount() {
163 return mAccount;
164 }
165
166 public void setContentResolver(ContentResolver cr) {
167 mContentResolver = cr;
168 }
169
170 public ContentResolver getContentResolver() {
171 return mContentResolver;
172 }
173
174 public void setContentProvider(ContentProviderClient cp) {
175 mContentProvider = cp;
176 }
177
178 public ContentProviderClient getContentProvider() {
179 return mContentProvider;
180 }
181
182 public Vector<OCFile> getDirectoryContent(OCFile f) {
183 if (f != null && f.isDirectory() && f.getFileId() != -1) {
184 Vector<OCFile> ret = new Vector<OCFile>();
185
186 Uri req_uri = Uri.withAppendedPath(
187 ProviderTableMeta.CONTENT_URI_DIR,
188 String.valueOf(f.getFileId()));
189 Cursor c = null;
190
191 if (getContentProvider() != null) {
192 try {
193 c = getContentProvider().query(req_uri, null,
194 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
195 new String[] { mAccount.name }, null);
196 } catch (RemoteException e) {
197 Log.e(TAG, e.getMessage());
198 return ret;
199 }
200 } else {
201 c = getContentResolver().query(req_uri, null,
202 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
203 new String[] { mAccount.name }, null);
204 }
205
206 if (c.moveToFirst()) {
207 do {
208 OCFile child = createFileInstance(c);
209 ret.add(child);
210 } while (c.moveToNext());
211 }
212
213 c.close();
214
215 Collections.sort(ret);
216
217 return ret;
218 }
219 return null;
220 }
221
222 private boolean fileExists(String cmp_key, String value) {
223 Cursor c;
224 if (getContentResolver() != null) {
225 c = getContentResolver()
226 .query(ProviderTableMeta.CONTENT_URI,
227 null,
228 cmp_key + "=? AND "
229 + ProviderTableMeta.FILE_ACCOUNT_OWNER
230 + "=?",
231 new String[] { value, mAccount.name }, null);
232 } else {
233 try {
234 c = getContentProvider().query(
235 ProviderTableMeta.CONTENT_URI,
236 null,
237 cmp_key + "=? AND "
238 + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
239 new String[] { value, mAccount.name }, null);
240 } catch (RemoteException e) {
241 Log.e(TAG,
242 "Couldn't determine file existance, assuming non existance: "
243 + e.getMessage());
244 return false;
245 }
246 }
247 boolean retval = c.moveToFirst();
248 c.close();
249 return retval;
250 }
251
252 private Cursor getCursorForValue(String key, String value) {
253 Cursor c = null;
254 if (getContentResolver() != null) {
255 c = getContentResolver()
256 .query(ProviderTableMeta.CONTENT_URI,
257 null,
258 key + "=? AND "
259 + ProviderTableMeta.FILE_ACCOUNT_OWNER
260 + "=?",
261 new String[] { value, mAccount.name }, null);
262 } else {
263 try {
264 c = getContentProvider().query(
265 ProviderTableMeta.CONTENT_URI,
266 null,
267 key + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER
268 + "=?", new String[] { value, mAccount.name },
269 null);
270 } catch (RemoteException e) {
271 Log.e(TAG, "Could not get file details: " + e.getMessage());
272 c = null;
273 }
274 }
275 return c;
276 }
277
278 private OCFile createFileInstance(Cursor c) {
279 OCFile file = null;
280 if (c != null) {
281 file = new OCFile(c.getString(c
282 .getColumnIndex(ProviderTableMeta.FILE_PATH)));
283 file.setFileId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
284 file.setParentId(c.getLong(c
285 .getColumnIndex(ProviderTableMeta.FILE_PARENT)));
286 file.setMimetype(c.getString(c
287 .getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)));
288 if (!file.isDirectory()) {
289 file.setStoragePath(c.getString(c
290 .getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)));
291 if (file.getStoragePath() == null) {
292 // try to find existing file and bind it with current account
293 File sdCard = Environment.getExternalStorageDirectory();
294 File f = new File(sdCard.getAbsolutePath() + "/owncloud/" + mAccount.name + file.getURLDecodedRemotePath());
295 if (f.exists())
296 file.setStoragePath(f.getAbsolutePath());
297 }
298 }
299 file.setFileLength(c.getLong(c
300 .getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH)));
301 file.setCreationTimestamp(c.getLong(c
302 .getColumnIndex(ProviderTableMeta.FILE_CREATION)));
303 file.setModificationTimestamp(c.getLong(c
304 .getColumnIndex(ProviderTableMeta.FILE_MODIFIED)));
305 file.setLastSyncDate(c.getLong(c
306 .getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE)));
307 file.setKeepInSync(c.getInt(
308 c.getColumnIndex(ProviderTableMeta.FILE_KEEP_IN_SYNC)) == 1 ? true : false);
309 }
310 return file;
311 }
312
313 public void removeFile(OCFile file) {
314 Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, ""+file.getFileId());
315 if (getContentProvider() != null) {
316 try {
317 getContentProvider().delete(file_uri,
318 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
319 new String[]{mAccount.name});
320 } catch (RemoteException e) {
321 e.printStackTrace();
322 }
323 } else {
324 getContentResolver().delete(file_uri,
325 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
326 new String[]{mAccount.name});
327 }
328 if (file.getStoragePath() != null) {
329 new File(file.getStoragePath()).delete();
330 }
331 }
332
333 }