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