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 as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 package com
.owncloud
.android
.datamodel
;
24 import com
.owncloud
.android
.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
= "/";
46 private static final String TAG
= OCFile
.class.getSimpleName();
49 private long mParentId
;
51 private long mCreationTimestamp
;
52 private long mModifiedTimestamp
;
53 private long mModifiedTimestampAtLastSyncForData
;
54 private String mRemotePath
;
55 private String mLocalPath
;
56 private String mMimeType
;
57 private boolean mNeedsUpdating
;
58 private long mLastSyncDateForProperties
;
59 private long mLastSyncDateForData
;
60 private boolean mKeepInSync
;
65 * Create new {@link OCFile} with given path.
67 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
69 * @param path The remote path of the file.
71 public OCFile(String path
) {
73 mNeedsUpdating
= false
;
74 if (path
== null
|| path
.length() <= 0 || !path
.startsWith(PATH_SEPARATOR
)) {
75 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path
);
81 * Reconstruct from parcel
83 * @param source The source parcel
85 private OCFile(Parcel source
) {
86 mId
= source
.readLong();
87 mParentId
= source
.readLong();
88 mLength
= source
.readLong();
89 mCreationTimestamp
= source
.readLong();
90 mModifiedTimestamp
= source
.readLong();
91 mModifiedTimestampAtLastSyncForData
= source
.readLong();
92 mRemotePath
= source
.readString();
93 mLocalPath
= source
.readString();
94 mMimeType
= source
.readString();
95 mNeedsUpdating
= source
.readInt() == 0;
96 mKeepInSync
= source
.readInt() == 1;
97 mLastSyncDateForProperties
= source
.readLong();
98 mLastSyncDateForData
= source
.readLong();
102 public void writeToParcel(Parcel dest
, int flags
) {
104 dest
.writeLong(mParentId
);
105 dest
.writeLong(mLength
);
106 dest
.writeLong(mCreationTimestamp
);
107 dest
.writeLong(mModifiedTimestamp
);
108 dest
.writeLong(mModifiedTimestampAtLastSyncForData
);
109 dest
.writeString(mRemotePath
);
110 dest
.writeString(mLocalPath
);
111 dest
.writeString(mMimeType
);
112 dest
.writeInt(mNeedsUpdating ?
1 : 0);
113 dest
.writeInt(mKeepInSync ?
1 : 0);
114 dest
.writeLong(mLastSyncDateForProperties
);
115 dest
.writeLong(mLastSyncDateForData
);
119 * Gets the ID of the file
121 * @return the file ID
123 public long getFileId() {
128 * Returns the remote path of the file on ownCloud
130 * @return The remote path to the file
132 public String
getRemotePath() {
137 * Can be used to check, whether or not this file exists in the database
140 * @return true, if the file exists in the database
142 public boolean fileExists() {
147 * Use this to find out if this file is a Directory
149 * @return true if it is a directory
151 public boolean isDirectory() {
152 return mMimeType
!= null
&& mMimeType
.equals("DIR");
156 * Use this to check if this file is available locally
158 * @return true if it is
160 public boolean isDown() {
161 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
162 File file
= new File(mLocalPath
);
163 return (file
.exists());
169 * The path, where the file is stored locally
171 * @return The local path to the file
173 public String
getStoragePath() {
178 * Can be used to set the path where the file is stored
180 * @param storage_path to set
182 public void setStoragePath(String storage_path
) {
183 mLocalPath
= storage_path
;
187 * Get a UNIX timestamp of the file creation time
189 * @return A UNIX timestamp of the time that file was created
191 public long getCreationTimestamp() {
192 return mCreationTimestamp
;
196 * Set a UNIX timestamp of the time the file was created
198 * @param creation_timestamp to set
200 public void setCreationTimestamp(long creation_timestamp
) {
201 mCreationTimestamp
= creation_timestamp
;
205 * Get a UNIX timestamp of the file modification time.
207 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
208 * in the last synchronization of the properties of this file.
210 public long getModificationTimestamp() {
211 return mModifiedTimestamp
;
215 * Set a UNIX timestamp of the time the time the file was modified.
217 * To update with the value returned by the server in every synchronization of the properties
220 * @param modification_timestamp to set
222 public void setModificationTimestamp(long modification_timestamp
) {
223 mModifiedTimestamp
= modification_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 CONTENTS of this file.
233 public long getModificationTimestampAtLastSyncForData() {
234 return mModifiedTimestampAtLastSyncForData
;
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 CONTENTS
243 * @param modification_timestamp to set
245 public void setModificationTimestampAtLastSyncForData(long modificationTimestamp
) {
246 mModifiedTimestampAtLastSyncForData
= modificationTimestamp
;
252 * Returns the filename and "/" for the root directory
254 * @return The name of the file
256 public String
getFileName() {
257 File f
= new File(getRemotePath());
258 return f
.getName().length() == 0 ? PATH_SEPARATOR
: f
.getName();
262 * Sets the name of the file
264 * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root directory
266 public void setFileName(String name
) {
267 Log_OC
.d(TAG
, "OCFile name changin from " + mRemotePath
);
268 if (name
!= null
&& name
.length() > 0 && !name
.contains(PATH_SEPARATOR
) && !mRemotePath
.equals(PATH_SEPARATOR
)) {
269 String parent
= (new File(getRemotePath())).getParent();
270 parent
= (parent
.endsWith(PATH_SEPARATOR
)) ? parent
: parent
+ PATH_SEPARATOR
;
271 mRemotePath
= parent
+ name
;
273 mRemotePath
+= PATH_SEPARATOR
;
275 Log_OC
.d(TAG
, "OCFile name changed to " + mRemotePath
);
280 * Can be used to get the Mimetype
282 * @return the Mimetype as a String
284 public String
getMimetype() {
289 * Adds a file to this directory. If this file is not a directory, an
290 * exception gets thrown.
293 * @throws IllegalStateException if you try to add a something and this is
296 public void addFile(OCFile file
) throws IllegalStateException
{
298 file
.mParentId
= mId
;
299 mNeedsUpdating
= true
;
302 throw new IllegalStateException(
303 "This is not a directory where you can add stuff to!");
307 * Used internally. Reset all file properties
309 private void resetData() {
316 mCreationTimestamp
= 0;
317 mModifiedTimestamp
= 0;
318 mModifiedTimestampAtLastSyncForData
= 0;
319 mLastSyncDateForProperties
= 0;
320 mLastSyncDateForData
= 0;
322 mNeedsUpdating
= false
;
326 * Sets the ID of the file
328 * @param file_id to set
330 public void setFileId(long file_id
) {
335 * Sets the Mime-Type of the
337 * @param mimetype to set
339 public void setMimetype(String mimetype
) {
340 mMimeType
= mimetype
;
344 * Sets the ID of the parent folder
346 * @param parent_id to set
348 public void setParentId(long parent_id
) {
349 mParentId
= parent_id
;
353 * Sets the file size in bytes
355 * @param file_len to set
357 public void setFileLength(long file_len
) {
362 * Returns the size of the file in bytes
364 * @return The filesize in bytes
366 public long getFileLength() {
371 * Returns the ID of the parent Folder
375 public long getParentId() {
380 * Check, if this file needs updating
384 public boolean needsUpdatingWhileSaving() {
385 return mNeedsUpdating
;
388 public long getLastSyncDateForProperties() {
389 return mLastSyncDateForProperties
;
392 public void setLastSyncDateForProperties(long lastSyncDate
) {
393 mLastSyncDateForProperties
= lastSyncDate
;
396 public long getLastSyncDateForData() {
397 return mLastSyncDateForData
;
400 public void setLastSyncDateForData(long lastSyncDate
) {
401 mLastSyncDateForData
= lastSyncDate
;
404 public void setKeepInSync(boolean keepInSync
) {
405 mKeepInSync
= keepInSync
;
408 public boolean keepInSync() {
413 public int describeContents() {
414 return this.hashCode();
418 public int compareTo(OCFile another
) {
419 if (isDirectory() && another
.isDirectory()) {
420 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
421 } else if (isDirectory()) {
423 } else if (another
.isDirectory()) {
426 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
430 public boolean equals(Object o
) {
431 if(o
instanceof OCFile
){
432 OCFile that
= (OCFile
) o
;
434 return this.mId
== that
.mId
;
442 public String
toString() {
443 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSinc=%s]";
444 asString
= String
.format(asString
, Long
.valueOf(mId
), getFileName(), mMimeType
, isDown(), mLocalPath
, mRemotePath
, Long
.valueOf(mParentId
), Boolean
.valueOf(mKeepInSync
));
448 public String
getEtag() {
452 public long getLocalModificationTimestamp() {
453 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
454 File f
= new File(mLocalPath
);
455 return f
.lastModified();
460 /** @return 'True' if the file contains audio */
461 public boolean isAudio() {
462 return (mMimeType
!= null
&& mMimeType
.startsWith("audio/"));
465 /** @return 'True' if the file contains video */
466 public boolean isVideo() {
467 return (mMimeType
!= null
&& mMimeType
.startsWith("video/"));
470 /** @return 'True' if the file contains an image */
471 public boolean isImage() {
472 return ((mMimeType
!= null
&& mMimeType
.startsWith("image/")) ||
473 getMimeTypeFromName().startsWith("image/"));
476 public String
getMimeTypeFromName() {
477 String extension
= "";
478 int pos
= mRemotePath
.lastIndexOf('.');
480 extension
= mRemotePath
.substring(pos
+ 1);
482 String result
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(extension
.toLowerCase());
483 return (result
!= null
) ? result
: "";