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 mLastSyncDate
;
55 private boolean mKeepInSync
;
58 * Create new {@link OCFile} with given path.
60 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
62 * @param path The remote path of the file.
64 public OCFile(String path
) {
66 mNeedsUpdating
= false
;
67 if (path
== null
|| path
.length() <= 0 || !path
.startsWith(PATH_SEPARATOR
)) {
68 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path
);
74 * Reconstruct from parcel
76 * @param source The source parcel
78 private OCFile(Parcel source
) {
79 mId
= source
.readLong();
80 mParentId
= source
.readLong();
81 mLength
= source
.readLong();
82 mCreationTimestamp
= source
.readLong();
83 mModifiedTimestamp
= source
.readLong();
84 mRemotePath
= source
.readString();
85 mLocalPath
= source
.readString();
86 mMimeType
= source
.readString();
87 mNeedsUpdating
= source
.readInt() == 0;
88 mKeepInSync
= source
.readInt() == 1;
89 mLastSyncDate
= source
.readLong();
93 public void writeToParcel(Parcel dest
, int flags
) {
95 dest
.writeLong(mParentId
);
96 dest
.writeLong(mLength
);
97 dest
.writeLong(mCreationTimestamp
);
98 dest
.writeLong(mModifiedTimestamp
);
99 dest
.writeString(mRemotePath
);
100 dest
.writeString(mLocalPath
);
101 dest
.writeString(mMimeType
);
102 dest
.writeInt(mNeedsUpdating ?
1 : 0);
103 dest
.writeInt(mKeepInSync ?
1 : 0);
104 dest
.writeLong(mLastSyncDate
);
108 * Gets the ID of the file
110 * @return the file ID
112 public long getFileId() {
117 * Returns the remote path of the file on ownCloud
119 * @return The remote path to the file
121 public String
getRemotePath() {
126 * Can be used to check, whether or not this file exists in the database
129 * @return true, if the file exists in the database
131 public boolean fileExists() {
136 * Use this to find out if this file is a Directory
138 * @return true if it is a directory
140 public boolean isDirectory() {
141 return mMimeType
!= null
&& mMimeType
.equals("DIR");
145 * Use this to check if this file is available locally
147 * @return true if it is
149 public boolean isDown() {
150 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
151 File file
= new File(mLocalPath
);
152 return (file
.exists());
158 * The path, where the file is stored locally
160 * @return The local path to the file
162 public String
getStoragePath() {
167 * Can be used to set the path where the file is stored
169 * @param storage_path to set
171 public void setStoragePath(String storage_path
) {
172 mLocalPath
= storage_path
;
176 * Get a UNIX timestamp of the file creation time
178 * @return A UNIX timestamp of the time that file was created
180 public long getCreationTimestamp() {
181 return mCreationTimestamp
;
185 * Set a UNIX timestamp of the time the file was created
187 * @param creation_timestamp to set
189 public void setCreationTimestamp(long creation_timestamp
) {
190 mCreationTimestamp
= creation_timestamp
;
194 * Get a UNIX timestamp of the file modification time
196 * @return A UNIX timestamp of the modification time
198 public long getModificationTimestamp() {
199 return mModifiedTimestamp
;
203 * Set a UNIX timestamp of the time the time the file was modified.
205 * @param modification_timestamp to set
207 public void setModificationTimestamp(long modification_timestamp
) {
208 mModifiedTimestamp
= modification_timestamp
;
212 * Returns the filename and "/" for the root directory
214 * @return The name of the file
216 public String
getFileName() {
217 File f
= new File(getRemotePath());
218 return f
.getName().length() == 0 ? PATH_SEPARATOR
: f
.getName();
222 * Sets the name of the file
224 * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root directory
226 public void setFileName(String name
) {
227 Log
.d(TAG
, "OCFile name changin from " + mRemotePath
);
228 if (name
!= null
&& name
.length() > 0 && !name
.contains(PATH_SEPARATOR
) && !mRemotePath
.equals(PATH_SEPARATOR
)) {
229 String parent
= (new File(getRemotePath())).getParent();
230 parent
= (parent
.endsWith(PATH_SEPARATOR
)) ? parent
: parent
+ PATH_SEPARATOR
;
231 mRemotePath
= parent
+ name
;
233 mRemotePath
+= PATH_SEPARATOR
;
235 Log
.d(TAG
, "OCFile name changed to " + mRemotePath
);
240 * Can be used to get the Mimetype
242 * @return the Mimetype as a String
244 public String
getMimetype() {
249 * Adds a file to this directory. If this file is not a directory, an
250 * exception gets thrown.
253 * @throws IllegalStateException if you try to add a something and this is
256 public void addFile(OCFile file
) throws IllegalStateException
{
258 file
.mParentId
= mId
;
259 mNeedsUpdating
= true
;
262 throw new IllegalStateException(
263 "This is not a directory where you can add stuff to!");
267 * Used internally. Reset all file properties
269 private void resetData() {
276 mCreationTimestamp
= 0;
277 mModifiedTimestamp
= 0;
280 mNeedsUpdating
= false
;
284 * Sets the ID of the file
286 * @param file_id to set
288 public void setFileId(long file_id
) {
293 * Sets the Mime-Type of the
295 * @param mimetype to set
297 public void setMimetype(String mimetype
) {
298 mMimeType
= mimetype
;
302 * Sets the ID of the parent folder
304 * @param parent_id to set
306 public void setParentId(long parent_id
) {
307 mParentId
= parent_id
;
311 * Sets the file size in bytes
313 * @param file_len to set
315 public void setFileLength(long file_len
) {
320 * Returns the size of the file in bytes
322 * @return The filesize in bytes
324 public long getFileLength() {
329 * Returns the ID of the parent Folder
333 public long getParentId() {
338 * Check, if this file needs updating
342 public boolean needsUpdatingWhileSaving() {
343 return mNeedsUpdating
;
346 public long getLastSyncDate() {
347 return mLastSyncDate
;
350 public void setLastSyncDate(long lastSyncDate
) {
351 mLastSyncDate
= lastSyncDate
;
354 public void setKeepInSync(boolean keepInSync
) {
355 mKeepInSync
= keepInSync
;
358 public boolean keepInSync() {
363 public int describeContents() {
364 return this.hashCode();
368 public int compareTo(OCFile another
) {
369 if (isDirectory() && another
.isDirectory()) {
370 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
371 } else if (isDirectory()) {
373 } else if (another
.isDirectory()) {
376 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
380 public boolean equals(Object o
) {
381 if(o
instanceof OCFile
){
382 OCFile that
= (OCFile
) o
;
384 return this.mId
== that
.mId
;
392 public String
toString() {
393 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSinc=%s]";
394 asString
= String
.format(asString
, Long
.valueOf(mId
), getFileName(), mMimeType
, isDown(), mLocalPath
, mRemotePath
, Long
.valueOf(mParentId
), Boolean
.valueOf(mKeepInSync
));