@Override
public OCFile getFileByPath(String path) {
Cursor c = getCursorForValue(ProviderTableMeta.FILE_PATH, path);
- if (c.moveToFirst())
- return createFileInstance(c);
- return null;
+ OCFile file = null;
+ if (c.moveToFirst()) {
+ file = createFileInstance(c);
+ c.close();
+ }
+ return file;
}
@Override
public OCFile getFileById(long id) {
Cursor c = getCursorForValue(ProviderTableMeta._ID, String.valueOf(id));
- if (c.moveToFirst())
- return createFileInstance(c);
- return null;
+ OCFile file = null;
+ if (c.moveToFirst()) {
+ file = createFileInstance(c);
+ c.close();
+ }
+ return file;
}
@Override
import java.io.IOException;\r
import java.net.UnknownHostException;\r
import java.util.Date;\r
-import java.util.LinkedList;\r
\r
import org.apache.http.HttpHost;\r
import org.apache.http.HttpRequest;\r
import org.apache.http.HttpResponse;\r
import org.apache.http.client.ClientProtocolException;\r
import org.apache.http.conn.ConnectionKeepAliveStrategy;\r
-import org.apache.http.impl.auth.BasicScheme;\r
-import org.apache.http.impl.client.DefaultHttpClient;\r
-import org.apache.http.protocol.BasicHttpContext;\r
import org.apache.http.protocol.HttpContext;\r
\r
import android.accounts.Account;\r
import android.content.ContentProviderClient;\r
import android.content.Context;\r
import android.net.Uri;\r
-import android.text.TextUtils;\r
-import android.util.Log;\r
import eu.alefzero.owncloud.authenticator.AccountAuthenticator;\r
import eu.alefzero.owncloud.datamodel.DataStorageManager;\r
-import eu.alefzero.owncloud.datamodel.OCFile;\r
import eu.alefzero.webdav.HttpPropFind;\r
-import eu.alefzero.webdav.TreeNode;\r
-import eu.alefzero.webdav.TreeNode.NodeProperty;\r
import eu.alefzero.webdav.WebdavClient;\r
-import eu.alefzero.webdav.WebdavUtils;\r
\r
/**\r
* Base SyncAdapter for OwnCloud Designed to be subclassed for the concrete\r
return null;\r
}\r
\r
- protected TreeNode fireRequest(HttpRequest query)\r
- throws ClientProtocolException, OperationCanceledException,\r
- AuthenticatorException, IOException {\r
- HttpResponse response = fireRawRequest(query);\r
-\r
- TreeNode root = new TreeNode();\r
- root.setProperty(TreeNode.NodeProperty.NAME, "");\r
- //this.parseResponse(response, getUri(), getClient(), mHost,\r
- // root.getChildList(), false, 0);\r
- return root;\r
- }\r
-\r
protected Uri getUri() {\r
return Uri.parse(this.getAccountManager().getUserData(getAccount(),\r
AccountAuthenticator.KEY_OC_URL));\r
\r
return mClient;\r
}\r
-\r
- private void parseResponse(HttpResponse resp, Uri uri,\r
- DefaultHttpClient client, HttpHost targetHost,\r
- LinkedList<TreeNode> insertList, boolean sf, long parent_id)\r
- throws IOException, OperationCanceledException,\r
- AuthenticatorException {\r
- boolean skipFirst = sf, override_parent = !sf;\r
- for (TreeNode n : WebdavUtils.parseResponseToNodes(resp.getEntity()\r
- .getContent())) {\r
- if (skipFirst) {\r
- skipFirst = false;\r
- continue;\r
- }\r
- String path = n.stripPathFromFilename(uri.getPath());\r
-\r
- long mod = n.getProperty(NodeProperty.LAST_MODIFIED_DATE) == null ? 0\r
- : Long.parseLong(n\r
- .getProperty(NodeProperty.LAST_MODIFIED_DATE));\r
- \r
- OCFile file = getStorageManager().getFileByPath(n.getProperty(NodeProperty.PATH));\r
- if (file != null && file.fileExists() && file.getModificationTimestamp() >= mod) {\r
- Log.d(TAG, "No update for file/dir " + file.getFileName()\r
- + " is needed");\r
- } else {\r
- file = new OCFile(n.getProperty(NodeProperty.PATH));\r
- Log.d(TAG, "File " + n.getProperty(NodeProperty.PATH)\r
- + " will be "\r
- + (file.fileExists() ? "updated" : "created"));\r
- long len = n.getProperty(NodeProperty.CONTENT_LENGTH) == null ? 0\r
- : Long.parseLong(n\r
- .getProperty(NodeProperty.CONTENT_LENGTH));\r
- long create = n.getProperty(NodeProperty.CREATE_DATE) == null ? 0\r
- : Long.parseLong(n\r
- .getProperty(NodeProperty.CREATE_DATE));\r
-\r
- file.setFileLength(len);\r
- file.setCreationTimestamp(create);\r
- file.setModificationTimestamp(mod);\r
- file.setMimetype(n.getProperty(NodeProperty.RESOURCE_TYPE));\r
- file.setParentId(parent_id);\r
- getStorageManager().saveFile(file);\r
- if (override_parent) {\r
- parent_id = file.getFileId();\r
- override_parent = false;\r
- }\r
- }\r
-\r
- if (!TextUtils.isEmpty(n.getProperty(NodeProperty.NAME))\r
- && n.getProperty(NodeProperty.RESOURCE_TYPE).equals("DIR")) {\r
-\r
- HttpPropFind method = new HttpPropFind(uri.getPath() + path\r
- + n.getProperty(NodeProperty.NAME).replace(" ", "%20")\r
- + "/");\r
- HttpResponse response = fireRawRequest(method);\r
- parseResponse(response, uri, client, targetHost,\r
- n.getChildList(), true, file.getFileId());\r
- }\r
- }\r
- }\r
}
\ No newline at end of file
import android.content.Context;\r
import android.content.SyncResult;\r
import android.os.Bundle;\r
-import android.util.Log;\r
import eu.alefzero.owncloud.datamodel.FileDataStorageManager;\r
import eu.alefzero.owncloud.datamodel.OCFile;\r
import eu.alefzero.webdav.WebdavEntry;\r
* @author Bartek Przybylski\r
*/\r
public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {\r
- private static final String TAG = "FileSyncAdapter";\r
\r
public FileSyncAdapter(Context context, boolean autoInitialize) {\r
super(context, autoInitialize);\r
OCFile file = fillOCFile(we);\r
file.setParentId(0);\r
getStorageManager().saveFile(file);\r
- Log.d(TAG, file.getPath() + " " + file.getFileId());\r
fetchData(getUri().toString(), syncResult, file.getFileId());\r
}\r
} catch (OperationCanceledException e) {\r
+++ /dev/null
-/* ownCloud Android client application
- *
- * Copyright (C) 2011 Bartek Przybylski
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-package eu.alefzero.webdav;
-
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.Map.Entry;
-
-import org.w3c.dom.Document;
-
-import android.text.TextUtils;
-import android.util.Log;
-
-public class TreeNode {
- public enum NodeProperty {
- NAME,
- PARENT,
- PATH,
- RESOURCE_TYPE,
- CREATE_DATE,
- LAST_MODIFIED_DATE,
- CONTENT_LENGTH
- }
-
- private LinkedList<TreeNode> mChildren;
-
- public TreeNode() {
- propertyMap_ = new HashMap<NodeProperty, String>();
- mChildren = new LinkedList<TreeNode>();
- }
-
- public void setProperty(NodeProperty propertyName, String propertyValue) {
- propertyMap_.put(propertyName, propertyValue);
- }
-
- public String getProperty(NodeProperty propertyName) {
- return propertyMap_.get(propertyName);
- }
-
- void refreshData(Document document) {
- throw new RuntimeException("Unimplemented refreshData");
- }
-
- public String toString() {
- String str = "TreeNode {";
- for (Entry<NodeProperty, String> e : propertyMap_.entrySet()) {
- str += e.getKey() + ": " + e.getValue() + ",";
- }
- str += "}";
- return str;
- }
-
- private HashMap<NodeProperty, String> propertyMap_;
-
- public String stripPathFromFilename(String oc_path) {
- if (propertyMap_.containsKey(NodeProperty.NAME)) {
- String name = propertyMap_.get(NodeProperty.NAME);
- name = name.replace(oc_path, "");
- String path = "";
- String name2 = name;
- if (name.endsWith("/")) {
- name = name.substring(0, name.length()-1);
- }
- path = name.substring(0, name.lastIndexOf('/')+1);
- name = name.substring(name.lastIndexOf('/')+1);
- name = name.replace("%20", " ");
-
- propertyMap_.remove(NodeProperty.NAME);
- propertyMap_.put(NodeProperty.NAME, name);
- propertyMap_.remove(NodeProperty.PATH);
- propertyMap_.put(NodeProperty.PATH, name2);
- return path;
- }
- return null;
- }
-
- public LinkedList<TreeNode> getChildList() {
- return mChildren;
- }
-
- public String[] getChildrenNames() {
- String[] names = new String[mChildren.size()];
- for (int i = 0; i < mChildren.size(); ++i) {
- names[i] = mChildren.get(i).getProperty(NodeProperty.NAME);
- }
- return names;
- }
-
- public boolean hasChildren() {
- return !mChildren.isEmpty();
- }
-}
+++ /dev/null
-/* ownCloud Android client application\r
- * Copyright (C) 2011 Bartek Przybylski\r
- *\r
- * This program is free software: you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation, either version 3 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with this program. If not, see <http://www.gnu.org/licenses/>.\r
- *\r
- */\r
-package eu.alefzero.webdav;\r
-\r
-import java.util.List;\r
-import java.util.ListIterator;\r
-\r
-import org.w3c.dom.Document;\r
-\r
-public class TreeNodeContainer extends TreeNode {\r
- \r
- @Override\r
- void refreshData(Document document) {\r
- ListIterator<TreeNode> iterator = children_.listIterator();\r
- while (iterator.hasNext()) {\r
- iterator.next().refreshData(document);\r
- }\r
- }\r
- \r
- private List<TreeNode> children_;\r
-}\r
+++ /dev/null
-/* ownCloud Android client application
- * Copyright (C) 2011 Bartek Przybylski
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-package eu.alefzero.webdav;
-
-import org.w3c.dom.Document;
-
-public class TreeNodeFile extends TreeNode {
-
- public TreeNodeFile() {
- }
-
- @Override
- void refreshData(Document document) {
- /*if (is_pinned_) {
- String fullPath = getProperty(NodeProperty.PATH);
- if (document.hasChildNodes()) {
- Node child = document.getFirstChild();
- do {
-
- } while ((child = child.getNextSibling()) != null);
- }
-
- //TODO: update file
- }*/
- }
-}
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
-import eu.alefzero.webdav.TreeNode.NodeProperty;
-
import android.util.Log;
public class WebdavUtils {
}
return null;
}
-
- public static List<TreeNode> parseResponseToNodes(InputStream response) {
- LinkedList<TreeNode> rList = new LinkedList<TreeNode>();
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder;
- try {
- builder = factory.newDocumentBuilder();
- Document document = builder.parse(response);
- String davPrefix = determineDAVPrefix(document.getDocumentElement());
-
- NodeList nodes = document.getElementsByTagName(davPrefix + RESPONSE);
- Log.i("WebdavUtils", "Parsing " + nodes.getLength() + " response nodes");
-
- for (int i = 0; i < nodes.getLength(); ++i) {
- Node currentNode = nodes.item(i);
- TreeNode resultNode = new TreeNode();
- parseResourceType(currentNode, resultNode, davPrefix);
- parseResourceDates(currentNode, resultNode, davPrefix);
- parseDisplayName(currentNode, resultNode, davPrefix);
- rList.add(resultNode);
- }
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return rList;
- }
-
- private static void parseDisplayName(Node currentNode, TreeNode resultNode,
- String davPrefix) {
- Element currentElement = (Element) currentNode;
- if (currentElement.getElementsByTagName(davPrefix + HREF).getLength() != 0) {
- String filepath = currentElement.getElementsByTagName(davPrefix + HREF).item(0).getFirstChild().getNodeValue();
- resultNode.setProperty(NodeProperty.NAME, filepath);
- }
- }
-
- private static void parseResourceDates(Node currentNode, TreeNode resultNode, String davPrefix) {
- Element currentElement = (Element)currentNode;
- if (currentElement.getElementsByTagName(davPrefix + LAST_MODIFIED).getLength() != 0) {
- Date date = parseResponseDate(
- currentElement.getElementsByTagName(davPrefix + LAST_MODIFIED).item(0).getFirstChild().getNodeValue());
- resultNode.setProperty(NodeProperty.LAST_MODIFIED_DATE, String.valueOf(date.getTime()));
- }
- if (currentElement.getElementsByTagName(davPrefix + CREATE_DATE).getLength() != 0) {
- Date date = parseResponseDate(
- currentElement.getElementsByTagName(davPrefix + CREATE_DATE).item(0).getFirstChild().getNodeValue());
- resultNode.setProperty(NodeProperty.CREATE_DATE, String.valueOf(date.getTime()));
- }
- }
-
- private static void parseResourceType(Node currentNode, TreeNode resultNode, String davPrefix) {
- Element currentElement = (Element)currentNode;
- if (currentElement.getElementsByTagName(davPrefix + RESOURCE_TYPE).getLength() != 0 &&
- currentElement.getElementsByTagName(davPrefix + RESOURCE_TYPE).item(0).hasChildNodes()) {
- resultNode.setProperty(NodeProperty.RESOURCE_TYPE, "DIR");
- } else {
- if (currentElement.getElementsByTagName(davPrefix + CONTENT_TYPE).getLength() != 0) {
- resultNode.setProperty(NodeProperty.RESOURCE_TYPE,
- currentElement.getElementsByTagName(davPrefix + CONTENT_TYPE).item(0).getFirstChild().getNodeValue());
- }
- if (currentElement.getElementsByTagName(davPrefix + CONTENT_LENGTH).getLength() != 0) {
- resultNode.setProperty(NodeProperty.CONTENT_LENGTH,
- currentElement.getElementsByTagName(davPrefix + CONTENT_LENGTH).item(0).getFirstChild().getNodeValue());
- }
- }
- }
}