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