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