[tx-robot] updated from transifex
[pub/Android/ownCloud.git] / src / com / owncloud / android / datamodel / OCFile.java
index 2aa5dbc..2b9bd5e 100644 (file)
@@ -1,6 +1,8 @@
 /**
  *   ownCloud Android client application
  *
+ *   @author Bartek Przybylski
+ *   @author David A. Velasco
  *   Copyright (C) 2012  Bartek Przybylski
  *   Copyright (C) 2015 ownCloud Inc.
  *
 
 package com.owncloud.android.datamodel;
 
+import android.content.ContentResolver;
+import android.net.Uri;
 import android.os.Parcel;
 import android.os.Parcelable;
+import android.webkit.MimeTypeMap;
 
 import com.owncloud.android.lib.common.utils.Log_OC;
-import com.owncloud.android.utils.FileStorageUtils;
 
 import java.io.File;
 
@@ -43,6 +47,8 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
         }
     };
 
+    private final static String PERMISSION_SHARED_WITH_ME = "S";    // TODO move to better location
+
     public static final String PATH_SEPARATOR = "/";
     public static final String ROOT_PATH = PATH_SEPARATOR;
 
@@ -74,6 +80,16 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
 
     private boolean mIsDownloading;
 
+    private String mEtagInConflict;    // Save file etag in the server, when there is a conflict. No conflict =  null
+
+    private boolean mShareWithSharee;
+
+    /**
+     * URI to the local path of the file contents, if stored in the device; cached after first call
+     * to {@link #getStorageUri()}
+     */
+    private Uri mLocalUri;
+
 
     /**
      * Create new {@link OCFile} with given path.
@@ -115,8 +131,10 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
         mPublicLink = source.readString();
         mPermissions = source.readString();
         mRemoteId = source.readString();
-        mNeedsUpdateThumbnail = source.readInt() == 0;
-        mIsDownloading = source.readInt() == 0;
+        mNeedsUpdateThumbnail = source.readInt() == 1;
+        mIsDownloading = source.readInt() == 1;
+        mEtagInConflict = source.readString();
+        mShareWithSharee = source.readInt() == 1;
 
     }
 
@@ -142,6 +160,8 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
         dest.writeString(mRemoteId);
         dest.writeInt(mNeedsUpdateThumbnail ? 1 : 0);
         dest.writeInt(mIsDownloading ? 1 : 0);
+        dest.writeString(mEtagInConflict);
+        dest.writeInt(mShareWithSharee ? 1 : 0);
     }
 
     /**
@@ -204,12 +224,31 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
     }
 
     /**
+     * The URI to the file contents, if stored locally
+     *
+     * @return A URI to the local copy of the file, or NULL if not stored in the device
+     */
+    public Uri getStorageUri() {
+        if (mLocalPath == null || mLocalPath.length() == 0) {
+            return null;
+        }
+        if (mLocalUri == null) {
+            Uri.Builder builder = new Uri.Builder();
+            builder.scheme(ContentResolver.SCHEME_FILE);
+            builder.path(mLocalPath);
+            mLocalUri = builder.build();
+        }
+        return mLocalUri;
+    }
+
+    /**
      * Can be used to set the path where the file is stored
      *
      * @param storage_path to set
      */
     public void setStoragePath(String storage_path) {
         mLocalPath = storage_path;
+        mLocalUri = null;
     }
 
     /**
@@ -289,11 +328,13 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
     /**
      * Sets the name of the file
      * <p/>
-     * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root directory
+     * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root
+     * directory
      */
     public void setFileName(String name) {
         Log_OC.d(TAG, "OCFile name changin from " + mRemotePath);
-        if (name != null && name.length() > 0 && !name.contains(PATH_SEPARATOR) && !mRemotePath.equals(ROOT_PATH)) {
+        if (name != null && name.length() > 0 && !name.contains(PATH_SEPARATOR) &&
+                !mRemotePath.equals(ROOT_PATH)) {
             String parent = (new File(getRemotePath())).getParent();
             parent = (parent.endsWith(PATH_SEPARATOR)) ? parent : parent + PATH_SEPARATOR;
             mRemotePath = parent + name;
@@ -314,24 +355,6 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
     }
 
     /**
-     * Adds a file to this directory. If this file is not a directory, an
-     * exception gets thrown.
-     *
-     * @param file to add
-     * @throws IllegalStateException if you try to add a something and this is
-     *                               not a directory
-     */
-    public void addFile(OCFile file) throws IllegalStateException {
-        if (isFolder()) {
-            file.mParentId = mId;
-            mNeedsUpdating = true;
-            return;
-        }
-        throw new IllegalStateException(
-                "This is not a directory where you can add stuff to!");
-    }
-
-    /**
      * Used internally. Reset all file properties
      */
     private void resetData() {
@@ -355,6 +378,8 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
         mRemoteId = null;
         mNeedsUpdateThumbnail = false;
         mIsDownloading = false;
+        mEtagInConflict = null;
+        mShareWithSharee = false;
     }
 
     /**
@@ -454,7 +479,7 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
 
     @Override
     public int describeContents() {
-        return ((Object) this).hashCode();
+        return super.hashCode();
     }
 
     @Override
@@ -483,8 +508,11 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
 
     @Override
     public String toString() {
-        String asString = "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, favorite=%s etag=%s]";
-        asString = String.format(asString, Long.valueOf(mId), getFileName(), mMimeType, isDown(), mLocalPath, mRemotePath, Long.valueOf(mParentId), Boolean.valueOf(mFavorite), mEtag);
+        String asString = "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, " +
+                "parentId=%s, favorite=%s etag=%s]";
+        asString = String.format(asString, Long.valueOf(mId), getFileName(), mMimeType, isDown(),
+                mLocalPath, mRemotePath, Long.valueOf(mParentId), Boolean.valueOf(mFavorite),
+                mEtag);
         return asString;
     }
 
@@ -493,15 +521,15 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
     }
 
     public void setEtag(String etag) {
-        this.mEtag = etag;
+        this.mEtag = (etag != null ? etag : "");
     }
 
 
-    public boolean isShareByLink() {
+    public boolean isSharedViaLink() {
         return mShareByLink;
     }
 
-    public void setShareByLink(boolean shareByLink) {
+    public void setShareViaLink(boolean shareByLink) {
         this.mShareByLink = shareByLink;
     }
 
@@ -540,7 +568,26 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
      */
     public boolean isImage() {
         return ((mMimeType != null && mMimeType.startsWith("image/")) ||
-                FileStorageUtils.getMimeTypeFromName(mRemotePath).startsWith("image/"));
+                getMimeTypeFromName().startsWith("image/"));
+    }
+
+    /**
+     * @return 'True' if the file is simple text (e.g. not application-dependent, like .doc or .docx)
+     */
+    public boolean isText() {
+        return ((mMimeType != null && mMimeType.startsWith("text/")) ||
+                getMimeTypeFromName().startsWith("text/"));
+    }
+
+    public String getMimeTypeFromName() {
+        String extension = "";
+        int pos = mRemotePath.lastIndexOf('.');
+        if (pos >= 0) {
+            extension = mRemotePath.substring(pos + 1);
+        }
+        String result = MimeTypeMap.getSingleton().
+                getMimeTypeFromExtension(extension.toLowerCase());
+        return (result != null) ? result : "";
     }
 
     /**
@@ -574,8 +621,24 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
         this.mIsDownloading = isDownloading;
     }
 
-    public boolean isSynchronizing() {
-        // TODO real implementation
-        return false;
+    public String getEtagInConflict() {
+        return mEtagInConflict;
+    }
+
+    public void setEtagInConflict(String etagInConflict) {
+        mEtagInConflict = etagInConflict;
+    }
+
+    public boolean isSharedWithSharee() {
+        return mShareWithSharee;
+    }
+
+    public void setShareWithSharee(boolean shareWithSharee) {
+        this.mShareWithSharee = shareWithSharee;
+    }
+
+    public boolean isSharedWithMe() {
+        String permissions = getPermissions();
+        return (permissions != null && permissions.contains(PERMISSION_SHARED_WITH_ME));
     }
 }