Merge branch 'develop' into setup_buttons
[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 version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17 package eu.alefzero.webdav;
18
19 import java.util.Date;
20
21 import org.apache.jackrabbit.webdav.MultiStatusResponse;
22 import org.apache.jackrabbit.webdav.property.DavProperty;
23 import org.apache.jackrabbit.webdav.property.DavPropertyName;
24 import org.apache.jackrabbit.webdav.property.DavPropertySet;
25
26 import com.owncloud.android.Log_OC;
27
28
29 import android.net.Uri;
30
31 public class WebdavEntry {
32 private String mName, mPath, mUri, mContentType;
33 private long mContentLength, mCreateTimestamp, mModifiedTimestamp;
34
35 public WebdavEntry(MultiStatusResponse ms, String splitElement) {
36 resetData();
37 if (ms.getStatus().length != 0) {
38 mUri = ms.getHref();
39
40 mPath = mUri.split(splitElement, 2)[1];
41
42 int status = ms.getStatus()[0].getStatusCode();
43 DavPropertySet propSet = ms.getProperties(status);
44 @SuppressWarnings("rawtypes")
45 DavProperty prop = propSet.get(DavPropertyName.DISPLAYNAME);
46 if (prop != null)
47 mName = (String) prop.getName().toString();
48 else {
49 String[] tmp = mPath.split("/");
50 if (tmp.length > 0)
51 mName = tmp[tmp.length - 1];
52 }
53
54 // use unknown mimetype as default behavior
55 mContentType = "application/octet-stream";
56 prop = propSet.get(DavPropertyName.GETCONTENTTYPE);
57 if (prop != null) {
58 mContentType = (String) prop.getValue();
59 // dvelasco: some builds of ownCloud server 4.0.x added a trailing ';' to the MIME type ; if looks fixed, but let's be cautious
60 if (mContentType.indexOf(";") >= 0) {
61 mContentType = mContentType.substring(0, mContentType.indexOf(";"));
62 }
63 }
64
65 // check if it's a folder in the standard way: see RFC2518 12.2 . RFC4918 14.3
66 prop = propSet.get(DavPropertyName.RESOURCETYPE);
67 if (prop!= null) {
68 Object value = prop.getValue();
69 if (value != null) {
70 mContentType = "DIR"; // a specific attribute would be better, but this is enough; unless while we have no reason to distinguish MIME types for folders
71 }
72 }
73
74 prop = propSet.get(DavPropertyName.GETCONTENTLENGTH);
75 if (prop != null)
76 mContentLength = Long.parseLong((String) prop.getValue());
77
78 prop = propSet.get(DavPropertyName.GETLASTMODIFIED);
79 if (prop != null) {
80 Date d = WebdavUtils
81 .parseResponseDate((String) prop.getValue());
82 mModifiedTimestamp = (d != null) ? d.getTime() : 0;
83 }
84
85 prop = propSet.get(DavPropertyName.CREATIONDATE);
86 if (prop != null) {
87 Date d = WebdavUtils
88 .parseResponseDate((String) prop.getValue());
89 mCreateTimestamp = (d != null) ? d.getTime() : 0;
90 }
91
92 } else {
93 Log_OC.e("WebdavEntry",
94 "General fuckup, no status for webdav response");
95 }
96 }
97
98 public String path() {
99 return mPath;
100 }
101
102 public String decodedPath() {
103 return Uri.decode(mPath);
104 }
105
106 public String name() {
107 return mName;
108 }
109
110 public boolean isDirectory() {
111 return mContentType.equals("DIR");
112 }
113
114 public String contentType() {
115 return mContentType;
116 }
117
118 public String uri() {
119 return mUri;
120 }
121
122 public long contentLength() {
123 return mContentLength;
124 }
125
126 public long createTimestamp() {
127 return mCreateTimestamp;
128 }
129
130 public long modifiedTimestamp() {
131 return mModifiedTimestamp;
132 }
133
134 private void resetData() {
135 mName = mUri = mContentType = null;
136 mContentLength = mCreateTimestamp = mModifiedTimestamp = 0;
137 }
138 }