From 74fe20cf6e9a77c5889bab397b3045f0ba3b1150 Mon Sep 17 00:00:00 2001
From: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
Date: Thu, 14 Jun 2018 12:05:53 -0400
Subject: [PATCH] app store: support sandboxing

- remove unnecessary entitlements
- save recorded calls and incoming files in the app folder.

Change-Id: Id2852f7cac65a1d75c76ed907a697dd0e1f5d069
Reviewed-by: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
---
 data/Ring.entitlements         |  8 --------
 src/AudioPrefsVC.mm            | 25 ++++++++++++++++++++-----
 src/GeneralPrefsVC.mm          | 12 ++++++++----
 src/RingWindowController.mm    | 15 +++++++++------
 ui/Base.lproj/AudioPrefs.xib   |  8 ++++++--
 ui/Base.lproj/GeneralPrefs.xib |  5 +++--
 6 files changed, 46 insertions(+), 27 deletions(-)

diff --git a/data/Ring.entitlements b/data/Ring.entitlements
index 881c5534..f8b23576 100644
--- a/data/Ring.entitlements
+++ b/data/Ring.entitlements
@@ -4,18 +4,10 @@
 <dict>
 	<key>com.apple.security.app-sandbox</key>
 	<true/>
-	<key>com.apple.security.assets.movies.read-write</key>
-	<true/>
-	<key>com.apple.security.assets.music.read-write</key>
-	<true/>
-	<key>com.apple.security.assets.pictures.read-write</key>
-	<true/>
 	<key>com.apple.security.device.camera</key>
 	<true/>
 	<key>com.apple.security.device.microphone</key>
 	<true/>
-	<key>com.apple.security.files.downloads.read-write</key>
-	<true/>
 	<key>com.apple.security.files.user-selected.read-write</key>
 	<true/>
 	<key>com.apple.security.network.client</key>
diff --git a/src/AudioPrefsVC.mm b/src/AudioPrefsVC.mm
index 543d63c9..901846ac 100644
--- a/src/AudioPrefsVC.mm
+++ b/src/AudioPrefsVC.mm
@@ -33,15 +33,23 @@
 @property (assign) IBOutlet NSPopUpButton *inputDeviceList;
 @property (assign) IBOutlet NSButton *alwaysRecordingButton;
 @property (assign) IBOutlet NSButton *muteDTMFButton;
+@property (assign) IBOutlet NSTextField *recordingHeaderTitle;
+@property (assign) IBOutlet NSTextField *recordingpathLabel;
+@property (assign) IBOutlet NSLayoutConstraint* audioMarginTopConstraint;
+@property (assign) IBOutlet NSLayoutConstraint* audioMarginBottomConstraint;
+
+
+
 
 @end
 
 @implementation AudioPrefsVC
-@synthesize recordingsPathControl;
+@synthesize recordingsPathControl, recordingHeaderTitle, recordingpathLabel;
 @synthesize outputDeviceList;
 @synthesize inputDeviceList;
 @synthesize alwaysRecordingButton;
 @synthesize muteDTMFButton;
+@synthesize audioMarginTopConstraint, audioMarginBottomConstraint;
 
 - (void)loadView
 {
@@ -62,6 +70,16 @@
             Audio::Settings::instance().areDTMFMuted()?NSOnState:NSOffState];
     NSArray* pathComponentArray = [self pathComponentArrayWithCurrentUrl:Media::RecordingModel::instance().recordPath().toNSString()];
     [recordingsPathControl setPathComponentCells:pathComponentArray];
+
+    if (appSandboxed()) {
+        [alwaysRecordingButton setHidden:YES];
+        [recordingsPathControl setEnabled:NO];
+        [recordingsPathControl setHidden: YES];
+        [recordingHeaderTitle setHidden: YES];
+        [recordingpathLabel setHidden: YES];
+        audioMarginTopConstraint.constant = 10.0f;
+        audioMarginBottomConstraint.constant = 67.0f;
+    }
 }
 
 - (IBAction)toggleMuteDTMF:(NSButton *)sender
@@ -171,10 +189,7 @@
 }
 
 - (BOOL) panel:(id)sender shouldEnableURL:(NSURL*)url {
-    if(!appSandboxed()) {
-        return YES;
-    }
-    return isUrlAccessibleFromSandbox(url);
+    return YES;
 }
 
 #pragma mark - NSMenuDelegate methods
diff --git a/src/GeneralPrefsVC.mm b/src/GeneralPrefsVC.mm
index 7435098d..38574bd2 100644
--- a/src/GeneralPrefsVC.mm
+++ b/src/GeneralPrefsVC.mm
@@ -56,6 +56,7 @@
     __unsafe_unretained IBOutlet NSTextField* profileNameField;
     __unsafe_unretained IBOutlet NSImageView* addProfilePhotoImage;
     __unsafe_unretained IBOutlet NSButton *downloadFolder;
+    __unsafe_unretained IBOutlet NSTextField *downloadFolderLabel;
 }
 @end
 
@@ -115,6 +116,12 @@
             [photoView setBordered:NO];
         }
     }
+    if (appSandboxed()) {
+        [downloadFolder setHidden:YES];
+        [downloadFolder setEnabled:NO];
+        [downloadFolderLabel setHidden: YES];
+        return;
+    }
     if (dataTransferModel) {
         downloadFolder.title = [@(dataTransferModel->downloadDirectory.c_str()) lastPathComponent];
     }
@@ -276,10 +283,7 @@
 #pragma mark - NSOpenSavePanelDelegate delegate methods
 
 - (BOOL) panel:(id)sender shouldEnableURL:(NSURL*)url {
-    if(!appSandboxed()) {
-        return YES;
-    }
-    return isUrlAccessibleFromSandbox(url);
+    return YES;
 }
 
 @end
diff --git a/src/RingWindowController.mm b/src/RingWindowController.mm
index 64e59a43..9107eba5 100644
--- a/src/RingWindowController.mm
+++ b/src/RingWindowController.mm
@@ -40,6 +40,7 @@
 #import <api/contactmodel.h>
 #import <api/contact.h>
 #import <api/datatransfermodel.h>
+#import <media/recordingmodel.h>
 
 // Ring
 #import "AppDelegate.h"
@@ -53,6 +54,7 @@
 #import "views/NSColor+RingTheme.h"
 #import "views/BackgroundView.h"
 #import "ChooseAccountVC.h"
+#import "utils.h"
 
 @interface RingWindowController () <MigrateRingAccountsDelegate, NSToolbarDelegate>
 
@@ -124,16 +126,17 @@ NSString* const kChangeAccountToolBarItemIdentifier  = @"ChangeAccountToolBarIte
     NSToolbar *toolbar = self.window.toolbar;
     toolbar.delegate = self;
     [toolbar insertItemWithItemIdentifier:kChangeAccountToolBarItemIdentifier atIndex:1];
-    // set download folder (default - 'Downloads')
+    // set download folder (default - 'Documents')
     NSString* path = [[NSUserDefaults standardUserDefaults] stringForKey:Preferences::DownloadFolder];
     if (!path || path.length == 0) {
-        NSURL *downloadsURL = [[NSFileManager defaultManager]
-                               URLForDirectory:NSDownloadsDirectory
-                               inDomain:NSUserDomainMask appropriateForURL:nil
-                               create:YES error:nil];
-        path = [[downloadsURL path] stringByAppendingString:@"/"];
+        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
+        path = [[paths objectAtIndex:0] stringByAppendingString:@"/"];
     }
     lrc_->getDataTransferModel().downloadDirectory = std::string([path UTF8String]);
+    if(appSandboxed()) {
+        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
+        Media::RecordingModel::instance().setRecordPath(QString::fromNSString([paths objectAtIndex:0]));
+    }
 }
 
 - (void) connect
diff --git a/ui/Base.lproj/AudioPrefs.xib b/ui/Base.lproj/AudioPrefs.xib
index b0cd6244..9bdfb548 100644
--- a/ui/Base.lproj/AudioPrefs.xib
+++ b/ui/Base.lproj/AudioPrefs.xib
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14109" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
+<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14113" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
     <dependencies>
-        <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14109"/>
+        <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14113"/>
         <capability name="Alignment constraints to the first baseline" minToolsVersion="6.0"/>
         <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
     </dependencies>
@@ -9,9 +9,13 @@
         <customObject id="-2" userLabel="File's Owner" customClass="AudioPrefsVC">
             <connections>
                 <outlet property="alwaysRecordingButton" destination="kOg-hX-1Q5" id="n6R-hv-JLY"/>
+                <outlet property="audioMarginBottomConstraint" destination="Oln-9b-2Ri" id="gcF-44-IOP"/>
+                <outlet property="audioMarginTopConstraint" destination="Fo0-5T-cmf" id="oRv-y8-en7"/>
                 <outlet property="inputDeviceList" destination="bK9-uH-jDb" id="zhZ-Cr-n6B"/>
                 <outlet property="muteDTMFButton" destination="Jyg-5H-Ygw" id="PDq-aH-ieL"/>
                 <outlet property="outputDeviceList" destination="9SA-9E-IzQ" id="YpE-Fz-J8P"/>
+                <outlet property="recordingHeaderTitle" destination="ftI-JV-7cE" id="pKF-X5-Ykn"/>
+                <outlet property="recordingpathLabel" destination="She-fN-vzD" id="DKZ-Ln-xNz"/>
                 <outlet property="recordingsPathControl" destination="nyE-tZ-v4C" id="4cw-pY-c3Z"/>
                 <outlet property="view" destination="deY-JV-Rfe" id="ebJ-o2-NpL"/>
             </connections>
diff --git a/ui/Base.lproj/GeneralPrefs.xib b/ui/Base.lproj/GeneralPrefs.xib
index 6f427663..5600c2a0 100644
--- a/ui/Base.lproj/GeneralPrefs.xib
+++ b/ui/Base.lproj/GeneralPrefs.xib
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14109" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
+<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14113" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
     <dependencies>
-        <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14109"/>
+        <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14113"/>
         <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
     </dependencies>
     <objects>
@@ -10,6 +10,7 @@
                 <outlet property="addProfilePhotoImage" destination="3l7-TU-AMG" id="oQC-jN-JV0"/>
                 <outlet property="checkIntervalPopUp" destination="RYP-3d-PCa" id="JNO-GR-CV8"/>
                 <outlet property="downloadFolder" destination="7bY-JW-z0U" id="ylD-Yb-I2N"/>
+                <outlet property="downloadFolderLabel" destination="IHD-1X-sid" id="XnP-ui-hIA"/>
                 <outlet property="historyChangedLabel" destination="Gyi-ID-Z3v" id="aoO-Fh-UCQ"/>
                 <outlet property="historyStepper" destination="QmA-ZI-ZL5" id="dDV-1G-rZs"/>
                 <outlet property="historySwitch" destination="DgD-2y-4g5" id="GYk-pz-jGT"/>
-- 
GitLab