Fixed check of modifications applied in server (still based on modification date)
[pub/Android/ownCloud.git] / src / eu / alefzero / webdav / WebdavEntry.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 ownCloud
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 package eu.alefzero.webdav;
19
20 import java.util.Date;
21
22 import org.apache.jackrabbit.webdav.MultiStatusResponse;
23 import org.apache.jackrabbit.webdav.property.DavProperty;
24 import org.apache.jackrabbit.webdav.property.DavPropertyName;
25 import org.apache.jackrabbit.webdav.property.DavPropertySet;
26
27 import android.net.Uri;
28 import android.util.Log;
29
30 public class WebdavEntry {
31 private String mName, mPath, mUri, mContentType;
32 private long mContentLength, mCreateTimestamp, mModifiedTimestamp;
33
34 public WebdavEntry(MultiStatusResponse ms, String splitElement) {
35 resetData();
36 if (ms.getStatus().length != 0) {
37 mUri = ms.getHref();
38
39 mPath = mUri.split(splitElement, 2)[1];
40
41 int status = ms.getStatus()[0].getStatusCode();
42 DavPropertySet propSet = ms.getProperties(status);
43 @SuppressWarnings("rawtypes")
44 DavProperty prop = propSet.get(DavPropertyName.DISPLAYNAME);
45 if (prop != null)
46 mName = (String) prop.getName().toString();
47 else {
48 String[] tmp = mPath.split("/");
49 if (tmp.length > 0)
50 mName = tmp[tmp.length - 1];
51 }
52
53 // use unknown mimetype as default behavior
54 mContentType = "application/octet-stream";
55 prop = propSet.get(DavPropertyName.GETCONTENTTYPE);
56 if (prop != null) {
57 mContentType = (String) prop.getValue();
58 // dvelasco: some builds of ownCloud server 4.0.x added a trailing ';' to the MIME type ; if looks fixed, but let's be cautious
59 if (mContentType.indexOf(";") >= 0) {
60 mContentType = mContentType.substring(0, mContentType.indexOf(";"));
61 }
62 }
63
64 // check if it's a folder in the standard way: see RFC2518 12.2 , or RFC4918 14.3
65 prop = propSet.get(DavPropertyName.RESOURCETYPE);
66 if (prop!= null) {
67 Object value = prop.getValue();
68 if (value != null) {
69 mContentType = "DIR"; // a specific attribute would be better, but this is enough; unless while we have no reason to distinguish MIME types for folders
70 }
71 }
72
73 prop = propSet.get(DavPropertyName.GETCONTENTLENGTH);
74 if (prop != null)
75 mContentLength = Long.parseLong((String) prop.getValue());
76
77 prop = propSet.get(DavPropertyName.GETLASTMODIFIED);
78 if (prop != null) {
79 Date d = WebdavUtils
80 .parseResponseDate((String) prop.getValue());
81 mModifiedTimestamp = (d != null) ? d.getTime() : 0;
82 }
83
84 prop = propSet.get(DavPropertyName.CREATIONDATE);
85 if (prop != null) {
86 Date d = WebdavUtils
87 .parseResponseDate((String) prop.getValue());
88 mCreateTimestamp = (d != null) ? d.getTime() : 0;
89 }
90
91 } else {
92 Log.e("WebdavEntry",
93 "General fuckup, no status for webdav response");
94 }
95 }
96
97 public String path() {
98 return mPath;
99 }
100
101 public String decodedPath() {
102 return Uri.decode(mPath);
103 }
104
105 public String name() {
106 return mName;
107 }
108
109 public boolean isDirectory() {
110 return mContentType.equals("DIR");
111 }
112
113 public String contentType() {
114 return mContentType;
115 }
116
117 public String uri() {
118 return mUri;
119 }
120
121 public long contentLength() {
122 return mContentLength;
123 }
124
125 public long createTimestamp() {
126 return mCreateTimestamp;
127 }
128
129 public long modifiedTimestamp() {
130 return mModifiedTimestamp;
131 }
132
133 private void resetData() {
134 mName = mUri = mContentType = null;
135 mContentLength = mCreateTimestamp = mModifiedTimestamp = 0;
136 }
137 }