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 boolean mInConflict
;
81 * Create new {@link OCFile} with given path.
83 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
85 * @param path The remote path of the file.
87 public OCFile(String path
) {
89 mNeedsUpdating
= false
;
90 if (path
== null
|| path
.length() <= 0 || !path
.startsWith(PATH_SEPARATOR
)) {
91 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path
);
97 * Reconstruct from parcel
99 * @param source The source parcel
101 private OCFile(Parcel source
) {
102 mId
= source
.readLong();
103 mParentId
= source
.readLong();
104 mLength
= source
.readLong();
105 mCreationTimestamp
= source
.readLong();
106 mModifiedTimestamp
= source
.readLong();
107 mModifiedTimestampAtLastSyncForData
= source
.readLong();
108 mRemotePath
= source
.readString();
109 mLocalPath
= source
.readString();
110 mMimeType
= source
.readString();
111 mNeedsUpdating
= source
.readInt() == 0;
112 mFavorite
= source
.readInt() == 1;
113 mLastSyncDateForProperties
= source
.readLong();
114 mLastSyncDateForData
= source
.readLong();
115 mEtag
= source
.readString();
116 mShareByLink
= source
.readInt() == 1;
117 mPublicLink
= source
.readString();
118 mPermissions
= source
.readString();
119 mRemoteId
= source
.readString();
120 mNeedsUpdateThumbnail
= source
.readInt() == 1;
121 mIsDownloading
= source
.readInt() == 1;
122 mInConflict
= source
.readInt() == 1;
127 public void writeToParcel(Parcel dest
, int flags
) {
129 dest
.writeLong(mParentId
);
130 dest
.writeLong(mLength
);
131 dest
.writeLong(mCreationTimestamp
);
132 dest
.writeLong(mModifiedTimestamp
);
133 dest
.writeLong(mModifiedTimestampAtLastSyncForData
);
134 dest
.writeString(mRemotePath
);
135 dest
.writeString(mLocalPath
);
136 dest
.writeString(mMimeType
);
137 dest
.writeInt(mNeedsUpdating ?
1 : 0);
138 dest
.writeInt(mFavorite ?
1 : 0);
139 dest
.writeLong(mLastSyncDateForProperties
);
140 dest
.writeLong(mLastSyncDateForData
);
141 dest
.writeString(mEtag
);
142 dest
.writeInt(mShareByLink ?
1 : 0);
143 dest
.writeString(mPublicLink
);
144 dest
.writeString(mPermissions
);
145 dest
.writeString(mRemoteId
);
146 dest
.writeInt(mNeedsUpdateThumbnail ?
1 : 0);
147 dest
.writeInt(mIsDownloading ?
1 : 0);
148 dest
.writeInt(mInConflict ?
1 : 0);
152 * Gets the ID of the file
154 * @return the file ID
156 public long getFileId() {
161 * Returns the remote path of the file on ownCloud
163 * @return The remote path to the file
165 public String
getRemotePath() {
170 * Can be used to check, whether or not this file exists in the database
173 * @return true, if the file exists in the database
175 public boolean fileExists() {
180 * Use this to find out if this file is a folder.
182 * @return true if it is a folder
184 public boolean isFolder() {
185 return mMimeType
!= null
&& mMimeType
.equals("DIR");
189 * Use this to check if this file is available locally
191 * @return true if it is
193 public boolean isDown() {
194 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
195 File file
= new File(mLocalPath
);
196 return (file
.exists());
202 * The path, where the file is stored locally
204 * @return The local path to the file
206 public String
getStoragePath() {
211 * Can be used to set the path where the file is stored
213 * @param storage_path to set
215 public void setStoragePath(String storage_path
) {
216 mLocalPath
= storage_path
;
220 * Get a UNIX timestamp of the file creation time
222 * @return A UNIX timestamp of the time that file was created
224 public long getCreationTimestamp() {
225 return mCreationTimestamp
;
229 * Set a UNIX timestamp of the time the file was created
231 * @param creation_timestamp to set
233 public void setCreationTimestamp(long creation_timestamp
) {
234 mCreationTimestamp
= creation_timestamp
;
238 * Get a UNIX timestamp of the file modification time.
240 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
241 * in the last synchronization of the properties of this file.
243 public long getModificationTimestamp() {
244 return mModifiedTimestamp
;
248 * Set a UNIX timestamp of the time the time the file was modified.
250 * To update with the value returned by the server in every synchronization of the properties
253 * @param modification_timestamp to set
255 public void setModificationTimestamp(long modification_timestamp
) {
256 mModifiedTimestamp
= modification_timestamp
;
261 * Get a UNIX timestamp of the file modification time.
263 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
264 * in the last synchronization of THE CONTENTS of this file.
266 public long getModificationTimestampAtLastSyncForData() {
267 return mModifiedTimestampAtLastSyncForData
;
271 * Set a UNIX timestamp of the time the time the file was modified.
273 * To update with the value returned by the server in every synchronization of THE CONTENTS
276 * @param modificationTimestamp to set
278 public void setModificationTimestampAtLastSyncForData(long modificationTimestamp
) {
279 mModifiedTimestampAtLastSyncForData
= modificationTimestamp
;
284 * Returns the filename and "/" for the root directory
286 * @return The name of the file
288 public String
getFileName() {
289 File f
= new File(getRemotePath());
290 return f
.getName().length() == 0 ? ROOT_PATH
: f
.getName();
294 * Sets the name of the file
296 * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root
299 public void setFileName(String name
) {
300 Log_OC
.d(TAG
, "OCFile name changin from " + mRemotePath
);
301 if (name
!= null
&& name
.length() > 0 && !name
.contains(PATH_SEPARATOR
) &&
302 !mRemotePath
.equals(ROOT_PATH
)) {
303 String parent
= (new File(getRemotePath())).getParent();
304 parent
= (parent
.endsWith(PATH_SEPARATOR
)) ? parent
: parent
+ PATH_SEPARATOR
;
305 mRemotePath
= parent
+ name
;
307 mRemotePath
+= PATH_SEPARATOR
;
309 Log_OC
.d(TAG
, "OCFile name changed to " + mRemotePath
);
314 * Can be used to get the Mimetype
316 * @return the Mimetype as a String
318 public String
getMimetype() {
323 * Used internally. Reset all file properties
325 private void resetData() {
332 mCreationTimestamp
= 0;
333 mModifiedTimestamp
= 0;
334 mModifiedTimestampAtLastSyncForData
= 0;
335 mLastSyncDateForProperties
= 0;
336 mLastSyncDateForData
= 0;
338 mNeedsUpdating
= false
;
340 mShareByLink
= false
;
344 mNeedsUpdateThumbnail
= false
;
345 mIsDownloading
= false
;
350 * Sets the ID of the file
352 * @param file_id to set
354 public void setFileId(long file_id
) {
359 * Sets the Mime-Type of the
361 * @param mimetype to set
363 public void setMimetype(String mimetype
) {
364 mMimeType
= mimetype
;
368 * Sets the ID of the parent folder
370 * @param parent_id to set
372 public void setParentId(long parent_id
) {
373 mParentId
= parent_id
;
377 * Sets the file size in bytes
379 * @param file_len to set
381 public void setFileLength(long file_len
) {
386 * Returns the size of the file in bytes
388 * @return The filesize in bytes
390 public long getFileLength() {
395 * Returns the ID of the parent Folder
399 public long getParentId() {
404 * Check, if this file needs updating
408 public boolean needsUpdatingWhileSaving() {
409 return mNeedsUpdating
;
412 public boolean needsUpdateThumbnail() {
413 return mNeedsUpdateThumbnail
;
416 public void setNeedsUpdateThumbnail(boolean needsUpdateThumbnail
) {
417 this.mNeedsUpdateThumbnail
= needsUpdateThumbnail
;
420 public long getLastSyncDateForProperties() {
421 return mLastSyncDateForProperties
;
424 public void setLastSyncDateForProperties(long lastSyncDate
) {
425 mLastSyncDateForProperties
= lastSyncDate
;
428 public long getLastSyncDateForData() {
429 return mLastSyncDateForData
;
432 public void setLastSyncDateForData(long lastSyncDate
) {
433 mLastSyncDateForData
= lastSyncDate
;
436 public void setFavorite(boolean favorite
) {
437 mFavorite
= favorite
;
440 public boolean isFavorite() {
445 public int describeContents() {
446 return super.hashCode();
450 public int compareTo(OCFile another
) {
451 if (isFolder() && another
.isFolder()) {
452 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
453 } else if (isFolder()) {
455 } else if (another
.isFolder()) {
458 return new AlphanumComparator().compare(this, another
);
462 public boolean equals(Object o
) {
463 if (o
instanceof OCFile
) {
464 OCFile that
= (OCFile
) o
;
466 return this.mId
== that
.mId
;
474 public String
toString() {
475 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, " +
476 "parentId=%s, favorite=%s etag=%s]";
477 asString
= String
.format(asString
, Long
.valueOf(mId
), getFileName(), mMimeType
, isDown(),
478 mLocalPath
, mRemotePath
, Long
.valueOf(mParentId
), Boolean
.valueOf(mFavorite
),
483 public String
getEtag() {
487 public void setEtag(String etag
) {
488 this.mEtag
= (etag
!= null ? etag
: "");
491 public boolean isShareByLink() {
495 public void setShareByLink(boolean shareByLink
) {
496 this.mShareByLink
= shareByLink
;
499 public String
getPublicLink() {
503 public void setPublicLink(String publicLink
) {
504 this.mPublicLink
= publicLink
;
507 public long getLocalModificationTimestamp() {
508 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
509 File f
= new File(mLocalPath
);
510 return f
.lastModified();
516 * @return 'True' if the file contains audio
518 public boolean isAudio() {
519 return (mMimeType
!= null
&& mMimeType
.startsWith("audio/"));
523 * @return 'True' if the file contains video
525 public boolean isVideo() {
526 return (mMimeType
!= null
&& mMimeType
.startsWith("video/"));
530 * @return 'True' if the file contains an image
532 public boolean isImage() {
533 return ((mMimeType
!= null
&& mMimeType
.startsWith("image/")) ||
534 getMimeTypeFromName().startsWith("image/"));
538 * @return 'True' if the file is simple text (e.g. not application-dependent, like .doc or .docx)
540 public boolean isText() {
541 return ((mMimeType
!= null
&& mMimeType
.startsWith("text/")) ||
542 getMimeTypeFromName().startsWith("text/"));
545 public String
getMimeTypeFromName() {
546 String extension
= "";
547 int pos
= mRemotePath
.lastIndexOf('.');
549 extension
= mRemotePath
.substring(pos
+ 1);
551 String result
= MimeTypeMap
.getSingleton().
552 getMimeTypeFromExtension(extension
.toLowerCase());
553 return (result
!= null
) ? result
: "";
557 * @return 'True' if the file is hidden
559 public boolean isHidden() {
560 return getFileName().startsWith(".");
563 public String
getPermissions() {
567 public void setPermissions(String permissions
) {
568 this.mPermissions
= permissions
;
571 public String
getRemoteId() {
575 public void setRemoteId(String remoteId
) {
576 this.mRemoteId
= remoteId
;
579 public boolean isDownloading() {
580 return mIsDownloading
;
583 public void setDownloading(boolean isDownloading
) {
584 this.mIsDownloading
= isDownloading
;
587 public boolean isInConflict() {
591 public void setInConflict(boolean inConflict
) {
592 mInConflict
= inConflict
;