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