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