cleanup
[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 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 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 /** @return 'True' if the file contains audio */
533 public boolean isAudio() {
534 return (mMimeType != null && mMimeType.startsWith("audio/"));
535 }
536
537 /** @return 'True' if the file contains video */
538 public boolean isVideo() {
539 return (mMimeType != null && mMimeType.startsWith("video/"));
540 }
541
542 /** @return 'True' if the file contains an image */
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 }