ca454d1576296154a29df259131748bf4f75624a
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
;
55 * Create new {@link OCFile} with given path
57 * @param path The remote path of the file
58 * @throws MalformedURLException
60 public OCFile(String path
) {
62 mNeedsUpdating
= false
;
63 /// dvelasco: the encoding / decoding problem should be completely translated to WebdavClient & WebdavEntry, but at this moment we are in a little hurry
64 if (path
!= null
&& path
.length() > 0) {
66 new URL("http://silly.test.com:8888" + path
);
67 } catch (MalformedURLException e
) {
68 throw new RuntimeException("Trying to create a OCFile with a non valid remote path: " + path
, e
);
70 } else throw new RuntimeException("Trying to create a OCFile with a non valid remote path: " + path
);
71 // save encoded paths have a problem: normalization; this is a quick&dirty fix to avoid duplications
72 mRemotePath
= Uri
.encode(Uri
.decode(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;
93 * Gets the ID of the file
97 public long getFileId() {
102 * Returns the remote path of the file on ownCloud
104 * @return The remote path to the file
106 public String
getRemotePath() {
111 * Returns the remote path of the file on ownCloud
113 * @return The remote path to the file
115 public String
getURLDecodedRemotePath() {
116 return Uri
.decode(mRemotePath
);
120 * Can be used to check, whether or not this file exists in the database
123 * @return true, if the file exists in the database
125 public boolean fileExists() {
130 * Use this to find out if this file is a Directory
132 * @return true if it is a directory
134 public boolean isDirectory() {
135 return mMimeType
!= null
&& mMimeType
.equals("DIR");
139 * Use this to check if this file is available locally
141 * @return true if it is
143 public boolean isDownloaded() {
144 return mLocalPath
!= null
&& !mLocalPath
.equals("");
148 * The path, where the file is stored locally
150 * @return The local path to the file
152 public String
getStoragePath() {
157 * Can be used to set the path where the file is stored
159 * @param storage_path to set
161 public void setStoragePath(String storage_path
) {
162 mLocalPath
= storage_path
;
166 * Get a UNIX timestamp of the file creation time
168 * @return A UNIX timestamp of the time that file was created
170 public long getCreationTimestamp() {
171 return mCreationTimestamp
;
175 * Set a UNIX timestamp of the time the file was created
177 * @param creation_timestamp to set
179 public void setCreationTimestamp(long creation_timestamp
) {
180 mCreationTimestamp
= creation_timestamp
;
184 * Get a UNIX timestamp of the file modification time
186 * @return A UNIX timestamp of the modification time
188 public long getModificationTimestamp() {
189 return mModifiedTimestamp
;
193 * Set a UNIX timestamp of the time the time the file was modified.
195 * @param modification_timestamp to set
197 public void setModificationTimestamp(long modification_timestamp
) {
198 mModifiedTimestamp
= modification_timestamp
;
202 * Returns the filename and "/" for the root directory
204 * @return The name of the file
206 public String
getFileName() {
207 File f
= new File(getURLDecodedRemotePath());
208 return f
.getName().length() == 0 ?
"/" : f
.getName();
212 * Can be used to get the Mimetype
214 * @return the Mimetype as a String
216 public String
getMimetype() {
221 * Adds a file to this directory. If this file is not a directory, an
222 * exception gets thrown.
225 * @throws IllegalStateException if you try to add a something and this is
228 public void addFile(OCFile file
) throws IllegalStateException
{
230 file
.mParentId
= mId
;
231 mNeedsUpdating
= true
;
234 throw new IllegalStateException(
235 "This is not a directory where you can add stuff to!");
239 * Used internally. Reset all file properties
241 private void resetData() {
248 mCreationTimestamp
= 0;
249 mModifiedTimestamp
= 0;
254 * Sets the ID of the file
256 * @param file_id to set
258 public void setFileId(long file_id
) {
263 * Sets the Mime-Type of the
265 * @param mimetype to set
267 public void setMimetype(String mimetype
) {
268 mMimeType
= mimetype
;
272 * Sets the ID of the parent folder
274 * @param parent_id to set
276 public void setParentId(long parent_id
) {
277 mParentId
= parent_id
;
281 * Sets the file size in bytes
283 * @param file_len to set
285 public void setFileLength(long file_len
) {
290 * Returns the size of the file in bytes
292 * @return The filesize in bytes
294 public long getFileLength() {
299 * Returns the ID of the parent Folder
303 public long getParentId() {
308 * Check, if this file needs updating
312 public boolean needsUpdatingWhileSaving() {
313 return mNeedsUpdating
;
316 public long getLastSyncDate() {
317 return mLastSyncDate
;
320 public void setLastSyncDate(long lastSyncDate
) {
321 mLastSyncDate
= lastSyncDate
;
325 public int describeContents() {
326 return this.hashCode();
330 public void writeToParcel(Parcel dest
, int flags
) {
332 dest
.writeLong(mParentId
);
333 dest
.writeLong(mLength
);
334 dest
.writeLong(mCreationTimestamp
);
335 dest
.writeLong(mModifiedTimestamp
);
336 dest
.writeString(mRemotePath
);
337 dest
.writeString(mLocalPath
);
338 dest
.writeString(mMimeType
);
339 dest
.writeInt(mNeedsUpdating ?
1 : 0);
340 dest
.writeLong(mLastSyncDate
);
344 public int compareTo(OCFile another
) {
345 if (isDirectory() && another
.isDirectory()) {
346 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
347 } else if (isDirectory()) {
349 } else if (another
.isDirectory()) {
352 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
355 public boolean equals(Object o
) {
356 if(o
instanceof OCFile
){
357 OCFile that
= (OCFile
) o
;
359 return this.mId
== that
.mId
;
367 public String
toString() {
368 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s]";
369 asString
= String
.format(asString
, new Long(mId
), getFileName(), mMimeType
, isDownloaded(), mLocalPath
, mRemotePath
);