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