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 com
.owncloud
.android
.utils
.Log_OC
;
26 import android
.os
.Parcel
;
27 import android
.os
.Parcelable
;
28 import android
.webkit
.MimeTypeMap
;
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
;
70 * Create new {@link OCFile} with given path.
72 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
74 * @param path The remote path of the file.
76 public OCFile(String path
) {
78 mNeedsUpdating
= false
;
79 if (path
== null
|| path
.length() <= 0 || !path
.startsWith(PATH_SEPARATOR
)) {
80 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path
);
86 * Reconstruct from parcel
88 * @param source The source parcel
90 private OCFile(Parcel source
) {
91 mId
= source
.readLong();
92 mParentId
= source
.readLong();
93 mLength
= source
.readLong();
94 mCreationTimestamp
= source
.readLong();
95 mModifiedTimestamp
= source
.readLong();
96 mModifiedTimestampAtLastSyncForData
= source
.readLong();
97 mRemotePath
= source
.readString();
98 mLocalPath
= source
.readString();
99 mMimeType
= source
.readString();
100 mNeedsUpdating
= source
.readInt() == 0;
101 mKeepInSync
= source
.readInt() == 1;
102 mLastSyncDateForProperties
= source
.readLong();
103 mLastSyncDateForData
= source
.readLong();
104 mEtag
= source
.readString();
105 mShareByLink
= source
.readInt() == 1;
106 mPublicLink
= source
.readString();
110 public void writeToParcel(Parcel dest
, int flags
) {
112 dest
.writeLong(mParentId
);
113 dest
.writeLong(mLength
);
114 dest
.writeLong(mCreationTimestamp
);
115 dest
.writeLong(mModifiedTimestamp
);
116 dest
.writeLong(mModifiedTimestampAtLastSyncForData
);
117 dest
.writeString(mRemotePath
);
118 dest
.writeString(mLocalPath
);
119 dest
.writeString(mMimeType
);
120 dest
.writeInt(mNeedsUpdating ?
1 : 0);
121 dest
.writeInt(mKeepInSync ?
1 : 0);
122 dest
.writeLong(mLastSyncDateForProperties
);
123 dest
.writeLong(mLastSyncDateForData
);
124 dest
.writeString(mEtag
);
125 dest
.writeInt(mShareByLink ?
1 : 0);
126 dest
.writeString(mPublicLink
);
130 * Gets the ID of the file
132 * @return the file ID
134 public long getFileId() {
139 * Returns the remote path of the file on ownCloud
141 * @return The remote path to the file
143 public String
getRemotePath() {
148 * Can be used to check, whether or not this file exists in the database
151 * @return true, if the file exists in the database
153 public boolean fileExists() {
158 * Use this to find out if this file is a folder.
160 * @return true if it is a folder
162 public boolean isFolder() {
163 return mMimeType
!= null
&& mMimeType
.equals("DIR");
167 * Use this to check if this file is available locally
169 * @return true if it is
171 public boolean isDown() {
172 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
173 File file
= new File(mLocalPath
);
174 return (file
.exists());
180 * The path, where the file is stored locally
182 * @return The local path to the file
184 public String
getStoragePath() {
189 * Can be used to set the path where the file is stored
191 * @param storage_path to set
193 public void setStoragePath(String storage_path
) {
194 mLocalPath
= storage_path
;
198 * Get a UNIX timestamp of the file creation time
200 * @return A UNIX timestamp of the time that file was created
202 public long getCreationTimestamp() {
203 return mCreationTimestamp
;
207 * Set a UNIX timestamp of the time the file was created
209 * @param creation_timestamp to set
211 public void setCreationTimestamp(long creation_timestamp
) {
212 mCreationTimestamp
= creation_timestamp
;
216 * Get a UNIX timestamp of the file modification time.
218 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
219 * in the last synchronization of the properties of this file.
221 public long getModificationTimestamp() {
222 return mModifiedTimestamp
;
226 * Set a UNIX timestamp of the time the time the file was modified.
228 * To update with the value returned by the server in every synchronization of the properties
231 * @param modification_timestamp to set
233 public void setModificationTimestamp(long modification_timestamp
) {
234 mModifiedTimestamp
= modification_timestamp
;
239 * Get a UNIX timestamp of the file modification time.
241 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
242 * in the last synchronization of THE CONTENTS of this file.
244 public long getModificationTimestampAtLastSyncForData() {
245 return mModifiedTimestampAtLastSyncForData
;
249 * Set a UNIX timestamp of the time the time the file was modified.
251 * To update with the value returned by the server in every synchronization of THE CONTENTS
254 * @param modification_timestamp to set
256 public void setModificationTimestampAtLastSyncForData(long modificationTimestamp
) {
257 mModifiedTimestampAtLastSyncForData
= modificationTimestamp
;
263 * Returns the filename and "/" for the root directory
265 * @return The name of the file
267 public String
getFileName() {
268 File f
= new File(getRemotePath());
269 return f
.getName().length() == 0 ? ROOT_PATH
: f
.getName();
273 * Sets the name of the file
275 * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root directory
277 public void setFileName(String name
) {
278 Log_OC
.d(TAG
, "OCFile name changin from " + mRemotePath
);
279 if (name
!= null
&& name
.length() > 0 && !name
.contains(PATH_SEPARATOR
) && !mRemotePath
.equals(ROOT_PATH
)) {
280 String parent
= (new File(getRemotePath())).getParent();
281 parent
= (parent
.endsWith(PATH_SEPARATOR
)) ? parent
: parent
+ PATH_SEPARATOR
;
282 mRemotePath
= parent
+ name
;
284 mRemotePath
+= PATH_SEPARATOR
;
286 Log_OC
.d(TAG
, "OCFile name changed to " + mRemotePath
);
291 * Can be used to get the Mimetype
293 * @return the Mimetype as a String
295 public String
getMimetype() {
300 * Adds a file to this directory. If this file is not a directory, an
301 * exception gets thrown.
304 * @throws IllegalStateException if you try to add a something and this is
307 public void addFile(OCFile file
) throws IllegalStateException
{
309 file
.mParentId
= mId
;
310 mNeedsUpdating
= true
;
313 throw new IllegalStateException(
314 "This is not a directory where you can add stuff to!");
318 * Used internally. Reset all file properties
320 private void resetData() {
327 mCreationTimestamp
= 0;
328 mModifiedTimestamp
= 0;
329 mModifiedTimestampAtLastSyncForData
= 0;
330 mLastSyncDateForProperties
= 0;
331 mLastSyncDateForData
= 0;
333 mNeedsUpdating
= false
;
335 mShareByLink
= false
;
340 * Sets the ID of the file
342 * @param file_id to set
344 public void setFileId(long file_id
) {
349 * Sets the Mime-Type of the
351 * @param mimetype to set
353 public void setMimetype(String mimetype
) {
354 mMimeType
= mimetype
;
358 * Sets the ID of the parent folder
360 * @param parent_id to set
362 public void setParentId(long parent_id
) {
363 mParentId
= parent_id
;
367 * Sets the file size in bytes
369 * @param file_len to set
371 public void setFileLength(long file_len
) {
376 * Returns the size of the file in bytes
378 * @return The filesize in bytes
380 public long getFileLength() {
385 * Returns the ID of the parent Folder
389 public long getParentId() {
394 * Check, if this file needs updating
398 public boolean needsUpdatingWhileSaving() {
399 return mNeedsUpdating
;
402 public long getLastSyncDateForProperties() {
403 return mLastSyncDateForProperties
;
406 public void setLastSyncDateForProperties(long lastSyncDate
) {
407 mLastSyncDateForProperties
= lastSyncDate
;
410 public long getLastSyncDateForData() {
411 return mLastSyncDateForData
;
414 public void setLastSyncDateForData(long lastSyncDate
) {
415 mLastSyncDateForData
= lastSyncDate
;
418 public void setKeepInSync(boolean keepInSync
) {
419 mKeepInSync
= keepInSync
;
422 public boolean keepInSync() {
427 public int describeContents() {
428 return this.hashCode();
432 public int compareTo(OCFile another
) {
433 if (isFolder() && another
.isFolder()) {
434 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
435 } else if (isFolder()) {
437 } else if (another
.isFolder()) {
440 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
444 public boolean equals(Object o
) {
445 if(o
instanceof OCFile
){
446 OCFile that
= (OCFile
) o
;
448 return this.mId
== that
.mId
;
456 public String
toString() {
457 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSync=%s etag=%s]";
458 asString
= String
.format(asString
, Long
.valueOf(mId
), getFileName(), mMimeType
, isDown(), mLocalPath
, mRemotePath
, Long
.valueOf(mParentId
), Boolean
.valueOf(mKeepInSync
), mEtag
);
462 public String
getEtag() {
466 public void setEtag(String etag
) {
471 public boolean isShareByLink() {
475 public void setShareByLink(boolean shareByLink
) {
476 this.mShareByLink
= shareByLink
;
479 public String
getPublicLink() {
483 public void setPublicLink(String publicLink
) {
484 this.mPublicLink
= publicLink
;
487 public long getLocalModificationTimestamp() {
488 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
489 File f
= new File(mLocalPath
);
490 return f
.lastModified();
495 /** @return 'True' if the file contains audio */
496 public boolean isAudio() {
497 return (mMimeType
!= null
&& mMimeType
.startsWith("audio/"));
500 /** @return 'True' if the file contains video */
501 public boolean isVideo() {
502 return (mMimeType
!= null
&& mMimeType
.startsWith("video/"));
505 /** @return 'True' if the file contains an image */
506 public boolean isImage() {
507 return ((mMimeType
!= null
&& mMimeType
.startsWith("image/")) ||
508 getMimeTypeFromName().startsWith("image/"));
511 public String
getMimeTypeFromName() {
512 String extension
= "";
513 int pos
= mRemotePath
.lastIndexOf('.');
515 extension
= mRemotePath
.substring(pos
+ 1);
517 String result
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(extension
.toLowerCase());
518 return (result
!= null
) ? result
: "";