de16b137b0eceada40176fded72efd4848faf14c
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / oc_framework / operations / RemoteFile.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
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
18 package com.owncloud.android.oc_framework.operations;
19
20 import java.io.Serializable;
21
22 import android.os.Parcel;
23 import android.os.Parcelable;
24
25 import com.owncloud.android.oc_framework.network.webdav.WebdavEntry;
26 import com.owncloud.android.oc_framework.utils.FileUtils;
27
28 /**
29 * Contains the data of a Remote File from a WebDavEntry
30 *
31 * @author masensio
32 */
33
34 public class RemoteFile implements Parcelable, Serializable {
35
36 /** Generated - should be refreshed every time the class changes!! */
37 private static final long serialVersionUID = 532139091191390616L;
38
39 private String mRemotePath;
40 private String mMimeType;
41 private long mLength;
42 private long mCreationTimestamp;
43 private long mModifiedTimestamp;
44 private String mEtag;
45
46 /**
47 * Getters and Setters
48 */
49
50 public String getRemotePath() {
51 return mRemotePath;
52 }
53
54 public void setRemotePath(String remotePath) {
55 this.mRemotePath = remotePath;
56 }
57
58 public String getMimeType() {
59 return mMimeType;
60 }
61
62 public void setMimeType(String mimeType) {
63 this.mMimeType = mimeType;
64 }
65
66 public long getLength() {
67 return mLength;
68 }
69
70 public void setLength(long length) {
71 this.mLength = length;
72 }
73
74 public long getCreationTimestamp() {
75 return mCreationTimestamp;
76 }
77
78 public void setCreationTimestamp(long creationTimestamp) {
79 this.mCreationTimestamp = creationTimestamp;
80 }
81
82 public long getModifiedTimestamp() {
83 return mModifiedTimestamp;
84 }
85
86 public void setModifiedTimestamp(long modifiedTimestamp) {
87 this.mModifiedTimestamp = modifiedTimestamp;
88 }
89
90 public String getEtag() {
91 return mEtag;
92 }
93
94 public void setEtag(String etag) {
95 this.mEtag = etag;
96 }
97
98 /**
99 * Create new {@link RemoteFile} with given path.
100 *
101 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
102 *
103 * @param path The remote path of the file.
104 */
105 public RemoteFile(String path) {
106 resetData();
107 if (path == null || path.length() <= 0 || !path.startsWith(FileUtils.PATH_SEPARATOR)) {
108 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path);
109 }
110 mRemotePath = path;
111 }
112
113 public RemoteFile(WebdavEntry we) {
114 this(we.decodedPath());
115 this.setCreationTimestamp(we.createTimestamp());
116 this.setLength(we.contentLength());
117 this.setMimeType(we.contentType());
118 this.setModifiedTimestamp(we.modifiedTimestamp());
119 this.setEtag(we.etag());
120 }
121
122 /**
123 * Used internally. Reset all file properties
124 */
125 private void resetData() {
126 mRemotePath = null;
127 mMimeType = null;
128 mLength = 0;
129 mCreationTimestamp = 0;
130 mModifiedTimestamp = 0;
131 mEtag = null;
132 }
133
134 /**
135 * Parcelable Methods
136 */
137 public static final Parcelable.Creator<RemoteFile> CREATOR = new Parcelable.Creator<RemoteFile>() {
138 @Override
139 public RemoteFile createFromParcel(Parcel source) {
140 return new RemoteFile(source);
141 }
142
143 @Override
144 public RemoteFile[] newArray(int size) {
145 return new RemoteFile[size];
146 }
147 };
148
149
150 /**
151 * Reconstruct from parcel
152 *
153 * @param source The source parcel
154 */
155 protected RemoteFile(Parcel source) {
156 readFromParcel(source);
157 }
158
159 public void readFromParcel (Parcel source) {
160 mRemotePath = source.readString();
161 mMimeType = source.readString();
162 mLength = source.readLong();
163 mCreationTimestamp = source.readLong();
164 mModifiedTimestamp = source.readLong();
165 mEtag = source.readString();
166 }
167
168 @Override
169 public int describeContents() {
170 return this.hashCode();
171 }
172
173 @Override
174 public void writeToParcel(Parcel dest, int flags) {
175 dest.writeString(mRemotePath);
176 dest.writeString(mMimeType);
177 dest.writeLong(mLength);
178 dest.writeLong(mCreationTimestamp);
179 dest.writeLong(mModifiedTimestamp);
180 dest.writeString(mEtag);
181 }
182
183
184 }