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