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 android
.os
.Parcel
;
25 import android
.os
.Parcelable
;
26 import android
.util
.Log
;
28 public class OCFile
implements Parcelable
, Comparable
<OCFile
> {
30 public static final Parcelable
.Creator
<OCFile
> CREATOR
= new Parcelable
.Creator
<OCFile
>() {
32 public OCFile
createFromParcel(Parcel source
) {
33 return new OCFile(source
);
37 public OCFile
[] newArray(int size
) {
38 return new OCFile
[size
];
42 public static final String PATH_SEPARATOR
= "/";
44 private static final String TAG
= OCFile
.class.getSimpleName();
47 private long mParentId
;
49 private long mCreationTimestamp
;
50 private long mModifiedTimestamp
;
51 private long mModifiedTimestampAtLastSyncForData
;
52 private String mRemotePath
;
53 private String mLocalPath
;
54 private String mMimeType
;
55 private boolean mNeedsUpdating
;
56 private long mLastSyncDateForProperties
;
57 private long mLastSyncDateForData
;
58 private boolean mKeepInSync
;
63 * Create new {@link OCFile} with given path.
65 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
67 * @param path The remote path of the file.
69 public OCFile(String path
) {
71 mNeedsUpdating
= false
;
72 if (path
== null
|| path
.length() <= 0 || !path
.startsWith(PATH_SEPARATOR
)) {
73 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path
);
79 * Reconstruct from parcel
81 * @param source The source parcel
83 private OCFile(Parcel source
) {
84 mId
= source
.readLong();
85 mParentId
= source
.readLong();
86 mLength
= source
.readLong();
87 mCreationTimestamp
= source
.readLong();
88 mModifiedTimestamp
= source
.readLong();
89 mModifiedTimestampAtLastSyncForData
= source
.readLong();
90 mRemotePath
= source
.readString();
91 mLocalPath
= source
.readString();
92 mMimeType
= source
.readString();
93 mNeedsUpdating
= source
.readInt() == 0;
94 mKeepInSync
= source
.readInt() == 1;
95 mLastSyncDateForProperties
= source
.readLong();
96 mLastSyncDateForData
= source
.readLong();
100 public void writeToParcel(Parcel dest
, int flags
) {
102 dest
.writeLong(mParentId
);
103 dest
.writeLong(mLength
);
104 dest
.writeLong(mCreationTimestamp
);
105 dest
.writeLong(mModifiedTimestamp
);
106 dest
.writeLong(mModifiedTimestampAtLastSyncForData
);
107 dest
.writeString(mRemotePath
);
108 dest
.writeString(mLocalPath
);
109 dest
.writeString(mMimeType
);
110 dest
.writeInt(mNeedsUpdating ?
1 : 0);
111 dest
.writeInt(mKeepInSync ?
1 : 0);
112 dest
.writeLong(mLastSyncDateForProperties
);
113 dest
.writeLong(mLastSyncDateForData
);
117 * Gets the ID of the file
119 * @return the file ID
121 public long getFileId() {
126 * Returns the remote path of the file on ownCloud
128 * @return The remote path to the file
130 public String
getRemotePath() {
135 * Can be used to check, whether or not this file exists in the database
138 * @return true, if the file exists in the database
140 public boolean fileExists() {
145 * Use this to find out if this file is a Directory
147 * @return true if it is a directory
149 public boolean isDirectory() {
150 return mMimeType
!= null
&& mMimeType
.equals("DIR");
154 * Use this to check if this file is available locally
156 * @return true if it is
158 public boolean isDown() {
159 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
160 File file
= new File(mLocalPath
);
161 return (file
.exists());
167 * The path, where the file is stored locally
169 * @return The local path to the file
171 public String
getStoragePath() {
176 * Can be used to set the path where the file is stored
178 * @param storage_path to set
180 public void setStoragePath(String storage_path
) {
181 mLocalPath
= storage_path
;
185 * Get a UNIX timestamp of the file creation time
187 * @return A UNIX timestamp of the time that file was created
189 public long getCreationTimestamp() {
190 return mCreationTimestamp
;
194 * Set a UNIX timestamp of the time the file was created
196 * @param creation_timestamp to set
198 public void setCreationTimestamp(long creation_timestamp
) {
199 mCreationTimestamp
= creation_timestamp
;
203 * Get a UNIX timestamp of the file modification time.
205 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
206 * in the last synchronization of the properties of this file.
208 public long getModificationTimestamp() {
209 return mModifiedTimestamp
;
213 * Set a UNIX timestamp of the time the time the file was modified.
215 * To update with the value returned by the server in every synchronization of the properties
218 * @param modification_timestamp to set
220 public void setModificationTimestamp(long modification_timestamp
) {
221 mModifiedTimestamp
= modification_timestamp
;
226 * Get a UNIX timestamp of the file modification time.
228 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
229 * in the last synchronization of THE CONTENTS of this file.
231 public long getModificationTimestampAtLastSyncForData() {
232 return mModifiedTimestampAtLastSyncForData
;
236 * Set a UNIX timestamp of the time the time the file was modified.
238 * To update with the value returned by the server in every synchronization of THE CONTENTS
241 * @param modification_timestamp to set
243 public void setModificationTimestampAtLastSyncForData(long modificationTimestamp
) {
244 mModifiedTimestampAtLastSyncForData
= modificationTimestamp
;
250 * Returns the filename and "/" for the root directory
252 * @return The name of the file
254 public String
getFileName() {
255 File f
= new File(getRemotePath());
256 return f
.getName().length() == 0 ? PATH_SEPARATOR
: f
.getName();
260 * Sets the name of the file
262 * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root directory
264 public void setFileName(String name
) {
265 Log
.d(TAG
, "OCFile name changin from " + mRemotePath
);
266 if (name
!= null
&& name
.length() > 0 && !name
.contains(PATH_SEPARATOR
) && !mRemotePath
.equals(PATH_SEPARATOR
)) {
267 String parent
= (new File(getRemotePath())).getParent();
268 parent
= (parent
.endsWith(PATH_SEPARATOR
)) ? parent
: parent
+ PATH_SEPARATOR
;
269 mRemotePath
= parent
+ name
;
271 mRemotePath
+= PATH_SEPARATOR
;
273 Log
.d(TAG
, "OCFile name changed to " + mRemotePath
);
278 * Can be used to get the Mimetype
280 * @return the Mimetype as a String
282 public String
getMimetype() {
287 * Adds a file to this directory. If this file is not a directory, an
288 * exception gets thrown.
291 * @throws IllegalStateException if you try to add a something and this is
294 public void addFile(OCFile file
) throws IllegalStateException
{
296 file
.mParentId
= mId
;
297 mNeedsUpdating
= true
;
300 throw new IllegalStateException(
301 "This is not a directory where you can add stuff to!");
305 * Used internally. Reset all file properties
307 private void resetData() {
314 mCreationTimestamp
= 0;
315 mModifiedTimestamp
= 0;
316 mModifiedTimestampAtLastSyncForData
= 0;
317 mLastSyncDateForProperties
= 0;
318 mLastSyncDateForData
= 0;
320 mNeedsUpdating
= false
;
324 * Sets the ID of the file
326 * @param file_id to set
328 public void setFileId(long file_id
) {
333 * Sets the Mime-Type of the
335 * @param mimetype to set
337 public void setMimetype(String mimetype
) {
338 mMimeType
= mimetype
;
342 * Sets the ID of the parent folder
344 * @param parent_id to set
346 public void setParentId(long parent_id
) {
347 mParentId
= parent_id
;
351 * Sets the file size in bytes
353 * @param file_len to set
355 public void setFileLength(long file_len
) {
360 * Returns the size of the file in bytes
362 * @return The filesize in bytes
364 public long getFileLength() {
369 * Returns the ID of the parent Folder
373 public long getParentId() {
378 * Check, if this file needs updating
382 public boolean needsUpdatingWhileSaving() {
383 return mNeedsUpdating
;
386 public long getLastSyncDateForProperties() {
387 return mLastSyncDateForProperties
;
390 public void setLastSyncDateForProperties(long lastSyncDate
) {
391 mLastSyncDateForProperties
= lastSyncDate
;
394 public long getLastSyncDateForData() {
395 return mLastSyncDateForData
;
398 public void setLastSyncDateForData(long lastSyncDate
) {
399 mLastSyncDateForData
= lastSyncDate
;
402 public void setKeepInSync(boolean keepInSync
) {
403 mKeepInSync
= keepInSync
;
406 public boolean keepInSync() {
411 public int describeContents() {
412 return this.hashCode();
416 public int compareTo(OCFile another
) {
417 if (isDirectory() && another
.isDirectory()) {
418 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
419 } else if (isDirectory()) {
421 } else if (another
.isDirectory()) {
424 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
428 public boolean equals(Object o
) {
429 if(o
instanceof OCFile
){
430 OCFile that
= (OCFile
) o
;
432 return this.mId
== that
.mId
;
440 public String
toString() {
441 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSinc=%s]";
442 asString
= String
.format(asString
, Long
.valueOf(mId
), getFileName(), mMimeType
, isDown(), mLocalPath
, mRemotePath
, Long
.valueOf(mParentId
), Boolean
.valueOf(mKeepInSync
));
446 public String
getEtag() {
450 public long getLocalModificationTimestamp() {
451 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
452 File f
= new File(mLocalPath
);
453 return f
.lastModified();