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