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