2 * ownCloud Android client application
4 * Copyright (C) 2012 Bartek Przybylski
5 * Copyright (C) 2015 ownCloud Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 package com
.owncloud
.android
.datamodel
;
23 import android
.os
.Parcel
;
24 import android
.os
.Parcelable
;
25 import android
.webkit
.MimeTypeMap
;
27 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
31 import third_parties
.daveKoeller
.AlphanumComparator
;
32 public class OCFile
implements Parcelable
, Comparable
<OCFile
> {
34 public static final Parcelable
.Creator
<OCFile
> CREATOR
= new Parcelable
.Creator
<OCFile
>() {
36 public OCFile
createFromParcel(Parcel source
) {
37 return new OCFile(source
);
41 public OCFile
[] newArray(int size
) {
42 return new OCFile
[size
];
46 public static final String PATH_SEPARATOR
= "/";
47 public static final String ROOT_PATH
= PATH_SEPARATOR
;
49 private static final String TAG
= OCFile
.class.getSimpleName();
52 private long mParentId
;
54 private long mCreationTimestamp
;
55 private long mModifiedTimestamp
;
56 private long mModifiedTimestampAtLastSyncForData
;
57 private String mRemotePath
;
58 private String mLocalPath
;
59 private String mMimeType
;
60 private boolean mNeedsUpdating
;
61 private long mLastSyncDateForProperties
;
62 private long mLastSyncDateForData
;
63 private boolean mFavorite
;
67 private boolean mShareByLink
;
68 private String mPublicLink
;
70 private String mPermissions
;
71 private String mRemoteId
;
73 private boolean mNeedsUpdateThumbnail
;
75 private boolean mIsDownloading
;
77 private String mEtagInConflict
; // Save file etag in the server, when there is a conflict. No conflict = null
78 private boolean mShareWithUser
;
82 * Create new {@link OCFile} with given path.
84 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
86 * @param path The remote path of the file.
88 public OCFile(String path
) {
90 mNeedsUpdating
= false
;
91 if (path
== null
|| path
.length() <= 0 || !path
.startsWith(PATH_SEPARATOR
)) {
92 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path
);
98 * Reconstruct from parcel
100 * @param source The source parcel
102 private OCFile(Parcel source
) {
103 mId
= source
.readLong();
104 mParentId
= source
.readLong();
105 mLength
= source
.readLong();
106 mCreationTimestamp
= source
.readLong();
107 mModifiedTimestamp
= source
.readLong();
108 mModifiedTimestampAtLastSyncForData
= source
.readLong();
109 mRemotePath
= source
.readString();
110 mLocalPath
= source
.readString();
111 mMimeType
= source
.readString();
112 mNeedsUpdating
= source
.readInt() == 0;
113 mFavorite
= source
.readInt() == 1;
114 mLastSyncDateForProperties
= source
.readLong();
115 mLastSyncDateForData
= source
.readLong();
116 mEtag
= source
.readString();
117 mShareByLink
= source
.readInt() == 1;
118 mPublicLink
= source
.readString();
119 mPermissions
= source
.readString();
120 mRemoteId
= source
.readString();
121 mNeedsUpdateThumbnail
= source
.readInt() == 1;
122 mIsDownloading
= source
.readInt() == 1;
123 mEtagInConflict
= source
.readString();
124 mShareWithUser
= source
.readInt() == 1;
129 public void writeToParcel(Parcel dest
, int flags
) {
131 dest
.writeLong(mParentId
);
132 dest
.writeLong(mLength
);
133 dest
.writeLong(mCreationTimestamp
);
134 dest
.writeLong(mModifiedTimestamp
);
135 dest
.writeLong(mModifiedTimestampAtLastSyncForData
);
136 dest
.writeString(mRemotePath
);
137 dest
.writeString(mLocalPath
);
138 dest
.writeString(mMimeType
);
139 dest
.writeInt(mNeedsUpdating ?
1 : 0);
140 dest
.writeInt(mFavorite ?
1 : 0);
141 dest
.writeLong(mLastSyncDateForProperties
);
142 dest
.writeLong(mLastSyncDateForData
);
143 dest
.writeString(mEtag
);
144 dest
.writeInt(mShareByLink ?
1 : 0);
145 dest
.writeString(mPublicLink
);
146 dest
.writeString(mPermissions
);
147 dest
.writeString(mRemoteId
);
148 dest
.writeInt(mNeedsUpdateThumbnail ?
1 : 0);
149 dest
.writeInt(mIsDownloading ?
1 : 0);
150 dest
.writeString(mEtagInConflict
);
151 dest
.writeInt(mShareWithUser ?
1 : 0);
155 * Gets the ID of the file
157 * @return the file ID
159 public long getFileId() {
164 * Returns the remote path of the file on ownCloud
166 * @return The remote path to the file
168 public String
getRemotePath() {
173 * Can be used to check, whether or not this file exists in the database
176 * @return true, if the file exists in the database
178 public boolean fileExists() {
183 * Use this to find out if this file is a folder.
185 * @return true if it is a folder
187 public boolean isFolder() {
188 return mMimeType
!= null
&& mMimeType
.equals("DIR");
192 * Use this to check if this file is available locally
194 * @return true if it is
196 public boolean isDown() {
197 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
198 File file
= new File(mLocalPath
);
199 return (file
.exists());
205 * The path, where the file is stored locally
207 * @return The local path to the file
209 public String
getStoragePath() {
214 * Can be used to set the path where the file is stored
216 * @param storage_path to set
218 public void setStoragePath(String storage_path
) {
219 mLocalPath
= storage_path
;
223 * Get a UNIX timestamp of the file creation time
225 * @return A UNIX timestamp of the time that file was created
227 public long getCreationTimestamp() {
228 return mCreationTimestamp
;
232 * Set a UNIX timestamp of the time the file was created
234 * @param creation_timestamp to set
236 public void setCreationTimestamp(long creation_timestamp
) {
237 mCreationTimestamp
= creation_timestamp
;
241 * Get a UNIX timestamp of the file modification time.
243 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
244 * in the last synchronization of the properties of this file.
246 public long getModificationTimestamp() {
247 return mModifiedTimestamp
;
251 * Set a UNIX timestamp of the time the time the file was modified.
253 * To update with the value returned by the server in every synchronization of the properties
256 * @param modification_timestamp to set
258 public void setModificationTimestamp(long modification_timestamp
) {
259 mModifiedTimestamp
= modification_timestamp
;
264 * Get a UNIX timestamp of the file modification time.
266 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
267 * in the last synchronization of THE CONTENTS of this file.
269 public long getModificationTimestampAtLastSyncForData() {
270 return mModifiedTimestampAtLastSyncForData
;
274 * Set a UNIX timestamp of the time the time the file was modified.
276 * To update with the value returned by the server in every synchronization of THE CONTENTS
279 * @param modificationTimestamp to set
281 public void setModificationTimestampAtLastSyncForData(long modificationTimestamp
) {
282 mModifiedTimestampAtLastSyncForData
= modificationTimestamp
;
287 * Returns the filename and "/" for the root directory
289 * @return The name of the file
291 public String
getFileName() {
292 File f
= new File(getRemotePath());
293 return f
.getName().length() == 0 ? ROOT_PATH
: f
.getName();
297 * Sets the name of the file
299 * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root
302 public void setFileName(String name
) {
303 Log_OC
.d(TAG
, "OCFile name changin from " + mRemotePath
);
304 if (name
!= null
&& name
.length() > 0 && !name
.contains(PATH_SEPARATOR
) &&
305 !mRemotePath
.equals(ROOT_PATH
)) {
306 String parent
= (new File(getRemotePath())).getParent();
307 parent
= (parent
.endsWith(PATH_SEPARATOR
)) ? parent
: parent
+ PATH_SEPARATOR
;
308 mRemotePath
= parent
+ name
;
310 mRemotePath
+= PATH_SEPARATOR
;
312 Log_OC
.d(TAG
, "OCFile name changed to " + mRemotePath
);
317 * Can be used to get the Mimetype
319 * @return the Mimetype as a String
321 public String
getMimetype() {
326 * Used internally. Reset all file properties
328 private void resetData() {
335 mCreationTimestamp
= 0;
336 mModifiedTimestamp
= 0;
337 mModifiedTimestampAtLastSyncForData
= 0;
338 mLastSyncDateForProperties
= 0;
339 mLastSyncDateForData
= 0;
341 mNeedsUpdating
= false
;
343 mShareByLink
= false
;
347 mNeedsUpdateThumbnail
= false
;
348 mIsDownloading
= false
;
349 mEtagInConflict
= null
;
350 mShareWithUser
= false
;
354 * Sets the ID of the file
356 * @param file_id to set
358 public void setFileId(long file_id
) {
363 * Sets the Mime-Type of the
365 * @param mimetype to set
367 public void setMimetype(String mimetype
) {
368 mMimeType
= mimetype
;
372 * Sets the ID of the parent folder
374 * @param parent_id to set
376 public void setParentId(long parent_id
) {
377 mParentId
= parent_id
;
381 * Sets the file size in bytes
383 * @param file_len to set
385 public void setFileLength(long file_len
) {
390 * Returns the size of the file in bytes
392 * @return The filesize in bytes
394 public long getFileLength() {
399 * Returns the ID of the parent Folder
403 public long getParentId() {
408 * Check, if this file needs updating
412 public boolean needsUpdatingWhileSaving() {
413 return mNeedsUpdating
;
416 public boolean needsUpdateThumbnail() {
417 return mNeedsUpdateThumbnail
;
420 public void setNeedsUpdateThumbnail(boolean needsUpdateThumbnail
) {
421 this.mNeedsUpdateThumbnail
= needsUpdateThumbnail
;
424 public long getLastSyncDateForProperties() {
425 return mLastSyncDateForProperties
;
428 public void setLastSyncDateForProperties(long lastSyncDate
) {
429 mLastSyncDateForProperties
= lastSyncDate
;
432 public long getLastSyncDateForData() {
433 return mLastSyncDateForData
;
436 public void setLastSyncDateForData(long lastSyncDate
) {
437 mLastSyncDateForData
= lastSyncDate
;
440 public void setFavorite(boolean favorite
) {
441 mFavorite
= favorite
;
444 public boolean isFavorite() {
449 public int describeContents() {
450 return super.hashCode();
454 public int compareTo(OCFile another
) {
455 if (isFolder() && another
.isFolder()) {
456 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
457 } else if (isFolder()) {
459 } else if (another
.isFolder()) {
462 return new AlphanumComparator().compare(this, another
);
466 public boolean equals(Object o
) {
467 if (o
instanceof OCFile
) {
468 OCFile that
= (OCFile
) o
;
470 return this.mId
== that
.mId
;
478 public String
toString() {
479 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, " +
480 "parentId=%s, favorite=%s etag=%s]";
481 asString
= String
.format(asString
, Long
.valueOf(mId
), getFileName(), mMimeType
, isDown(),
482 mLocalPath
, mRemotePath
, Long
.valueOf(mParentId
), Boolean
.valueOf(mFavorite
),
487 public String
getEtag() {
491 public void setEtag(String etag
) {
492 this.mEtag
= (etag
!= null ? etag
: "");
495 public boolean isShareByLink() {
499 public void setShareByLink(boolean shareByLink
) {
500 this.mShareByLink
= shareByLink
;
503 public String
getPublicLink() {
507 public void setPublicLink(String publicLink
) {
508 this.mPublicLink
= publicLink
;
511 public long getLocalModificationTimestamp() {
512 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
513 File f
= new File(mLocalPath
);
514 return f
.lastModified();
520 * @return 'True' if the file contains audio
522 public boolean isAudio() {
523 return (mMimeType
!= null
&& mMimeType
.startsWith("audio/"));
527 * @return 'True' if the file contains video
529 public boolean isVideo() {
530 return (mMimeType
!= null
&& mMimeType
.startsWith("video/"));
534 * @return 'True' if the file contains an image
536 public boolean isImage() {
537 return ((mMimeType
!= null
&& mMimeType
.startsWith("image/")) ||
538 getMimeTypeFromName().startsWith("image/"));
542 * @return 'True' if the file is simple text (e.g. not application-dependent, like .doc or .docx)
544 public boolean isText() {
545 return ((mMimeType
!= null
&& mMimeType
.startsWith("text/")) ||
546 getMimeTypeFromName().startsWith("text/"));
549 public String
getMimeTypeFromName() {
550 String extension
= "";
551 int pos
= mRemotePath
.lastIndexOf('.');
553 extension
= mRemotePath
.substring(pos
+ 1);
555 String result
= MimeTypeMap
.getSingleton().
556 getMimeTypeFromExtension(extension
.toLowerCase());
557 return (result
!= null
) ? result
: "";
561 * @return 'True' if the file is hidden
563 public boolean isHidden() {
564 return getFileName().startsWith(".");
567 public String
getPermissions() {
571 public void setPermissions(String permissions
) {
572 this.mPermissions
= permissions
;
575 public String
getRemoteId() {
579 public void setRemoteId(String remoteId
) {
580 this.mRemoteId
= remoteId
;
583 public boolean isDownloading() {
584 return mIsDownloading
;
587 public void setDownloading(boolean isDownloading
) {
588 this.mIsDownloading
= isDownloading
;
591 public String
getEtagInConflict() {
592 return mEtagInConflict
;
595 public void setEtagInConflict(String etagInConflict
) {
596 mEtagInConflict
= etagInConflict
;
599 public boolean isShareWithUser() {
600 return mShareWithUser
;
603 public void setShareWithUser(boolean shareWithUser
) {
604 this.mShareWithUser
= shareWithUser
;