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