0ad0d1bbc13543522cb2bf7ef92e097533b2c4cd
[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
105 if (fileExists(file.getRemotePath())) {
106 OCFile tmpfile = getFileByPath(file.getRemotePath());
107 file.setStoragePath(tmpfile.getStoragePath());
108 if (!file.isDirectory());
109 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
110 file.setFileId(tmpfile.getFileId());
111
112 overriden = true;
113 if (getContentResolver() != null) {
114 getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv,
115 ProviderTableMeta._ID + "=?",
116 new String[] { String.valueOf(file.getFileId()) });
117 } else {
118 try {
119 getContentProvider().update(ProviderTableMeta.CONTENT_URI,
120 cv, ProviderTableMeta._ID + "=?",
121 new String[] { String.valueOf(file.getFileId()) });
122 } catch (RemoteException e) {
123 Log.e(TAG,
124 "Fail to insert insert file to database "
125 + e.getMessage());
126 }
127 }
128 } else {
129 Uri result_uri = null;
130 if (getContentResolver() != null) {
131 result_uri = getContentResolver().insert(
132 ProviderTableMeta.CONTENT_URI_FILE, cv);
133 } else {
134 try {
135 result_uri = getContentProvider().insert(
136 ProviderTableMeta.CONTENT_URI_FILE, cv);
137 } catch (RemoteException e) {
138 Log.e(TAG,
139 "Fail to insert insert file to database "
140 + e.getMessage());
141 }
142 }
143 if (result_uri != null) {
144 long new_id = Long.parseLong(result_uri.getPathSegments()
145 .get(1));
146 file.setFileId(new_id);
147 }
148 }
149
150 if (file.isDirectory() && file.needsUpdatingWhileSaving())
151 for (OCFile f : getDirectoryContent(file))
152 saveFile(f);
153
154 return overriden;
155 }
156
157 public void setAccount(Account account) {
158 mAccount = account;
159 }
160
161 public Account getAccount() {
162 return mAccount;
163 }
164
165 public void setContentResolver(ContentResolver cr) {
166 mContentResolver = cr;
167 }
168
169 public ContentResolver getContentResolver() {
170 return mContentResolver;
171 }
172
173 public void setContentProvider(ContentProviderClient cp) {
174 mContentProvider = cp;
175 }
176
177 public ContentProviderClient getContentProvider() {
178 return mContentProvider;
179 }
180
181 public Vector<OCFile> getDirectoryContent(OCFile f) {
182 if (f != null && f.isDirectory() && f.getFileId() != -1) {
183 Vector<OCFile> ret = new Vector<OCFile>();
184
185 Uri req_uri = Uri.withAppendedPath(
186 ProviderTableMeta.CONTENT_URI_DIR,
187 String.valueOf(f.getFileId()));
188 Cursor c = null;
189
190 if (getContentProvider() != null) {
191 try {
192 c = getContentProvider().query(req_uri, null,
193 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
194 new String[] { mAccount.name }, null);
195 } catch (RemoteException e) {
196 Log.e(TAG, e.getMessage());
197 return ret;
198 }
199 } else {
200 c = getContentResolver().query(req_uri, null,
201 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
202 new String[] { mAccount.name }, null);
203 }
204
205 if (c.moveToFirst()) {
206 do {
207 OCFile child = createFileInstance(c);
208 ret.add(child);
209 } while (c.moveToNext());
210 }
211
212 c.close();
213
214 Collections.sort(ret);
215
216 return ret;
217 }
218 return null;
219 }
220
221 private boolean fileExists(String cmp_key, String value) {
222 Cursor c;
223 if (getContentResolver() != null) {
224 c = getContentResolver()
225 .query(ProviderTableMeta.CONTENT_URI,
226 null,
227 cmp_key + "=? AND "
228 + ProviderTableMeta.FILE_ACCOUNT_OWNER
229 + "=?",
230 new String[] { value, mAccount.name }, null);
231 } else {
232 try {
233 c = getContentProvider().query(
234 ProviderTableMeta.CONTENT_URI,
235 null,
236 cmp_key + "=? AND "
237 + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
238 new String[] { value, mAccount.name }, null);
239 } catch (RemoteException e) {
240 Log.e(TAG,
241 "Couldn't determine file existance, assuming non existance: "
242 + e.getMessage());
243 return false;
244 }
245 }
246 boolean retval = c.moveToFirst();
247 c.close();
248 return retval;
249 }
250
251 private Cursor getCursorForValue(String key, String value) {
252 Cursor c = null;
253 if (getContentResolver() != null) {
254 c = getContentResolver()
255 .query(ProviderTableMeta.CONTENT_URI,
256 null,
257 key + "=? AND "
258 + ProviderTableMeta.FILE_ACCOUNT_OWNER
259 + "=?",
260 new String[] { value, mAccount.name }, null);
261 } else {
262 try {
263 c = getContentProvider().query(
264 ProviderTableMeta.CONTENT_URI,
265 null,
266 key + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER
267 + "=?", new String[] { value, mAccount.name },
268 null);
269 } catch (RemoteException e) {
270 Log.e(TAG, "Could not get file details: " + e.getMessage());
271 c = null;
272 }
273 }
274 return c;
275 }
276
277 private OCFile createFileInstance(Cursor c) {
278 OCFile file = null;
279 if (c != null) {
280 file = new OCFile(c.getString(c
281 .getColumnIndex(ProviderTableMeta.FILE_PATH)));
282 file.setFileId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
283 file.setParentId(c.getLong(c
284 .getColumnIndex(ProviderTableMeta.FILE_PARENT)));
285 file.setMimetype(c.getString(c
286 .getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)));
287 if (!file.isDirectory()) {
288 file.setStoragePath(c.getString(c
289 .getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)));
290 if (file.getStoragePath() == null) {
291 // try to find existing file and bind it with current account
292 File sdCard = Environment.getExternalStorageDirectory();
293 File f = new File(sdCard.getAbsolutePath() + "/owncloud/" + mAccount.name + file.getURLDecodedRemotePath());
294 if (f.exists())
295 file.setStoragePath(f.getAbsolutePath());
296 }
297 }
298 file.setFileLength(c.getLong(c
299 .getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH)));
300 file.setCreationTimestamp(c.getLong(c
301 .getColumnIndex(ProviderTableMeta.FILE_CREATION)));
302 file.setModificationTimestamp(c.getLong(c
303 .getColumnIndex(ProviderTableMeta.FILE_MODIFIED)));
304 file.setLastSyncDate(c.getLong(c
305 .getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE)));
306 }
307 return file;
308 }
309
310 public void removeFile(OCFile file) {
311 Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, ""+file.getFileId());
312 if (getContentProvider() != null) {
313 try {
314 getContentProvider().delete(file_uri,
315 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
316 new String[]{mAccount.name});
317 } catch (RemoteException e) {
318 e.printStackTrace();
319 }
320 } else {
321 getContentResolver().delete(file_uri,
322 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
323 new String[]{mAccount.name});
324 }
325 if (file.getStoragePath() != null) {
326 new File(file.getStoragePath()).delete();
327 }
328 }
329
330 }