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
.Log_OC
; 
  25 import android
.os
.Parcel
; 
  26 import android
.os
.Parcelable
; 
  27 import android
.webkit
.MimeTypeMap
; 
  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
; 
  66      * Create new {@link OCFile} with given path. 
  68      * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'. 
  70      * @param path The remote path of the file. 
  72     public OCFile(String path
) { 
  74         mNeedsUpdating 
= false
; 
  75         if (path 
== null 
|| path
.length() <= 0 || !path
.startsWith(PATH_SEPARATOR
)) { 
  76             throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path
); 
  82      * Reconstruct from parcel 
  84      * @param source The source parcel 
  86     private OCFile(Parcel source
) { 
  87         mId 
= source
.readLong(); 
  88         mParentId 
= source
.readLong(); 
  89         mLength 
= source
.readLong(); 
  90         mCreationTimestamp 
= source
.readLong(); 
  91         mModifiedTimestamp 
= source
.readLong(); 
  92         mModifiedTimestampAtLastSyncForData 
= source
.readLong(); 
  93         mRemotePath 
= source
.readString(); 
  94         mLocalPath 
= source
.readString(); 
  95         mMimeType 
= source
.readString(); 
  96         mNeedsUpdating 
= source
.readInt() == 0; 
  97         mKeepInSync 
= source
.readInt() == 1; 
  98         mLastSyncDateForProperties 
= source
.readLong(); 
  99         mLastSyncDateForData 
= source
.readLong(); 
 100         mEtag 
= source
.readString(); 
 104     public void writeToParcel(Parcel dest
, int flags
) { 
 106         dest
.writeLong(mParentId
); 
 107         dest
.writeLong(mLength
); 
 108         dest
.writeLong(mCreationTimestamp
); 
 109         dest
.writeLong(mModifiedTimestamp
); 
 110         dest
.writeLong(mModifiedTimestampAtLastSyncForData
); 
 111         dest
.writeString(mRemotePath
); 
 112         dest
.writeString(mLocalPath
); 
 113         dest
.writeString(mMimeType
); 
 114         dest
.writeInt(mNeedsUpdating ? 
1 : 0); 
 115         dest
.writeInt(mKeepInSync ? 
1 : 0); 
 116         dest
.writeLong(mLastSyncDateForProperties
); 
 117         dest
.writeLong(mLastSyncDateForData
); 
 118         dest
.writeString(mEtag
); 
 122      * Gets the ID of the file 
 124      * @return the file ID 
 126     public long getFileId() { 
 131      * Returns the remote path of the file on ownCloud 
 133      * @return The remote path to the file 
 135     public String 
getRemotePath() { 
 140      * Can be used to check, whether or not this file exists in the database 
 143      * @return true, if the file exists in the database 
 145     public boolean fileExists() { 
 150      * Use this to find out if this file is a folder. 
 152      * @return true if it is a folder 
 154     public boolean isFolder() { 
 155         return mMimeType 
!= null 
&& mMimeType
.equals("DIR"); 
 159      * Use this to check if this file is available locally 
 161      * @return true if it is 
 163     public boolean isDown() { 
 164         if (mLocalPath 
!= null 
&& mLocalPath
.length() > 0) { 
 165             File file 
= new File(mLocalPath
); 
 166             return (file
.exists()); 
 172      * The path, where the file is stored locally 
 174      * @return The local path to the file 
 176     public String 
getStoragePath() { 
 181      * Can be used to set the path where the file is stored 
 183      * @param storage_path to set 
 185     public void setStoragePath(String storage_path
) { 
 186         mLocalPath 
= storage_path
; 
 190      * Get a UNIX timestamp of the file creation time 
 192      * @return A UNIX timestamp of the time that file was created 
 194     public long getCreationTimestamp() { 
 195         return mCreationTimestamp
; 
 199      * Set a UNIX timestamp of the time the file was created 
 201      * @param creation_timestamp to set 
 203     public void setCreationTimestamp(long creation_timestamp
) { 
 204         mCreationTimestamp 
= creation_timestamp
; 
 208      * Get a UNIX timestamp of the file modification time. 
 210      * @return  A UNIX timestamp of the modification time, corresponding to the value returned by the server 
 211      *          in the last synchronization of the properties of this file.  
 213     public long getModificationTimestamp() { 
 214         return mModifiedTimestamp
; 
 218      * Set a UNIX timestamp of the time the time the file was modified. 
 220      * To update with the value returned by the server in every synchronization of the properties  
 223      * @param modification_timestamp to set 
 225     public void setModificationTimestamp(long modification_timestamp
) { 
 226         mModifiedTimestamp 
= modification_timestamp
; 
 231      * Get a UNIX timestamp of the file modification time. 
 233      * @return  A UNIX timestamp of the modification time, corresponding to the value returned by the server 
 234      *          in the last synchronization of THE CONTENTS of this file.  
 236     public long getModificationTimestampAtLastSyncForData() { 
 237         return mModifiedTimestampAtLastSyncForData
; 
 241      * Set a UNIX timestamp of the time the time the file was modified. 
 243      * To update with the value returned by the server in every synchronization of THE CONTENTS  
 246      * @param modification_timestamp to set 
 248     public void setModificationTimestampAtLastSyncForData(long modificationTimestamp
) { 
 249         mModifiedTimestampAtLastSyncForData 
= modificationTimestamp
; 
 255      * Returns the filename and "/" for the root directory 
 257      * @return The name of the file 
 259     public String 
getFileName() { 
 260         File f 
= new File(getRemotePath()); 
 261         return f
.getName().length() == 0 ? ROOT_PATH 
: f
.getName(); 
 265      * Sets the name of the file 
 267      * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root directory  
 269     public void setFileName(String name
) { 
 270         Log_OC
.d(TAG
, "OCFile name changin from " + mRemotePath
); 
 271         if (name 
!= null 
&& name
.length() > 0 && !name
.contains(PATH_SEPARATOR
) && !mRemotePath
.equals(ROOT_PATH
)) { 
 272             String parent 
= (new File(getRemotePath())).getParent(); 
 273             parent 
= (parent
.endsWith(PATH_SEPARATOR
)) ? parent 
: parent 
+ PATH_SEPARATOR
; 
 274             mRemotePath 
=  parent 
+ name
; 
 276                 mRemotePath 
+= PATH_SEPARATOR
; 
 278             Log_OC
.d(TAG
, "OCFile name changed to " + mRemotePath
); 
 283      * Can be used to get the Mimetype 
 285      * @return the Mimetype as a String 
 287     public String 
getMimetype() { 
 292      * Adds a file to this directory. If this file is not a directory, an 
 293      * exception gets thrown. 
 296      * @throws IllegalStateException if you try to add a something and this is 
 299     public void addFile(OCFile file
) throws IllegalStateException 
{ 
 301             file
.mParentId 
= mId
; 
 302             mNeedsUpdating 
= true
; 
 305         throw new IllegalStateException( 
 306                 "This is not a directory where you can add stuff to!"); 
 310      * Used internally. Reset all file properties 
 312     private void resetData() { 
 319         mCreationTimestamp 
= 0; 
 320         mModifiedTimestamp 
= 0; 
 321         mModifiedTimestampAtLastSyncForData 
= 0; 
 322         mLastSyncDateForProperties 
= 0; 
 323         mLastSyncDateForData 
= 0; 
 325         mNeedsUpdating 
= false
; 
 330      * Sets the ID of the file 
 332      * @param file_id to set 
 334     public void setFileId(long file_id
) { 
 339      * Sets the Mime-Type of the 
 341      * @param mimetype to set 
 343     public void setMimetype(String mimetype
) { 
 344         mMimeType 
= mimetype
; 
 348      * Sets the ID of the parent folder 
 350      * @param parent_id to set 
 352     public void setParentId(long parent_id
) { 
 353         mParentId 
= parent_id
; 
 357      * Sets the file size in bytes 
 359      * @param file_len to set 
 361     public void setFileLength(long file_len
) { 
 366      * Returns the size of the file in bytes 
 368      * @return The filesize in bytes 
 370     public long getFileLength() { 
 375      * Returns the ID of the parent Folder 
 379     public long getParentId() { 
 384      * Check, if this file needs updating 
 388     public boolean needsUpdatingWhileSaving() { 
 389         return mNeedsUpdating
; 
 392     public long getLastSyncDateForProperties() { 
 393         return mLastSyncDateForProperties
; 
 396     public void setLastSyncDateForProperties(long lastSyncDate
) { 
 397         mLastSyncDateForProperties 
= lastSyncDate
; 
 400     public long getLastSyncDateForData() { 
 401         return mLastSyncDateForData
; 
 404     public void setLastSyncDateForData(long lastSyncDate
) { 
 405         mLastSyncDateForData 
= lastSyncDate
; 
 408     public void setKeepInSync(boolean keepInSync
) { 
 409         mKeepInSync 
= keepInSync
; 
 412     public boolean keepInSync() { 
 417     public int describeContents() { 
 418         return this.hashCode(); 
 422     public int compareTo(OCFile another
) { 
 423         if (isFolder() && another
.isFolder()) { 
 424             return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase()); 
 425         } else if (isFolder()) { 
 427         } else if (another
.isFolder()) { 
 430         return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase()); 
 434     public boolean equals(Object o
) { 
 435         if(o 
instanceof OCFile
){ 
 436             OCFile that 
= (OCFile
) o
; 
 438                 return this.mId 
== that
.mId
; 
 446     public String 
toString() { 
 447         String asString 
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSinc=%s etag=%s]"; 
 448         asString 
= String
.format(asString
, Long
.valueOf(mId
), getFileName(), mMimeType
, isDown(), mLocalPath
, mRemotePath
, Long
.valueOf(mParentId
), Boolean
.valueOf(mKeepInSync
), mEtag
); 
 452     public String 
getEtag() { 
 456     public void setEtag(String etag
) { 
 460     public long getLocalModificationTimestamp() { 
 461         if (mLocalPath 
!= null 
&& mLocalPath
.length() > 0) { 
 462             File f 
= new File(mLocalPath
); 
 463             return f
.lastModified(); 
 468     /** @return  'True' if the file contains audio */ 
 469     public boolean isAudio() { 
 470         return (mMimeType 
!= null 
&& mMimeType
.startsWith("audio/")); 
 473     /** @return  'True' if the file contains video */ 
 474     public boolean isVideo() { 
 475         return (mMimeType 
!= null 
&& mMimeType
.startsWith("video/")); 
 478     /** @return  'True' if the file contains an image */ 
 479     public boolean isImage() { 
 480         return ((mMimeType 
!= null 
&& mMimeType
.startsWith("image/")) || 
 481                  getMimeTypeFromName().startsWith("image/")); 
 484     public String 
getMimeTypeFromName() { 
 485         String extension 
= ""; 
 486         int pos 
= mRemotePath
.lastIndexOf('.'); 
 488             extension 
= mRemotePath
.substring(pos 
+ 1); 
 490         String result 
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(extension
.toLowerCase()); 
 491         return (result 
!= null
) ? result 
: "";