ce9c03b5ea636e007749aeb488240285d5f36e96
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
];
44 private long mParentId
;
46 private long mCreationTimestamp
;
47 private long mModifiedTimestamp
;
48 private String mRemotePath
;
49 private String mLocalPath
;
50 private String mMimeType
;
51 private boolean mNeedsUpdating
;
52 private long mLastSyncDate
;
53 private boolean mKeepInSync
;
56 * Create new {@link OCFile} with given path
58 * @param path The remote path of the file
59 * @throws MalformedURLException
61 public OCFile(String path
) {
63 mNeedsUpdating
= false
;
64 /// dvelasco: the encoding / decoding problem should be completely translated to WebdavClient & WebdavEntry, but at this moment we are in a little hurry
65 if (path
!= null
&& path
.length() > 0) {
67 new URL("http://silly.test.com:8888" + path
);
68 } catch (MalformedURLException e
) {
69 throw new RuntimeException("Trying to create a OCFile with a non valid remote path: " + path
, e
);
71 } else throw new RuntimeException("Trying to create a OCFile with a non valid remote path: " + path
);
72 // save encoded paths have a problem: normalization; this is a quick&dirty fix to avoid duplications
73 mRemotePath
= Uri
.encode(Uri
.decode(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 * Returns the remote path of the file on ownCloud
131 * @return The remote path to the file
133 public String
getURLDecodedRemotePath() {
134 return Uri
.decode(mRemotePath
);
138 * Can be used to check, whether or not this file exists in the database
141 * @return true, if the file exists in the database
143 public boolean fileExists() {
148 * Use this to find out if this file is a Directory
150 * @return true if it is a directory
152 public boolean isDirectory() {
153 return mMimeType
!= null
&& mMimeType
.equals("DIR");
157 * Use this to check if this file is available locally
159 * @return true if it is
161 public boolean isDownloaded() {
162 return mLocalPath
!= null
&& !mLocalPath
.equals("");
166 * The path, where the file is stored locally
168 * @return The local path to the file
170 public String
getStoragePath() {
175 * Can be used to set the path where the file is stored
177 * @param storage_path to set
179 public void setStoragePath(String storage_path
) {
180 mLocalPath
= storage_path
;
184 * Get a UNIX timestamp of the file creation time
186 * @return A UNIX timestamp of the time that file was created
188 public long getCreationTimestamp() {
189 return mCreationTimestamp
;
193 * Set a UNIX timestamp of the time the file was created
195 * @param creation_timestamp to set
197 public void setCreationTimestamp(long creation_timestamp
) {
198 mCreationTimestamp
= creation_timestamp
;
202 * Get a UNIX timestamp of the file modification time
204 * @return A UNIX timestamp of the modification time
206 public long getModificationTimestamp() {
207 return mModifiedTimestamp
;
211 * Set a UNIX timestamp of the time the time the file was modified.
213 * @param modification_timestamp to set
215 public void setModificationTimestamp(long modification_timestamp
) {
216 mModifiedTimestamp
= modification_timestamp
;
220 * Returns the filename and "/" for the root directory
222 * @return The name of the file
224 public String
getFileName() {
225 File f
= new File(getURLDecodedRemotePath());
226 return f
.getName().length() == 0 ?
"/" : f
.getName();
230 * Can be used to get the Mimetype
232 * @return the Mimetype as a String
234 public String
getMimetype() {
239 * Adds a file to this directory. If this file is not a directory, an
240 * exception gets thrown.
243 * @throws IllegalStateException if you try to add a something and this is
246 public void addFile(OCFile file
) throws IllegalStateException
{
248 file
.mParentId
= mId
;
249 mNeedsUpdating
= true
;
252 throw new IllegalStateException(
253 "This is not a directory where you can add stuff to!");
257 * Used internally. Reset all file properties
259 private void resetData() {
266 mCreationTimestamp
= 0;
267 mModifiedTimestamp
= 0;
270 mNeedsUpdating
= false
;
274 * Sets the ID of the file
276 * @param file_id to set
278 public void setFileId(long file_id
) {
283 * Sets the Mime-Type of the
285 * @param mimetype to set
287 public void setMimetype(String mimetype
) {
288 mMimeType
= mimetype
;
292 * Sets the ID of the parent folder
294 * @param parent_id to set
296 public void setParentId(long parent_id
) {
297 mParentId
= parent_id
;
301 * Sets the file size in bytes
303 * @param file_len to set
305 public void setFileLength(long file_len
) {
310 * Returns the size of the file in bytes
312 * @return The filesize in bytes
314 public long getFileLength() {
319 * Returns the ID of the parent Folder
323 public long getParentId() {
328 * Check, if this file needs updating
332 public boolean needsUpdatingWhileSaving() {
333 return mNeedsUpdating
;
336 public long getLastSyncDate() {
337 return mLastSyncDate
;
340 public void setLastSyncDate(long lastSyncDate
) {
341 mLastSyncDate
= lastSyncDate
;
344 public void setKeepInSync(boolean keepInSync
) {
345 mKeepInSync
= keepInSync
;
348 public boolean keepInSync() {
353 public int describeContents() {
354 return this.hashCode();
358 public int compareTo(OCFile another
) {
359 if (isDirectory() && another
.isDirectory()) {
360 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
361 } else if (isDirectory()) {
363 } else if (another
.isDirectory()) {
366 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
369 public boolean equals(Object o
) {
370 if(o
instanceof OCFile
){
371 OCFile that
= (OCFile
) o
;
373 return this.mId
== that
.mId
;
381 public String
toString() {
382 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s]";
383 asString
= String
.format(asString
, new Long(mId
), getFileName(), mMimeType
, isDownloaded(), mLocalPath
, mRemotePath
);