1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
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.
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.
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/>.
19 package eu
.alefzero
.owncloud
.datamodel
;
22 import java
.util
.Collections
;
23 import java
.util
.Vector
;
25 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
;
26 import android
.accounts
.Account
;
27 import android
.content
.ContentProviderClient
;
28 import android
.content
.ContentResolver
;
29 import android
.content
.ContentValues
;
30 import android
.database
.Cursor
;
31 import android
.net
.Uri
;
32 import android
.os
.RemoteException
;
33 import android
.util
.Log
;
35 public class FileDataStorageManager
implements DataStorageManager
{
37 private ContentResolver mContentResolver
;
38 private ContentProviderClient mContentProvider
;
39 private Account mAccount
;
41 private static String TAG
= "FileDataStorageManager";
43 public FileDataStorageManager(Account account
, ContentResolver cr
) {
44 mContentProvider
= null
;
45 mContentResolver
= cr
;
49 public FileDataStorageManager(Account account
, ContentProviderClient cp
) {
50 mContentProvider
= cp
;
51 mContentResolver
= null
;
56 public OCFile
getFileByPath(String path
) {
57 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_PATH
, path
);
59 if (c
.moveToFirst()) {
60 file
= createFileInstance(c
);
67 public OCFile
getFileById(long id
) {
68 Cursor c
= getCursorForValue(ProviderTableMeta
._ID
, String
.valueOf(id
));
70 if (c
.moveToFirst()) {
71 file
= createFileInstance(c
);
78 public boolean fileExists(long id
) {
79 return fileExists(ProviderTableMeta
._ID
, String
.valueOf(id
));
83 public boolean fileExists(String path
) {
84 return fileExists(ProviderTableMeta
.FILE_PATH
, path
);
88 public boolean saveFile(OCFile file
) {
89 boolean overriden
= false
;
90 ContentValues cv
= new ContentValues();
91 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
92 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
93 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
94 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
95 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
96 if (file
.getParentId() != 0)
97 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
98 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
99 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
100 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
101 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDate());
103 if (fileExists(file
.getRemotePath())) {
104 OCFile tmpfile
= getFileByPath(file
.getRemotePath());
105 file
.setStoragePath(tmpfile
.getStoragePath());
106 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
107 file
.setFileId(tmpfile
.getFileId());
110 if (getContentResolver() != null
) {
111 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
,
112 ProviderTableMeta
._ID
+ "=?",
113 new String
[] { String
.valueOf(file
.getFileId()) });
116 getContentProvider().update(ProviderTableMeta
.CONTENT_URI
,
117 cv
, ProviderTableMeta
._ID
+ "=?",
118 new String
[] { String
.valueOf(file
.getFileId()) });
119 } catch (RemoteException e
) {
121 "Fail to insert insert file to database "
126 Uri result_uri
= null
;
127 if (getContentResolver() != null
) {
128 result_uri
= getContentResolver().insert(
129 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
132 result_uri
= getContentProvider().insert(
133 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
134 } catch (RemoteException e
) {
136 "Fail to insert insert file to database "
140 if (result_uri
!= null
) {
141 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
143 file
.setFileId(new_id
);
147 if (file
.isDirectory() && file
.needsUpdatingWhileSaving())
148 for (OCFile f
: getDirectoryContent(file
))
154 public void setAccount(Account account
) {
158 public Account
getAccount() {
162 public void setContentResolver(ContentResolver cr
) {
163 mContentResolver
= cr
;
166 public ContentResolver
getContentResolver() {
167 return mContentResolver
;
170 public void setContentProvider(ContentProviderClient cp
) {
171 mContentProvider
= cp
;
174 public ContentProviderClient
getContentProvider() {
175 return mContentProvider
;
178 public Vector
<OCFile
> getDirectoryContent(OCFile f
) {
179 if (f
!= null
&& f
.isDirectory() && f
.getFileId() != -1) {
180 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
182 Uri req_uri
= Uri
.withAppendedPath(
183 ProviderTableMeta
.CONTENT_URI_DIR
,
184 String
.valueOf(f
.getFileId()));
187 if (getContentProvider() != null
) {
189 c
= getContentProvider().query(req_uri
, null
,
190 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
191 new String
[] { mAccount
.name
}, null
);
192 } catch (RemoteException e
) {
193 Log
.e(TAG
, e
.getMessage());
197 c
= getContentResolver().query(req_uri
, null
,
198 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
199 new String
[] { mAccount
.name
}, null
);
202 if (c
.moveToFirst()) {
204 OCFile child
= createFileInstance(c
);
206 } while (c
.moveToNext());
211 Collections
.sort(ret
);
218 private boolean fileExists(String cmp_key
, String value
) {
220 if (getContentResolver() != null
) {
221 c
= getContentResolver()
222 .query(ProviderTableMeta
.CONTENT_URI
,
225 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
227 new String
[] { value
, mAccount
.name
}, null
);
230 c
= getContentProvider().query(
231 ProviderTableMeta
.CONTENT_URI
,
234 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
235 new String
[] { value
, mAccount
.name
}, null
);
236 } catch (RemoteException e
) {
238 "Couldn't determine file existance, assuming non existance: "
243 boolean retval
= c
.moveToFirst();
248 private Cursor
getCursorForValue(String key
, String value
) {
250 if (getContentResolver() != null
) {
251 c
= getContentResolver()
252 .query(ProviderTableMeta
.CONTENT_URI
,
255 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
257 new String
[] { value
, mAccount
.name
}, null
);
260 c
= getContentProvider().query(
261 ProviderTableMeta
.CONTENT_URI
,
263 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
264 + "=?", new String
[] { value
, mAccount
.name
},
266 } catch (RemoteException e
) {
267 Log
.e(TAG
, "Could not get file details: " + e
.getMessage());
274 private OCFile
createFileInstance(Cursor c
) {
277 file
= new OCFile(c
.getString(c
278 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
279 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
280 file
.setParentId(c
.getLong(c
281 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
282 file
.setStoragePath(c
.getString(c
283 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
284 file
.setMimetype(c
.getString(c
285 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
286 file
.setFileLength(c
.getLong(c
287 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
288 file
.setCreationTimestamp(c
.getLong(c
289 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
290 file
.setModificationTimestamp(c
.getLong(c
291 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
292 file
.setLastSyncDate(c
.getLong(c
293 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
298 public void removeFile(OCFile file
) {
299 Uri file_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_FILE
, ""+file
.getFileId());
300 if (getContentProvider() != null
) {
302 getContentProvider().delete(file_uri
,
303 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
304 new String
[]{mAccount
.name
});
305 } catch (RemoteException e
) {
309 getContentResolver().delete(file_uri
,
310 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
311 new String
[]{mAccount
.name
});
313 if (file
.getStoragePath() != null
) {
314 new File(file
.getStoragePath()).delete();