1 /* ownCloud Android client application 
   2  *   Copyright (C) 2011  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
.syncadapter
; 
  21 import java
.io
.IOException
; 
  23 import org
.apache
.http
.entity
.StringEntity
; 
  25 import android
.accounts
.Account
; 
  26 import android
.accounts
.AuthenticatorException
; 
  27 import android
.accounts
.OperationCanceledException
; 
  28 import android
.content
.ContentProviderClient
; 
  29 import android
.content
.ContentValues
; 
  30 import android
.content
.Context
; 
  31 import android
.content
.SyncResult
; 
  32 import android
.database
.Cursor
; 
  33 import android
.net
.Uri
; 
  34 import android
.os
.Bundle
; 
  35 import android
.os
.RemoteException
; 
  36 import android
.util
.Log
; 
  37 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
; 
  38 import eu
.alefzero
.webdav
.HttpPropFind
; 
  39 import eu
.alefzero
.webdav
.TreeNode
; 
  40 import eu
.alefzero
.webdav
.TreeNode
.NodeProperty
; 
  41 import eu
.alefzero
.webdav
.WebdavUtils
; 
  44  * SyncAdapter implementation for syncing sample SyncAdapter contacts to the 
  45  * platform ContactOperations provider. 
  47  * @author Bartek Przybylski 
  49 public class FileSyncAdapter 
extends AbstractOwnCloudSyncAdapter 
{ 
  50         private static final String TAG 
= "FileSyncAdapter"; 
  52         public FileSyncAdapter(Context context
, boolean autoInitialize
) { 
  53                 super(context
, autoInitialize
); 
  57         public synchronized void onPerformSync( 
  61                         ContentProviderClient provider
,  
  62                         SyncResult syncResult
) { 
  65                         this.setAccount(account
); 
  66                         this.setContentProvider(provider
); 
  68                         HttpPropFind query 
= this.getPropFindQuery(); 
  69                         query
.setEntity(new StringEntity(WebdavUtils
.prepareXmlForPropFind())); 
  70                         TreeNode root 
= this.fireRequest(query
); 
  72                 //      commitToDatabase(root, null); 
  73                 } catch (OperationCanceledException e
) { 
  75                 } catch (AuthenticatorException e
) { 
  76                         syncResult
.stats
.numAuthExceptions
++; 
  78                 } catch (IOException e
) { 
  79                         syncResult
.stats
.numIoExceptions
++; 
  81                 }// catch (RemoteException e) { 
  82                 //      e.printStackTrace(); 
  86         private void commitToDatabase(TreeNode root
, String parentId
) throws RemoteException 
{ 
  87                 for (TreeNode n 
: root
.getChildList()) { 
  88                         Log
.d(TAG
, n
.toString()); 
  89                         ContentValues cv 
= new ContentValues(); 
  90                         cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, n
.getProperty(NodeProperty
.CONTENT_LENGTH
)); 
  91                         cv
.put(ProviderTableMeta
.FILE_MODIFIED
, n
.getProperty(NodeProperty
.LAST_MODIFIED_DATE
)); 
  92                         cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, n
.getProperty(NodeProperty
.RESOURCE_TYPE
)); 
  93                         cv
.put(ProviderTableMeta
.FILE_PARENT
, parentId
); 
  95                         String name 
= n
.getProperty(NodeProperty
.NAME
), 
  96                                         path 
= n
.getProperty(NodeProperty
.PATH
); 
  97                         Cursor c 
= this.getContentProvider().query(ProviderTableMeta
.CONTENT_URI_FILE
, 
  99                                         ProviderTableMeta
.FILE_NAME
+"=? AND " + ProviderTableMeta
.FILE_PATH 
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER 
+ "=?", 
 100                                         new String
[]{name
, path
, this.getAccount().name
}, 
 102                         if (c
.moveToFirst()) { 
 103                                 this.getContentProvider().update(ProviderTableMeta
.CONTENT_URI
, 
 105                                                 ProviderTableMeta
._ID
+"=?", 
 106                                                                 new String
[]{c
.getString(c
.getColumnIndex(ProviderTableMeta
._ID
))}); 
 107                                 Log
.d(TAG
, "ID of: "+name
+":"+c
.getString(c
.getColumnIndex(ProviderTableMeta
._ID
))); 
 109                                 cv
.put(ProviderTableMeta
.FILE_NAME
, n
.getProperty(NodeProperty
.NAME
)); 
 110                                 cv
.put(ProviderTableMeta
.FILE_PATH
, n
.getProperty(NodeProperty
.PATH
)); 
 111                                 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, this.getAccount().name
); 
 112                                 Uri entry 
= this.getContentProvider().insert(ProviderTableMeta
.CONTENT_URI_FILE
, cv
); 
 113                                 Log
.d(TAG
, "Inserting new entry " + path
); 
 114                                 c 
= this.getContentProvider().query(entry
, null
, null
, null
, null
); 
 117                         if (n
.getProperty(NodeProperty
.RESOURCE_TYPE
).equals("DIR")) { 
 118                                 commitToDatabase(n
, c
.getString(c
.getColumnIndex(ProviderTableMeta
._ID
))); 
 121                 // clean removed files 
 122                 String
[] selection 
= new String
[root
.getChildList().size()+2]; 
 123                 selection
[0] = this.getAccount().name
; 
 124                 selection
[1] = parentId
; 
 126                 for (int i 
= 2; i 
< selection
.length
-1; ++i
) { 
 128                         selection
[i
] = root
.getChildList().get(i
-2).getProperty(NodeProperty
.NAME
); 
 130                 if (selection
.length 
>= 3) { 
 131                         selection
[selection
.length
-1] = root
.getChildrenNames()[selection
.length
-3]; 
 134                 for (int i 
= 0; i 
< selection
.length
; ++i
) { 
 135                         Log
.d(TAG
,selection
[i
]+""); 
 137                 Log
.d(TAG
,"Removing files "+ parentId
); 
 138                 this.getContentProvider().delete(ProviderTableMeta
.CONTENT_URI
, 
 139                                 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=? AND " + ProviderTableMeta
.FILE_PARENT 
+ (parentId
==null?
" IS ":"=")+"? AND " + ProviderTableMeta
.FILE_NAME 
+ " NOT IN ("+qm
+")",