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
; 
  69     private String mRemoteId
; 
  73      * Create new {@link OCFile} with given path. 
  75      * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'. 
  77      * @param path The remote path of the file. 
  79     public OCFile(String path
) { 
  81         mNeedsUpdating 
= false
; 
  82         if (path 
== null 
|| path
.length() <= 0 || !path
.startsWith(PATH_SEPARATOR
)) { 
  83             throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path
); 
  89      * Reconstruct from parcel 
  91      * @param source The source parcel 
  93     private OCFile(Parcel source
) { 
  94         mId 
= source
.readLong(); 
  95         mParentId 
= source
.readLong(); 
  96         mLength 
= source
.readLong(); 
  97         mCreationTimestamp 
= source
.readLong(); 
  98         mModifiedTimestamp 
= source
.readLong(); 
  99         mModifiedTimestampAtLastSyncForData 
= source
.readLong(); 
 100         mRemotePath 
= source
.readString(); 
 101         mLocalPath 
= source
.readString(); 
 102         mMimeType 
= source
.readString(); 
 103         mNeedsUpdating 
= source
.readInt() == 0; 
 104         mKeepInSync 
= source
.readInt() == 1; 
 105         mLastSyncDateForProperties 
= source
.readLong(); 
 106         mLastSyncDateForData 
= source
.readLong(); 
 107         mEtag 
= source
.readString(); 
 108         mShareByLink 
= source
.readInt() == 1; 
 109         mPublicLink 
= source
.readString(); 
 110         mPermissions 
= source
.readString(); 
 111         mRemoteId 
= source
.readString(); 
 115     public void writeToParcel(Parcel dest
, int flags
) { 
 117         dest
.writeLong(mParentId
); 
 118         dest
.writeLong(mLength
); 
 119         dest
.writeLong(mCreationTimestamp
); 
 120         dest
.writeLong(mModifiedTimestamp
); 
 121         dest
.writeLong(mModifiedTimestampAtLastSyncForData
); 
 122         dest
.writeString(mRemotePath
); 
 123         dest
.writeString(mLocalPath
); 
 124         dest
.writeString(mMimeType
); 
 125         dest
.writeInt(mNeedsUpdating ? 
1 : 0); 
 126         dest
.writeInt(mKeepInSync ? 
1 : 0); 
 127         dest
.writeLong(mLastSyncDateForProperties
); 
 128         dest
.writeLong(mLastSyncDateForData
); 
 129         dest
.writeString(mEtag
); 
 130         dest
.writeInt(mShareByLink ? 
1 : 0); 
 131         dest
.writeString(mPublicLink
); 
 132         dest
.writeString(mPermissions
); 
 133         dest
.writeString(mRemoteId
); 
 137      * Gets the ID of the file 
 139      * @return the file ID 
 141     public long getFileId() { 
 146      * Returns the remote path of the file on ownCloud 
 148      * @return The remote path to the file 
 150     public String 
getRemotePath() { 
 155      * Can be used to check, whether or not this file exists in the database 
 158      * @return true, if the file exists in the database 
 160     public boolean fileExists() { 
 165      * Use this to find out if this file is a folder. 
 167      * @return true if it is a folder 
 169     public boolean isFolder() { 
 170         return mMimeType 
!= null 
&& mMimeType
.equals("DIR"); 
 174      * Use this to check if this file is available locally 
 176      * @return true if it is 
 178     public boolean isDown() { 
 179         if (mLocalPath 
!= null 
&& mLocalPath
.length() > 0) { 
 180             File file 
= new File(mLocalPath
); 
 181             return (file
.exists()); 
 187      * The path, where the file is stored locally 
 189      * @return The local path to the file 
 191     public String 
getStoragePath() { 
 196      * Can be used to set the path where the file is stored 
 198      * @param storage_path to set 
 200     public void setStoragePath(String storage_path
) { 
 201         mLocalPath 
= storage_path
; 
 205      * Get a UNIX timestamp of the file creation time 
 207      * @return A UNIX timestamp of the time that file was created 
 209     public long getCreationTimestamp() { 
 210         return mCreationTimestamp
; 
 214      * Set a UNIX timestamp of the time the file was created 
 216      * @param creation_timestamp to set 
 218     public void setCreationTimestamp(long creation_timestamp
) { 
 219         mCreationTimestamp 
= creation_timestamp
; 
 223      * Get a UNIX timestamp of the file modification time. 
 225      * @return  A UNIX timestamp of the modification time, corresponding to the value returned by the server 
 226      *          in the last synchronization of the properties of this file.  
 228     public long getModificationTimestamp() { 
 229         return mModifiedTimestamp
; 
 233      * Set a UNIX timestamp of the time the time the file was modified. 
 235      * To update with the value returned by the server in every synchronization of the properties  
 238      * @param modification_timestamp to set 
 240     public void setModificationTimestamp(long modification_timestamp
) { 
 241         mModifiedTimestamp 
= modification_timestamp
; 
 246      * Get a UNIX timestamp of the file modification time. 
 248      * @return  A UNIX timestamp of the modification time, corresponding to the value returned by the server 
 249      *          in the last synchronization of THE CONTENTS of this file.  
 251     public long getModificationTimestampAtLastSyncForData() { 
 252         return mModifiedTimestampAtLastSyncForData
; 
 256      * Set a UNIX timestamp of the time the time the file was modified. 
 258      * To update with the value returned by the server in every synchronization of THE CONTENTS  
 261      * @param modification_timestamp to set 
 263     public void setModificationTimestampAtLastSyncForData(long modificationTimestamp
) { 
 264         mModifiedTimestampAtLastSyncForData 
= modificationTimestamp
; 
 270      * Returns the filename and "/" for the root directory 
 272      * @return The name of the file 
 274     public String 
getFileName() { 
 275         File f 
= new File(getRemotePath()); 
 276         return f
.getName().length() == 0 ? ROOT_PATH 
: f
.getName(); 
 280      * Sets the name of the file 
 282      * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root directory  
 284     public void setFileName(String name
) { 
 285         Log_OC
.d(TAG
, "OCFile name changin from " + mRemotePath
); 
 286         if (name 
!= null 
&& name
.length() > 0 && !name
.contains(PATH_SEPARATOR
) && !mRemotePath
.equals(ROOT_PATH
)) { 
 287             String parent 
= (new File(getRemotePath())).getParent(); 
 288             parent 
= (parent
.endsWith(PATH_SEPARATOR
)) ? parent 
: parent 
+ PATH_SEPARATOR
; 
 289             mRemotePath 
=  parent 
+ name
; 
 291                 mRemotePath 
+= PATH_SEPARATOR
; 
 293             Log_OC
.d(TAG
, "OCFile name changed to " + mRemotePath
); 
 298      * Can be used to get the Mimetype 
 300      * @return the Mimetype as a String 
 302     public String 
getMimetype() { 
 307      * Adds a file to this directory. If this file is not a directory, an 
 308      * exception gets thrown. 
 311      * @throws IllegalStateException if you try to add a something and this is 
 314     public void addFile(OCFile file
) throws IllegalStateException 
{ 
 316             file
.mParentId 
= mId
; 
 317             mNeedsUpdating 
= true
; 
 320         throw new IllegalStateException( 
 321                 "This is not a directory where you can add stuff to!"); 
 325      * Used internally. Reset all file properties 
 327     private void resetData() { 
 334         mCreationTimestamp 
= 0; 
 335         mModifiedTimestamp 
= 0; 
 336         mModifiedTimestampAtLastSyncForData 
= 0; 
 337         mLastSyncDateForProperties 
= 0; 
 338         mLastSyncDateForData 
= 0; 
 340         mNeedsUpdating 
= false
; 
 342         mShareByLink 
= false
; 
 349      * Sets the ID of the file 
 351      * @param file_id to set 
 353     public void setFileId(long file_id
) { 
 358      * Sets the Mime-Type of the 
 360      * @param mimetype to set 
 362     public void setMimetype(String mimetype
) { 
 363         mMimeType 
= mimetype
; 
 367      * Sets the ID of the parent folder 
 369      * @param parent_id to set 
 371     public void setParentId(long parent_id
) { 
 372         mParentId 
= parent_id
; 
 376      * Sets the file size in bytes 
 378      * @param file_len to set 
 380     public void setFileLength(long file_len
) { 
 385      * Returns the size of the file in bytes 
 387      * @return The filesize in bytes 
 389     public long getFileLength() { 
 394      * Returns the ID of the parent Folder 
 398     public long getParentId() { 
 403      * Check, if this file needs updating 
 407     public boolean needsUpdatingWhileSaving() { 
 408         return mNeedsUpdating
; 
 411     public long getLastSyncDateForProperties() { 
 412         return mLastSyncDateForProperties
; 
 415     public void setLastSyncDateForProperties(long lastSyncDate
) { 
 416         mLastSyncDateForProperties 
= lastSyncDate
; 
 419     public long getLastSyncDateForData() { 
 420         return mLastSyncDateForData
; 
 423     public void setLastSyncDateForData(long lastSyncDate
) { 
 424         mLastSyncDateForData 
= lastSyncDate
; 
 427     public void setKeepInSync(boolean keepInSync
) { 
 428         mKeepInSync 
= keepInSync
; 
 431     public boolean keepInSync() { 
 436     public int describeContents() { 
 437         return this.hashCode(); 
 441     public int compareTo(OCFile another
) { 
 442         if (isFolder() && another
.isFolder()) { 
 443             return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase()); 
 444         } else if (isFolder()) { 
 446         } else if (another
.isFolder()) { 
 449         return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase()); 
 453     public boolean equals(Object o
) { 
 454         if(o 
instanceof OCFile
){ 
 455             OCFile that 
= (OCFile
) o
; 
 457                 return this.mId 
== that
.mId
; 
 465     public String 
toString() { 
 466         String asString 
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSync=%s etag=%s]"; 
 467         asString 
= String
.format(asString
, Long
.valueOf(mId
), getFileName(), mMimeType
, isDown(), mLocalPath
, mRemotePath
, Long
.valueOf(mParentId
), Boolean
.valueOf(mKeepInSync
), mEtag
); 
 471     public String 
getEtag() { 
 475     public void setEtag(String etag
) { 
 480     public boolean isShareByLink() { 
 484     public void setShareByLink(boolean shareByLink
) { 
 485         this.mShareByLink 
= shareByLink
; 
 488     public String 
getPublicLink() { 
 492     public void setPublicLink(String publicLink
) { 
 493         this.mPublicLink 
= publicLink
; 
 496     public long getLocalModificationTimestamp() { 
 497         if (mLocalPath 
!= null 
&& mLocalPath
.length() > 0) { 
 498             File f 
= new File(mLocalPath
); 
 499             return f
.lastModified(); 
 504     /** @return  'True' if the file contains audio */ 
 505     public boolean isAudio() { 
 506         return (mMimeType 
!= null 
&& mMimeType
.startsWith("audio/")); 
 509     /** @return  'True' if the file contains video */ 
 510     public boolean isVideo() { 
 511         return (mMimeType 
!= null 
&& mMimeType
.startsWith("video/")); 
 514     /** @return  'True' if the file contains an image */ 
 515     public boolean isImage() { 
 516         return ((mMimeType 
!= null 
&& mMimeType
.startsWith("image/")) || 
 517                  getMimeTypeFromName().startsWith("image/")); 
 520     public String 
getMimeTypeFromName() { 
 521         String extension 
= ""; 
 522         int pos 
= mRemotePath
.lastIndexOf('.'); 
 524             extension 
= mRemotePath
.substring(pos 
+ 1); 
 526         String result 
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(extension
.toLowerCase()); 
 527         return (result 
!= null
) ? result 
: ""; 
 530     public String 
getPermissions() { 
 534     public void setPermissions(String permissions
) { 
 535         this.mPermissions 
= permissions
; 
 538     public String 
getRemoteId() { 
 542     public void setRemoteId(String remoteId
) { 
 543         this.mRemoteId 
= remoteId
;