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
;
26 public class OCFile
implements Parcelable
, Comparable
<OCFile
> {
28 public static final Parcelable
.Creator
<OCFile
> CREATOR
= new Parcelable
.Creator
<OCFile
>() {
30 public OCFile
createFromParcel(Parcel source
) {
31 return new OCFile(source
);
35 public OCFile
[] newArray(int size
) {
36 return new OCFile
[size
];
40 public static final String PATH_SEPARATOR
= "/";
43 private long mParentId
;
45 private long mCreationTimestamp
;
46 private long mModifiedTimestamp
;
47 private String mRemotePath
;
48 private String mLocalPath
;
49 private String mMimeType
;
50 private boolean mNeedsUpdating
;
51 private long mLastSyncDate
;
52 private boolean mKeepInSync
;
55 * Create new {@link OCFile} with given path.
57 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
59 * @param path The remote path of the file.
61 public OCFile(String path
) {
63 mNeedsUpdating
= false
;
64 if (path
== null
|| path
.length() <= 0 || !path
.startsWith(PATH_SEPARATOR
)) {
65 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path
);
71 * Reconstruct from parcel
73 * @param source The source parcel
75 private OCFile(Parcel source
) {
76 mId
= source
.readLong();
77 mParentId
= source
.readLong();
78 mLength
= source
.readLong();
79 mCreationTimestamp
= source
.readLong();
80 mModifiedTimestamp
= source
.readLong();
81 mRemotePath
= source
.readString();
82 mLocalPath
= source
.readString();
83 mMimeType
= source
.readString();
84 mNeedsUpdating
= source
.readInt() == 0;
85 mKeepInSync
= source
.readInt() == 1;
86 mLastSyncDate
= source
.readLong();
90 public void writeToParcel(Parcel dest
, int flags
) {
92 dest
.writeLong(mParentId
);
93 dest
.writeLong(mLength
);
94 dest
.writeLong(mCreationTimestamp
);
95 dest
.writeLong(mModifiedTimestamp
);
96 dest
.writeString(mRemotePath
);
97 dest
.writeString(mLocalPath
);
98 dest
.writeString(mMimeType
);
99 dest
.writeInt(mNeedsUpdating ?
1 : 0);
100 dest
.writeInt(mKeepInSync ?
1 : 0);
101 dest
.writeLong(mLastSyncDate
);
105 * Gets the ID of the file
107 * @return the file ID
109 public long getFileId() {
114 * Returns the remote path of the file on ownCloud
116 * @return The remote path to the file
118 public String
getRemotePath() {
123 * Can be used to check, whether or not this file exists in the database
126 * @return true, if the file exists in the database
128 public boolean fileExists() {
133 * Use this to find out if this file is a Directory
135 * @return true if it is a directory
137 public boolean isDirectory() {
138 return mMimeType
!= null
&& mMimeType
.equals("DIR");
142 * Use this to check if this file is available locally
144 * @return true if it is
146 public boolean isDown() {
147 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
148 File file
= new File(mLocalPath
);
149 return (file
.exists());
155 * The path, where the file is stored locally
157 * @return The local path to the file
159 public String
getStoragePath() {
164 * Can be used to set the path where the file is stored
166 * @param storage_path to set
168 public void setStoragePath(String storage_path
) {
169 mLocalPath
= storage_path
;
173 * Get a UNIX timestamp of the file creation time
175 * @return A UNIX timestamp of the time that file was created
177 public long getCreationTimestamp() {
178 return mCreationTimestamp
;
182 * Set a UNIX timestamp of the time the file was created
184 * @param creation_timestamp to set
186 public void setCreationTimestamp(long creation_timestamp
) {
187 mCreationTimestamp
= creation_timestamp
;
191 * Get a UNIX timestamp of the file modification time
193 * @return A UNIX timestamp of the modification time
195 public long getModificationTimestamp() {
196 return mModifiedTimestamp
;
200 * Set a UNIX timestamp of the time the time the file was modified.
202 * @param modification_timestamp to set
204 public void setModificationTimestamp(long modification_timestamp
) {
205 mModifiedTimestamp
= modification_timestamp
;
209 * Returns the filename and "/" for the root directory
211 * @return The name of the file
213 public String
getFileName() {
214 File f
= new File(getRemotePath());
215 return f
.getName().length() == 0 ?
"/" : f
.getName();
219 * Can be used to get the Mimetype
221 * @return the Mimetype as a String
223 public String
getMimetype() {
228 * Adds a file to this directory. If this file is not a directory, an
229 * exception gets thrown.
232 * @throws IllegalStateException if you try to add a something and this is
235 public void addFile(OCFile file
) throws IllegalStateException
{
237 file
.mParentId
= mId
;
238 mNeedsUpdating
= true
;
241 throw new IllegalStateException(
242 "This is not a directory where you can add stuff to!");
246 * Used internally. Reset all file properties
248 private void resetData() {
255 mCreationTimestamp
= 0;
256 mModifiedTimestamp
= 0;
259 mNeedsUpdating
= false
;
263 * Sets the ID of the file
265 * @param file_id to set
267 public void setFileId(long file_id
) {
272 * Sets the Mime-Type of the
274 * @param mimetype to set
276 public void setMimetype(String mimetype
) {
277 mMimeType
= mimetype
;
281 * Sets the ID of the parent folder
283 * @param parent_id to set
285 public void setParentId(long parent_id
) {
286 mParentId
= parent_id
;
290 * Sets the file size in bytes
292 * @param file_len to set
294 public void setFileLength(long file_len
) {
299 * Returns the size of the file in bytes
301 * @return The filesize in bytes
303 public long getFileLength() {
308 * Returns the ID of the parent Folder
312 public long getParentId() {
317 * Check, if this file needs updating
321 public boolean needsUpdatingWhileSaving() {
322 return mNeedsUpdating
;
325 public long getLastSyncDate() {
326 return mLastSyncDate
;
329 public void setLastSyncDate(long lastSyncDate
) {
330 mLastSyncDate
= lastSyncDate
;
333 public void setKeepInSync(boolean keepInSync
) {
334 mKeepInSync
= keepInSync
;
337 public boolean keepInSync() {
342 public int describeContents() {
343 return this.hashCode();
347 public int compareTo(OCFile another
) {
348 if (isDirectory() && another
.isDirectory()) {
349 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
350 } else if (isDirectory()) {
352 } else if (another
.isDirectory()) {
355 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
359 public boolean equals(Object o
) {
360 if(o
instanceof OCFile
){
361 OCFile that
= (OCFile
) o
;
363 return this.mId
== that
.mId
;
371 public String
toString() {
372 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSinc=%s]";
373 asString
= String
.format(asString
, new Long(mId
), getFileName(), mMimeType
, isDown(), mLocalPath
, mRemotePath
, new Long(mParentId
), new Boolean(mKeepInSync
));