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
.Log_OC
;
25 import android
.os
.Parcel
;
26 import android
.os
.Parcelable
;
27 import android
.webkit
.MimeTypeMap
;
29 public class OCFile
implements Parcelable
, Comparable
<OCFile
> {
31 public static final Parcelable
.Creator
<OCFile
> CREATOR
= new Parcelable
.Creator
<OCFile
>() {
33 public OCFile
createFromParcel(Parcel source
) {
34 return new OCFile(source
);
38 public OCFile
[] newArray(int size
) {
39 return new OCFile
[size
];
43 public static final String PATH_SEPARATOR
= "/";
45 private static final String TAG
= OCFile
.class.getSimpleName();
48 private long mParentId
;
50 private long mCreationTimestamp
;
51 private long mModifiedTimestamp
;
52 private long mModifiedTimestampAtLastSyncForData
;
53 private String mRemotePath
;
54 private String mLocalPath
;
55 private String mMimeType
;
56 private boolean mNeedsUpdating
;
57 private long mLastSyncDateForProperties
;
58 private long mLastSyncDateForData
;
59 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();
99 mEtag
= source
.readString();
103 public void writeToParcel(Parcel dest
, int flags
) {
105 dest
.writeLong(mParentId
);
106 dest
.writeLong(mLength
);
107 dest
.writeLong(mCreationTimestamp
);
108 dest
.writeLong(mModifiedTimestamp
);
109 dest
.writeLong(mModifiedTimestampAtLastSyncForData
);
110 dest
.writeString(mRemotePath
);
111 dest
.writeString(mLocalPath
);
112 dest
.writeString(mMimeType
);
113 dest
.writeInt(mNeedsUpdating ?
1 : 0);
114 dest
.writeInt(mKeepInSync ?
1 : 0);
115 dest
.writeLong(mLastSyncDateForProperties
);
116 dest
.writeLong(mLastSyncDateForData
);
117 dest
.writeString(mEtag
);
121 * Gets the ID of the file
123 * @return the file ID
125 public long getFileId() {
130 * Returns the remote path of the file on ownCloud
132 * @return The remote path to the file
134 public String
getRemotePath() {
139 * Can be used to check, whether or not this file exists in the database
142 * @return true, if the file exists in the database
144 public boolean fileExists() {
149 * Use this to find out if this file is a Directory
151 * @return true if it is a directory
153 public boolean isDirectory() {
154 return mMimeType
!= null
&& mMimeType
.equals("DIR");
158 * Use this to check if this file is available locally
160 * @return true if it is
162 public boolean isDown() {
163 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
164 File file
= new File(mLocalPath
);
165 return (file
.exists());
171 * The path, where the file is stored locally
173 * @return The local path to the file
175 public String
getStoragePath() {
180 * Can be used to set the path where the file is stored
182 * @param storage_path to set
184 public void setStoragePath(String storage_path
) {
185 mLocalPath
= storage_path
;
189 * Get a UNIX timestamp of the file creation time
191 * @return A UNIX timestamp of the time that file was created
193 public long getCreationTimestamp() {
194 return mCreationTimestamp
;
198 * Set a UNIX timestamp of the time the file was created
200 * @param creation_timestamp to set
202 public void setCreationTimestamp(long creation_timestamp
) {
203 mCreationTimestamp
= creation_timestamp
;
207 * Get a UNIX timestamp of the file modification time.
209 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
210 * in the last synchronization of the properties of this file.
212 public long getModificationTimestamp() {
213 return mModifiedTimestamp
;
217 * Set a UNIX timestamp of the time the time the file was modified.
219 * To update with the value returned by the server in every synchronization of the properties
222 * @param modification_timestamp to set
224 public void setModificationTimestamp(long modification_timestamp
) {
225 mModifiedTimestamp
= modification_timestamp
;
230 * Get a UNIX timestamp of the file modification time.
232 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
233 * in the last synchronization of THE CONTENTS of this file.
235 public long getModificationTimestampAtLastSyncForData() {
236 return mModifiedTimestampAtLastSyncForData
;
240 * Set a UNIX timestamp of the time the time the file was modified.
242 * To update with the value returned by the server in every synchronization of THE CONTENTS
245 * @param modification_timestamp to set
247 public void setModificationTimestampAtLastSyncForData(long modificationTimestamp
) {
248 mModifiedTimestampAtLastSyncForData
= modificationTimestamp
;
254 * Returns the filename and "/" for the root directory
256 * @return The name of the file
258 public String
getFileName() {
259 File f
= new File(getRemotePath());
260 return f
.getName().length() == 0 ? PATH_SEPARATOR
: f
.getName();
264 * Sets the name of the file
266 * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root directory
268 public void setFileName(String name
) {
269 Log_OC
.d(TAG
, "OCFile name changin from " + mRemotePath
);
270 if (name
!= null
&& name
.length() > 0 && !name
.contains(PATH_SEPARATOR
) && !mRemotePath
.equals(PATH_SEPARATOR
)) {
271 String parent
= (new File(getRemotePath())).getParent();
272 parent
= (parent
.endsWith(PATH_SEPARATOR
)) ? parent
: parent
+ PATH_SEPARATOR
;
273 mRemotePath
= parent
+ name
;
275 mRemotePath
+= PATH_SEPARATOR
;
277 Log_OC
.d(TAG
, "OCFile name changed to " + mRemotePath
);
282 * Can be used to get the Mimetype
284 * @return the Mimetype as a String
286 public String
getMimetype() {
291 * Adds a file to this directory. If this file is not a directory, an
292 * exception gets thrown.
295 * @throws IllegalStateException if you try to add a something and this is
298 public void addFile(OCFile file
) throws IllegalStateException
{
300 file
.mParentId
= mId
;
301 mNeedsUpdating
= true
;
304 throw new IllegalStateException(
305 "This is not a directory where you can add stuff to!");
309 * Used internally. Reset all file properties
311 private void resetData() {
318 mCreationTimestamp
= 0;
319 mModifiedTimestamp
= 0;
320 mModifiedTimestampAtLastSyncForData
= 0;
321 mLastSyncDateForProperties
= 0;
322 mLastSyncDateForData
= 0;
324 mNeedsUpdating
= false
;
329 * Sets the ID of the file
331 * @param file_id to set
333 public void setFileId(long file_id
) {
338 * Sets the Mime-Type of the
340 * @param mimetype to set
342 public void setMimetype(String mimetype
) {
343 mMimeType
= mimetype
;
347 * Sets the ID of the parent folder
349 * @param parent_id to set
351 public void setParentId(long parent_id
) {
352 mParentId
= parent_id
;
356 * Sets the file size in bytes
358 * @param file_len to set
360 public void setFileLength(long file_len
) {
365 * Returns the size of the file in bytes
367 * @return The filesize in bytes
369 public long getFileLength() {
374 * Returns the ID of the parent Folder
378 public long getParentId() {
383 * Check, if this file needs updating
387 public boolean needsUpdatingWhileSaving() {
388 return mNeedsUpdating
;
391 public long getLastSyncDateForProperties() {
392 return mLastSyncDateForProperties
;
395 public void setLastSyncDateForProperties(long lastSyncDate
) {
396 mLastSyncDateForProperties
= lastSyncDate
;
399 public long getLastSyncDateForData() {
400 return mLastSyncDateForData
;
403 public void setLastSyncDateForData(long lastSyncDate
) {
404 mLastSyncDateForData
= lastSyncDate
;
407 public void setKeepInSync(boolean keepInSync
) {
408 mKeepInSync
= keepInSync
;
411 public boolean keepInSync() {
416 public int describeContents() {
417 return this.hashCode();
421 public int compareTo(OCFile another
) {
422 if (isDirectory() && another
.isDirectory()) {
423 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
424 } else if (isDirectory()) {
426 } else if (another
.isDirectory()) {
429 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
433 public boolean equals(Object o
) {
434 if(o
instanceof OCFile
){
435 OCFile that
= (OCFile
) o
;
437 return this.mId
== that
.mId
;
445 public String
toString() {
446 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSinc=%s etag=%s]";
447 asString
= String
.format(asString
, Long
.valueOf(mId
), getFileName(), mMimeType
, isDown(), mLocalPath
, mRemotePath
, Long
.valueOf(mParentId
), Boolean
.valueOf(mKeepInSync
), mEtag
);
451 public String
getEtag() {
455 public void setEtag(String etag
) {
459 public long getLocalModificationTimestamp() {
460 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
461 File f
= new File(mLocalPath
);
462 return f
.lastModified();
467 /** @return 'True' if the file contains audio */
468 public boolean isAudio() {
469 return (mMimeType
!= null
&& mMimeType
.startsWith("audio/"));
472 /** @return 'True' if the file contains video */
473 public boolean isVideo() {
474 return (mMimeType
!= null
&& mMimeType
.startsWith("video/"));
477 /** @return 'True' if the file contains an image */
478 public boolean isImage() {
479 return ((mMimeType
!= null
&& mMimeType
.startsWith("image/")) ||
480 getMimeTypeFromName().startsWith("image/"));
483 public String
getMimeTypeFromName() {
484 String extension
= "";
485 int pos
= mRemotePath
.lastIndexOf('.');
487 extension
= mRemotePath
.substring(pos
+ 1);
489 String result
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(extension
.toLowerCase());
490 return (result
!= null
) ? result
: "";