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