OCFile stores again decoded remote paths; WebdavUtils.encode(...) added; fixed space...
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / datamodel / OCFile.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
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
19 package eu.alefzero.owncloud.datamodel;
20
21 import java.io.File;
22 import java.net.MalformedURLException;
23 import java.net.URL;
24
25 import android.net.Uri;
26 import android.os.Parcel;
27 import android.os.Parcelable;
28
29 public class OCFile implements Parcelable, Comparable<OCFile> {
30
31 public static final Parcelable.Creator<OCFile> CREATOR = new Parcelable.Creator<OCFile>() {
32 @Override
33 public OCFile createFromParcel(Parcel source) {
34 return new OCFile(source);
35 }
36
37 @Override
38 public OCFile[] newArray(int size) {
39 return new OCFile[size];
40 }
41 };
42
43 public static final String PATH_SEPARATOR = "/";
44
45 private long mId;
46 private long mParentId;
47 private long mLength;
48 private long mCreationTimestamp;
49 private long mModifiedTimestamp;
50 private String mRemotePath;
51 private String mLocalPath;
52 private String mMimeType;
53 private boolean mNeedsUpdating;
54 private long mLastSyncDate;
55 private boolean mKeepInSync;
56
57 /**
58 * Create new {@link OCFile} with given path.
59 *
60 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
61 *
62 * @param path The remote path of the file.
63 */
64 public OCFile(String path) {
65 resetData();
66 mNeedsUpdating = false;
67 if (path == null || path.length() <= 0 || !path.startsWith(PATH_SEPARATOR)) {
68 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path);
69 }
70 mRemotePath = path;
71 }
72
73 /**
74 * Reconstruct from parcel
75 *
76 * @param source The source parcel
77 */
78 private OCFile(Parcel source) {
79 mId = source.readLong();
80 mParentId = source.readLong();
81 mLength = source.readLong();
82 mCreationTimestamp = source.readLong();
83 mModifiedTimestamp = source.readLong();
84 mRemotePath = source.readString();
85 mLocalPath = source.readString();
86 mMimeType = source.readString();
87 mNeedsUpdating = source.readInt() == 0;
88 mKeepInSync = source.readInt() == 1;
89 mLastSyncDate = source.readLong();
90 }
91
92 @Override
93 public void writeToParcel(Parcel dest, int flags) {
94 dest.writeLong(mId);
95 dest.writeLong(mParentId);
96 dest.writeLong(mLength);
97 dest.writeLong(mCreationTimestamp);
98 dest.writeLong(mModifiedTimestamp);
99 dest.writeString(mRemotePath);
100 dest.writeString(mLocalPath);
101 dest.writeString(mMimeType);
102 dest.writeInt(mNeedsUpdating ? 1 : 0);
103 dest.writeInt(mKeepInSync ? 1 : 0);
104 dest.writeLong(mLastSyncDate);
105 }
106
107 /**
108 * Gets the ID of the file
109 *
110 * @return the file ID
111 */
112 public long getFileId() {
113 return mId;
114 }
115
116 /**
117 * Returns the remote path of the file on ownCloud
118 *
119 * @return The remote path to the file
120 */
121 public String getRemotePath() {
122 return mRemotePath;
123 }
124
125 /**
126 * Can be used to check, whether or not this file exists in the database
127 * already
128 *
129 * @return true, if the file exists in the database
130 */
131 public boolean fileExists() {
132 return mId != -1;
133 }
134
135 /**
136 * Use this to find out if this file is a Directory
137 *
138 * @return true if it is a directory
139 */
140 public boolean isDirectory() {
141 return mMimeType != null && mMimeType.equals("DIR");
142 }
143
144 /**
145 * Use this to check if this file is available locally
146 *
147 * @return true if it is
148 */
149 public boolean isDownloaded() {
150 return mLocalPath != null && !mLocalPath.equals("");
151 }
152
153 /**
154 * The path, where the file is stored locally
155 *
156 * @return The local path to the file
157 */
158 public String getStoragePath() {
159 return mLocalPath;
160 }
161
162 /**
163 * Can be used to set the path where the file is stored
164 *
165 * @param storage_path to set
166 */
167 public void setStoragePath(String storage_path) {
168 mLocalPath = storage_path;
169 }
170
171 /**
172 * Get a UNIX timestamp of the file creation time
173 *
174 * @return A UNIX timestamp of the time that file was created
175 */
176 public long getCreationTimestamp() {
177 return mCreationTimestamp;
178 }
179
180 /**
181 * Set a UNIX timestamp of the time the file was created
182 *
183 * @param creation_timestamp to set
184 */
185 public void setCreationTimestamp(long creation_timestamp) {
186 mCreationTimestamp = creation_timestamp;
187 }
188
189 /**
190 * Get a UNIX timestamp of the file modification time
191 *
192 * @return A UNIX timestamp of the modification time
193 */
194 public long getModificationTimestamp() {
195 return mModifiedTimestamp;
196 }
197
198 /**
199 * Set a UNIX timestamp of the time the time the file was modified.
200 *
201 * @param modification_timestamp to set
202 */
203 public void setModificationTimestamp(long modification_timestamp) {
204 mModifiedTimestamp = modification_timestamp;
205 }
206
207 /**
208 * Returns the filename and "/" for the root directory
209 *
210 * @return The name of the file
211 */
212 public String getFileName() {
213 File f = new File(getRemotePath());
214 return f.getName().length() == 0 ? "/" : f.getName();
215 }
216
217 /**
218 * Can be used to get the Mimetype
219 *
220 * @return the Mimetype as a String
221 */
222 public String getMimetype() {
223 return mMimeType;
224 }
225
226 /**
227 * Adds a file to this directory. If this file is not a directory, an
228 * exception gets thrown.
229 *
230 * @param file to add
231 * @throws IllegalStateException if you try to add a something and this is
232 * not a directory
233 */
234 public void addFile(OCFile file) throws IllegalStateException {
235 if (isDirectory()) {
236 file.mParentId = mId;
237 mNeedsUpdating = true;
238 return;
239 }
240 throw new IllegalStateException(
241 "This is not a directory where you can add stuff to!");
242 }
243
244 /**
245 * Used internally. Reset all file properties
246 */
247 private void resetData() {
248 mId = -1;
249 mRemotePath = null;
250 mParentId = 0;
251 mLocalPath = null;
252 mMimeType = null;
253 mLength = 0;
254 mCreationTimestamp = 0;
255 mModifiedTimestamp = 0;
256 mLastSyncDate = 0;
257 mKeepInSync = false;
258 mNeedsUpdating = false;
259 }
260
261 /**
262 * Sets the ID of the file
263 *
264 * @param file_id to set
265 */
266 public void setFileId(long file_id) {
267 mId = file_id;
268 }
269
270 /**
271 * Sets the Mime-Type of the
272 *
273 * @param mimetype to set
274 */
275 public void setMimetype(String mimetype) {
276 mMimeType = mimetype;
277 }
278
279 /**
280 * Sets the ID of the parent folder
281 *
282 * @param parent_id to set
283 */
284 public void setParentId(long parent_id) {
285 mParentId = parent_id;
286 }
287
288 /**
289 * Sets the file size in bytes
290 *
291 * @param file_len to set
292 */
293 public void setFileLength(long file_len) {
294 mLength = file_len;
295 }
296
297 /**
298 * Returns the size of the file in bytes
299 *
300 * @return The filesize in bytes
301 */
302 public long getFileLength() {
303 return mLength;
304 }
305
306 /**
307 * Returns the ID of the parent Folder
308 *
309 * @return The ID
310 */
311 public long getParentId() {
312 return mParentId;
313 }
314
315 /**
316 * Check, if this file needs updating
317 *
318 * @return
319 */
320 public boolean needsUpdatingWhileSaving() {
321 return mNeedsUpdating;
322 }
323
324 public long getLastSyncDate() {
325 return mLastSyncDate;
326 }
327
328 public void setLastSyncDate(long lastSyncDate) {
329 mLastSyncDate = lastSyncDate;
330 }
331
332 public void setKeepInSync(boolean keepInSync) {
333 mKeepInSync = keepInSync;
334 }
335
336 public boolean keepInSync() {
337 return mKeepInSync;
338 }
339
340 @Override
341 public int describeContents() {
342 return this.hashCode();
343 }
344
345 @Override
346 public int compareTo(OCFile another) {
347 if (isDirectory() && another.isDirectory()) {
348 return getRemotePath().toLowerCase().compareTo(another.getRemotePath().toLowerCase());
349 } else if (isDirectory()) {
350 return -1;
351 } else if (another.isDirectory()) {
352 return 1;
353 }
354 return getRemotePath().toLowerCase().compareTo(another.getRemotePath().toLowerCase());
355 }
356
357 public boolean equals(Object o) {
358 if(o instanceof OCFile){
359 OCFile that = (OCFile) o;
360 if(that != null){
361 return this.mId == that.mId;
362 }
363 }
364
365 return false;
366 }
367
368 @Override
369 public String toString() {
370 String asString = "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s]";
371 asString = String.format(asString, new Long(mId), getFileName(), mMimeType, isDownloaded(), mLocalPath, mRemotePath);
372 return asString;
373 }
374
375 }