70a51547c5979e94bc5abd26ae51e3165e17ec69
[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 com.owncloud.android.MainApp;
30
31 import third_parties.daveKoeller.AlphanumComparator;
32 import android.content.Intent;
33 import android.net.Uri;
34 public class OCFile implements Parcelable, Comparable<OCFile> {
35
36 public static final Parcelable.Creator<OCFile> CREATOR = new Parcelable.Creator<OCFile>() {
37 @Override
38 public OCFile createFromParcel(Parcel source) {
39 return new OCFile(source);
40 }
41
42 @Override
43 public OCFile[] newArray(int size) {
44 return new OCFile[size];
45 }
46 };
47
48 public static final String PATH_SEPARATOR = "/";
49 public static final String ROOT_PATH = PATH_SEPARATOR;
50
51 private static final String TAG = OCFile.class.getSimpleName();
52
53 private long mId;
54 private long mParentId;
55 private long mLength;
56 private long mCreationTimestamp;
57 private long mModifiedTimestamp;
58 private long mModifiedTimestampAtLastSyncForData;
59 private String mRemotePath;
60 private String mLocalPath;
61 private String mMimeType;
62 private boolean mNeedsUpdating;
63 private long mLastSyncDateForProperties;
64 private long mLastSyncDateForData;
65 private boolean mKeepInSync;
66
67 private String mEtag;
68
69 private boolean mShareByLink;
70 private String mPublicLink;
71
72 private String mPermissions;
73 private String mRemoteId;
74
75 private boolean mNeedsUpdateThumbnail;
76
77
78 /**
79 * Create new {@link OCFile} with given path.
80 * <p/>
81 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
82 *
83 * @param path The remote path of the file.
84 */
85 public OCFile(String path) {
86 resetData();
87 mNeedsUpdating = false;
88 if (path == null || path.length() <= 0 || !path.startsWith(PATH_SEPARATOR)) {
89 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path);
90 }
91 mRemotePath = path;
92 }
93
94 /**
95 * Reconstruct from parcel
96 *
97 * @param source The source parcel
98 */
99 private OCFile(Parcel source) {
100 mId = source.readLong();
101 mParentId = source.readLong();
102 mLength = source.readLong();
103 mCreationTimestamp = source.readLong();
104 mModifiedTimestamp = source.readLong();
105 mModifiedTimestampAtLastSyncForData = source.readLong();
106 mRemotePath = source.readString();
107 mLocalPath = source.readString();
108 mMimeType = source.readString();
109 mNeedsUpdating = source.readInt() == 0;
110 mKeepInSync = source.readInt() == 1;
111 mLastSyncDateForProperties = source.readLong();
112 mLastSyncDateForData = source.readLong();
113 mEtag = source.readString();
114 mShareByLink = source.readInt() == 1;
115 mPublicLink = source.readString();
116 mPermissions = source.readString();
117 mRemoteId = source.readString();
118 mNeedsUpdateThumbnail = source.readInt() == 0;
119
120 }
121
122 @Override
123 public void writeToParcel(Parcel dest, int flags) {
124 dest.writeLong(mId);
125 dest.writeLong(mParentId);
126 dest.writeLong(mLength);
127 dest.writeLong(mCreationTimestamp);
128 dest.writeLong(mModifiedTimestamp);
129 dest.writeLong(mModifiedTimestampAtLastSyncForData);
130 dest.writeString(mRemotePath);
131 dest.writeString(mLocalPath);
132 dest.writeString(mMimeType);
133 dest.writeInt(mNeedsUpdating ? 1 : 0);
134 dest.writeInt(mKeepInSync ? 1 : 0);
135 dest.writeLong(mLastSyncDateForProperties);
136 dest.writeLong(mLastSyncDateForData);
137 dest.writeString(mEtag);
138 dest.writeInt(mShareByLink ? 1 : 0);
139 dest.writeString(mPublicLink);
140 dest.writeString(mPermissions);
141 dest.writeString(mRemoteId);
142 dest.writeInt(mNeedsUpdateThumbnail ? 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 // Notify MediaScanner about removed file
304 Intent intent1 = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
305 intent1.setData(Uri.fromFile(new File(this.getStoragePath())));
306 MainApp.getAppContext().sendBroadcast(intent1);
307
308 // Notify MediaScanner about new file
309 Intent intent2 = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
310 String folder = new File(this.getStoragePath()).getParent();
311 intent2.setData(Uri.fromFile(new File(folder+ PATH_SEPARATOR+name)));
312 MainApp.getAppContext().sendBroadcast(intent2);
313 }
314 }
315
316 /**
317 * Can be used to get the Mimetype
318 *
319 * @return the Mimetype as a String
320 */
321 public String getMimetype() {
322 return mMimeType;
323 }
324
325 /**
326 * Adds a file to this directory. If this file is not a directory, an
327 * exception gets thrown.
328 *
329 * @param file to add
330 * @throws IllegalStateException if you try to add a something and this is
331 * not a directory
332 */
333 public void addFile(OCFile file) throws IllegalStateException {
334 if (isFolder()) {
335 file.mParentId = mId;
336 mNeedsUpdating = true;
337 return;
338 }
339 throw new IllegalStateException(
340 "This is not a directory where you can add stuff to!");
341 }
342
343 /**
344 * Used internally. Reset all file properties
345 */
346 private void resetData() {
347 mId = -1;
348 mRemotePath = null;
349 mParentId = 0;
350 mLocalPath = null;
351 mMimeType = null;
352 mLength = 0;
353 mCreationTimestamp = 0;
354 mModifiedTimestamp = 0;
355 mModifiedTimestampAtLastSyncForData = 0;
356 mLastSyncDateForProperties = 0;
357 mLastSyncDateForData = 0;
358 mKeepInSync = false;
359 mNeedsUpdating = false;
360 mEtag = null;
361 mShareByLink = false;
362 mPublicLink = null;
363 mPermissions = null;
364 mRemoteId = null;
365 mNeedsUpdateThumbnail = false;
366 }
367
368 /**
369 * Sets the ID of the file
370 *
371 * @param file_id to set
372 */
373 public void setFileId(long file_id) {
374 mId = file_id;
375 }
376
377 /**
378 * Sets the Mime-Type of the
379 *
380 * @param mimetype to set
381 */
382 public void setMimetype(String mimetype) {
383 mMimeType = mimetype;
384 }
385
386 /**
387 * Sets the ID of the parent folder
388 *
389 * @param parent_id to set
390 */
391 public void setParentId(long parent_id) {
392 mParentId = parent_id;
393 }
394
395 /**
396 * Sets the file size in bytes
397 *
398 * @param file_len to set
399 */
400 public void setFileLength(long file_len) {
401 mLength = file_len;
402 }
403
404 /**
405 * Returns the size of the file in bytes
406 *
407 * @return The filesize in bytes
408 */
409 public long getFileLength() {
410 return mLength;
411 }
412
413 /**
414 * Returns the ID of the parent Folder
415 *
416 * @return The ID
417 */
418 public long getParentId() {
419 return mParentId;
420 }
421
422 /**
423 * Check, if this file needs updating
424 *
425 * @return
426 */
427 public boolean needsUpdatingWhileSaving() {
428 return mNeedsUpdating;
429 }
430
431 public boolean needsUpdateThumbnail() {
432 return mNeedsUpdateThumbnail;
433 }
434
435 public void setNeedsUpdateThumbnail(boolean needsUpdateThumbnail) {
436 this.mNeedsUpdateThumbnail = needsUpdateThumbnail;
437 }
438
439 public long getLastSyncDateForProperties() {
440 return mLastSyncDateForProperties;
441 }
442
443 public void setLastSyncDateForProperties(long lastSyncDate) {
444 mLastSyncDateForProperties = lastSyncDate;
445 }
446
447 public long getLastSyncDateForData() {
448 return mLastSyncDateForData;
449 }
450
451 public void setLastSyncDateForData(long lastSyncDate) {
452 mLastSyncDateForData = lastSyncDate;
453 }
454
455 public void setKeepInSync(boolean keepInSync) {
456 mKeepInSync = keepInSync;
457 }
458
459 public boolean keepInSync() {
460 return mKeepInSync;
461 }
462
463 @Override
464 public int describeContents() {
465 return ((Object) this).hashCode();
466 }
467
468 @Override
469 public int compareTo(OCFile another) {
470 if (isFolder() && another.isFolder()) {
471 return getRemotePath().toLowerCase().compareTo(another.getRemotePath().toLowerCase());
472 } else if (isFolder()) {
473 return -1;
474 } else if (another.isFolder()) {
475 return 1;
476 }
477 return new AlphanumComparator().compare(this, another);
478 }
479
480 @Override
481 public boolean equals(Object o) {
482 if (o instanceof OCFile) {
483 OCFile that = (OCFile) o;
484 if (that != null) {
485 return this.mId == that.mId;
486 }
487 }
488
489 return false;
490 }
491
492 @Override
493 public String toString() {
494 String asString = "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSync=%s etag=%s]";
495 asString = String.format(asString, Long.valueOf(mId), getFileName(), mMimeType, isDown(), mLocalPath, mRemotePath, Long.valueOf(mParentId), Boolean.valueOf(mKeepInSync), mEtag);
496 return asString;
497 }
498
499 public String getEtag() {
500 return mEtag;
501 }
502
503 public void setEtag(String etag) {
504 this.mEtag = etag;
505 }
506
507
508 public boolean isShareByLink() {
509 return mShareByLink;
510 }
511
512 public void setShareByLink(boolean shareByLink) {
513 this.mShareByLink = shareByLink;
514 }
515
516 public String getPublicLink() {
517 return mPublicLink;
518 }
519
520 public void setPublicLink(String publicLink) {
521 this.mPublicLink = publicLink;
522 }
523
524 public long getLocalModificationTimestamp() {
525 if (mLocalPath != null && mLocalPath.length() > 0) {
526 File f = new File(mLocalPath);
527 return f.lastModified();
528 }
529 return 0;
530 }
531
532 /**
533 * @return 'True' if the file contains audio
534 */
535 public boolean isAudio() {
536 return (mMimeType != null && mMimeType.startsWith("audio/"));
537 }
538
539 /**
540 * @return 'True' if the file contains video
541 */
542 public boolean isVideo() {
543 return (mMimeType != null && mMimeType.startsWith("video/"));
544 }
545
546 /**
547 * @return 'True' if the file contains an image
548 */
549 public boolean isImage() {
550 return ((mMimeType != null && mMimeType.startsWith("image/")) ||
551 getMimeTypeFromName().startsWith("image/"));
552 }
553
554 public String getMimeTypeFromName() {
555 String extension = "";
556 int pos = mRemotePath.lastIndexOf('.');
557 if (pos >= 0) {
558 extension = mRemotePath.substring(pos + 1);
559 }
560 String result = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase());
561 return (result != null) ? result : "";
562 }
563
564 public String getPermissions() {
565 return mPermissions;
566 }
567
568 public void setPermissions(String permissions) {
569 this.mPermissions = permissions;
570 }
571
572 public String getRemoteId() {
573 return mRemoteId;
574 }
575
576 public void setRemoteId(String remoteId) {
577 this.mRemoteId = remoteId;
578 }
579
580 }