70296d5fb0bd6833b8c64d81e1e0b6e7ed2cc1fe
[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 java.io.File;
22
23 import com.owncloud.android.lib.common.utils.Log_OC;
24 import third_parties.daveKoeller.AlphanumComparator;
25
26 import android.os.Parcel;
27 import android.os.Parcelable;
28 import android.webkit.MimeTypeMap;
29
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
74 /**
75 * Create new {@link OCFile} with given path.
76 *
77 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
78 *
79 * @param path The remote path of the file.
80 */
81 public OCFile(String path) {
82 resetData();
83 mNeedsUpdating = false;
84 if (path == null || path.length() <= 0 || !path.startsWith(PATH_SEPARATOR)) {
85 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path);
86 }
87 mRemotePath = path;
88 }
89
90 /**
91 * Reconstruct from parcel
92 *
93 * @param source The source parcel
94 */
95 private OCFile(Parcel source) {
96 mId = source.readLong();
97 mParentId = source.readLong();
98 mLength = source.readLong();
99 mCreationTimestamp = source.readLong();
100 mModifiedTimestamp = source.readLong();
101 mModifiedTimestampAtLastSyncForData = source.readLong();
102 mRemotePath = source.readString();
103 mLocalPath = source.readString();
104 mMimeType = source.readString();
105 mNeedsUpdating = source.readInt() == 0;
106 mKeepInSync = source.readInt() == 1;
107 mLastSyncDateForProperties = source.readLong();
108 mLastSyncDateForData = source.readLong();
109 mEtag = source.readString();
110 mShareByLink = source.readInt() == 1;
111 mPublicLink = source.readString();
112 mPermissions = source.readString();
113 mRemoteId = source.readString();
114 mNeedsUpdateThumbnail = source.readInt() == 0;
115
116 }
117
118 @Override
119 public void writeToParcel(Parcel dest, int flags) {
120 dest.writeLong(mId);
121 dest.writeLong(mParentId);
122 dest.writeLong(mLength);
123 dest.writeLong(mCreationTimestamp);
124 dest.writeLong(mModifiedTimestamp);
125 dest.writeLong(mModifiedTimestampAtLastSyncForData);
126 dest.writeString(mRemotePath);
127 dest.writeString(mLocalPath);
128 dest.writeString(mMimeType);
129 dest.writeInt(mNeedsUpdating ? 1 : 0);
130 dest.writeInt(mKeepInSync ? 1 : 0);
131 dest.writeLong(mLastSyncDateForProperties);
132 dest.writeLong(mLastSyncDateForData);
133 dest.writeString(mEtag);
134 dest.writeInt(mShareByLink ? 1 : 0);
135 dest.writeString(mPublicLink);
136 dest.writeString(mPermissions);
137 dest.writeString(mRemoteId);
138 dest.writeInt(mNeedsUpdateThumbnail ? 1 : 0);
139 }
140
141 /**
142 * Gets the ID of the file
143 *
144 * @return the file ID
145 */
146 public long getFileId() {
147 return mId;
148 }
149
150 /**
151 * Returns the remote path of the file on ownCloud
152 *
153 * @return The remote path to the file
154 */
155 public String getRemotePath() {
156 return mRemotePath;
157 }
158
159 /**
160 * Can be used to set the path where the file will be stored
161 *
162 * @param remote_path to set
163 */
164 public void setRemotePath(String remote_path) {
165 mRemotePath = remote_path;
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 *
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 *
272 * To update with the value returned by the server in every synchronization of THE CONTENTS
273 * of this file.
274 *
275 * @param modification_timestamp to set
276 */
277 public void setModificationTimestampAtLastSyncForData(long modificationTimestamp) {
278 mModifiedTimestampAtLastSyncForData = modificationTimestamp;
279 }
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 *
296 * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root 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) && !mRemotePath.equals(ROOT_PATH)) {
301 String parent = (new File(getRemotePath())).getParent();
302 parent = (parent.endsWith(PATH_SEPARATOR)) ? parent : parent + PATH_SEPARATOR;
303 mRemotePath = parent + name;
304 if (isFolder()) {
305 mRemotePath += PATH_SEPARATOR;
306 }
307 Log_OC.d(TAG, "OCFile name changed to " + mRemotePath);
308 }
309 }
310
311 /**
312 * Can be used to get the Mimetype
313 *
314 * @return the Mimetype as a String
315 */
316 public String getMimetype() {
317 return mMimeType;
318 }
319
320 /**
321 * Adds a file to this directory. If this file is not a directory, an
322 * exception gets thrown.
323 *
324 * @param file to add
325 * @throws IllegalStateException if you try to add a something and this is
326 * not a directory
327 */
328 public void addFile(OCFile file) throws IllegalStateException {
329 if (isFolder()) {
330 file.mParentId = mId;
331 mNeedsUpdating = true;
332 return;
333 }
334 throw new IllegalStateException(
335 "This is not a directory where you can add stuff to!");
336 }
337
338 /**
339 * Used internally. Reset all file properties
340 */
341 private void resetData() {
342 mId = -1;
343 mRemotePath = null;
344 mParentId = 0;
345 mLocalPath = null;
346 mMimeType = null;
347 mLength = 0;
348 mCreationTimestamp = 0;
349 mModifiedTimestamp = 0;
350 mModifiedTimestampAtLastSyncForData = 0;
351 mLastSyncDateForProperties = 0;
352 mLastSyncDateForData = 0;
353 mKeepInSync = false;
354 mNeedsUpdating = false;
355 mEtag = null;
356 mShareByLink = false;
357 mPublicLink = null;
358 mPermissions = null;
359 mRemoteId = null;
360 mNeedsUpdateThumbnail = false;
361 }
362
363 /**
364 * Sets the ID of the file
365 *
366 * @param file_id to set
367 */
368 public void setFileId(long file_id) {
369 mId = file_id;
370 }
371
372 /**
373 * Sets the Mime-Type of the
374 *
375 * @param mimetype to set
376 */
377 public void setMimetype(String mimetype) {
378 mMimeType = mimetype;
379 }
380
381 /**
382 * Sets the ID of the parent folder
383 *
384 * @param parent_id to set
385 */
386 public void setParentId(long parent_id) {
387 mParentId = parent_id;
388 }
389
390 /**
391 * Sets the file size in bytes
392 *
393 * @param file_len to set
394 */
395 public void setFileLength(long file_len) {
396 mLength = file_len;
397 }
398
399 /**
400 * Returns the size of the file in bytes
401 *
402 * @return The filesize in bytes
403 */
404 public long getFileLength() {
405 return mLength;
406 }
407
408 /**
409 * Returns the ID of the parent Folder
410 *
411 * @return The ID
412 */
413 public long getParentId() {
414 return mParentId;
415 }
416
417 /**
418 * Check, if this file needs updating
419 *
420 * @return
421 */
422 public boolean needsUpdatingWhileSaving() {
423 return mNeedsUpdating;
424 }
425
426 public boolean needsUpdateThumbnail() {
427 return mNeedsUpdateThumbnail;
428 }
429
430 public void setNeedsUpdateThumbnail(boolean needsUpdateThumbnail) {
431 this.mNeedsUpdateThumbnail = needsUpdateThumbnail;
432 }
433
434 public long getLastSyncDateForProperties() {
435 return mLastSyncDateForProperties;
436 }
437
438 public void setLastSyncDateForProperties(long lastSyncDate) {
439 mLastSyncDateForProperties = lastSyncDate;
440 }
441
442 public long getLastSyncDateForData() {
443 return mLastSyncDateForData;
444 }
445
446 public void setLastSyncDateForData(long lastSyncDate) {
447 mLastSyncDateForData = lastSyncDate;
448 }
449
450 public void setKeepInSync(boolean keepInSync) {
451 mKeepInSync = keepInSync;
452 }
453
454 public boolean keepInSync() {
455 return mKeepInSync;
456 }
457
458 @Override
459 public int describeContents() {
460 return this.hashCode();
461 }
462
463 @Override
464 public int compareTo(OCFile another) {
465 if (isFolder() && another.isFolder()) {
466 return getRemotePath().toLowerCase().compareTo(another.getRemotePath().toLowerCase());
467 } else if (isFolder()) {
468 return -1;
469 } else if (another.isFolder()) {
470 return 1;
471 }
472 return new AlphanumComparator().compare(this, another);
473 }
474
475 @Override
476 public boolean equals(Object o) {
477 if(o instanceof OCFile){
478 OCFile that = (OCFile) o;
479 if(that != null){
480 return this.mId == that.mId;
481 }
482 }
483
484 return false;
485 }
486
487 @Override
488 public String toString() {
489 String asString = "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSync=%s etag=%s]";
490 asString = String.format(asString, Long.valueOf(mId), getFileName(), mMimeType, isDown(), mLocalPath, mRemotePath, Long.valueOf(mParentId), Boolean.valueOf(mKeepInSync), mEtag);
491 return asString;
492 }
493
494 public String getEtag() {
495 return mEtag;
496 }
497
498 public void setEtag(String etag) {
499 this.mEtag = etag;
500 }
501
502
503 public boolean isShareByLink() {
504 return mShareByLink;
505 }
506
507 public void setShareByLink(boolean shareByLink) {
508 this.mShareByLink = shareByLink;
509 }
510
511 public String getPublicLink() {
512 return mPublicLink;
513 }
514
515 public void setPublicLink(String publicLink) {
516 this.mPublicLink = publicLink;
517 }
518
519 public long getLocalModificationTimestamp() {
520 if (mLocalPath != null && mLocalPath.length() > 0) {
521 File f = new File(mLocalPath);
522 return f.lastModified();
523 }
524 return 0;
525 }
526
527 /** @return 'True' if the file contains audio */
528 public boolean isAudio() {
529 return (mMimeType != null && mMimeType.startsWith("audio/"));
530 }
531
532 /** @return 'True' if the file contains video */
533 public boolean isVideo() {
534 return (mMimeType != null && mMimeType.startsWith("video/"));
535 }
536
537 /** @return 'True' if the file contains an image */
538 public boolean isImage() {
539 return ((mMimeType != null && mMimeType.startsWith("image/")) ||
540 getMimeTypeFromName().startsWith("image/"));
541 }
542
543 public String getMimeTypeFromName() {
544 String extension = "";
545 int pos = mRemotePath.lastIndexOf('.');
546 if (pos >= 0) {
547 extension = mRemotePath.substring(pos + 1);
548 }
549 String result = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase());
550 return (result != null) ? result : "";
551 }
552
553 public String getPermissions() {
554 return mPermissions;
555 }
556
557 public void setPermissions(String permissions) {
558 this.mPermissions = permissions;
559 }
560
561 public String getRemoteId() {
562 return mRemoteId;
563 }
564
565 public void setRemoteId(String remoteId) {
566 this.mRemoteId = remoteId;
567 }
568
569 }