Merge pull request #1088 from owncloud/updated_docs_about_target_of_PRs
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / CreateShareOperation.java
index 17151ab..7cdcc08 100644 (file)
@@ -1,5 +1,8 @@
-/* ownCloud Android client application
- *   Copyright (C) 2014 ownCloud Inc.
+/**
+ *   ownCloud Android client application
+ *
+ *   @author masensio
+ *   Copyright (C) 2015 ownCloud Inc.
  *
  *   This program is free software: you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License version 2,
  *
  *   This program is free software: you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License version 2,
@@ -19,35 +22,32 @@ package com.owncloud.android.operations;
 
 /**
  * Creates a new share from a given file
 
 /**
  * Creates a new share from a given file
- * 
- * @author masensio
- *
  */
 
  */
 
+import android.content.Context;
 import android.content.Intent;
 
 import android.content.Intent;
 
+import com.owncloud.android.R;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.resources.shares.OCShare;
 import com.owncloud.android.lib.common.operations.RemoteOperation;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.resources.shares.OCShare;
 import com.owncloud.android.lib.common.operations.RemoteOperation;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
+import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation;
 import com.owncloud.android.lib.resources.shares.ShareType;
 import com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation;
 import com.owncloud.android.lib.resources.files.FileUtils;
 import com.owncloud.android.operations.common.SyncOperation;
 import com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation;
 import com.owncloud.android.lib.resources.shares.ShareType;
 import com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation;
 import com.owncloud.android.lib.resources.files.FileUtils;
 import com.owncloud.android.operations.common.SyncOperation;
-import com.owncloud.android.utils.Log_OC;
 
 public class CreateShareOperation extends SyncOperation {
 
     private static final String TAG = CreateShareOperation.class.getSimpleName();
 
 public class CreateShareOperation extends SyncOperation {
 
     private static final String TAG = CreateShareOperation.class.getSimpleName();
-    
-    // String to build the link with the token of a share: server address + "/public.php?service=files&t=" + token
-    private final String SHARING_LINK_TOKEN = "/public.php?service=files&t=";
 
     protected FileDataStorageManager mStorageManager;
 
 
     protected FileDataStorageManager mStorageManager;
 
+    private Context mContext;
     private String mPath;
     private ShareType mShareType;
     private String mShareWith;
     private String mPath;
     private ShareType mShareType;
     private String mShareWith;
@@ -58,25 +58,31 @@ public class CreateShareOperation extends SyncOperation {
 
     /**
      * Constructor
 
     /**
      * Constructor
+     * @param context       The context that the share is coming from.
      * @param path          Full path of the file/folder being shared. Mandatory argument
      * @param path          Full path of the file/folder being shared. Mandatory argument
-     * @param shareType     \910\92 = user, \911\92 = group, \913\92 = Public link. Mandatory argument
-     * @param shareWith     User/group ID with who the file should be shared.  This is mandatory for shareType of 0 or 1
-     * @param publicUpload  If \91false\92 (default) public cannot upload to a public shared folder. 
-     *                      If \91true\92 public can upload to a shared folder. Only available for public link shares
-     * @param password      Password to protect a public link share. Only available for public link shares
-     * @param permissions   1 - Read only \96 Default for \93public\94 shares
+     * @param shareType     0 = user, 1 = group, 3 = Public link. Mandatory argument
+     * @param shareWith     User/group ID with who the file should be shared.
+     *                      This is mandatory for shareType of 0 or 1
+     * @param publicUpload  If false (default) public cannot upload to a public shared folder. 
+     *                      If true public can upload to a shared folder.
+     *                      Only available for public link shares
+     * @param password      Password to protect a public link share.
+     *                      Only available for public link shares
+     * @param permissions   1 - Read only - Default for public shares
      *                      2 - Update
      *                      4 - Create
      *                      8 - Delete
      *                      16- Re-share
      *                      2 - Update
      *                      4 - Create
      *                      8 - Delete
      *                      16- Re-share
-     *                      31- All above \96 Default for \93private\94 shares
+     *                      31- All above - Default for private shares
      *                      For user or group shares.
      *                      To obtain combinations, add the desired values together.  
      *                      For user or group shares.
      *                      To obtain combinations, add the desired values together.  
-     *                      For instance, for \93Re-Share\94\93delete\94\93read\94\93update\94, add 16+8+2+1 = 27.
+     *                      For instance, for Re-Share, delete, read, update, add 16+8+2+1 = 27.
      */
      */
-    public CreateShareOperation(String path, ShareType shareType, String shareWith, boolean publicUpload, 
-            String password, int permissions, Intent sendIntent) {
+    public CreateShareOperation(Context context, String path, ShareType shareType, String shareWith,
+                                boolean publicUpload, String password, int permissions,
+                                Intent sendIntent) {
 
 
+        mContext = context;
         mPath = path;
         mShareType = shareType;
         mShareWith = shareWith;
         mPath = path;
         mShareType = shareType;
         mShareWith = shareWith;
@@ -92,31 +98,49 @@ public class CreateShareOperation extends SyncOperation {
         
         // Check if the share link already exists
         operation = new GetRemoteSharesForFileOperation(mPath, false, false);
         
         // Check if the share link already exists
         operation = new GetRemoteSharesForFileOperation(mPath, false, false);
-        RemoteOperationResult result = ((GetRemoteSharesForFileOperation)operation).execute(client);
+        RemoteOperationResult result =
+                ((GetRemoteSharesForFileOperation)operation).execute(client);
 
 
+        if (!result.isSuccess() || result.getData().size() <= 0) {
+            operation = new CreateRemoteShareOperation(mPath, mShareType, mShareWith, mPublicUpload,
+                    mPassword, mPermissions);
+            result = ((CreateRemoteShareOperation)operation).execute(client);
+        }
+        
         if (result.isSuccess()) {
             if (result.getData().size() > 0) {
                 OCShare share = (OCShare) result.getData().get(0);
         if (result.isSuccess()) {
             if (result.getData().size() > 0) {
                 OCShare share = (OCShare) result.getData().get(0);
-                // Update the link, build it with the token: server address + "/public.php?service=files&t=" + token
-                share.setShareLink(client.getBaseUri() + SHARING_LINK_TOKEN + share.getToken());
-                Log_OC.d(TAG, "Build Share link= " + share.getShareLink());
                 updateData(share);
                 updateData(share);
-            } else {
-                operation = new CreateRemoteShareOperation(mPath, mShareType, mShareWith, mPublicUpload, mPassword, mPermissions);
-                result = ((CreateRemoteShareOperation)operation).execute(client);
-
-                if (result.isSuccess()) {
-                    if (result.getData().size() > 0) {
-                        OCShare share = (OCShare) result.getData().get(0);
-                        updateData(share);
-                    }
-                }
-            }
+            } 
         }
         }
+        
         return result;
     }
     
         return result;
     }
     
-    
+    public String getPath() {
+        return mPath;
+    }
+
+    public ShareType getShareType() {
+        return mShareType;
+    }
+
+    public String getShareWith() {
+        return mShareWith;
+    }
+
+    public boolean getPublicUpload() {
+        return mPublicUpload;
+    }
+
+    public String getPassword() {
+        return mPassword;
+    }
+
+    public int getPermissions() {
+        return mPermissions;
+    }
+
     public Intent getSendIntent() {
         return mSendIntent;
     }
     public Intent getSendIntent() {
         return mSendIntent;
     }
@@ -137,6 +161,9 @@ public class CreateShareOperation extends SyncOperation {
         OCFile file = getStorageManager().getFileByPath(mPath);
         if (file!=null) {
             mSendIntent.putExtra(Intent.EXTRA_TEXT, share.getShareLink());
         OCFile file = getStorageManager().getFileByPath(mPath);
         if (file!=null) {
             mSendIntent.putExtra(Intent.EXTRA_TEXT, share.getShareLink());
+            mSendIntent.putExtra(Intent.EXTRA_SUBJECT,
+                    String.format(mContext.getString(R.string.subject_token),
+                    getClient().getCredentials().getUsername(), file.getFileName()));
             file.setPublicLink(share.getShareLink());
             file.setShareByLink(true);
             getStorageManager().saveFile(file);
             file.setPublicLink(share.getShareLink());
             file.setShareByLink(true);
             getStorageManager().saveFile(file);