Merge remote-tracking branch 'origin/develop' into add_cookie_based_session_support
[pub/Android/ownCloud.git] / src / com / owncloud / android / datamodel / OCFile.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
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 com.owncloud.android.datamodel;
20
21 import java.io.File;
22
23 import com.owncloud.android.utils.Log_OC;
24
25
26 import android.os.Parcel;
27 import android.os.Parcelable;
28 import android.webkit.MimeTypeMap;
29
30 public class OCFile implements Parcelable, Comparable<OCFile> {
31
32 public static final Parcelable.Creator<OCFile> CREATOR = new Parcelable.Creator<OCFile>() {
33 @Override
34 public OCFile createFromParcel(Parcel source) {
35 return new OCFile(source);
36 }
37
38 @Override
39 public OCFile[] newArray(int size) {
40 return new OCFile[size];
41 }
42 };
43
44 public static final String PATH_SEPARATOR = "/";
45 public static final String ROOT_PATH = PATH_SEPARATOR;
46
47 private static final String TAG = OCFile.class.getSimpleName();
48
49 private long mId;
50 private long mParentId;
51 private long mLength;
52 private long mCreationTimestamp;
53 private long mModifiedTimestamp;
54 private long mModifiedTimestampAtLastSyncForData;
55 private String mRemotePath;
56 private String mLocalPath;
57 private String mMimeType;
58 private boolean mNeedsUpdating;
59 private long mLastSyncDateForProperties;
60 private long mLastSyncDateForData;
61 private boolean mKeepInSync;
62
63 private String mEtag;
64
65 private boolean mShareByLink;
66 private String mPublicLink;
67
68 private String mPermissions;
69 private String mRemoteId;
70
71
72 /**
73 * Create new {@link OCFile} with given path.
74 *
75 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
76 *
77 * @param path The remote path of the file.
78 */
79 public OCFile(String path) {
80 resetData();
81 mNeedsUpdating = false;
82 if (path == null || path.length() <= 0 || !path.startsWith(PATH_SEPARATOR)) {
83 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path);
84 }
85 mRemotePath = path;
86 }
87
88 /**
89 * Reconstruct from parcel
90 *
91 * @param source The source parcel
92 */
93 private OCFile(Parcel source) {
94 mId = source.readLong();
95 mParentId = source.readLong();
96 mLength = source.readLong();
97 mCreationTimestamp = source.readLong();
98 mModifiedTimestamp = source.readLong();
99 mModifiedTimestampAtLastSyncForData = source.readLong();
100 mRemotePath = source.readString();
101 mLocalPath = source.readString();
102 mMimeType = source.readString();
103 mNeedsUpdating = source.readInt() == 0;
104 mKeepInSync = source.readInt() == 1;
105 mLastSyncDateForProperties = source.readLong();
106 mLastSyncDateForData = source.readLong();
107 mEtag = source.readString();
108 mShareByLink = source.readInt() == 1;
109 mPublicLink = source.readString();
110 mPermissions = source.readString();
111 mRemoteId = source.readString();
112 }
113
114 @Override
115 public void writeToParcel(Parcel dest, int flags) {
116 dest.writeLong(mId);
117 dest.writeLong(mParentId);
118 dest.writeLong(mLength);
119 dest.writeLong(mCreationTimestamp);
120 dest.writeLong(mModifiedTimestamp);
121 dest.writeLong(mModifiedTimestampAtLastSyncForData);
122 dest.writeString(mRemotePath);
123 dest.writeString(mLocalPath);
124 dest.writeString(mMimeType);
125 dest.writeInt(mNeedsUpdating ? 1 : 0);
126 dest.writeInt(mKeepInSync ? 1 : 0);
127 dest.writeLong(mLastSyncDateForProperties);
128 dest.writeLong(mLastSyncDateForData);
129 dest.writeString(mEtag);
130 dest.writeInt(mShareByLink ? 1 : 0);
131 dest.writeString(mPublicLink);
132 dest.writeString(mPermissions);
133 dest.writeString(mRemoteId);
134 }
135
136 /**
137 * Gets the ID of the file
138 *
139 * @return the file ID
140 */
141 public long getFileId() {
142 return mId;
143 }
144
145 /**
146 * Returns the remote path of the file on ownCloud
147 *
148 * @return The remote path to the file
149 */
150 public String getRemotePath() {
151 return mRemotePath;
152 }
153
154 /**
155 * Can be used to check, whether or not this file exists in the database
156 * already
157 *
158 * @return true, if the file exists in the database
159 */
160 public boolean fileExists() {
161 return mId != -1;
162 }
163
164 /**
165 * Use this to find out if this file is a folder.
166 *
167 * @return true if it is a folder
168 */
169 public boolean isFolder() {
170 return mMimeType != null && mMimeType.equals("DIR");
171 }
172
173 /**
174 * Use this to check if this file is available locally
175 *
176 * @return true if it is
177 */
178 public boolean isDown() {
179 if (mLocalPath != null && mLocalPath.length() > 0) {
180 File file = new File(mLocalPath);
181 return (file.exists());
182 }
183 return false;
184 }
185
186 /**
187 * The path, where the file is stored locally
188 *
189 * @return The local path to the file
190 */
191 public String getStoragePath() {
192 return mLocalPath;
193 }
194
195 /**
196 * Can be used to set the path where the file is stored
197 *
198 * @param storage_path to set
199 */
200 public void setStoragePath(String storage_path) {
201 mLocalPath = storage_path;
202 }
203
204 /**
205 * Get a UNIX timestamp of the file creation time
206 *
207 * @return A UNIX timestamp of the time that file was created
208 */
209 public long getCreationTimestamp() {
210 return mCreationTimestamp;
211 }
212
213 /**
214 * Set a UNIX timestamp of the time the file was created
215 *
216 * @param creation_timestamp to set
217 */
218 public void setCreationTimestamp(long creation_timestamp) {
219 mCreationTimestamp = creation_timestamp;
220 }
221
222 /**
223 * Get a UNIX timestamp of the file modification time.
224 *
225 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
226 * in the last synchronization of the properties of this file.
227 */
228 public long getModificationTimestamp() {
229 return mModifiedTimestamp;
230 }
231
232 /**
233 * Set a UNIX timestamp of the time the time the file was modified.
234 *
235 * To update with the value returned by the server in every synchronization of the properties
236 * of this file.
237 *
238 * @param modification_timestamp to set
239 */
240 public void setModificationTimestamp(long modification_timestamp) {
241 mModifiedTimestamp = modification_timestamp;
242 }
243
244
245 /**
246 * Get a UNIX timestamp of the file modification time.
247 *
248 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
249 * in the last synchronization of THE CONTENTS of this file.
250 */
251 public long getModificationTimestampAtLastSyncForData() {
252 return mModifiedTimestampAtLastSyncForData;
253 }
254
255 /**
256 * Set a UNIX timestamp of the time the time the file was modified.
257 *
258 * To update with the value returned by the server in every synchronization of THE CONTENTS
259 * of this file.
260 *
261 * @param modification_timestamp to set
262 */
263 public void setModificationTimestampAtLastSyncForData(long modificationTimestamp) {
264 mModifiedTimestampAtLastSyncForData = modificationTimestamp;
265 }
266
267
268
269 /**
270 * Returns the filename and "/" for the root directory
271 *
272 * @return The name of the file
273 */
274 public String getFileName() {
275 File f = new File(getRemotePath());
276 return f.getName().length() == 0 ? ROOT_PATH : f.getName();
277 }
278
279 /**
280 * Sets the name of the file
281 *
282 * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root directory
283 */
284 public void setFileName(String name) {
285 Log_OC.d(TAG, "OCFile name changin from " + mRemotePath);
286 if (name != null && name.length() > 0 && !name.contains(PATH_SEPARATOR) && !mRemotePath.equals(ROOT_PATH)) {
287 String parent = (new File(getRemotePath())).getParent();
288 parent = (parent.endsWith(PATH_SEPARATOR)) ? parent : parent + PATH_SEPARATOR;
289 mRemotePath = parent + name;
290 if (isFolder()) {
291 mRemotePath += PATH_SEPARATOR;
292 }
293 Log_OC.d(TAG, "OCFile name changed to " + mRemotePath);
294 }
295 }
296
297 /**
298 * Can be used to get the Mimetype
299 *
300 * @return the Mimetype as a String
301 */
302 public String getMimetype() {
303 return mMimeType;
304 }
305
306 /**
307 * Adds a file to this directory. If this file is not a directory, an
308 * exception gets thrown.
309 *
310 * @param file to add
311 * @throws IllegalStateException if you try to add a something and this is
312 * not a directory
313 */
314 public void addFile(OCFile file) throws IllegalStateException {
315 if (isFolder()) {
316 file.mParentId = mId;
317 mNeedsUpdating = true;
318 return;
319 }
320 throw new IllegalStateException(
321 "This is not a directory where you can add stuff to!");
322 }
323
324 /**
325 * Used internally. Reset all file properties
326 */
327 private void resetData() {
328 mId = -1;
329 mRemotePath = null;
330 mParentId = 0;
331 mLocalPath = null;
332 mMimeType = null;
333 mLength = 0;
334 mCreationTimestamp = 0;
335 mModifiedTimestamp = 0;
336 mModifiedTimestampAtLastSyncForData = 0;
337 mLastSyncDateForProperties = 0;
338 mLastSyncDateForData = 0;
339 mKeepInSync = false;
340 mNeedsUpdating = false;
341 mEtag = null;
342 mShareByLink = false;
343 mPublicLink = null;
344 mPermissions = null;
345 mRemoteId = null;
346 }
347
348 /**
349 * Sets the ID of the file
350 *
351 * @param file_id to set
352 */
353 public void setFileId(long file_id) {
354 mId = file_id;
355 }
356
357 /**
358 * Sets the Mime-Type of the
359 *
360 * @param mimetype to set
361 */
362 public void setMimetype(String mimetype) {
363 mMimeType = mimetype;
364 }
365
366 /**
367 * Sets the ID of the parent folder
368 *
369 * @param parent_id to set
370 */
371 public void setParentId(long parent_id) {
372 mParentId = parent_id;
373 }
374
375 /**
376 * Sets the file size in bytes
377 *
378 * @param file_len to set
379 */
380 public void setFileLength(long file_len) {
381 mLength = file_len;
382 }
383
384 /**
385 * Returns the size of the file in bytes
386 *
387 * @return The filesize in bytes
388 */
389 public long getFileLength() {
390 return mLength;
391 }
392
393 /**
394 * Returns the ID of the parent Folder
395 *
396 * @return The ID
397 */
398 public long getParentId() {
399 return mParentId;
400 }
401
402 /**
403 * Check, if this file needs updating
404 *
405 * @return
406 */
407 public boolean needsUpdatingWhileSaving() {
408 return mNeedsUpdating;
409 }
410
411 public long getLastSyncDateForProperties() {
412 return mLastSyncDateForProperties;
413 }
414
415 public void setLastSyncDateForProperties(long lastSyncDate) {
416 mLastSyncDateForProperties = lastSyncDate;
417 }
418
419 public long getLastSyncDateForData() {
420 return mLastSyncDateForData;
421 }
422
423 public void setLastSyncDateForData(long lastSyncDate) {
424 mLastSyncDateForData = lastSyncDate;
425 }
426
427 public void setKeepInSync(boolean keepInSync) {
428 mKeepInSync = keepInSync;
429 }
430
431 public boolean keepInSync() {
432 return mKeepInSync;
433 }
434
435 @Override
436 public int describeContents() {
437 return this.hashCode();
438 }
439
440 @Override
441 public int compareTo(OCFile another) {
442 if (isFolder() && another.isFolder()) {
443 return getRemotePath().toLowerCase().compareTo(another.getRemotePath().toLowerCase());
444 } else if (isFolder()) {
445 return -1;
446 } else if (another.isFolder()) {
447 return 1;
448 }
449 return getRemotePath().toLowerCase().compareTo(another.getRemotePath().toLowerCase());
450 }
451
452 @Override
453 public boolean equals(Object o) {
454 if(o instanceof OCFile){
455 OCFile that = (OCFile) o;
456 if(that != null){
457 return this.mId == that.mId;
458 }
459 }
460
461 return false;
462 }
463
464 @Override
465 public String toString() {
466 String asString = "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSync=%s etag=%s]";
467 asString = String.format(asString, Long.valueOf(mId), getFileName(), mMimeType, isDown(), mLocalPath, mRemotePath, Long.valueOf(mParentId), Boolean.valueOf(mKeepInSync), mEtag);
468 return asString;
469 }
470
471 public String getEtag() {
472 return mEtag;
473 }
474
475 public void setEtag(String etag) {
476 this.mEtag = etag;
477 }
478
479
480 public boolean isShareByLink() {
481 return mShareByLink;
482 }
483
484 public void setShareByLink(boolean shareByLink) {
485 this.mShareByLink = shareByLink;
486 }
487
488 public String getPublicLink() {
489 return mPublicLink;
490 }
491
492 public void setPublicLink(String publicLink) {
493 this.mPublicLink = publicLink;
494 }
495
496 public long getLocalModificationTimestamp() {
497 if (mLocalPath != null && mLocalPath.length() > 0) {
498 File f = new File(mLocalPath);
499 return f.lastModified();
500 }
501 return 0;
502 }
503
504 /** @return 'True' if the file contains audio */
505 public boolean isAudio() {
506 return (mMimeType != null && mMimeType.startsWith("audio/"));
507 }
508
509 /** @return 'True' if the file contains video */
510 public boolean isVideo() {
511 return (mMimeType != null && mMimeType.startsWith("video/"));
512 }
513
514 /** @return 'True' if the file contains an image */
515 public boolean isImage() {
516 return ((mMimeType != null && mMimeType.startsWith("image/")) ||
517 getMimeTypeFromName().startsWith("image/"));
518 }
519
520 public String getMimeTypeFromName() {
521 String extension = "";
522 int pos = mRemotePath.lastIndexOf('.');
523 if (pos >= 0) {
524 extension = mRemotePath.substring(pos + 1);
525 }
526 String result = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase());
527 return (result != null) ? result : "";
528 }
529
530 public String getPermissions() {
531 return mPermissions;
532 }
533
534 public void setPermissions(String permissions) {
535 this.mPermissions = permissions;
536 }
537
538 public String getRemoteId() {
539 return mRemoteId;
540 }
541
542 public void setRemoteId(String remoteId) {
543 this.mRemoteId = remoteId;
544 }
545
546 }