2  *   ownCloud Android client application 
   4  *   Copyright (C) 2012  Bartek Przybylski 
   5  *   Copyright (C) 2015 ownCloud Inc. 
   7  *   This program is free software: you can redistribute it and/or modify 
   8  *   it under the terms of the GNU General Public License version 2, 
   9  *   as published by the Free Software Foundation. 
  11  *   This program is distributed in the hope that it will be useful, 
  12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
  13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  14  *   GNU General Public License for more details. 
  16  *   You should have received a copy of the GNU General Public License 
  17  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. 
  21 package com
.owncloud
.android
.datamodel
; 
  23 import android
.os
.Parcel
; 
  24 import android
.os
.Parcelable
; 
  25 import android
.webkit
.MimeTypeMap
; 
  27 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
; 
  31 import third_parties
.daveKoeller
.AlphanumComparator
; 
  32 public class OCFile 
implements Parcelable
, Comparable
<OCFile
> { 
  34     public static final Parcelable
.Creator
<OCFile
> CREATOR 
= new Parcelable
.Creator
<OCFile
>() { 
  36         public OCFile 
createFromParcel(Parcel source
) { 
  37             return new OCFile(source
); 
  41         public OCFile
[] newArray(int size
) { 
  42             return new OCFile
[size
]; 
  46     public static final String PATH_SEPARATOR 
= "/"; 
  47     public static final String ROOT_PATH 
= PATH_SEPARATOR
; 
  49     private static final String TAG 
= OCFile
.class.getSimpleName(); 
  52     private long mParentId
; 
  54     private long mCreationTimestamp
; 
  55     private long mModifiedTimestamp
; 
  56     private long mModifiedTimestampAtLastSyncForData
; 
  57     private String mRemotePath
; 
  58     private String mLocalPath
; 
  59     private String mMimeType
; 
  60     private boolean mNeedsUpdating
; 
  61     private long mLastSyncDateForProperties
; 
  62     private long mLastSyncDateForData
; 
  63     private boolean mFavorite
; 
  67     private boolean mShareByLink
; 
  68     private String mPublicLink
; 
  70     private String mPermissions
; 
  71     private String mRemoteId
; 
  73     private boolean mNeedsUpdateThumbnail
; 
  75     private boolean mIsDownloading
; 
  79      * Create new {@link OCFile} with given path. 
  81      * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'. 
  83      * @param path The remote path of the file. 
  85     public OCFile(String path
) { 
  87         mNeedsUpdating 
= false
; 
  88         if (path 
== null 
|| path
.length() <= 0 || !path
.startsWith(PATH_SEPARATOR
)) { 
  89             throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path
); 
  95      * Reconstruct from parcel 
  97      * @param source The source parcel 
  99     private OCFile(Parcel source
) { 
 100         mId 
= source
.readLong(); 
 101         mParentId 
= source
.readLong(); 
 102         mLength 
= source
.readLong(); 
 103         mCreationTimestamp 
= source
.readLong(); 
 104         mModifiedTimestamp 
= source
.readLong(); 
 105         mModifiedTimestampAtLastSyncForData 
= source
.readLong(); 
 106         mRemotePath 
= source
.readString(); 
 107         mLocalPath 
= source
.readString(); 
 108         mMimeType 
= source
.readString(); 
 109         mNeedsUpdating 
= source
.readInt() == 0; 
 110         mFavorite 
= source
.readInt() == 1; 
 111         mLastSyncDateForProperties 
= source
.readLong(); 
 112         mLastSyncDateForData 
= source
.readLong(); 
 113         mEtag 
= source
.readString(); 
 114         mShareByLink 
= source
.readInt() == 1; 
 115         mPublicLink 
= source
.readString(); 
 116         mPermissions 
= source
.readString(); 
 117         mRemoteId 
= source
.readString(); 
 118         mNeedsUpdateThumbnail 
= source
.readInt() == 0; 
 119         mIsDownloading 
= 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(mFavorite ? 
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); 
 148      * Gets the ID of the file 
 150      * @return the file ID 
 152     public long getFileId() { 
 157      * Returns the remote path of the file on ownCloud 
 159      * @return The remote path to the file 
 161     public String 
getRemotePath() { 
 166      * Can be used to check, whether or not this file exists in the database 
 169      * @return true, if the file exists in the database 
 171     public boolean fileExists() { 
 176      * Use this to find out if this file is a folder. 
 178      * @return true if it is a folder 
 180     public boolean isFolder() { 
 181         return mMimeType 
!= null 
&& mMimeType
.equals("DIR"); 
 185      * Use this to check if this file is available locally 
 187      * @return true if it is 
 189     public boolean isDown() { 
 190         if (mLocalPath 
!= null 
&& mLocalPath
.length() > 0) { 
 191             File file 
= new File(mLocalPath
); 
 192             return (file
.exists()); 
 198      * The path, where the file is stored locally 
 200      * @return The local path to the file 
 202     public String 
getStoragePath() { 
 207      * Can be used to set the path where the file is stored 
 209      * @param storage_path to set 
 211     public void setStoragePath(String storage_path
) { 
 212         mLocalPath 
= storage_path
; 
 216      * Get a UNIX timestamp of the file creation time 
 218      * @return A UNIX timestamp of the time that file was created 
 220     public long getCreationTimestamp() { 
 221         return mCreationTimestamp
; 
 225      * Set a UNIX timestamp of the time the file was created 
 227      * @param creation_timestamp to set 
 229     public void setCreationTimestamp(long creation_timestamp
) { 
 230         mCreationTimestamp 
= creation_timestamp
; 
 234      * Get a UNIX timestamp of the file modification time. 
 236      * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server 
 237      * in the last synchronization of the properties of this file. 
 239     public long getModificationTimestamp() { 
 240         return mModifiedTimestamp
; 
 244      * Set a UNIX timestamp of the time the time the file was modified. 
 246      * To update with the value returned by the server in every synchronization of the properties 
 249      * @param modification_timestamp to set 
 251     public void setModificationTimestamp(long modification_timestamp
) { 
 252         mModifiedTimestamp 
= modification_timestamp
; 
 257      * Get a UNIX timestamp of the file modification time. 
 259      * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server 
 260      * in the last synchronization of THE CONTENTS of this file. 
 262     public long getModificationTimestampAtLastSyncForData() { 
 263         return mModifiedTimestampAtLastSyncForData
; 
 267      * Set a UNIX timestamp of the time the time the file was modified. 
 269      * To update with the value returned by the server in every synchronization of THE CONTENTS 
 272      * @param modificationTimestamp to set 
 274     public void setModificationTimestampAtLastSyncForData(long modificationTimestamp
) { 
 275         mModifiedTimestampAtLastSyncForData 
= modificationTimestamp
; 
 280      * Returns the filename and "/" for the root directory 
 282      * @return The name of the file 
 284     public String 
getFileName() { 
 285         File f 
= new File(getRemotePath()); 
 286         return f
.getName().length() == 0 ? ROOT_PATH 
: f
.getName(); 
 290      * Sets the name of the file 
 292      * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root 
 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
) && 
 298                 !mRemotePath
.equals(ROOT_PATH
)) { 
 299             String parent 
= (new File(getRemotePath())).getParent(); 
 300             parent 
= (parent
.endsWith(PATH_SEPARATOR
)) ? parent 
: parent 
+ PATH_SEPARATOR
; 
 301             mRemotePath 
= parent 
+ name
; 
 303                 mRemotePath 
+= PATH_SEPARATOR
; 
 305             Log_OC
.d(TAG
, "OCFile name changed to " + mRemotePath
); 
 310      * Can be used to get the Mimetype 
 312      * @return the Mimetype as a String 
 314     public String 
getMimetype() { 
 319      * Adds a file to this directory. If this file is not a directory, an 
 320      * exception gets thrown. 
 323      * @throws IllegalStateException if you try to add a something and this is 
 326     public void addFile(OCFile file
) throws IllegalStateException 
{ 
 328             file
.mParentId 
= mId
; 
 329             mNeedsUpdating 
= true
; 
 332         throw new IllegalStateException( 
 333                 "This is not a directory where you can add stuff to!"); 
 337      * Used internally. Reset all file properties 
 339     private void resetData() { 
 346         mCreationTimestamp 
= 0; 
 347         mModifiedTimestamp 
= 0; 
 348         mModifiedTimestampAtLastSyncForData 
= 0; 
 349         mLastSyncDateForProperties 
= 0; 
 350         mLastSyncDateForData 
= 0; 
 352         mNeedsUpdating 
= false
; 
 354         mShareByLink 
= false
; 
 358         mNeedsUpdateThumbnail 
= false
; 
 359         mIsDownloading 
= 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 setFavorite(boolean favorite
) { 
 450         mFavorite 
= favorite
; 
 453     public boolean isFavorite() { 
 458     public int describeContents() { 
 459         return super.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, " + 
 489                 "parentId=%s, favorite=%s etag=%s]"; 
 490         asString 
= String
.format(asString
, Long
.valueOf(mId
), getFileName(), mMimeType
, isDown(), 
 491                 mLocalPath
, mRemotePath
, Long
.valueOf(mParentId
), Boolean
.valueOf(mFavorite
), 
 496     public String 
getEtag() { 
 500     public void setEtag(String etag
) { 
 505     public boolean isShareByLink() { 
 509     public void setShareByLink(boolean shareByLink
) { 
 510         this.mShareByLink 
= shareByLink
; 
 513     public String 
getPublicLink() { 
 517     public void setPublicLink(String publicLink
) { 
 518         this.mPublicLink 
= publicLink
; 
 521     public long getLocalModificationTimestamp() { 
 522         if (mLocalPath 
!= null 
&& mLocalPath
.length() > 0) { 
 523             File f 
= new File(mLocalPath
); 
 524             return f
.lastModified(); 
 530      * @return 'True' if the file contains audio 
 532     public boolean isAudio() { 
 533         return (mMimeType 
!= null 
&& mMimeType
.startsWith("audio/")); 
 537      * @return 'True' if the file contains video 
 539     public boolean isVideo() { 
 540         return (mMimeType 
!= null 
&& mMimeType
.startsWith("video/")); 
 544      * @return 'True' if the file contains an image 
 546     public boolean isImage() { 
 547         return ((mMimeType 
!= null 
&& mMimeType
.startsWith("image/")) || 
 548                 getMimeTypeFromName().startsWith("image/")); 
 552      * @return 'True' if the file is simple text (e.g. not application-dependent, like .doc or .docx) 
 554     public boolean isText() { 
 555         return ((mMimeType 
!= null 
&& mMimeType
.startsWith("text/")) || 
 556                 getMimeTypeFromName().startsWith("text/")); 
 559     public String 
getMimeTypeFromName() { 
 560         String extension 
= ""; 
 561         int pos 
= mRemotePath
.lastIndexOf('.'); 
 563             extension 
= mRemotePath
.substring(pos 
+ 1); 
 565         String result 
= MimeTypeMap
.getSingleton(). 
 566                 getMimeTypeFromExtension(extension
.toLowerCase()); 
 567         return (result 
!= null
) ? result 
: ""; 
 571      * @return 'True' if the file is hidden 
 573     public boolean isHidden() { 
 574         return getFileName().startsWith("."); 
 577     public String 
getPermissions() { 
 581     public void setPermissions(String permissions
) { 
 582         this.mPermissions 
= permissions
; 
 585     public String 
getRemoteId() { 
 589     public void setRemoteId(String remoteId
) { 
 590         this.mRemoteId 
= remoteId
; 
 593     public boolean isDownloading() { 
 594         return mIsDownloading
; 
 597     public void setDownloading(boolean isDownloading
) { 
 598         this.mIsDownloading 
= isDownloading
; 
 601     public boolean isSynchronizing() { 
 602         // TODO real implementation