01a1f58f8330694028e0f8fba003856fd997b260
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 eu
.alefzero
.owncloud
.files
.services
.FileDownloader
;
27 import android
.net
.Uri
;
28 import android
.os
.Parcel
;
29 import android
.os
.Parcelable
;
31 public class OCFile
implements Parcelable
, Comparable
<OCFile
> {
33 public static final Parcelable
.Creator
<OCFile
> CREATOR
= new Parcelable
.Creator
<OCFile
>() {
35 public OCFile
createFromParcel(Parcel source
) {
36 return new OCFile(source
);
40 public OCFile
[] newArray(int size
) {
41 return new OCFile
[size
];
45 public static final String PATH_SEPARATOR
= "/";
48 private long mParentId
;
50 private long mCreationTimestamp
;
51 private long mModifiedTimestamp
;
52 private String mRemotePath
;
53 private String mLocalPath
;
54 private String mMimeType
;
55 private boolean mNeedsUpdating
;
56 private long mLastSyncDate
;
57 private boolean mKeepInSync
;
60 * Create new {@link OCFile} with given path.
62 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
64 * @param path The remote path of the file.
66 public OCFile(String path
) {
68 mNeedsUpdating
= false
;
69 if (path
== null
|| path
.length() <= 0 || !path
.startsWith(PATH_SEPARATOR
)) {
70 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path
);
76 * Reconstruct from parcel
78 * @param source The source parcel
80 private OCFile(Parcel source
) {
81 mId
= source
.readLong();
82 mParentId
= source
.readLong();
83 mLength
= source
.readLong();
84 mCreationTimestamp
= source
.readLong();
85 mModifiedTimestamp
= source
.readLong();
86 mRemotePath
= source
.readString();
87 mLocalPath
= source
.readString();
88 mMimeType
= source
.readString();
89 mNeedsUpdating
= source
.readInt() == 0;
90 mKeepInSync
= source
.readInt() == 1;
91 mLastSyncDate
= source
.readLong();
95 public void writeToParcel(Parcel dest
, int flags
) {
97 dest
.writeLong(mParentId
);
98 dest
.writeLong(mLength
);
99 dest
.writeLong(mCreationTimestamp
);
100 dest
.writeLong(mModifiedTimestamp
);
101 dest
.writeString(mRemotePath
);
102 dest
.writeString(mLocalPath
);
103 dest
.writeString(mMimeType
);
104 dest
.writeInt(mNeedsUpdating ?
1 : 0);
105 dest
.writeInt(mKeepInSync ?
1 : 0);
106 dest
.writeLong(mLastSyncDate
);
110 * Gets the ID of the file
112 * @return the file ID
114 public long getFileId() {
119 * Returns the remote path of the file on ownCloud
121 * @return The remote path to the file
123 public String
getRemotePath() {
128 * Can be used to check, whether or not this file exists in the database
131 * @return true, if the file exists in the database
133 public boolean fileExists() {
138 * Use this to find out if this file is a Directory
140 * @return true if it is a directory
142 public boolean isDirectory() {
143 return mMimeType
!= null
&& mMimeType
.equals("DIR");
147 * Use this to check if this file is available locally
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());
160 * The path, where the file is stored locally
162 * @return The local path to the file
164 public String
getStoragePath() {
169 * Can be used to set the path where the file is stored
171 * @param storage_path to set
173 public void setStoragePath(String storage_path
) {
174 mLocalPath
= storage_path
;
178 * Get a UNIX timestamp of the file creation time
180 * @return A UNIX timestamp of the time that file was created
182 public long getCreationTimestamp() {
183 return mCreationTimestamp
;
187 * Set a UNIX timestamp of the time the file was created
189 * @param creation_timestamp to set
191 public void setCreationTimestamp(long creation_timestamp
) {
192 mCreationTimestamp
= creation_timestamp
;
196 * Get a UNIX timestamp of the file modification time
198 * @return A UNIX timestamp of the modification time
200 public long getModificationTimestamp() {
201 return mModifiedTimestamp
;
205 * Set a UNIX timestamp of the time the time the file was modified.
207 * @param modification_timestamp to set
209 public void setModificationTimestamp(long modification_timestamp
) {
210 mModifiedTimestamp
= modification_timestamp
;
214 * Returns the filename and "/" for the root directory
216 * @return The name of the file
218 public String
getFileName() {
219 File f
= new File(getRemotePath());
220 return f
.getName().length() == 0 ?
"/" : f
.getName();
224 * Can be used to get the Mimetype
226 * @return the Mimetype as a String
228 public String
getMimetype() {
233 * Adds a file to this directory. If this file is not a directory, an
234 * exception gets thrown.
237 * @throws IllegalStateException if you try to add a something and this is
240 public void addFile(OCFile file
) throws IllegalStateException
{
242 file
.mParentId
= mId
;
243 mNeedsUpdating
= true
;
246 throw new IllegalStateException(
247 "This is not a directory where you can add stuff to!");
251 * Used internally. Reset all file properties
253 private void resetData() {
260 mCreationTimestamp
= 0;
261 mModifiedTimestamp
= 0;
264 mNeedsUpdating
= false
;
268 * Sets the ID of the file
270 * @param file_id to set
272 public void setFileId(long file_id
) {
277 * Sets the Mime-Type of the
279 * @param mimetype to set
281 public void setMimetype(String mimetype
) {
282 mMimeType
= mimetype
;
286 * Sets the ID of the parent folder
288 * @param parent_id to set
290 public void setParentId(long parent_id
) {
291 mParentId
= parent_id
;
295 * Sets the file size in bytes
297 * @param file_len to set
299 public void setFileLength(long file_len
) {
304 * Returns the size of the file in bytes
306 * @return The filesize in bytes
308 public long getFileLength() {
313 * Returns the ID of the parent Folder
317 public long getParentId() {
322 * Check, if this file needs updating
326 public boolean needsUpdatingWhileSaving() {
327 return mNeedsUpdating
;
330 public long getLastSyncDate() {
331 return mLastSyncDate
;
334 public void setLastSyncDate(long lastSyncDate
) {
335 mLastSyncDate
= lastSyncDate
;
338 public void setKeepInSync(boolean keepInSync
) {
339 mKeepInSync
= keepInSync
;
342 public boolean keepInSync() {
347 public int describeContents() {
348 return this.hashCode();
352 public int compareTo(OCFile another
) {
353 if (isDirectory() && another
.isDirectory()) {
354 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
355 } else if (isDirectory()) {
357 } else if (another
.isDirectory()) {
360 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
363 public boolean equals(Object o
) {
364 if(o
instanceof OCFile
){
365 OCFile that
= (OCFile
) o
;
367 return this.mId
== that
.mId
;
375 public String
toString() {
376 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSinc=%s]";
377 asString
= String
.format(asString
, new Long(mId
), getFileName(), mMimeType
, isDown(), mLocalPath
, mRemotePath
, new Long(mParentId
), new Boolean(mKeepInSync
));