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 * @return true if it is
149 public boolean isDownloaded() {
150 return mLocalPath
!= null
&& !mLocalPath
.equals("");
154 * The path, where the file is stored locally
156 * @return The local path to the file
158 public String
getStoragePath() {
163 * Can be used to set the path where the file is stored
165 * @param storage_path to set
167 public void setStoragePath(String storage_path
) {
168 mLocalPath
= storage_path
;
172 * Get a UNIX timestamp of the file creation time
174 * @return A UNIX timestamp of the time that file was created
176 public long getCreationTimestamp() {
177 return mCreationTimestamp
;
181 * Set a UNIX timestamp of the time the file was created
183 * @param creation_timestamp to set
185 public void setCreationTimestamp(long creation_timestamp
) {
186 mCreationTimestamp
= creation_timestamp
;
190 * Get a UNIX timestamp of the file modification time
192 * @return A UNIX timestamp of the modification time
194 public long getModificationTimestamp() {
195 return mModifiedTimestamp
;
199 * Set a UNIX timestamp of the time the time the file was modified.
201 * @param modification_timestamp to set
203 public void setModificationTimestamp(long modification_timestamp
) {
204 mModifiedTimestamp
= modification_timestamp
;
208 * Returns the filename and "/" for the root directory
210 * @return The name of the file
212 public String
getFileName() {
213 File f
= new File(getRemotePath());
214 return f
.getName().length() == 0 ?
"/" : f
.getName();
218 * Can be used to get the Mimetype
220 * @return the Mimetype as a String
222 public String
getMimetype() {
227 * Adds a file to this directory. If this file is not a directory, an
228 * exception gets thrown.
231 * @throws IllegalStateException if you try to add a something and this is
234 public void addFile(OCFile file
) throws IllegalStateException
{
236 file
.mParentId
= mId
;
237 mNeedsUpdating
= true
;
240 throw new IllegalStateException(
241 "This is not a directory where you can add stuff to!");
245 * Used internally. Reset all file properties
247 private void resetData() {
254 mCreationTimestamp
= 0;
255 mModifiedTimestamp
= 0;
258 mNeedsUpdating
= false
;
262 * Sets the ID of the file
264 * @param file_id to set
266 public void setFileId(long file_id
) {
271 * Sets the Mime-Type of the
273 * @param mimetype to set
275 public void setMimetype(String mimetype
) {
276 mMimeType
= mimetype
;
280 * Sets the ID of the parent folder
282 * @param parent_id to set
284 public void setParentId(long parent_id
) {
285 mParentId
= parent_id
;
289 * Sets the file size in bytes
291 * @param file_len to set
293 public void setFileLength(long file_len
) {
298 * Returns the size of the file in bytes
300 * @return The filesize in bytes
302 public long getFileLength() {
307 * Returns the ID of the parent Folder
311 public long getParentId() {
316 * Check, if this file needs updating
320 public boolean needsUpdatingWhileSaving() {
321 return mNeedsUpdating
;
324 public long getLastSyncDate() {
325 return mLastSyncDate
;
328 public void setLastSyncDate(long lastSyncDate
) {
329 mLastSyncDate
= lastSyncDate
;
332 public void setKeepInSync(boolean keepInSync
) {
333 mKeepInSync
= keepInSync
;
336 public boolean keepInSync() {
341 public int describeContents() {
342 return this.hashCode();
346 public int compareTo(OCFile another
) {
347 if (isDirectory() && another
.isDirectory()) {
348 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
349 } else if (isDirectory()) {
351 } else if (another
.isDirectory()) {
354 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
357 public boolean equals(Object o
) {
358 if(o
instanceof OCFile
){
359 OCFile that
= (OCFile
) o
;
361 return this.mId
== that
.mId
;
369 public String
toString() {
370 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s]";
371 asString
= String
.format(asString
, new Long(mId
), getFileName(), mMimeType
, isDownloaded(), mLocalPath
, mRemotePath
);