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
;
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 mKeepInSync
;
67 private boolean mShareByLink
;
68 private String mPublicLink
;
70 private String mPermissions
;
71 private String mRemoteId
;
73 private boolean mNeedsUpdateThumbnail
;
75 private boolean mIsDownloading
;
79 * Create new {@link OCFile} with given path.
81 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
83 * @param path The remote path of the file.
85 public OCFile(String path
) {
87 mNeedsUpdating
= false
;
88 if (path
== null
|| path
.length() <= 0 || !path
.startsWith(PATH_SEPARATOR
)) {
89 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path
);
95 * Reconstruct from parcel
97 * @param source The source parcel
99 private OCFile(Parcel source
) {
100 mId
= source
.readLong();
101 mParentId
= source
.readLong();
102 mLength
= source
.readLong();
103 mCreationTimestamp
= source
.readLong();
104 mModifiedTimestamp
= source
.readLong();
105 mModifiedTimestampAtLastSyncForData
= source
.readLong();
106 mRemotePath
= source
.readString();
107 mLocalPath
= source
.readString();
108 mMimeType
= source
.readString();
109 mNeedsUpdating
= source
.readInt() == 0;
110 mKeepInSync
= source
.readInt() == 1;
111 mLastSyncDateForProperties
= source
.readLong();
112 mLastSyncDateForData
= source
.readLong();
113 mEtag
= source
.readString();
114 mShareByLink
= source
.readInt() == 1;
115 mPublicLink
= source
.readString();
116 mPermissions
= source
.readString();
117 mRemoteId
= source
.readString();
118 mNeedsUpdateThumbnail
= source
.readInt() == 0;
119 mIsDownloading
= source
.readInt() == 0;
124 public void writeToParcel(Parcel dest
, int flags
) {
126 dest
.writeLong(mParentId
);
127 dest
.writeLong(mLength
);
128 dest
.writeLong(mCreationTimestamp
);
129 dest
.writeLong(mModifiedTimestamp
);
130 dest
.writeLong(mModifiedTimestampAtLastSyncForData
);
131 dest
.writeString(mRemotePath
);
132 dest
.writeString(mLocalPath
);
133 dest
.writeString(mMimeType
);
134 dest
.writeInt(mNeedsUpdating ?
1 : 0);
135 dest
.writeInt(mKeepInSync ?
1 : 0);
136 dest
.writeLong(mLastSyncDateForProperties
);
137 dest
.writeLong(mLastSyncDateForData
);
138 dest
.writeString(mEtag
);
139 dest
.writeInt(mShareByLink ?
1 : 0);
140 dest
.writeString(mPublicLink
);
141 dest
.writeString(mPermissions
);
142 dest
.writeString(mRemoteId
);
143 dest
.writeInt(mNeedsUpdateThumbnail ?
1 : 0);
144 dest
.writeInt(mIsDownloading ?
1 : 0);
148 * Gets the ID of the file
150 * @return the file ID
152 public long getFileId() {
157 * Returns the remote path of the file on ownCloud
159 * @return The remote path to the file
161 public String
getRemotePath() {
166 * Can be used to check, whether or not this file exists in the database
169 * @return true, if the file exists in the database
171 public boolean fileExists() {
176 * Use this to find out if this file is a folder.
178 * @return true if it is a folder
180 public boolean isFolder() {
181 return mMimeType
!= null
&& mMimeType
.equals("DIR");
185 * Use this to check if this file is available locally
187 * @return true if it is
189 public boolean isDown() {
190 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
191 File file
= new File(mLocalPath
);
192 return (file
.exists());
198 * The path, where the file is stored locally
200 * @return The local path to the file
202 public String
getStoragePath() {
207 * Can be used to set the path where the file is stored
209 * @param storage_path to set
211 public void setStoragePath(String storage_path
) {
212 mLocalPath
= storage_path
;
216 * Get a UNIX timestamp of the file creation time
218 * @return A UNIX timestamp of the time that file was created
220 public long getCreationTimestamp() {
221 return mCreationTimestamp
;
225 * Set a UNIX timestamp of the time the file was created
227 * @param creation_timestamp to set
229 public void setCreationTimestamp(long creation_timestamp
) {
230 mCreationTimestamp
= creation_timestamp
;
234 * Get a UNIX timestamp of the file modification time.
236 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
237 * in the last synchronization of the properties of this file.
239 public long getModificationTimestamp() {
240 return mModifiedTimestamp
;
244 * Set a UNIX timestamp of the time the time the file was modified.
246 * To update with the value returned by the server in every synchronization of the properties
249 * @param modification_timestamp to set
251 public void setModificationTimestamp(long modification_timestamp
) {
252 mModifiedTimestamp
= modification_timestamp
;
257 * Get a UNIX timestamp of the file modification time.
259 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
260 * in the last synchronization of THE CONTENTS of this file.
262 public long getModificationTimestampAtLastSyncForData() {
263 return mModifiedTimestampAtLastSyncForData
;
267 * Set a UNIX timestamp of the time the time the file was modified.
269 * To update with the value returned by the server in every synchronization of THE CONTENTS
272 * @param modificationTimestamp to set
274 public void setModificationTimestampAtLastSyncForData(long modificationTimestamp
) {
275 mModifiedTimestampAtLastSyncForData
= modificationTimestamp
;
280 * Returns the filename and "/" for the root directory
282 * @return The name of the file
284 public String
getFileName() {
285 File f
= new File(getRemotePath());
286 return f
.getName().length() == 0 ? ROOT_PATH
: f
.getName();
290 * Sets the name of the file
292 * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root directory
294 public void setFileName(String name
) {
295 Log_OC
.d(TAG
, "OCFile name changin from " + mRemotePath
);
296 if (name
!= null
&& name
.length() > 0 && !name
.contains(PATH_SEPARATOR
) && !mRemotePath
.equals(ROOT_PATH
)) {
297 String parent
= (new File(getRemotePath())).getParent();
298 parent
= (parent
.endsWith(PATH_SEPARATOR
)) ? parent
: parent
+ PATH_SEPARATOR
;
299 mRemotePath
= parent
+ name
;
301 mRemotePath
+= PATH_SEPARATOR
;
303 Log_OC
.d(TAG
, "OCFile name changed to " + mRemotePath
);
308 * Can be used to get the Mimetype
310 * @return the Mimetype as a String
312 public String
getMimetype() {
317 * Adds a file to this directory. If this file is not a directory, an
318 * exception gets thrown.
321 * @throws IllegalStateException if you try to add a something and this is
324 public void addFile(OCFile file
) throws IllegalStateException
{
326 file
.mParentId
= mId
;
327 mNeedsUpdating
= true
;
330 throw new IllegalStateException(
331 "This is not a directory where you can add stuff to!");
335 * Used internally. Reset all file properties
337 private void resetData() {
344 mCreationTimestamp
= 0;
345 mModifiedTimestamp
= 0;
346 mModifiedTimestampAtLastSyncForData
= 0;
347 mLastSyncDateForProperties
= 0;
348 mLastSyncDateForData
= 0;
350 mNeedsUpdating
= false
;
352 mShareByLink
= false
;
356 mNeedsUpdateThumbnail
= false
;
357 mIsDownloading
= false
;
361 * Sets the ID of the file
363 * @param file_id to set
365 public void setFileId(long file_id
) {
370 * Sets the Mime-Type of the
372 * @param mimetype to set
374 public void setMimetype(String mimetype
) {
375 mMimeType
= mimetype
;
379 * Sets the ID of the parent folder
381 * @param parent_id to set
383 public void setParentId(long parent_id
) {
384 mParentId
= parent_id
;
388 * Sets the file size in bytes
390 * @param file_len to set
392 public void setFileLength(long file_len
) {
397 * Returns the size of the file in bytes
399 * @return The filesize in bytes
401 public long getFileLength() {
406 * Returns the ID of the parent Folder
410 public long getParentId() {
415 * Check, if this file needs updating
419 public boolean needsUpdatingWhileSaving() {
420 return mNeedsUpdating
;
423 public boolean needsUpdateThumbnail() {
424 return mNeedsUpdateThumbnail
;
427 public void setNeedsUpdateThumbnail(boolean needsUpdateThumbnail
) {
428 this.mNeedsUpdateThumbnail
= needsUpdateThumbnail
;
431 public long getLastSyncDateForProperties() {
432 return mLastSyncDateForProperties
;
435 public void setLastSyncDateForProperties(long lastSyncDate
) {
436 mLastSyncDateForProperties
= lastSyncDate
;
439 public long getLastSyncDateForData() {
440 return mLastSyncDateForData
;
443 public void setLastSyncDateForData(long lastSyncDate
) {
444 mLastSyncDateForData
= lastSyncDate
;
447 public void setKeepInSync(boolean keepInSync
) {
448 mKeepInSync
= keepInSync
;
451 public boolean keepInSync() {
456 public int describeContents() {
457 return ((Object
) this).hashCode();
461 public int compareTo(OCFile another
) {
462 if (isFolder() && another
.isFolder()) {
463 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
464 } else if (isFolder()) {
466 } else if (another
.isFolder()) {
469 return new AlphanumComparator().compare(this, another
);
473 public boolean equals(Object o
) {
474 if (o
instanceof OCFile
) {
475 OCFile that
= (OCFile
) o
;
477 return this.mId
== that
.mId
;
485 public String
toString() {
486 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSync=%s etag=%s]";
487 asString
= String
.format(asString
, Long
.valueOf(mId
), getFileName(), mMimeType
, isDown(), mLocalPath
, mRemotePath
, Long
.valueOf(mParentId
), Boolean
.valueOf(mKeepInSync
), mEtag
);
491 public String
getEtag() {
495 public void setEtag(String etag
) {
500 public boolean isShareByLink() {
504 public void setShareByLink(boolean shareByLink
) {
505 this.mShareByLink
= shareByLink
;
508 public String
getPublicLink() {
512 public void setPublicLink(String publicLink
) {
513 this.mPublicLink
= publicLink
;
516 public long getLocalModificationTimestamp() {
517 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
518 File f
= new File(mLocalPath
);
519 return f
.lastModified();
525 * @return 'True' if the file contains audio
527 public boolean isAudio() {
528 return (mMimeType
!= null
&& mMimeType
.startsWith("audio/"));
532 * @return 'True' if the file contains video
534 public boolean isVideo() {
535 return (mMimeType
!= null
&& mMimeType
.startsWith("video/"));
539 * @return 'True' if the file contains an image
541 public boolean isImage() {
542 return ((mMimeType
!= null
&& mMimeType
.startsWith("image/")) ||
543 FileStorageUtils
.getMimeTypeFromName(mRemotePath
).startsWith("image/"));
546 public String
getPermissions() {
550 public void setPermissions(String permissions
) {
551 this.mPermissions
= permissions
;
554 public String
getRemoteId() {
558 public void setRemoteId(String remoteId
) {
559 this.mRemoteId
= remoteId
;
562 public boolean isDownloading() {
563 return mIsDownloading
;
566 public void setDownloading(boolean isDownloading
) {
567 this.mIsDownloading
= isDownloading
;
570 public boolean isSynchronizing() {
571 // TODO real implementation