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