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: let's make mandatory that mRemotePath is a valid URL always; this will make our life easier with the URL-encoding/decoding
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
);
75 * Reconstruct from parcel
77 * @param source The source parcel
79 private OCFile(Parcel source
) {
80 mId
= source
.readLong();
81 mParentId
= source
.readLong();
82 mLength
= source
.readLong();
83 mCreationTimestamp
= source
.readLong();
84 mModifiedTimestamp
= source
.readLong();
85 mRemotePath
= source
.readString();
86 mLocalPath
= source
.readString();
87 mMimeType
= source
.readString();
88 mNeedsUpdating
= source
.readInt() == 0;
92 * Gets the ID of the file
96 public long getFileId() {
101 * Returns the remote path of the file on ownCloud
103 * @return The remote path to the file
105 public String
getRemotePath() {
110 * Returns the remote path of the file on ownCloud
112 * @return The remote path to the file
114 public String
getURLDecodedRemotePath() {
115 return Uri
.decode(mRemotePath
);
119 * Can be used to check, whether or not this file exists in the database
122 * @return true, if the file exists in the database
124 public boolean fileExists() {
129 * Use this to find out if this file is a Directory
131 * @return true if it is a directory
133 public boolean isDirectory() {
134 return mMimeType
!= null
&& mMimeType
.equals("DIR");
138 * Use this to check if this file is available locally
140 * @return true if it is
142 public boolean isDownloaded() {
143 return mLocalPath
!= null
&& !mLocalPath
.equals("");
147 * The path, where the file is stored locally
149 * @return The local path to the file
151 public String
getStoragePath() {
156 * Can be used to set the path where the file is stored
158 * @param storage_path to set
160 public void setStoragePath(String storage_path
) {
161 mLocalPath
= storage_path
;
165 * Get a UNIX timestamp of the file creation time
167 * @return A UNIX timestamp of the time that file was created
169 public long getCreationTimestamp() {
170 return mCreationTimestamp
;
174 * Set a UNIX timestamp of the time the file was created
176 * @param creation_timestamp to set
178 public void setCreationTimestamp(long creation_timestamp
) {
179 mCreationTimestamp
= creation_timestamp
;
183 * Get a UNIX timestamp of the file modification time
185 * @return A UNIX timestamp of the modification time
187 public long getModificationTimestamp() {
188 return mModifiedTimestamp
;
192 * Set a UNIX timestamp of the time the time the file was modified.
194 * @param modification_timestamp to set
196 public void setModificationTimestamp(long modification_timestamp
) {
197 mModifiedTimestamp
= modification_timestamp
;
201 * Returns the filename and "/" for the root directory
203 * @return The name of the file
205 public String
getFileName() {
206 File f
= new File(getURLDecodedRemotePath());
207 return f
.getName().length() == 0 ?
"/" : f
.getName();
211 * Can be used to get the Mimetype
213 * @return the Mimetype as a String
215 public String
getMimetype() {
220 * Adds a file to this directory. If this file is not a directory, an
221 * exception gets thrown.
224 * @throws IllegalStateException if you try to add a something and this is
227 public void addFile(OCFile file
) throws IllegalStateException
{
229 file
.mParentId
= mId
;
230 mNeedsUpdating
= true
;
233 throw new IllegalStateException(
234 "This is not a directory where you can add stuff to!");
238 * Used internally. Reset all file properties
240 private void resetData() {
247 mCreationTimestamp
= 0;
248 mModifiedTimestamp
= 0;
253 * Sets the ID of the file
255 * @param file_id to set
257 public void setFileId(long file_id
) {
262 * Sets the Mime-Type of the
264 * @param mimetype to set
266 public void setMimetype(String mimetype
) {
267 mMimeType
= mimetype
;
271 * Sets the ID of the parent folder
273 * @param parent_id to set
275 public void setParentId(long parent_id
) {
276 mParentId
= parent_id
;
280 * Sets the file size in bytes
282 * @param file_len to set
284 public void setFileLength(long file_len
) {
289 * Returns the size of the file in bytes
291 * @return The filesize in bytes
293 public long getFileLength() {
298 * Returns the ID of the parent Folder
302 public long getParentId() {
307 * Check, if this file needs updating
311 public boolean needsUpdatingWhileSaving() {
312 return mNeedsUpdating
;
315 public long getLastSyncDate() {
316 return mLastSyncDate
;
319 public void setLastSyncDate(long lastSyncDate
) {
320 mLastSyncDate
= lastSyncDate
;
324 public int describeContents() {
325 return this.hashCode();
329 public void writeToParcel(Parcel dest
, int flags
) {
331 dest
.writeLong(mParentId
);
332 dest
.writeLong(mLength
);
333 dest
.writeLong(mCreationTimestamp
);
334 dest
.writeLong(mModifiedTimestamp
);
335 dest
.writeString(mRemotePath
);
336 dest
.writeString(mLocalPath
);
337 dest
.writeString(mMimeType
);
338 dest
.writeInt(mNeedsUpdating ?
1 : 0);
339 dest
.writeLong(mLastSyncDate
);
343 public int compareTo(OCFile another
) {
344 if (isDirectory() && another
.isDirectory()) {
345 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
346 } else if (isDirectory()) {
348 } else if (another
.isDirectory()) {
351 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
354 public boolean equals(Object o
) {
355 if(o
instanceof OCFile
){
356 OCFile that
= (OCFile
) o
;
358 return this.mId
== that
.mId
;
366 public String
toString() {
367 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s]";
368 asString
= String
.format(asString
, new Long(mId
), getFileName(), mMimeType
, isDownloaded(), mLocalPath
, mRemotePath
);