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