e862a5a371c6ff984cc312601b15c7fc01404b23
[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 if (c.moveToFirst())
57 return createFileInstance(c);
58 return null;
59 }
60
61 @Override
62 public OCFile getFileById(long id) {
63 Cursor c = getCursorForValue(ProviderTableMeta._ID, String.valueOf(id));
64 if (c.moveToFirst())
65 return createFileInstance(c);
66 return null;
67 }
68
69 @Override
70 public boolean fileExists(long id) {
71 return fileExists(ProviderTableMeta._ID, String.valueOf(id));
72 }
73
74 @Override
75 public boolean fileExists(String path) {
76 return fileExists(ProviderTableMeta.FILE_PATH, path);
77 }
78
79 @Override
80 public boolean saveFile(OCFile file) {
81 boolean overriden = false;
82 ContentValues cv = new ContentValues();
83 cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
84 cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
85 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
86 cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype());
87 cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
88 if (file.getParentId() != 0)
89 cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
90 cv.put(ProviderTableMeta.FILE_PATH, file.getPath());
91 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
92 cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
93
94 if (fileExists(file.getPath())) {
95 overriden = true;
96 if (getContentResolver() != null) {
97 getContentResolver().update(ProviderTableMeta.CONTENT_URI,
98 cv,
99 ProviderTableMeta._ID + "=?",
100 new String[] {String.valueOf(file.getFileId())});
101 } else {
102 try {
103 getContentProvider().update(ProviderTableMeta.CONTENT_URI,
104 cv,
105 ProviderTableMeta._ID + "=?",
106 new String[] {String.valueOf(file.getFileId())});
107 } catch (RemoteException e) {
108 Log.e(TAG, "Fail to insert insert file to database " + e.getMessage());
109 }
110 }
111 } else {
112 Uri result_uri = null;
113 if (getContentResolver() != null) {
114 result_uri = getContentResolver().insert(ProviderTableMeta.CONTENT_URI_FILE, cv);
115 } else {
116 try {
117 result_uri = getContentProvider().insert(ProviderTableMeta.CONTENT_URI_FILE, cv);
118 } catch (RemoteException e) {
119 Log.e(TAG, "Fail to insert insert file to database " + e.getMessage());
120 }
121 }
122 if (result_uri != null) {
123 long new_id = Long.parseLong(result_uri.getPathSegments().get(1));
124 file.setFileId(new_id);
125 }
126 }
127
128 if (file.isDirectory() && file.needsUpdatingWhileSaving())
129 for (OCFile f : getDirectoryContent(file))
130 saveFile(f);
131
132 return overriden;
133 }
134
135 public void setAccount(Account account) {
136 mAccount = account;
137 }
138
139 public Account getAccount() {
140 return mAccount;
141 }
142
143 public void setContentResolver(ContentResolver cr) {
144 mContentResolver = cr;
145 }
146
147 public ContentResolver getContentResolver() {
148 return mContentResolver;
149 }
150
151 public void setContentProvider(ContentProviderClient cp) {
152 mContentProvider = cp;
153 }
154
155 public ContentProviderClient getContentProvider() {
156 return mContentProvider;
157 }
158
159 public Vector<OCFile> getDirectoryContent(OCFile f) {
160 if (f.isDirectory() && f.getFileId() != -1) {
161 Vector<OCFile> ret = new Vector<OCFile>();
162
163 Uri req_uri = Uri.withAppendedPath(
164 ProviderTableMeta.CONTENT_URI_DIR, String.valueOf(f.getFileId()));
165 Cursor c = null;
166
167 if (getContentProvider() != null) {
168 try {
169 c = getContentProvider().query(req_uri, null, null, null, null);
170 } catch (RemoteException e) {
171 Log.e(TAG, e.getMessage());
172 return ret;
173 }
174 } else {
175 c = getContentResolver().query(req_uri, null, null, null, null);
176 }
177
178 if (c.moveToFirst()) {
179 do {
180 OCFile child = createFileInstance(c);
181 ret.add(child);
182 } while (c.moveToNext());
183 }
184
185 c.close();
186 return ret;
187 }
188 return null;
189 }
190
191
192 private boolean fileExists(String cmp_key, String value) {
193 Cursor c;
194 if (getContentResolver() != null) {
195 c = getContentResolver().query(ProviderTableMeta.CONTENT_URI,
196 null,
197 cmp_key + "=?",
198 new String[] {value},
199 null);
200 } else {
201 try {
202 c = getContentProvider().query(ProviderTableMeta.CONTENT_URI,
203 null,
204 cmp_key + "=?",
205 new String[] {value},
206 null);
207 } catch (RemoteException e) {
208 Log.e(TAG, "Couldn't determine file existance, assuming non existance: " + e.getMessage());
209 return false;
210 }
211 }
212 return c.moveToFirst();
213 }
214
215 private Cursor getCursorForValue(String key, String value) {
216 Cursor c = null;
217 if (getContentResolver() != null) {
218 c = getContentResolver().query(ProviderTableMeta.CONTENT_URI,
219 null,
220 key + "=?",
221 new String[] {value},
222 null);
223 } else {
224 try {
225 c = getContentProvider().query(ProviderTableMeta.CONTENT_URI,
226 null,
227 key + "=?",
228 new String[]{value},
229 null);
230 } catch (RemoteException e) {
231 Log.e(TAG, "Could not get file details: " + e.getMessage());
232 c = null;
233 }
234 }
235 return c;
236 }
237
238 private OCFile createFileInstance(Cursor c) {
239 OCFile file = null;
240 if (c != null) {
241 file = new OCFile(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH)));
242 file.setFileId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
243 file.setParentId(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_PARENT)));
244 file.setStoragePath(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)));
245 file.setMimetype(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)));
246 file.setFileLength(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH)));
247 file.setCreationTimestamp(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_CREATION)));
248 file.setModificationTimestamp(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_MODIFIED)));
249 }
250 return file;
251 }
252
253 }