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
;
21 import android
.os
.Parcel
;
22 import android
.os
.Parcelable
;
23 import android
.webkit
.MimeTypeMap
;
25 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
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
= "/";
44 public static final String ROOT_PATH
= PATH_SEPARATOR
;
46 private static final String TAG
= OCFile
.class.getSimpleName();
49 private long mParentId
;
51 private long mCreationTimestamp
;
52 private long mModifiedTimestamp
;
53 private long mModifiedTimestampAtLastSyncForData
;
54 private String mRemotePath
;
55 private String mLocalPath
;
56 private String mMimeType
;
57 private boolean mNeedsUpdating
;
58 private long mLastSyncDateForProperties
;
59 private long mLastSyncDateForData
;
60 private boolean mKeepInSync
;
64 private boolean mShareByLink
;
65 private String mPublicLink
;
67 private String mPermissions
;
68 private String mRemoteId
;
70 private boolean mNeedsUpdateThumbnail
;
74 * Create new {@link OCFile} with given path.
76 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
78 * @param path The remote path of the file.
80 public OCFile(String path
) {
82 mNeedsUpdating
= false
;
83 if (path
== null
|| path
.length() <= 0 || !path
.startsWith(PATH_SEPARATOR
)) {
84 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path
);
90 * Reconstruct from parcel
92 * @param source The source parcel
94 private OCFile(Parcel source
) {
95 mId
= source
.readLong();
96 mParentId
= source
.readLong();
97 mLength
= source
.readLong();
98 mCreationTimestamp
= source
.readLong();
99 mModifiedTimestamp
= source
.readLong();
100 mModifiedTimestampAtLastSyncForData
= source
.readLong();
101 mRemotePath
= source
.readString();
102 mLocalPath
= source
.readString();
103 mMimeType
= source
.readString();
104 mNeedsUpdating
= source
.readInt() == 0;
105 mKeepInSync
= source
.readInt() == 1;
106 mLastSyncDateForProperties
= source
.readLong();
107 mLastSyncDateForData
= source
.readLong();
108 mEtag
= source
.readString();
109 mShareByLink
= source
.readInt() == 1;
110 mPublicLink
= source
.readString();
111 mPermissions
= source
.readString();
112 mRemoteId
= source
.readString();
113 mNeedsUpdateThumbnail
= source
.readInt() == 0;
118 public void writeToParcel(Parcel dest
, int flags
) {
120 dest
.writeLong(mParentId
);
121 dest
.writeLong(mLength
);
122 dest
.writeLong(mCreationTimestamp
);
123 dest
.writeLong(mModifiedTimestamp
);
124 dest
.writeLong(mModifiedTimestampAtLastSyncForData
);
125 dest
.writeString(mRemotePath
);
126 dest
.writeString(mLocalPath
);
127 dest
.writeString(mMimeType
);
128 dest
.writeInt(mNeedsUpdating ?
1 : 0);
129 dest
.writeInt(mKeepInSync ?
1 : 0);
130 dest
.writeLong(mLastSyncDateForProperties
);
131 dest
.writeLong(mLastSyncDateForData
);
132 dest
.writeString(mEtag
);
133 dest
.writeInt(mShareByLink ?
1 : 0);
134 dest
.writeString(mPublicLink
);
135 dest
.writeString(mPermissions
);
136 dest
.writeString(mRemoteId
);
137 dest
.writeInt(mNeedsUpdateThumbnail ?
1 : 0);
141 * Gets the ID of the file
143 * @return the file ID
145 public long getFileId() {
150 * Returns the remote path of the file on ownCloud
152 * @return The remote path to the file
154 public String
getRemotePath() {
159 * Can be used to check, whether or not this file exists in the database
162 * @return true, if the file exists in the database
164 public boolean fileExists() {
169 * Use this to find out if this file is a folder.
171 * @return true if it is a folder
173 public boolean isFolder() {
174 return mMimeType
!= null
&& mMimeType
.equals("DIR");
178 * Use this to check if this file is available locally
180 * @return true if it is
182 public boolean isDown() {
183 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
184 File file
= new File(mLocalPath
);
185 return (file
.exists());
191 * The path, where the file is stored locally
193 * @return The local path to the file
195 public String
getStoragePath() {
200 * Can be used to set the path where the file is stored
202 * @param storage_path to set
204 public void setStoragePath(String storage_path
) {
205 mLocalPath
= storage_path
;
209 * Get a UNIX timestamp of the file creation time
211 * @return A UNIX timestamp of the time that file was created
213 public long getCreationTimestamp() {
214 return mCreationTimestamp
;
218 * Set a UNIX timestamp of the time the file was created
220 * @param creation_timestamp to set
222 public void setCreationTimestamp(long creation_timestamp
) {
223 mCreationTimestamp
= creation_timestamp
;
227 * Get a UNIX timestamp of the file modification time.
229 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
230 * in the last synchronization of the properties of this file.
232 public long getModificationTimestamp() {
233 return mModifiedTimestamp
;
237 * Set a UNIX timestamp of the time the time the file was modified.
239 * To update with the value returned by the server in every synchronization of the properties
242 * @param modification_timestamp to set
244 public void setModificationTimestamp(long modification_timestamp
) {
245 mModifiedTimestamp
= modification_timestamp
;
250 * Get a UNIX timestamp of the file modification time.
252 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
253 * in the last synchronization of THE CONTENTS of this file.
255 public long getModificationTimestampAtLastSyncForData() {
256 return mModifiedTimestampAtLastSyncForData
;
260 * Set a UNIX timestamp of the time the time the file was modified.
262 * To update with the value returned by the server in every synchronization of THE CONTENTS
265 * @param modificationTimestamp to set
267 public void setModificationTimestampAtLastSyncForData(long modificationTimestamp
) {
268 mModifiedTimestampAtLastSyncForData
= modificationTimestamp
;
273 * Returns the filename and "/" for the root directory
275 * @return The name of the file
277 public String
getFileName() {
278 File f
= new File(getRemotePath());
279 return f
.getName().length() == 0 ? ROOT_PATH
: f
.getName();
283 * Sets the name of the file
285 * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root directory
287 public void setFileName(String name
) {
288 Log_OC
.d(TAG
, "OCFile name changin from " + mRemotePath
);
289 if (name
!= null
&& name
.length() > 0 && !name
.contains(PATH_SEPARATOR
) && !mRemotePath
.equals(ROOT_PATH
)) {
290 String parent
= (new File(getRemotePath())).getParent();
291 parent
= (parent
.endsWith(PATH_SEPARATOR
)) ? parent
: parent
+ PATH_SEPARATOR
;
292 mRemotePath
= parent
+ name
;
294 mRemotePath
+= PATH_SEPARATOR
;
296 Log_OC
.d(TAG
, "OCFile name changed to " + mRemotePath
);
301 * Can be used to get the Mimetype
303 * @return the Mimetype as a String
305 public String
getMimetype() {
310 * Adds a file to this directory. If this file is not a directory, an
311 * exception gets thrown.
314 * @throws IllegalStateException if you try to add a something and this is
317 public void addFile(OCFile file
) throws IllegalStateException
{
319 file
.mParentId
= mId
;
320 mNeedsUpdating
= true
;
323 throw new IllegalStateException(
324 "This is not a directory where you can add stuff to!");
328 * Used internally. Reset all file properties
330 private void resetData() {
337 mCreationTimestamp
= 0;
338 mModifiedTimestamp
= 0;
339 mModifiedTimestampAtLastSyncForData
= 0;
340 mLastSyncDateForProperties
= 0;
341 mLastSyncDateForData
= 0;
343 mNeedsUpdating
= false
;
345 mShareByLink
= false
;
349 mNeedsUpdateThumbnail
= false
;
353 * Sets the ID of the file
355 * @param file_id to set
357 public void setFileId(long file_id
) {
362 * Sets the Mime-Type of the
364 * @param mimetype to set
366 public void setMimetype(String mimetype
) {
367 mMimeType
= mimetype
;
371 * Sets the ID of the parent folder
373 * @param parent_id to set
375 public void setParentId(long parent_id
) {
376 mParentId
= parent_id
;
380 * Sets the file size in bytes
382 * @param file_len to set
384 public void setFileLength(long file_len
) {
389 * Returns the size of the file in bytes
391 * @return The filesize in bytes
393 public long getFileLength() {
398 * Returns the ID of the parent Folder
402 public long getParentId() {
407 * Check, if this file needs updating
411 public boolean needsUpdatingWhileSaving() {
412 return mNeedsUpdating
;
415 public boolean needsUpdateThumbnail() {
416 return mNeedsUpdateThumbnail
;
419 public void setNeedsUpdateThumbnail(boolean needsUpdateThumbnail
) {
420 this.mNeedsUpdateThumbnail
= needsUpdateThumbnail
;
423 public long getLastSyncDateForProperties() {
424 return mLastSyncDateForProperties
;
427 public void setLastSyncDateForProperties(long lastSyncDate
) {
428 mLastSyncDateForProperties
= lastSyncDate
;
431 public long getLastSyncDateForData() {
432 return mLastSyncDateForData
;
435 public void setLastSyncDateForData(long lastSyncDate
) {
436 mLastSyncDateForData
= lastSyncDate
;
439 public void setKeepInSync(boolean keepInSync
) {
440 mKeepInSync
= keepInSync
;
443 public boolean keepInSync() {
448 public int describeContents() {
449 return super.hashCode();
453 public int compareTo(OCFile another
) {
454 if (isFolder() && another
.isFolder()) {
455 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
456 } else if (isFolder()) {
458 } else if (another
.isFolder()) {
461 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
465 public boolean equals(Object o
) {
466 if (o
instanceof OCFile
) {
467 OCFile that
= (OCFile
) o
;
469 return this.mId
== that
.mId
;
477 public String
toString() {
478 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSync=%s etag=%s]";
479 asString
= String
.format(asString
, Long
.valueOf(mId
), getFileName(), mMimeType
, isDown(), mLocalPath
, mRemotePath
, Long
.valueOf(mParentId
), Boolean
.valueOf(mKeepInSync
), mEtag
);
483 public String
getEtag() {
487 public void setEtag(String etag
) {
492 public boolean isShareByLink() {
496 public void setShareByLink(boolean shareByLink
) {
497 this.mShareByLink
= shareByLink
;
500 public String
getPublicLink() {
504 public void setPublicLink(String publicLink
) {
505 this.mPublicLink
= publicLink
;
508 public long getLocalModificationTimestamp() {
509 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
510 File f
= new File(mLocalPath
);
511 return f
.lastModified();
517 * @return 'True' if the file contains audio
519 public boolean isAudio() {
520 return (mMimeType
!= null
&& mMimeType
.startsWith("audio/"));
524 * @return 'True' if the file contains video
526 public boolean isVideo() {
527 return (mMimeType
!= null
&& mMimeType
.startsWith("video/"));
531 * @return 'True' if the file contains an image
533 public boolean isImage() {
534 return ((mMimeType
!= null
&& mMimeType
.startsWith("image/")) ||
535 getMimeTypeFromName().startsWith("image/"));
539 * @return 'True' if the file is simple text (e.g. not application-dependent, like .doc or .docx)
541 public boolean isText() {
542 return !isFolder() && !isAudio() && !isVideo() && !isImage() && ((mMimeType
!= null
&& mMimeType
.startsWith("text/")) ||
543 getMimeTypeFromName().startsWith("text/"));
546 public String
getMimeTypeFromName() {
547 String extension
= "";
548 int pos
= mRemotePath
.lastIndexOf('.');
550 extension
= mRemotePath
.substring(pos
+ 1);
552 String result
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(extension
.toLowerCase());
553 return (result
!= null
) ? result
: "";
556 public String
getPermissions() {
560 public void setPermissions(String permissions
) {
561 this.mPermissions
= permissions
;
564 public String
getRemoteId() {
568 public void setRemoteId(String remoteId
) {
569 this.mRemoteId
= remoteId
;