1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
19 package com
.owncloud
.android
.datamodel
;
23 import third_parties
.daveKoeller
.AlphanumComparator
;
25 import com
.owncloud
.android
.utils
.Log_OC
;
27 import android
.os
.Parcel
;
28 import android
.os
.Parcelable
;
29 import android
.webkit
.MimeTypeMap
;
31 public class OCFile
implements Parcelable
, Comparable
<OCFile
> {
33 public static final Parcelable
.Creator
<OCFile
> CREATOR
= new Parcelable
.Creator
<OCFile
>() {
35 public OCFile
createFromParcel(Parcel source
) {
36 return new OCFile(source
);
40 public OCFile
[] newArray(int size
) {
41 return new OCFile
[size
];
45 public static final String PATH_SEPARATOR
= "/";
46 public static final String ROOT_PATH
= PATH_SEPARATOR
;
48 private static final String TAG
= OCFile
.class.getSimpleName();
51 private long mParentId
;
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
;
66 private boolean mShareByLink
;
67 private String mPublicLink
;
69 private String mPermissions
;
70 private String mRemoteId
;
74 * Create new {@link OCFile} with given path.
76 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
78 * @param path The remote path of the file.
80 public OCFile(String path
) {
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
);
90 * Reconstruct from parcel
92 * @param source The source parcel
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();
116 public void writeToParcel(Parcel dest
, int flags
) {
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
);
138 * Gets the ID of the file
140 * @return the file ID
142 public long getFileId() {
147 * Returns the remote path of the file on ownCloud
149 * @return The remote path to the file
151 public String
getRemotePath() {
156 * Can be used to check, whether or not this file exists in the database
159 * @return true, if the file exists in the database
161 public boolean fileExists() {
166 * Use this to find out if this file is a folder.
168 * @return true if it is a folder
170 public boolean isFolder() {
171 return mMimeType
!= null
&& mMimeType
.equals("DIR");
175 * Use this to check if this file is available locally
177 * @return true if it is
179 public boolean isDown() {
180 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
181 File file
= new File(mLocalPath
);
182 return (file
.exists());
188 * The path, where the file is stored locally
190 * @return The local path to the file
192 public String
getStoragePath() {
197 * Can be used to set the path where the file is stored
199 * @param storage_path to set
201 public void setStoragePath(String storage_path
) {
202 mLocalPath
= storage_path
;
206 * Get a UNIX timestamp of the file creation time
208 * @return A UNIX timestamp of the time that file was created
210 public long getCreationTimestamp() {
211 return mCreationTimestamp
;
215 * Set a UNIX timestamp of the time the file was created
217 * @param creation_timestamp to set
219 public void setCreationTimestamp(long creation_timestamp
) {
220 mCreationTimestamp
= creation_timestamp
;
224 * Get a UNIX timestamp of the file modification time.
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.
229 public long getModificationTimestamp() {
230 return mModifiedTimestamp
;
234 * Set a UNIX timestamp of the time the time the file was modified.
236 * To update with the value returned by the server in every synchronization of the properties
239 * @param modification_timestamp to set
241 public void setModificationTimestamp(long modification_timestamp
) {
242 mModifiedTimestamp
= modification_timestamp
;
247 * Get a UNIX timestamp of the file modification time.
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.
252 public long getModificationTimestampAtLastSyncForData() {
253 return mModifiedTimestampAtLastSyncForData
;
257 * Set a UNIX timestamp of the time the time the file was modified.
259 * To update with the value returned by the server in every synchronization of THE CONTENTS
262 * @param modification_timestamp to set
264 public void setModificationTimestampAtLastSyncForData(long modificationTimestamp
) {
265 mModifiedTimestampAtLastSyncForData
= modificationTimestamp
;
271 * Returns the filename and "/" for the root directory
273 * @return The name of the file
275 public String
getFileName() {
276 File f
= new File(getRemotePath());
277 return f
.getName().length() == 0 ? ROOT_PATH
: f
.getName();
281 * Sets the name of the file
283 * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root directory
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
;
292 mRemotePath
+= PATH_SEPARATOR
;
294 Log_OC
.d(TAG
, "OCFile name changed to " + mRemotePath
);
299 * Can be used to get the Mimetype
301 * @return the Mimetype as a String
303 public String
getMimetype() {
308 * Adds a file to this directory. If this file is not a directory, an
309 * exception gets thrown.
312 * @throws IllegalStateException if you try to add a something and this is
315 public void addFile(OCFile file
) throws IllegalStateException
{
317 file
.mParentId
= mId
;
318 mNeedsUpdating
= true
;
321 throw new IllegalStateException(
322 "This is not a directory where you can add stuff to!");
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
;
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 long getLastSyncDateForProperties() {
413 return mLastSyncDateForProperties
;
416 public void setLastSyncDateForProperties(long lastSyncDate
) {
417 mLastSyncDateForProperties
= lastSyncDate
;
420 public long getLastSyncDateForData() {
421 return mLastSyncDateForData
;
424 public void setLastSyncDateForData(long lastSyncDate
) {
425 mLastSyncDateForData
= lastSyncDate
;
428 public void setKeepInSync(boolean keepInSync
) {
429 mKeepInSync
= keepInSync
;
432 public boolean keepInSync() {
437 public int describeContents() {
438 return this.hashCode();
442 public int compareTo(OCFile another
) {
443 if (isFolder() && another
.isFolder()) {
444 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
445 } else if (isFolder()) {
447 } else if (another
.isFolder()) {
450 return new AlphanumComparator().compare(this, another
);
454 public boolean equals(Object o
) {
455 if(o
instanceof OCFile
){
456 OCFile that
= (OCFile
) o
;
458 return this.mId
== that
.mId
;
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
);
472 public String
getEtag() {
476 public void setEtag(String etag
) {
481 public boolean isShareByLink() {
485 public void setShareByLink(boolean shareByLink
) {
486 this.mShareByLink
= shareByLink
;
489 public String
getPublicLink() {
493 public void setPublicLink(String publicLink
) {
494 this.mPublicLink
= publicLink
;
497 public long getLocalModificationTimestamp() {
498 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
499 File f
= new File(mLocalPath
);
500 return f
.lastModified();
505 /** @return 'True' if the file contains audio */
506 public boolean isAudio() {
507 return (mMimeType
!= null
&& mMimeType
.startsWith("audio/"));
510 /** @return 'True' if the file contains video */
511 public boolean isVideo() {
512 return (mMimeType
!= null
&& mMimeType
.startsWith("video/"));
515 /** @return 'True' if the file contains an image */
516 public boolean isImage() {
517 return ((mMimeType
!= null
&& mMimeType
.startsWith("image/")) ||
518 getMimeTypeFromName().startsWith("image/"));
521 public String
getMimeTypeFromName() {
522 String extension
= "";
523 int pos
= mRemotePath
.lastIndexOf('.');
525 extension
= mRemotePath
.substring(pos
+ 1);
527 String result
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(extension
.toLowerCase());
528 return (result
!= null
) ? result
: "";
531 public String
getPermissions() {
535 public void setPermissions(String permissions
) {
536 this.mPermissions
= permissions
;
539 public String
getRemoteId() {
543 public void setRemoteId(String remoteId
) {
544 this.mRemoteId
= remoteId
;