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