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
;
21 import android
.os
.Parcel
;
22 import android
.os
.Parcelable
;
23 import android
.webkit
.MimeTypeMap
;
25 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
29 import third_parties
.daveKoeller
.AlphanumComparator
;
30 public class OCFile
implements Parcelable
, Comparable
<OCFile
> {
32 public static final Parcelable
.Creator
<OCFile
> CREATOR
= new Parcelable
.Creator
<OCFile
>() {
34 public OCFile
createFromParcel(Parcel source
) {
35 return new OCFile(source
);
39 public OCFile
[] newArray(int size
) {
40 return new OCFile
[size
];
44 public static final String PATH_SEPARATOR
= "/";
45 public static final String ROOT_PATH
= PATH_SEPARATOR
;
47 private static final String TAG
= OCFile
.class.getSimpleName();
50 private long mParentId
;
52 private long mCreationTimestamp
;
53 private long mModifiedTimestamp
;
54 private long mModifiedTimestampAtLastSyncForData
;
55 private String mRemotePath
;
56 private String mLocalPath
;
57 private String mMimeType
;
58 private boolean mNeedsUpdating
;
59 private long mLastSyncDateForProperties
;
60 private long mLastSyncDateForData
;
61 private boolean mKeepInSync
;
65 private boolean mShareByLink
;
66 private String mPublicLink
;
68 private String mPermissions
;
69 private String mRemoteId
;
71 private boolean mNeedsUpdateThumbnail
;
75 * Create new {@link OCFile} with given path.
77 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
79 * @param path The remote path of the file.
81 public OCFile(String path
) {
83 mNeedsUpdating
= false
;
84 if (path
== null
|| path
.length() <= 0 || !path
.startsWith(PATH_SEPARATOR
)) {
85 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path
);
91 * Reconstruct from parcel
93 * @param source The source parcel
95 private OCFile(Parcel source
) {
96 mId
= source
.readLong();
97 mParentId
= source
.readLong();
98 mLength
= source
.readLong();
99 mCreationTimestamp
= source
.readLong();
100 mModifiedTimestamp
= source
.readLong();
101 mModifiedTimestampAtLastSyncForData
= source
.readLong();
102 mRemotePath
= source
.readString();
103 mLocalPath
= source
.readString();
104 mMimeType
= source
.readString();
105 mNeedsUpdating
= source
.readInt() == 0;
106 mKeepInSync
= source
.readInt() == 1;
107 mLastSyncDateForProperties
= source
.readLong();
108 mLastSyncDateForData
= source
.readLong();
109 mEtag
= source
.readString();
110 mShareByLink
= source
.readInt() == 1;
111 mPublicLink
= source
.readString();
112 mPermissions
= source
.readString();
113 mRemoteId
= source
.readString();
114 mNeedsUpdateThumbnail
= source
.readInt() == 0;
119 public void writeToParcel(Parcel dest
, int flags
) {
121 dest
.writeLong(mParentId
);
122 dest
.writeLong(mLength
);
123 dest
.writeLong(mCreationTimestamp
);
124 dest
.writeLong(mModifiedTimestamp
);
125 dest
.writeLong(mModifiedTimestampAtLastSyncForData
);
126 dest
.writeString(mRemotePath
);
127 dest
.writeString(mLocalPath
);
128 dest
.writeString(mMimeType
);
129 dest
.writeInt(mNeedsUpdating ?
1 : 0);
130 dest
.writeInt(mKeepInSync ?
1 : 0);
131 dest
.writeLong(mLastSyncDateForProperties
);
132 dest
.writeLong(mLastSyncDateForData
);
133 dest
.writeString(mEtag
);
134 dest
.writeInt(mShareByLink ?
1 : 0);
135 dest
.writeString(mPublicLink
);
136 dest
.writeString(mPermissions
);
137 dest
.writeString(mRemoteId
);
138 dest
.writeInt(mNeedsUpdateThumbnail ?
1 : 0);
142 * Gets the ID of the file
144 * @return the file ID
146 public long getFileId() {
151 * Returns the remote path of the file on ownCloud
153 * @return The remote path to the file
155 public String
getRemotePath() {
160 * Can be used to check, whether or not this file exists in the database
163 * @return true, if the file exists in the database
165 public boolean fileExists() {
170 * Use this to find out if this file is a folder.
172 * @return true if it is a folder
174 public boolean isFolder() {
175 return mMimeType
!= null
&& mMimeType
.equals("DIR");
179 * Use this to check if this file is available locally
181 * @return true if it is
183 public boolean isDown() {
184 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
185 File file
= new File(mLocalPath
);
186 return (file
.exists());
192 * The path, where the file is stored locally
194 * @return The local path to the file
196 public String
getStoragePath() {
201 * Can be used to set the path where the file is stored
203 * @param storage_path to set
205 public void setStoragePath(String storage_path
) {
206 mLocalPath
= storage_path
;
210 * Get a UNIX timestamp of the file creation time
212 * @return A UNIX timestamp of the time that file was created
214 public long getCreationTimestamp() {
215 return mCreationTimestamp
;
219 * Set a UNIX timestamp of the time the file was created
221 * @param creation_timestamp to set
223 public void setCreationTimestamp(long creation_timestamp
) {
224 mCreationTimestamp
= creation_timestamp
;
228 * Get a UNIX timestamp of the file modification time.
230 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
231 * in the last synchronization of the properties of this file.
233 public long getModificationTimestamp() {
234 return mModifiedTimestamp
;
238 * Set a UNIX timestamp of the time the time the file was modified.
240 * To update with the value returned by the server in every synchronization of the properties
243 * @param modification_timestamp to set
245 public void setModificationTimestamp(long modification_timestamp
) {
246 mModifiedTimestamp
= modification_timestamp
;
251 * Get a UNIX timestamp of the file modification time.
253 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
254 * in the last synchronization of THE CONTENTS of this file.
256 public long getModificationTimestampAtLastSyncForData() {
257 return mModifiedTimestampAtLastSyncForData
;
261 * Set a UNIX timestamp of the time the time the file was modified.
263 * To update with the value returned by the server in every synchronization of THE CONTENTS
266 * @param modificationTimestamp to set
268 public void setModificationTimestampAtLastSyncForData(long modificationTimestamp
) {
269 mModifiedTimestampAtLastSyncForData
= modificationTimestamp
;
274 * Returns the filename and "/" for the root directory
276 * @return The name of the file
278 public String
getFileName() {
279 File f
= new File(getRemotePath());
280 return f
.getName().length() == 0 ? ROOT_PATH
: f
.getName();
284 * Sets the name of the file
286 * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root directory
288 public void setFileName(String name
) {
289 Log_OC
.d(TAG
, "OCFile name changin from " + mRemotePath
);
290 if (name
!= null
&& name
.length() > 0 && !name
.contains(PATH_SEPARATOR
) && !mRemotePath
.equals(ROOT_PATH
)) {
291 String parent
= (new File(getRemotePath())).getParent();
292 parent
= (parent
.endsWith(PATH_SEPARATOR
)) ? parent
: parent
+ PATH_SEPARATOR
;
293 mRemotePath
= parent
+ name
;
295 mRemotePath
+= PATH_SEPARATOR
;
297 Log_OC
.d(TAG
, "OCFile name changed to " + mRemotePath
);
302 * Can be used to get the Mimetype
304 * @return the Mimetype as a String
306 public String
getMimetype() {
311 * Adds a file to this directory. If this file is not a directory, an
312 * exception gets thrown.
315 * @throws IllegalStateException if you try to add a something and this is
318 public void addFile(OCFile file
) throws IllegalStateException
{
320 file
.mParentId
= mId
;
321 mNeedsUpdating
= true
;
324 throw new IllegalStateException(
325 "This is not a directory where you can add stuff to!");
329 * Used internally. Reset all file properties
331 private void resetData() {
338 mCreationTimestamp
= 0;
339 mModifiedTimestamp
= 0;
340 mModifiedTimestampAtLastSyncForData
= 0;
341 mLastSyncDateForProperties
= 0;
342 mLastSyncDateForData
= 0;
344 mNeedsUpdating
= false
;
346 mShareByLink
= false
;
350 mNeedsUpdateThumbnail
= 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 setKeepInSync(boolean keepInSync
) {
441 mKeepInSync
= keepInSync
;
444 public boolean keepInSync() {
449 public int describeContents() {
450 return ((Object
) this).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, parentId=%s, keepInSync=%s etag=%s]";
480 asString
= String
.format(asString
, Long
.valueOf(mId
), getFileName(), mMimeType
, isDown(), mLocalPath
, mRemotePath
, Long
.valueOf(mParentId
), Boolean
.valueOf(mKeepInSync
), mEtag
);
484 public String
getEtag() {
488 public void setEtag(String etag
) {
493 public boolean isShareByLink() {
497 public void setShareByLink(boolean shareByLink
) {
498 this.mShareByLink
= shareByLink
;
501 public String
getPublicLink() {
505 public void setPublicLink(String publicLink
) {
506 this.mPublicLink
= publicLink
;
509 public long getLocalModificationTimestamp() {
510 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
511 File f
= new File(mLocalPath
);
512 return f
.lastModified();
518 * @return 'True' if the file contains audio
520 public boolean isAudio() {
521 return (mMimeType
!= null
&& mMimeType
.startsWith("audio/"));
525 * @return 'True' if the file contains video
527 public boolean isVideo() {
528 return (mMimeType
!= null
&& mMimeType
.startsWith("video/"));
532 * @return 'True' if the file contains an image
534 public boolean isImage() {
535 return ((mMimeType
!= null
&& mMimeType
.startsWith("image/")) ||
536 getMimeTypeFromName().startsWith("image/"));
539 public String
getMimeTypeFromName() {
540 String extension
= "";
541 int pos
= mRemotePath
.lastIndexOf('.');
543 extension
= mRemotePath
.substring(pos
+ 1);
545 String result
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(extension
.toLowerCase());
546 return (result
!= null
) ? result
: "";
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
;