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