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
;
26 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
27 import com
.owncloud
.android
.utils
.FileStorageUtils
;
30 import java
.util
.Enumeration
;
32 import third_parties
.daveKoeller
.AlphanumComparator
;
33 public class OCFile
implements Parcelable
, Comparable
<OCFile
> {
35 public static final Parcelable
.Creator
<OCFile
> CREATOR
= new Parcelable
.Creator
<OCFile
>() {
37 public OCFile
createFromParcel(Parcel source
) {
38 return new OCFile(source
);
42 public OCFile
[] newArray(int size
) {
43 return new OCFile
[size
];
47 public static final String PATH_SEPARATOR
= "/";
48 public static final String ROOT_PATH
= PATH_SEPARATOR
;
50 private static final String TAG
= OCFile
.class.getSimpleName();
53 private long mParentId
;
55 private long mCreationTimestamp
;
56 private long mModifiedTimestamp
;
57 private long mModifiedTimestampAtLastSyncForData
;
58 private String mRemotePath
;
59 private String mLocalPath
;
60 private String mMimeType
;
61 private boolean mNeedsUpdating
;
62 private long mLastSyncDateForProperties
;
63 private long mLastSyncDateForData
;
64 private boolean mFavorite
;
68 private boolean mShareByLink
;
69 private String mPublicLink
;
71 private String mPermissions
;
72 private String mRemoteId
;
74 private boolean mNeedsUpdateThumbnail
;
76 private boolean mIsDownloading
;
78 private boolean mShowGridView
;
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() == 0;
122 mIsDownloading
= source
.readInt() == 0;
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);
151 * Gets the ID of the file
153 * @return the file ID
155 public long getFileId() {
160 * Returns the remote path of the file on ownCloud
162 * @return The remote path to the file
164 public String
getRemotePath() {
169 * Can be used to check, whether or not this file exists in the database
172 * @return true, if the file exists in the database
174 public boolean fileExists() {
179 * Use this to find out if this file is a folder.
181 * @return true if it is a folder
183 public boolean isFolder() {
184 return mMimeType
!= null
&& mMimeType
.equals("DIR");
188 * Use this to check if this file is available locally
190 * @return true if it is
192 public boolean isDown() {
193 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
194 File file
= new File(mLocalPath
);
195 return (file
.exists());
201 * The path, where the file is stored locally
203 * @return The local path to the file
205 public String
getStoragePath() {
210 * Can be used to set the path where the file is stored
212 * @param storage_path to set
214 public void setStoragePath(String storage_path
) {
215 mLocalPath
= storage_path
;
219 * Get a UNIX timestamp of the file creation time
221 * @return A UNIX timestamp of the time that file was created
223 public long getCreationTimestamp() {
224 return mCreationTimestamp
;
228 * Set a UNIX timestamp of the time the file was created
230 * @param creation_timestamp to set
232 public void setCreationTimestamp(long creation_timestamp
) {
233 mCreationTimestamp
= creation_timestamp
;
237 * Get a UNIX timestamp of the file modification time.
239 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
240 * in the last synchronization of the properties of this file.
242 public long getModificationTimestamp() {
243 return mModifiedTimestamp
;
247 * Set a UNIX timestamp of the time the time the file was modified.
249 * To update with the value returned by the server in every synchronization of the properties
252 * @param modification_timestamp to set
254 public void setModificationTimestamp(long modification_timestamp
) {
255 mModifiedTimestamp
= modification_timestamp
;
260 * Get a UNIX timestamp of the file modification time.
262 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
263 * in the last synchronization of THE CONTENTS of this file.
265 public long getModificationTimestampAtLastSyncForData() {
266 return mModifiedTimestampAtLastSyncForData
;
270 * Set a UNIX timestamp of the time the time the file was modified.
272 * To update with the value returned by the server in every synchronization of THE CONTENTS
275 * @param modificationTimestamp to set
277 public void setModificationTimestampAtLastSyncForData(long modificationTimestamp
) {
278 mModifiedTimestampAtLastSyncForData
= modificationTimestamp
;
283 * Returns the filename and "/" for the root directory
285 * @return The name of the file
287 public String
getFileName() {
288 File f
= new File(getRemotePath());
289 return f
.getName().length() == 0 ? ROOT_PATH
: f
.getName();
293 * Sets the name of the file
295 * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root directory
297 public void setFileName(String name
) {
298 Log_OC
.d(TAG
, "OCFile name changin from " + mRemotePath
);
299 if (name
!= null
&& name
.length() > 0 && !name
.contains(PATH_SEPARATOR
) && !mRemotePath
.equals(ROOT_PATH
)) {
300 String parent
= (new File(getRemotePath())).getParent();
301 parent
= (parent
.endsWith(PATH_SEPARATOR
)) ? parent
: parent
+ PATH_SEPARATOR
;
302 mRemotePath
= parent
+ name
;
304 mRemotePath
+= PATH_SEPARATOR
;
306 Log_OC
.d(TAG
, "OCFile name changed to " + mRemotePath
);
311 * Can be used to get the Mimetype
313 * @return the Mimetype as a String
315 public String
getMimetype() {
320 * Adds a file to this directory. If this file is not a directory, an
321 * exception gets thrown.
324 * @throws IllegalStateException if you try to add a something and this is
327 public void addFile(OCFile file
) throws IllegalStateException
{
329 file
.mParentId
= mId
;
330 mNeedsUpdating
= true
;
333 throw new IllegalStateException(
334 "This is not a directory where you can add stuff to!");
338 * Used internally. Reset all file properties
340 private void resetData() {
347 mCreationTimestamp
= 0;
348 mModifiedTimestamp
= 0;
349 mModifiedTimestampAtLastSyncForData
= 0;
350 mLastSyncDateForProperties
= 0;
351 mLastSyncDateForData
= 0;
353 mNeedsUpdating
= false
;
355 mShareByLink
= false
;
359 mNeedsUpdateThumbnail
= false
;
360 mIsDownloading
= false
;
364 * Sets the ID of the file
366 * @param file_id to set
368 public void setFileId(long file_id
) {
373 * Sets the Mime-Type of the
375 * @param mimetype to set
377 public void setMimetype(String mimetype
) {
378 mMimeType
= mimetype
;
382 * Sets the ID of the parent folder
384 * @param parent_id to set
386 public void setParentId(long parent_id
) {
387 mParentId
= parent_id
;
391 * Sets the file size in bytes
393 * @param file_len to set
395 public void setFileLength(long file_len
) {
400 * Returns the size of the file in bytes
402 * @return The filesize in bytes
404 public long getFileLength() {
409 * Returns the ID of the parent Folder
413 public long getParentId() {
418 * Check, if this file needs updating
422 public boolean needsUpdatingWhileSaving() {
423 return mNeedsUpdating
;
426 public boolean needsUpdateThumbnail() {
427 return mNeedsUpdateThumbnail
;
430 public void setNeedsUpdateThumbnail(boolean needsUpdateThumbnail
) {
431 this.mNeedsUpdateThumbnail
= needsUpdateThumbnail
;
434 public long getLastSyncDateForProperties() {
435 return mLastSyncDateForProperties
;
438 public void setLastSyncDateForProperties(long lastSyncDate
) {
439 mLastSyncDateForProperties
= lastSyncDate
;
442 public long getLastSyncDateForData() {
443 return mLastSyncDateForData
;
446 public void setLastSyncDateForData(long lastSyncDate
) {
447 mLastSyncDateForData
= lastSyncDate
;
450 public void setFavorite(boolean favorite
) {
451 mFavorite
= favorite
;
454 public boolean isFavorite() {
459 public int describeContents() {
460 return ((Object
) this).hashCode();
464 public int compareTo(OCFile another
) {
465 if (isFolder() && another
.isFolder()) {
466 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
467 } else if (isFolder()) {
469 } else if (another
.isFolder()) {
472 return new AlphanumComparator().compare(this, another
);
476 public boolean equals(Object o
) {
477 if (o
instanceof OCFile
) {
478 OCFile that
= (OCFile
) o
;
480 return this.mId
== that
.mId
;
488 public String
toString() {
489 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, favorite=%s etag=%s]";
490 asString
= String
.format(asString
, Long
.valueOf(mId
), getFileName(), mMimeType
, isDown(), mLocalPath
, mRemotePath
, Long
.valueOf(mParentId
), Boolean
.valueOf(mFavorite
), mEtag
);
494 public String
getEtag() {
498 public void setEtag(String etag
) {
503 public boolean isShareByLink() {
507 public void setShareByLink(boolean shareByLink
) {
508 this.mShareByLink
= shareByLink
;
511 public String
getPublicLink() {
515 public void setPublicLink(String publicLink
) {
516 this.mPublicLink
= publicLink
;
519 public long getLocalModificationTimestamp() {
520 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
521 File f
= new File(mLocalPath
);
522 return f
.lastModified();
528 * @return 'True' if the file contains audio
530 public boolean isAudio() {
531 return (mMimeType
!= null
&& mMimeType
.startsWith("audio/"));
535 * @return 'True' if the file contains video
537 public boolean isVideo() {
538 return (mMimeType
!= null
&& mMimeType
.startsWith("video/"));
542 * @return 'True' if the file contains an image
544 public boolean isImage() {
545 return ((mMimeType
!= null
&& mMimeType
.startsWith("image/")) ||
546 FileStorageUtils
.getMimeTypeFromName(mRemotePath
).startsWith("image/"));
549 public String
getPermissions() {
553 public void setPermissions(String permissions
) {
554 this.mPermissions
= permissions
;
557 public String
getRemoteId() {
561 public void setRemoteId(String remoteId
) {
562 this.mRemoteId
= remoteId
;
565 public boolean isDownloading() {
566 return mIsDownloading
;
569 public void setDownloading(boolean isDownloading
) {
570 this.mIsDownloading
= isDownloading
;
573 public boolean isSynchronizing() {
574 // TODO real implementation