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
;
22 import java
.net
.MalformedURLException
;
25 import com
.owncloud
.android
.files
.services
.FileDownloader
;
28 import android
.net
.Uri
;
29 import android
.os
.Parcel
;
30 import android
.os
.Parcelable
;
32 public class OCFile
implements Parcelable
, Comparable
<OCFile
> {
34 public static final Parcelable
.Creator
<OCFile
> CREATOR
= new Parcelable
.Creator
<OCFile
>() {
36 public OCFile
createFromParcel(Parcel source
) {
37 return new OCFile(source
);
41 public OCFile
[] newArray(int size
) {
42 return new OCFile
[size
];
46 public static final String PATH_SEPARATOR
= "/";
49 private long mParentId
;
51 private long mCreationTimestamp
;
52 private long mModifiedTimestamp
;
53 private String mRemotePath
;
54 private String mLocalPath
;
55 private String mMimeType
;
56 private boolean mNeedsUpdating
;
57 private long mLastSyncDate
;
58 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 mLastSyncDate
= source
.readLong();
96 public void writeToParcel(Parcel dest
, int flags
) {
98 dest
.writeLong(mParentId
);
99 dest
.writeLong(mLength
);
100 dest
.writeLong(mCreationTimestamp
);
101 dest
.writeLong(mModifiedTimestamp
);
102 dest
.writeString(mRemotePath
);
103 dest
.writeString(mLocalPath
);
104 dest
.writeString(mMimeType
);
105 dest
.writeInt(mNeedsUpdating ?
1 : 0);
106 dest
.writeInt(mKeepInSync ?
1 : 0);
107 dest
.writeLong(mLastSyncDate
);
111 * Gets the ID of the file
113 * @return the file ID
115 public long getFileId() {
120 * Returns the remote path of the file on ownCloud
122 * @return The remote path to the file
124 public String
getRemotePath() {
129 * Can be used to check, whether or not this file exists in the database
132 * @return true, if the file exists in the database
134 public boolean fileExists() {
139 * Use this to find out if this file is a Directory
141 * @return true if it is a directory
143 public boolean isDirectory() {
144 return mMimeType
!= null
&& mMimeType
.equals("DIR");
148 * Use this to check if this file is available locally
150 * @return true if it is
152 public boolean isDown() {
153 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
154 File file
= new File(mLocalPath
);
155 return (file
.exists());
161 * The path, where the file is stored locally
163 * @return The local path to the file
165 public String
getStoragePath() {
170 * Can be used to set the path where the file is stored
172 * @param storage_path to set
174 public void setStoragePath(String storage_path
) {
175 mLocalPath
= storage_path
;
179 * Get a UNIX timestamp of the file creation time
181 * @return A UNIX timestamp of the time that file was created
183 public long getCreationTimestamp() {
184 return mCreationTimestamp
;
188 * Set a UNIX timestamp of the time the file was created
190 * @param creation_timestamp to set
192 public void setCreationTimestamp(long creation_timestamp
) {
193 mCreationTimestamp
= creation_timestamp
;
197 * Get a UNIX timestamp of the file modification time
199 * @return A UNIX timestamp of the modification time
201 public long getModificationTimestamp() {
202 return mModifiedTimestamp
;
206 * Set a UNIX timestamp of the time the time the file was modified.
208 * @param modification_timestamp to set
210 public void setModificationTimestamp(long modification_timestamp
) {
211 mModifiedTimestamp
= modification_timestamp
;
215 * Returns the filename and "/" for the root directory
217 * @return The name of the file
219 public String
getFileName() {
220 File f
= new File(getRemotePath());
221 return f
.getName().length() == 0 ?
"/" : f
.getName();
225 * Can be used to get the Mimetype
227 * @return the Mimetype as a String
229 public String
getMimetype() {
234 * Adds a file to this directory. If this file is not a directory, an
235 * exception gets thrown.
238 * @throws IllegalStateException if you try to add a something and this is
241 public void addFile(OCFile file
) throws IllegalStateException
{
243 file
.mParentId
= mId
;
244 mNeedsUpdating
= true
;
247 throw new IllegalStateException(
248 "This is not a directory where you can add stuff to!");
252 * Used internally. Reset all file properties
254 private void resetData() {
261 mCreationTimestamp
= 0;
262 mModifiedTimestamp
= 0;
265 mNeedsUpdating
= false
;
269 * Sets the ID of the file
271 * @param file_id to set
273 public void setFileId(long file_id
) {
278 * Sets the Mime-Type of the
280 * @param mimetype to set
282 public void setMimetype(String mimetype
) {
283 mMimeType
= mimetype
;
287 * Sets the ID of the parent folder
289 * @param parent_id to set
291 public void setParentId(long parent_id
) {
292 mParentId
= parent_id
;
296 * Sets the file size in bytes
298 * @param file_len to set
300 public void setFileLength(long file_len
) {
305 * Returns the size of the file in bytes
307 * @return The filesize in bytes
309 public long getFileLength() {
314 * Returns the ID of the parent Folder
318 public long getParentId() {
323 * Check, if this file needs updating
327 public boolean needsUpdatingWhileSaving() {
328 return mNeedsUpdating
;
331 public long getLastSyncDate() {
332 return mLastSyncDate
;
335 public void setLastSyncDate(long lastSyncDate
) {
336 mLastSyncDate
= lastSyncDate
;
339 public void setKeepInSync(boolean keepInSync
) {
340 mKeepInSync
= keepInSync
;
343 public boolean keepInSync() {
348 public int describeContents() {
349 return this.hashCode();
353 public int compareTo(OCFile another
) {
354 if (isDirectory() && another
.isDirectory()) {
355 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
356 } else if (isDirectory()) {
358 } else if (another
.isDirectory()) {
361 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
364 public boolean equals(Object o
) {
365 if(o
instanceof OCFile
){
366 OCFile that
= (OCFile
) o
;
368 return this.mId
== that
.mId
;
376 public String
toString() {
377 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSinc=%s]";
378 asString
= String
.format(asString
, new Long(mId
), getFileName(), mMimeType
, isDown(), mLocalPath
, mRemotePath
, new Long(mParentId
), new Boolean(mKeepInSync
));