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