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
; 
  21 import java
.util
.Vector
; 
  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
; 
  33 public class FileDataStorageManager 
implements DataStorageManager 
{ 
  35     private ContentResolver mContentResolver
; 
  36     private ContentProviderClient mContentProvider
; 
  37     private Account mAccount
; 
  39     private static String TAG 
= "FileDataStorageManager"; 
  41     public FileDataStorageManager(Account account
, ContentResolver cr
) { 
  42         mContentProvider 
= null
; 
  43         mContentResolver 
= cr
; 
  47     public FileDataStorageManager(Account account
, ContentProviderClient cp
) { 
  48         mContentProvider 
= cp
; 
  49         mContentResolver 
= null
; 
  54     public OCFile 
getFileByPath(String path
) { 
  55         Cursor c 
= getCursorForValue(ProviderTableMeta
.FILE_PATH
, path
); 
  57         if (c
.moveToFirst()) { 
  58             file 
= createFileInstance(c
); 
  65     public OCFile 
getFileById(long id
) { 
  66         Cursor c 
= getCursorForValue(ProviderTableMeta
._ID
, String
.valueOf(id
)); 
  68         if (c
.moveToFirst()) { 
  69             file 
= createFileInstance(c
); 
  76     public boolean fileExists(long id
) { 
  77         return fileExists(ProviderTableMeta
._ID
, String
.valueOf(id
)); 
  81     public boolean fileExists(String path
) { 
  82         return fileExists(ProviderTableMeta
.FILE_PATH
, path
); 
  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         cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDate()); 
 101         if (fileExists(file
.getPath())) { 
 102             OCFile tmpfile 
= getFileByPath(file
.getPath()); 
 103             file
.setStoragePath(tmpfile
.getStoragePath()); 
 104             cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath()); 
 105             file
.setFileId(tmpfile
.getFileId()); 
 108             if (getContentResolver() != null
) { 
 109                 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, 
 110                         ProviderTableMeta
._ID 
+ "=?", 
 111                         new String
[] { String
.valueOf(file
.getFileId()) }); 
 114                     getContentProvider().update(ProviderTableMeta
.CONTENT_URI
, 
 115                             cv
, ProviderTableMeta
._ID 
+ "=?", 
 116                             new String
[] { String
.valueOf(file
.getFileId()) }); 
 117                 } catch (RemoteException e
) { 
 119                             "Fail to insert insert file to database " 
 124             Uri result_uri 
= null
; 
 125             if (getContentResolver() != null
) { 
 126                 result_uri 
= getContentResolver().insert( 
 127                         ProviderTableMeta
.CONTENT_URI_FILE
, cv
); 
 130                     result_uri 
= getContentProvider().insert( 
 131                             ProviderTableMeta
.CONTENT_URI_FILE
, cv
); 
 132                 } catch (RemoteException e
) { 
 134                             "Fail to insert insert file to database " 
 138             if (result_uri 
!= null
) { 
 139                 long new_id 
= Long
.parseLong(result_uri
.getPathSegments() 
 141                 file
.setFileId(new_id
); 
 145         if (file
.isDirectory() && file
.needsUpdatingWhileSaving()) 
 146             for (OCFile f 
: getDirectoryContent(file
)) 
 152     public void setAccount(Account account
) { 
 156     public Account 
getAccount() { 
 160     public void setContentResolver(ContentResolver cr
) { 
 161         mContentResolver 
= cr
; 
 164     public ContentResolver 
getContentResolver() { 
 165         return mContentResolver
; 
 168     public void setContentProvider(ContentProviderClient cp
) { 
 169         mContentProvider 
= cp
; 
 172     public ContentProviderClient 
getContentProvider() { 
 173         return mContentProvider
; 
 176     public Vector
<OCFile
> getDirectoryContent(OCFile f
) { 
 177         if (f 
!= null 
&& f
.isDirectory() && f
.getFileId() != -1) { 
 178             Vector
<OCFile
> ret 
= new Vector
<OCFile
>(); 
 180             Uri req_uri 
= Uri
.withAppendedPath( 
 181                     ProviderTableMeta
.CONTENT_URI_DIR
, 
 182                     String
.valueOf(f
.getFileId())); 
 185             if (getContentProvider() != null
) { 
 187                     c 
= getContentProvider().query(req_uri
, null
, 
 188                             ProviderTableMeta
.FILE_ACCOUNT_OWNER 
+ "=?", 
 189                             new String
[] { mAccount
.name 
}, null
); 
 190                 } catch (RemoteException e
) { 
 191                     Log
.e(TAG
, e
.getMessage()); 
 195                 c 
= getContentResolver().query(req_uri
, null
, 
 196                         ProviderTableMeta
.FILE_ACCOUNT_OWNER 
+ "=?", 
 197                         new String
[] { mAccount
.name 
}, null
); 
 200             if (c
.moveToFirst()) { 
 202                     OCFile child 
= createFileInstance(c
); 
 204                 } while (c
.moveToNext()); 
 213     private boolean fileExists(String cmp_key
, String value
) { 
 215         if (getContentResolver() != null
) { 
 216             c 
= getContentResolver() 
 217                     .query(ProviderTableMeta
.CONTENT_URI
, 
 220                                     + ProviderTableMeta
.FILE_ACCOUNT_OWNER
 
 222                             new String
[] { value
, mAccount
.name 
}, null
); 
 225                 c 
= getContentProvider().query( 
 226                         ProviderTableMeta
.CONTENT_URI
, 
 229                                 + ProviderTableMeta
.FILE_ACCOUNT_OWNER 
+ "=?", 
 230                         new String
[] { value
, mAccount
.name 
}, null
); 
 231             } catch (RemoteException e
) { 
 233                         "Couldn't determine file existance, assuming non existance: " 
 238         boolean retval 
= c
.moveToFirst(); 
 243     private Cursor 
getCursorForValue(String key
, String value
) { 
 245         if (getContentResolver() != null
) { 
 246             c 
= getContentResolver() 
 247                     .query(ProviderTableMeta
.CONTENT_URI
, 
 250                                     + ProviderTableMeta
.FILE_ACCOUNT_OWNER
 
 252                             new String
[] { value
, mAccount
.name 
}, null
); 
 255                 c 
= getContentProvider().query( 
 256                         ProviderTableMeta
.CONTENT_URI
, 
 258                         key 
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
 
 259                                 + "=?", new String
[] { value
, mAccount
.name 
}, 
 261             } catch (RemoteException e
) { 
 262                 Log
.e(TAG
, "Could not get file details: " + e
.getMessage()); 
 269     private OCFile 
createFileInstance(Cursor c
) { 
 272             file 
= new OCFile(c
.getString(c
 
 273                     .getColumnIndex(ProviderTableMeta
.FILE_PATH
))); 
 274             file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
))); 
 275             file
.setParentId(c
.getLong(c
 
 276                     .getColumnIndex(ProviderTableMeta
.FILE_PARENT
))); 
 277             file
.setStoragePath(c
.getString(c
 
 278                     .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
))); 
 279             file
.setMimetype(c
.getString(c
 
 280                     .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
))); 
 281             file
.setFileLength(c
.getLong(c
 
 282                     .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
))); 
 283             file
.setCreationTimestamp(c
.getLong(c
 
 284                     .getColumnIndex(ProviderTableMeta
.FILE_CREATION
))); 
 285             file
.setModificationTimestamp(c
.getLong(c
 
 286                     .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
))); 
 287             file
.setLastSyncDate(c
.getLong(c
 
 288                     .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
))); 
 293     public void removeFile(OCFile file
) { 
 294         Uri file_uri 
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_FILE
, ""+file
.getFileId()); 
 295         if (getContentProvider() != null
) { 
 297                 getContentProvider().delete(file_uri
, 
 298                                             ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?", 
 299                                             new String
[]{mAccount
.name
}); 
 300             } catch (RemoteException e
) { 
 304             getContentResolver().delete(file_uri
, 
 305                                         ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?", 
 306                                         new String
[]{mAccount
.name
});