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 import third_parties
.daveKoeller
.AlphanumComparator
;
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
;
69 private String mRemoteId
;
71 private boolean mNeedsUpdateThumbnail
;
73 private boolean mIsDownloading
;
74 private boolean mIsUploading
;
78 * Create new {@link OCFile} with given path.
80 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
82 * @param path The remote path of the file.
84 public OCFile(String path
) {
86 mNeedsUpdating
= false
;
87 if (path
== null
|| path
.length() <= 0 || !path
.startsWith(PATH_SEPARATOR
)) {
88 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path
);
94 * Reconstruct from parcel
96 * @param source The source parcel
98 private OCFile(Parcel source
) {
99 mId
= source
.readLong();
100 mParentId
= source
.readLong();
101 mLength
= source
.readLong();
102 mCreationTimestamp
= source
.readLong();
103 mModifiedTimestamp
= source
.readLong();
104 mModifiedTimestampAtLastSyncForData
= source
.readLong();
105 mRemotePath
= source
.readString();
106 mLocalPath
= source
.readString();
107 mMimeType
= source
.readString();
108 mNeedsUpdating
= source
.readInt() == 0;
109 mKeepInSync
= source
.readInt() == 1;
110 mLastSyncDateForProperties
= source
.readLong();
111 mLastSyncDateForData
= source
.readLong();
112 mEtag
= source
.readString();
113 mShareByLink
= source
.readInt() == 1;
114 mPublicLink
= source
.readString();
115 mPermissions
= source
.readString();
116 mRemoteId
= source
.readString();
117 mNeedsUpdateThumbnail
= source
.readInt() == 0;
118 mIsDownloading
= source
.readInt() == 0;
119 mIsUploading
= source
.readInt() == 0;
124 public void writeToParcel(Parcel dest
, int flags
) {
126 dest
.writeLong(mParentId
);
127 dest
.writeLong(mLength
);
128 dest
.writeLong(mCreationTimestamp
);
129 dest
.writeLong(mModifiedTimestamp
);
130 dest
.writeLong(mModifiedTimestampAtLastSyncForData
);
131 dest
.writeString(mRemotePath
);
132 dest
.writeString(mLocalPath
);
133 dest
.writeString(mMimeType
);
134 dest
.writeInt(mNeedsUpdating ?
1 : 0);
135 dest
.writeInt(mKeepInSync ?
1 : 0);
136 dest
.writeLong(mLastSyncDateForProperties
);
137 dest
.writeLong(mLastSyncDateForData
);
138 dest
.writeString(mEtag
);
139 dest
.writeInt(mShareByLink ?
1 : 0);
140 dest
.writeString(mPublicLink
);
141 dest
.writeString(mPermissions
);
142 dest
.writeString(mRemoteId
);
143 dest
.writeInt(mNeedsUpdateThumbnail ?
1 : 0);
144 dest
.writeInt(mIsDownloading ?
1 : 0);
145 dest
.writeInt(mIsUploading ?
1 : 0);
149 * Gets the ID of the file
151 * @return the file ID
153 public long getFileId() {
158 * Returns the remote path of the file on ownCloud
160 * @return The remote path to the file
162 public String
getRemotePath() {
167 * Can be used to check, whether or not this file exists in the database
170 * @return true, if the file exists in the database
172 public boolean fileExists() {
177 * Use this to find out if this file is a folder.
179 * @return true if it is a folder
181 public boolean isFolder() {
182 return mMimeType
!= null
&& mMimeType
.equals("DIR");
186 * Use this to check if this file is available locally
188 * @return true if it is
190 public boolean isDown() {
191 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
192 File file
= new File(mLocalPath
);
193 return (file
.exists());
199 * The path, where the file is stored locally
201 * @return The local path to the file
203 public String
getStoragePath() {
208 * Can be used to set the path where the file is stored
210 * @param storage_path to set
212 public void setStoragePath(String storage_path
) {
213 mLocalPath
= storage_path
;
217 * Get a UNIX timestamp of the file creation time
219 * @return A UNIX timestamp of the time that file was created
221 public long getCreationTimestamp() {
222 return mCreationTimestamp
;
226 * Set a UNIX timestamp of the time the file was created
228 * @param creation_timestamp to set
230 public void setCreationTimestamp(long creation_timestamp
) {
231 mCreationTimestamp
= creation_timestamp
;
235 * Get a UNIX timestamp of the file modification time.
237 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
238 * in the last synchronization of the properties of this file.
240 public long getModificationTimestamp() {
241 return mModifiedTimestamp
;
245 * Set a UNIX timestamp of the time the time the file was modified.
247 * To update with the value returned by the server in every synchronization of the properties
250 * @param modification_timestamp to set
252 public void setModificationTimestamp(long modification_timestamp
) {
253 mModifiedTimestamp
= modification_timestamp
;
258 * Get a UNIX timestamp of the file modification time.
260 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
261 * in the last synchronization of THE CONTENTS of this file.
263 public long getModificationTimestampAtLastSyncForData() {
264 return mModifiedTimestampAtLastSyncForData
;
268 * Set a UNIX timestamp of the time the time the file was modified.
270 * To update with the value returned by the server in every synchronization of THE CONTENTS
273 * @param modificationTimestamp to set
275 public void setModificationTimestampAtLastSyncForData(long modificationTimestamp
) {
276 mModifiedTimestampAtLastSyncForData
= modificationTimestamp
;
281 * Returns the filename and "/" for the root directory
283 * @return The name of the file
285 public String
getFileName() {
286 File f
= new File(getRemotePath());
287 return f
.getName().length() == 0 ? ROOT_PATH
: f
.getName();
291 * Sets the name of the file
293 * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root directory
295 public void setFileName(String name
) {
296 Log_OC
.d(TAG
, "OCFile name changin from " + mRemotePath
);
297 if (name
!= null
&& name
.length() > 0 && !name
.contains(PATH_SEPARATOR
) && !mRemotePath
.equals(ROOT_PATH
)) {
298 String parent
= (new File(getRemotePath())).getParent();
299 parent
= (parent
.endsWith(PATH_SEPARATOR
)) ? parent
: parent
+ PATH_SEPARATOR
;
300 mRemotePath
= parent
+ name
;
302 mRemotePath
+= PATH_SEPARATOR
;
304 Log_OC
.d(TAG
, "OCFile name changed to " + mRemotePath
);
309 * Can be used to get the Mimetype
311 * @return the Mimetype as a String
313 public String
getMimetype() {
318 * Adds a file to this directory. If this file is not a directory, an
319 * exception gets thrown.
322 * @throws IllegalStateException if you try to add a something and this is
325 public void addFile(OCFile file
) throws IllegalStateException
{
327 file
.mParentId
= mId
;
328 mNeedsUpdating
= true
;
331 throw new IllegalStateException(
332 "This is not a directory where you can add stuff to!");
336 * Used internally. Reset all file properties
338 private void resetData() {
345 mCreationTimestamp
= 0;
346 mModifiedTimestamp
= 0;
347 mModifiedTimestampAtLastSyncForData
= 0;
348 mLastSyncDateForProperties
= 0;
349 mLastSyncDateForData
= 0;
351 mNeedsUpdating
= false
;
353 mShareByLink
= false
;
357 mNeedsUpdateThumbnail
= false
;
358 mIsDownloading
= false
;
359 mIsUploading
= false
;
363 * Sets the ID of the file
365 * @param file_id to set
367 public void setFileId(long file_id
) {
372 * Sets the Mime-Type of the
374 * @param mimetype to set
376 public void setMimetype(String mimetype
) {
377 mMimeType
= mimetype
;
381 * Sets the ID of the parent folder
383 * @param parent_id to set
385 public void setParentId(long parent_id
) {
386 mParentId
= parent_id
;
390 * Sets the file size in bytes
392 * @param file_len to set
394 public void setFileLength(long file_len
) {
399 * Returns the size of the file in bytes
401 * @return The filesize in bytes
403 public long getFileLength() {
408 * Returns the ID of the parent Folder
412 public long getParentId() {
417 * Check, if this file needs updating
421 public boolean needsUpdatingWhileSaving() {
422 return mNeedsUpdating
;
425 public boolean needsUpdateThumbnail() {
426 return mNeedsUpdateThumbnail
;
429 public void setNeedsUpdateThumbnail(boolean needsUpdateThumbnail
) {
430 this.mNeedsUpdateThumbnail
= needsUpdateThumbnail
;
433 public long getLastSyncDateForProperties() {
434 return mLastSyncDateForProperties
;
437 public void setLastSyncDateForProperties(long lastSyncDate
) {
438 mLastSyncDateForProperties
= lastSyncDate
;
441 public long getLastSyncDateForData() {
442 return mLastSyncDateForData
;
445 public void setLastSyncDateForData(long lastSyncDate
) {
446 mLastSyncDateForData
= lastSyncDate
;
449 public void setKeepInSync(boolean keepInSync
) {
450 mKeepInSync
= keepInSync
;
453 public boolean keepInSync() {
458 public int describeContents() {
459 return ((Object
) this).hashCode();
463 public int compareTo(OCFile another
) {
464 if (isFolder() && another
.isFolder()) {
465 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
466 } else if (isFolder()) {
468 } else if (another
.isFolder()) {
471 return new AlphanumComparator().compare(this, another
);
475 public boolean equals(Object o
) {
476 if (o
instanceof OCFile
) {
477 OCFile that
= (OCFile
) o
;
479 return this.mId
== that
.mId
;
487 public String
toString() {
488 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSync=%s etag=%s]";
489 asString
= String
.format(asString
, Long
.valueOf(mId
), getFileName(), mMimeType
, isDown(), mLocalPath
, mRemotePath
, Long
.valueOf(mParentId
), Boolean
.valueOf(mKeepInSync
), mEtag
);
493 public String
getEtag() {
497 public void setEtag(String etag
) {
502 public boolean isShareByLink() {
506 public void setShareByLink(boolean shareByLink
) {
507 this.mShareByLink
= shareByLink
;
510 public String
getPublicLink() {
514 public void setPublicLink(String publicLink
) {
515 this.mPublicLink
= publicLink
;
518 public long getLocalModificationTimestamp() {
519 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
520 File f
= new File(mLocalPath
);
521 return f
.lastModified();
527 * @return 'True' if the file contains audio
529 public boolean isAudio() {
530 return (mMimeType
!= null
&& mMimeType
.startsWith("audio/"));
534 * @return 'True' if the file contains video
536 public boolean isVideo() {
537 return (mMimeType
!= null
&& mMimeType
.startsWith("video/"));
541 * @return 'True' if the file contains an image
543 public boolean isImage() {
544 return ((mMimeType
!= null
&& mMimeType
.startsWith("image/")) ||
545 getMimeTypeFromName().startsWith("image/"));
548 public String
getMimeTypeFromName() {
549 String extension
= "";
550 int pos
= mRemotePath
.lastIndexOf('.');
552 extension
= mRemotePath
.substring(pos
+ 1);
554 String result
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(extension
.toLowerCase());
555 return (result
!= null
) ? result
: "";
558 public String
getPermissions() {
562 public void setPermissions(String permissions
) {
563 this.mPermissions
= permissions
;
566 public String
getRemoteId() {
570 public void setRemoteId(String remoteId
) {
571 this.mRemoteId
= remoteId
;
574 public boolean isDownloading() {
575 return mIsDownloading
;
578 public boolean isUploading() {
582 public void setUploading(boolean isUploading
) {
583 this.mIsUploading
= isUploading
;
586 public void setDownloading(boolean isDownloading
) {
587 this.mIsDownloading
= isDownloading
;