1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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 android
.os
.Parcel
;
24 import android
.os
.Parcelable
;
25 import android
.util
.Log
;
27 public class OCFile
implements Parcelable
, Comparable
<OCFile
> {
29 public static final Parcelable
.Creator
<OCFile
> CREATOR
= new Parcelable
.Creator
<OCFile
>() {
31 public OCFile
createFromParcel(Parcel source
) {
32 return new OCFile(source
);
36 public OCFile
[] newArray(int size
) {
37 return new OCFile
[size
];
41 public static final String PATH_SEPARATOR
= "/";
43 private static final String TAG
= OCFile
.class.getSimpleName();
46 private long mParentId
;
48 private long mCreationTimestamp
;
49 private long mModifiedTimestamp
;
50 private long mModifiedTimestampAtLastSyncForData
;
51 private String mRemotePath
;
52 private String mLocalPath
;
53 private String mMimeType
;
54 private boolean mNeedsUpdating
;
55 private long mLastSyncDateForProperties
;
56 private long mLastSyncDateForData
;
57 private boolean mKeepInSync
;
62 * Create new {@link OCFile} with given path.
64 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
66 * @param path The remote path of the file.
68 public OCFile(String path
) {
70 mNeedsUpdating
= false
;
71 if (path
== null
|| path
.length() <= 0 || !path
.startsWith(PATH_SEPARATOR
)) {
72 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path
);
78 * Reconstruct from parcel
80 * @param source The source parcel
82 private OCFile(Parcel source
) {
83 mId
= source
.readLong();
84 mParentId
= source
.readLong();
85 mLength
= source
.readLong();
86 mCreationTimestamp
= source
.readLong();
87 mModifiedTimestamp
= source
.readLong();
88 mModifiedTimestampAtLastSyncForData
= source
.readLong();
89 mRemotePath
= source
.readString();
90 mLocalPath
= source
.readString();
91 mMimeType
= source
.readString();
92 mNeedsUpdating
= source
.readInt() == 0;
93 mKeepInSync
= source
.readInt() == 1;
94 mLastSyncDateForProperties
= source
.readLong();
95 mLastSyncDateForData
= source
.readLong();
99 public void writeToParcel(Parcel dest
, int flags
) {
101 dest
.writeLong(mParentId
);
102 dest
.writeLong(mLength
);
103 dest
.writeLong(mCreationTimestamp
);
104 dest
.writeLong(mModifiedTimestamp
);
105 dest
.writeLong(mModifiedTimestampAtLastSyncForData
);
106 dest
.writeString(mRemotePath
);
107 dest
.writeString(mLocalPath
);
108 dest
.writeString(mMimeType
);
109 dest
.writeInt(mNeedsUpdating ?
1 : 0);
110 dest
.writeInt(mKeepInSync ?
1 : 0);
111 dest
.writeLong(mLastSyncDateForProperties
);
112 dest
.writeLong(mLastSyncDateForData
);
116 * Gets the ID of the file
118 * @return the file ID
120 public long getFileId() {
125 * Returns the remote path of the file on ownCloud
127 * @return The remote path to the file
129 public String
getRemotePath() {
134 * Can be used to check, whether or not this file exists in the database
137 * @return true, if the file exists in the database
139 public boolean fileExists() {
144 * Use this to find out if this file is a Directory
146 * @return true if it is a directory
148 public boolean isDirectory() {
149 return mMimeType
!= null
&& mMimeType
.equals("DIR");
153 * Use this to check if this file is available locally
155 * @return true if it is
157 public boolean isDown() {
158 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
159 File file
= new File(mLocalPath
);
160 return (file
.exists());
166 * The path, where the file is stored locally
168 * @return The local path to the file
170 public String
getStoragePath() {
175 * Can be used to set the path where the file is stored
177 * @param storage_path to set
179 public void setStoragePath(String storage_path
) {
180 mLocalPath
= storage_path
;
184 * Get a UNIX timestamp of the file creation time
186 * @return A UNIX timestamp of the time that file was created
188 public long getCreationTimestamp() {
189 return mCreationTimestamp
;
193 * Set a UNIX timestamp of the time the file was created
195 * @param creation_timestamp to set
197 public void setCreationTimestamp(long creation_timestamp
) {
198 mCreationTimestamp
= creation_timestamp
;
202 * Get a UNIX timestamp of the file modification time.
204 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
205 * in the last synchronization of the properties of this file.
207 public long getModificationTimestamp() {
208 return mModifiedTimestamp
;
212 * Set a UNIX timestamp of the time the time the file was modified.
214 * To update with the value returned by the server in every synchronization of the properties
217 * @param modification_timestamp to set
219 public void setModificationTimestamp(long modification_timestamp
) {
220 mModifiedTimestamp
= modification_timestamp
;
225 * Get a UNIX timestamp of the file modification time.
227 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
228 * in the last synchronization of THE CONTENTS of this file.
230 public long getModificationTimestampAtLastSyncForData() {
231 return mModifiedTimestampAtLastSyncForData
;
235 * Set a UNIX timestamp of the time the time the file was modified.
237 * To update with the value returned by the server in every synchronization of THE CONTENTS
240 * @param modification_timestamp to set
242 public void setModificationTimestampAtLastSyncForData(long modificationTimestamp
) {
243 mModifiedTimestampAtLastSyncForData
= modificationTimestamp
;
249 * Returns the filename and "/" for the root directory
251 * @return The name of the file
253 public String
getFileName() {
254 File f
= new File(getRemotePath());
255 return f
.getName().length() == 0 ? PATH_SEPARATOR
: f
.getName();
259 * Sets the name of the file
261 * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root directory
263 public void setFileName(String name
) {
264 Log
.d(TAG
, "OCFile name changin from " + mRemotePath
);
265 if (name
!= null
&& name
.length() > 0 && !name
.contains(PATH_SEPARATOR
) && !mRemotePath
.equals(PATH_SEPARATOR
)) {
266 String parent
= (new File(getRemotePath())).getParent();
267 parent
= (parent
.endsWith(PATH_SEPARATOR
)) ? parent
: parent
+ PATH_SEPARATOR
;
268 mRemotePath
= parent
+ name
;
270 mRemotePath
+= PATH_SEPARATOR
;
272 Log
.d(TAG
, "OCFile name changed to " + mRemotePath
);
277 * Can be used to get the Mimetype
279 * @return the Mimetype as a String
281 public String
getMimetype() {
286 * Adds a file to this directory. If this file is not a directory, an
287 * exception gets thrown.
290 * @throws IllegalStateException if you try to add a something and this is
293 public void addFile(OCFile file
) throws IllegalStateException
{
295 file
.mParentId
= mId
;
296 mNeedsUpdating
= true
;
299 throw new IllegalStateException(
300 "This is not a directory where you can add stuff to!");
304 * Used internally. Reset all file properties
306 private void resetData() {
313 mCreationTimestamp
= 0;
314 mModifiedTimestamp
= 0;
315 mModifiedTimestampAtLastSyncForData
= 0;
316 mLastSyncDateForProperties
= 0;
317 mLastSyncDateForData
= 0;
319 mNeedsUpdating
= false
;
323 * Sets the ID of the file
325 * @param file_id to set
327 public void setFileId(long file_id
) {
332 * Sets the Mime-Type of the
334 * @param mimetype to set
336 public void setMimetype(String mimetype
) {
337 mMimeType
= mimetype
;
341 * Sets the ID of the parent folder
343 * @param parent_id to set
345 public void setParentId(long parent_id
) {
346 mParentId
= parent_id
;
350 * Sets the file size in bytes
352 * @param file_len to set
354 public void setFileLength(long file_len
) {
359 * Returns the size of the file in bytes
361 * @return The filesize in bytes
363 public long getFileLength() {
368 * Returns the ID of the parent Folder
372 public long getParentId() {
377 * Check, if this file needs updating
381 public boolean needsUpdatingWhileSaving() {
382 return mNeedsUpdating
;
385 public long getLastSyncDateForProperties() {
386 return mLastSyncDateForProperties
;
389 public void setLastSyncDateForProperties(long lastSyncDate
) {
390 mLastSyncDateForProperties
= lastSyncDate
;
393 public long getLastSyncDateForData() {
394 return mLastSyncDateForData
;
397 public void setLastSyncDateForData(long lastSyncDate
) {
398 mLastSyncDateForData
= lastSyncDate
;
401 public void setKeepInSync(boolean keepInSync
) {
402 mKeepInSync
= keepInSync
;
405 public boolean keepInSync() {
410 public int describeContents() {
411 return this.hashCode();
415 public int compareTo(OCFile another
) {
416 if (isDirectory() && another
.isDirectory()) {
417 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
418 } else if (isDirectory()) {
420 } else if (another
.isDirectory()) {
423 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
427 public boolean equals(Object o
) {
428 if(o
instanceof OCFile
){
429 OCFile that
= (OCFile
) o
;
431 return this.mId
== that
.mId
;
439 public String
toString() {
440 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSinc=%s]";
441 asString
= String
.format(asString
, Long
.valueOf(mId
), getFileName(), mMimeType
, isDown(), mLocalPath
, mRemotePath
, Long
.valueOf(mParentId
), Boolean
.valueOf(mKeepInSync
));
445 public String
getEtag() {
449 public long getLocalModificationTimestamp() {
450 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
451 File f
= new File(mLocalPath
);
452 return f
.lastModified();
457 public boolean isAudio() {
458 return (mMimeType
.startsWith("audio/"));