32d36de37c29deb4f1a30bc144717f30a32c0480
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 eu
.alefzero
.owncloud
.datamodel
;
22 import java
.net
.MalformedURLException
;
25 import android
.net
.Uri
;
26 import android
.os
.Parcel
;
27 import android
.os
.Parcelable
;
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
= "/";
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 * TODO use a better condition not dependent upon mLenght being synchronized; to change when downloads are done through a temporal file
149 * @return true if it is
151 public boolean isDown() {
152 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
153 File file
= new File(mLocalPath
);
154 return (file
.exists() && file
.length() == mLength
);
160 * Use this to check if this file is downloading
162 * TODO use a better condition not dependent upon mLenght being synchronized; to change when downloads are done through a temporal file
164 * @return true if it is in a download in progress
166 public boolean isDownloading() {
167 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
168 File file
= new File(mLocalPath
);
169 return (file
.exists() && file
.length() < mLength
);
175 * The path, where the file is stored locally
177 * @return The local path to the file
179 public String
getStoragePath() {
184 * Can be used to set the path where the file is stored
186 * @param storage_path to set
188 public void setStoragePath(String storage_path
) {
189 mLocalPath
= storage_path
;
193 * Get a UNIX timestamp of the file creation time
195 * @return A UNIX timestamp of the time that file was created
197 public long getCreationTimestamp() {
198 return mCreationTimestamp
;
202 * Set a UNIX timestamp of the time the file was created
204 * @param creation_timestamp to set
206 public void setCreationTimestamp(long creation_timestamp
) {
207 mCreationTimestamp
= creation_timestamp
;
211 * Get a UNIX timestamp of the file modification time
213 * @return A UNIX timestamp of the modification time
215 public long getModificationTimestamp() {
216 return mModifiedTimestamp
;
220 * Set a UNIX timestamp of the time the time the file was modified.
222 * @param modification_timestamp to set
224 public void setModificationTimestamp(long modification_timestamp
) {
225 mModifiedTimestamp
= modification_timestamp
;
229 * Returns the filename and "/" for the root directory
231 * @return The name of the file
233 public String
getFileName() {
234 File f
= new File(getRemotePath());
235 return f
.getName().length() == 0 ?
"/" : f
.getName();
239 * Can be used to get the Mimetype
241 * @return the Mimetype as a String
243 public String
getMimetype() {
248 * Adds a file to this directory. If this file is not a directory, an
249 * exception gets thrown.
252 * @throws IllegalStateException if you try to add a something and this is
255 public void addFile(OCFile file
) throws IllegalStateException
{
257 file
.mParentId
= mId
;
258 mNeedsUpdating
= true
;
261 throw new IllegalStateException(
262 "This is not a directory where you can add stuff to!");
266 * Used internally. Reset all file properties
268 private void resetData() {
275 mCreationTimestamp
= 0;
276 mModifiedTimestamp
= 0;
279 mNeedsUpdating
= false
;
283 * Sets the ID of the file
285 * @param file_id to set
287 public void setFileId(long file_id
) {
292 * Sets the Mime-Type of the
294 * @param mimetype to set
296 public void setMimetype(String mimetype
) {
297 mMimeType
= mimetype
;
301 * Sets the ID of the parent folder
303 * @param parent_id to set
305 public void setParentId(long parent_id
) {
306 mParentId
= parent_id
;
310 * Sets the file size in bytes
312 * @param file_len to set
314 public void setFileLength(long file_len
) {
319 * Returns the size of the file in bytes
321 * @return The filesize in bytes
323 public long getFileLength() {
328 * Returns the ID of the parent Folder
332 public long getParentId() {
337 * Check, if this file needs updating
341 public boolean needsUpdatingWhileSaving() {
342 return mNeedsUpdating
;
345 public long getLastSyncDate() {
346 return mLastSyncDate
;
349 public void setLastSyncDate(long lastSyncDate
) {
350 mLastSyncDate
= lastSyncDate
;
353 public void setKeepInSync(boolean keepInSync
) {
354 mKeepInSync
= keepInSync
;
357 public boolean keepInSync() {
362 public int describeContents() {
363 return this.hashCode();
367 public int compareTo(OCFile another
) {
368 if (isDirectory() && another
.isDirectory()) {
369 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
370 } else if (isDirectory()) {
372 } else if (another
.isDirectory()) {
375 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
378 public boolean equals(Object o
) {
379 if(o
instanceof OCFile
){
380 OCFile that
= (OCFile
) o
;
382 return this.mId
== that
.mId
;
390 public String
toString() {
391 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s]";
392 asString
= String
.format(asString
, new Long(mId
), getFileName(), mMimeType
, isDown(), mLocalPath
, mRemotePath
);