1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
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 com
.owncloud
.android
.utils
.Log_OC
;
26 import android
.os
.Parcel
;
27 import android
.os
.Parcelable
;
28 import android
.webkit
.MimeTypeMap
;
30 public class OCFile
implements Parcelable
, Comparable
<OCFile
> {
32 public static final Parcelable
.Creator
<OCFile
> CREATOR
= new Parcelable
.Creator
<OCFile
>() {
34 public OCFile
createFromParcel(Parcel source
) {
35 return new OCFile(source
);
39 public OCFile
[] newArray(int size
) {
40 return new OCFile
[size
];
44 public static final String PATH_SEPARATOR
= "/";
45 public static final String ROOT_PATH
= PATH_SEPARATOR
;
47 private static final String TAG
= OCFile
.class.getSimpleName();
50 private long mParentId
;
52 private long mCreationTimestamp
;
53 private long mModifiedTimestamp
;
54 private long mModifiedTimestampAtLastSyncForData
;
55 private String mRemotePath
;
56 private String mLocalPath
;
57 private String mMimeType
;
58 private boolean mNeedsUpdating
;
59 private long mLastSyncDateForProperties
;
60 private long mLastSyncDateForData
;
61 private boolean mKeepInSync
;
65 private boolean mShareByLink
;
66 private String mPublicLink
;
68 private String mPermissions
;
72 * Create new {@link OCFile} with given path.
74 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
76 * @param path The remote path of the file.
78 public OCFile(String path
) {
80 mNeedsUpdating
= false
;
81 if (path
== null
|| path
.length() <= 0 || !path
.startsWith(PATH_SEPARATOR
)) {
82 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path
);
88 * Reconstruct from parcel
90 * @param source The source parcel
92 private OCFile(Parcel source
) {
93 mId
= source
.readLong();
94 mParentId
= source
.readLong();
95 mLength
= source
.readLong();
96 mCreationTimestamp
= source
.readLong();
97 mModifiedTimestamp
= source
.readLong();
98 mModifiedTimestampAtLastSyncForData
= source
.readLong();
99 mRemotePath
= source
.readString();
100 mLocalPath
= source
.readString();
101 mMimeType
= source
.readString();
102 mNeedsUpdating
= source
.readInt() == 0;
103 mKeepInSync
= source
.readInt() == 1;
104 mLastSyncDateForProperties
= source
.readLong();
105 mLastSyncDateForData
= source
.readLong();
106 mEtag
= source
.readString();
107 mShareByLink
= source
.readInt() == 1;
108 mPublicLink
= source
.readString();
109 mPermissions
= source
.readString();
113 public void writeToParcel(Parcel dest
, int flags
) {
115 dest
.writeLong(mParentId
);
116 dest
.writeLong(mLength
);
117 dest
.writeLong(mCreationTimestamp
);
118 dest
.writeLong(mModifiedTimestamp
);
119 dest
.writeLong(mModifiedTimestampAtLastSyncForData
);
120 dest
.writeString(mRemotePath
);
121 dest
.writeString(mLocalPath
);
122 dest
.writeString(mMimeType
);
123 dest
.writeInt(mNeedsUpdating ?
1 : 0);
124 dest
.writeInt(mKeepInSync ?
1 : 0);
125 dest
.writeLong(mLastSyncDateForProperties
);
126 dest
.writeLong(mLastSyncDateForData
);
127 dest
.writeString(mEtag
);
128 dest
.writeInt(mShareByLink ?
1 : 0);
129 dest
.writeString(mPublicLink
);
130 dest
.writeString(mPermissions
);
134 * Gets the ID of the file
136 * @return the file ID
138 public long getFileId() {
143 * Returns the remote path of the file on ownCloud
145 * @return The remote path to the file
147 public String
getRemotePath() {
152 * Can be used to check, whether or not this file exists in the database
155 * @return true, if the file exists in the database
157 public boolean fileExists() {
162 * Use this to find out if this file is a folder.
164 * @return true if it is a folder
166 public boolean isFolder() {
167 return mMimeType
!= null
&& mMimeType
.equals("DIR");
171 * Use this to check if this file is available locally
173 * @return true if it is
175 public boolean isDown() {
176 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
177 File file
= new File(mLocalPath
);
178 return (file
.exists());
184 * The path, where the file is stored locally
186 * @return The local path to the file
188 public String
getStoragePath() {
193 * Can be used to set the path where the file is stored
195 * @param storage_path to set
197 public void setStoragePath(String storage_path
) {
198 mLocalPath
= storage_path
;
202 * Get a UNIX timestamp of the file creation time
204 * @return A UNIX timestamp of the time that file was created
206 public long getCreationTimestamp() {
207 return mCreationTimestamp
;
211 * Set a UNIX timestamp of the time the file was created
213 * @param creation_timestamp to set
215 public void setCreationTimestamp(long creation_timestamp
) {
216 mCreationTimestamp
= creation_timestamp
;
220 * Get a UNIX timestamp of the file modification time.
222 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
223 * in the last synchronization of the properties of this file.
225 public long getModificationTimestamp() {
226 return mModifiedTimestamp
;
230 * Set a UNIX timestamp of the time the time the file was modified.
232 * To update with the value returned by the server in every synchronization of the properties
235 * @param modification_timestamp to set
237 public void setModificationTimestamp(long modification_timestamp
) {
238 mModifiedTimestamp
= modification_timestamp
;
243 * Get a UNIX timestamp of the file modification time.
245 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
246 * in the last synchronization of THE CONTENTS of this file.
248 public long getModificationTimestampAtLastSyncForData() {
249 return mModifiedTimestampAtLastSyncForData
;
253 * Set a UNIX timestamp of the time the time the file was modified.
255 * To update with the value returned by the server in every synchronization of THE CONTENTS
258 * @param modification_timestamp to set
260 public void setModificationTimestampAtLastSyncForData(long modificationTimestamp
) {
261 mModifiedTimestampAtLastSyncForData
= modificationTimestamp
;
267 * Returns the filename and "/" for the root directory
269 * @return The name of the file
271 public String
getFileName() {
272 File f
= new File(getRemotePath());
273 return f
.getName().length() == 0 ? ROOT_PATH
: f
.getName();
277 * Sets the name of the file
279 * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root directory
281 public void setFileName(String name
) {
282 Log_OC
.d(TAG
, "OCFile name changin from " + mRemotePath
);
283 if (name
!= null
&& name
.length() > 0 && !name
.contains(PATH_SEPARATOR
) && !mRemotePath
.equals(ROOT_PATH
)) {
284 String parent
= (new File(getRemotePath())).getParent();
285 parent
= (parent
.endsWith(PATH_SEPARATOR
)) ? parent
: parent
+ PATH_SEPARATOR
;
286 mRemotePath
= parent
+ name
;
288 mRemotePath
+= PATH_SEPARATOR
;
290 Log_OC
.d(TAG
, "OCFile name changed to " + mRemotePath
);
295 * Can be used to get the Mimetype
297 * @return the Mimetype as a String
299 public String
getMimetype() {
304 * Adds a file to this directory. If this file is not a directory, an
305 * exception gets thrown.
308 * @throws IllegalStateException if you try to add a something and this is
311 public void addFile(OCFile file
) throws IllegalStateException
{
313 file
.mParentId
= mId
;
314 mNeedsUpdating
= true
;
317 throw new IllegalStateException(
318 "This is not a directory where you can add stuff to!");
322 * Used internally. Reset all file properties
324 private void resetData() {
331 mCreationTimestamp
= 0;
332 mModifiedTimestamp
= 0;
333 mModifiedTimestampAtLastSyncForData
= 0;
334 mLastSyncDateForProperties
= 0;
335 mLastSyncDateForData
= 0;
337 mNeedsUpdating
= false
;
339 mShareByLink
= false
;
345 * Sets the ID of the file
347 * @param file_id to set
349 public void setFileId(long file_id
) {
354 * Sets the Mime-Type of the
356 * @param mimetype to set
358 public void setMimetype(String mimetype
) {
359 mMimeType
= mimetype
;
363 * Sets the ID of the parent folder
365 * @param parent_id to set
367 public void setParentId(long parent_id
) {
368 mParentId
= parent_id
;
372 * Sets the file size in bytes
374 * @param file_len to set
376 public void setFileLength(long file_len
) {
381 * Returns the size of the file in bytes
383 * @return The filesize in bytes
385 public long getFileLength() {
390 * Returns the ID of the parent Folder
394 public long getParentId() {
399 * Check, if this file needs updating
403 public boolean needsUpdatingWhileSaving() {
404 return mNeedsUpdating
;
407 public long getLastSyncDateForProperties() {
408 return mLastSyncDateForProperties
;
411 public void setLastSyncDateForProperties(long lastSyncDate
) {
412 mLastSyncDateForProperties
= lastSyncDate
;
415 public long getLastSyncDateForData() {
416 return mLastSyncDateForData
;
419 public void setLastSyncDateForData(long lastSyncDate
) {
420 mLastSyncDateForData
= lastSyncDate
;
423 public void setKeepInSync(boolean keepInSync
) {
424 mKeepInSync
= keepInSync
;
427 public boolean keepInSync() {
432 public int describeContents() {
433 return this.hashCode();
437 public int compareTo(OCFile another
) {
438 if (isFolder() && another
.isFolder()) {
439 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
440 } else if (isFolder()) {
442 } else if (another
.isFolder()) {
445 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
449 public boolean equals(Object o
) {
450 if(o
instanceof OCFile
){
451 OCFile that
= (OCFile
) o
;
453 return this.mId
== that
.mId
;
461 public String
toString() {
462 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSync=%s etag=%s]";
463 asString
= String
.format(asString
, Long
.valueOf(mId
), getFileName(), mMimeType
, isDown(), mLocalPath
, mRemotePath
, Long
.valueOf(mParentId
), Boolean
.valueOf(mKeepInSync
), mEtag
);
467 public String
getEtag() {
471 public void setEtag(String etag
) {
476 public boolean isShareByLink() {
480 public void setShareByLink(boolean shareByLink
) {
481 this.mShareByLink
= shareByLink
;
484 public String
getPublicLink() {
488 public void setPublicLink(String publicLink
) {
489 this.mPublicLink
= publicLink
;
492 public long getLocalModificationTimestamp() {
493 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
494 File f
= new File(mLocalPath
);
495 return f
.lastModified();
500 /** @return 'True' if the file contains audio */
501 public boolean isAudio() {
502 return (mMimeType
!= null
&& mMimeType
.startsWith("audio/"));
505 /** @return 'True' if the file contains video */
506 public boolean isVideo() {
507 return (mMimeType
!= null
&& mMimeType
.startsWith("video/"));
510 /** @return 'True' if the file contains an image */
511 public boolean isImage() {
512 return ((mMimeType
!= null
&& mMimeType
.startsWith("image/")) ||
513 getMimeTypeFromName().startsWith("image/"));
516 public String
getMimeTypeFromName() {
517 String extension
= "";
518 int pos
= mRemotePath
.lastIndexOf('.');
520 extension
= mRemotePath
.substring(pos
+ 1);
522 String result
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(extension
.toLowerCase());
523 return (result
!= null
) ? result
: "";
526 public String
getPermissions() {
530 public void setPermissions(String permissions
) {
531 this.mPermissions
= permissions
;