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