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 String mRemotePath
;
51 private String mLocalPath
;
52 private String mMimeType
;
53 private boolean mNeedsUpdating
;
54 private long mLastSyncDateForProperties
;
55 private long mLastSyncDateForData
;
56 private boolean mKeepInSync
;
61 * Create new {@link OCFile} with given path.
63 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
65 * @param path The remote path of the file.
67 public OCFile(String path
) {
69 mNeedsUpdating
= false
;
70 if (path
== null
|| path
.length() <= 0 || !path
.startsWith(PATH_SEPARATOR
)) {
71 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path
);
77 * Reconstruct from parcel
79 * @param source The source parcel
81 private OCFile(Parcel source
) {
82 mId
= source
.readLong();
83 mParentId
= source
.readLong();
84 mLength
= source
.readLong();
85 mCreationTimestamp
= source
.readLong();
86 mModifiedTimestamp
= source
.readLong();
87 mRemotePath
= source
.readString();
88 mLocalPath
= source
.readString();
89 mMimeType
= source
.readString();
90 mNeedsUpdating
= source
.readInt() == 0;
91 mKeepInSync
= source
.readInt() == 1;
92 mLastSyncDateForProperties
= source
.readLong();
93 mLastSyncDateForData
= source
.readLong();
97 public void writeToParcel(Parcel dest
, int flags
) {
99 dest
.writeLong(mParentId
);
100 dest
.writeLong(mLength
);
101 dest
.writeLong(mCreationTimestamp
);
102 dest
.writeLong(mModifiedTimestamp
);
103 dest
.writeString(mRemotePath
);
104 dest
.writeString(mLocalPath
);
105 dest
.writeString(mMimeType
);
106 dest
.writeInt(mNeedsUpdating ?
1 : 0);
107 dest
.writeInt(mKeepInSync ?
1 : 0);
108 dest
.writeLong(mLastSyncDateForProperties
);
109 dest
.writeLong(mLastSyncDateForData
);
113 * Gets the ID of the file
115 * @return the file ID
117 public long getFileId() {
122 * Returns the remote path of the file on ownCloud
124 * @return The remote path to the file
126 public String
getRemotePath() {
131 * Can be used to check, whether or not this file exists in the database
134 * @return true, if the file exists in the database
136 public boolean fileExists() {
141 * Use this to find out if this file is a Directory
143 * @return true if it is a directory
145 public boolean isDirectory() {
146 return mMimeType
!= null
&& mMimeType
.equals("DIR");
150 * Use this to check if this file is available locally
152 * @return true if it is
154 public boolean isDown() {
155 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
156 File file
= new File(mLocalPath
);
157 return (file
.exists());
163 * The path, where the file is stored locally
165 * @return The local path to the file
167 public String
getStoragePath() {
172 * Can be used to set the path where the file is stored
174 * @param storage_path to set
176 public void setStoragePath(String storage_path
) {
177 mLocalPath
= storage_path
;
181 * Get a UNIX timestamp of the file creation time
183 * @return A UNIX timestamp of the time that file was created
185 public long getCreationTimestamp() {
186 return mCreationTimestamp
;
190 * Set a UNIX timestamp of the time the file was created
192 * @param creation_timestamp to set
194 public void setCreationTimestamp(long creation_timestamp
) {
195 mCreationTimestamp
= creation_timestamp
;
199 * Get a UNIX timestamp of the file modification time
201 * @return A UNIX timestamp of the modification time
203 public long getModificationTimestamp() {
204 return mModifiedTimestamp
;
208 * Set a UNIX timestamp of the time the time the file was modified.
210 * @param modification_timestamp to set
212 public void setModificationTimestamp(long modification_timestamp
) {
213 mModifiedTimestamp
= modification_timestamp
;
217 * Returns the filename and "/" for the root directory
219 * @return The name of the file
221 public String
getFileName() {
222 File f
= new File(getRemotePath());
223 return f
.getName().length() == 0 ? PATH_SEPARATOR
: f
.getName();
227 * Sets the name of the file
229 * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root directory
231 public void setFileName(String name
) {
232 Log
.d(TAG
, "OCFile name changin from " + mRemotePath
);
233 if (name
!= null
&& name
.length() > 0 && !name
.contains(PATH_SEPARATOR
) && !mRemotePath
.equals(PATH_SEPARATOR
)) {
234 String parent
= (new File(getRemotePath())).getParent();
235 parent
= (parent
.endsWith(PATH_SEPARATOR
)) ? parent
: parent
+ PATH_SEPARATOR
;
236 mRemotePath
= parent
+ name
;
238 mRemotePath
+= PATH_SEPARATOR
;
240 Log
.d(TAG
, "OCFile name changed to " + mRemotePath
);
245 * Can be used to get the Mimetype
247 * @return the Mimetype as a String
249 public String
getMimetype() {
254 * Adds a file to this directory. If this file is not a directory, an
255 * exception gets thrown.
258 * @throws IllegalStateException if you try to add a something and this is
261 public void addFile(OCFile file
) throws IllegalStateException
{
263 file
.mParentId
= mId
;
264 mNeedsUpdating
= true
;
267 throw new IllegalStateException(
268 "This is not a directory where you can add stuff to!");
272 * Used internally. Reset all file properties
274 private void resetData() {
281 mCreationTimestamp
= 0;
282 mModifiedTimestamp
= 0;
283 mLastSyncDateForProperties
= 0;
284 mLastSyncDateForData
= 0;
286 mNeedsUpdating
= false
;
290 * Sets the ID of the file
292 * @param file_id to set
294 public void setFileId(long file_id
) {
299 * Sets the Mime-Type of the
301 * @param mimetype to set
303 public void setMimetype(String mimetype
) {
304 mMimeType
= mimetype
;
308 * Sets the ID of the parent folder
310 * @param parent_id to set
312 public void setParentId(long parent_id
) {
313 mParentId
= parent_id
;
317 * Sets the file size in bytes
319 * @param file_len to set
321 public void setFileLength(long file_len
) {
326 * Returns the size of the file in bytes
328 * @return The filesize in bytes
330 public long getFileLength() {
335 * Returns the ID of the parent Folder
339 public long getParentId() {
344 * Check, if this file needs updating
348 public boolean needsUpdatingWhileSaving() {
349 return mNeedsUpdating
;
352 public long getLastSyncDateForProperties() {
353 return mLastSyncDateForProperties
;
356 public void setLastSyncDateForProperties(long lastSyncDate
) {
357 mLastSyncDateForProperties
= lastSyncDate
;
360 public long getLastSyncDateForData() {
361 return mLastSyncDateForData
;
364 public void setLastSyncDateForData(long lastSyncDate
) {
365 mLastSyncDateForData
= lastSyncDate
;
368 public void setKeepInSync(boolean keepInSync
) {
369 mKeepInSync
= keepInSync
;
372 public boolean keepInSync() {
377 public int describeContents() {
378 return this.hashCode();
382 public int compareTo(OCFile another
) {
383 if (isDirectory() && another
.isDirectory()) {
384 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
385 } else if (isDirectory()) {
387 } else if (another
.isDirectory()) {
390 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
394 public boolean equals(Object o
) {
395 if(o
instanceof OCFile
){
396 OCFile that
= (OCFile
) o
;
398 return this.mId
== that
.mId
;
406 public String
toString() {
407 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSinc=%s]";
408 asString
= String
.format(asString
, Long
.valueOf(mId
), getFileName(), mMimeType
, isDown(), mLocalPath
, mRemotePath
, Long
.valueOf(mParentId
), Boolean
.valueOf(mKeepInSync
));
412 public String
getEtag() {
416 public long getLocalModificationTimestamp() {
417 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
418 File f
= new File(mLocalPath
);
419 return f
.lastModified();