Commit 4138db1b authored by Kateryna Kostiuk's avatar Kateryna Kostiuk Committed by Andreas Traczyk

preferences: disable inaccessible folders

Disable folders that could not be accessed from sandboxed version
when selecting download folder for incoming files and for recorded
calls.

Change-Id: I9277a87e829f7f68fc283ca2c67cdb75d52dfd41
Reviewed-by: Andreas Traczyk's avatarAndreas Traczyk <andreas.traczyk@savoirfairelinux.com>
parent 54d76970
......@@ -24,6 +24,7 @@
#import <audio/inputdevicemodel.h>
#import <audio/outputdevicemodel.h>
#import <qitemselectionmodel.h>
#import "utils.h"
@interface AudioPrefsVC ()
......@@ -59,14 +60,8 @@
[self.muteDTMFButton setState:
Audio::Settings::instance().areDTMFMuted()?NSOnState:NSOffState];
if([Media::RecordingModel::instance().recordPath().toNSString() isEqualToString:@""]) {
NSArray* pathComponentArray = [self pathComponentArray];
[recordingsPathControl setPathComponentCells:pathComponentArray];
} else {
[recordingsPathControl setURL:
[NSURL URLWithString:Media::RecordingModel::instance().recordPath().toNSString()]];
}
NSArray* pathComponentArray = [self pathComponentArrayWithCurrentUrl:Media::RecordingModel::instance().recordPath().toNSString()];
[recordingsPathControl setPathComponentCells:pathComponentArray];
}
- (IBAction)toggleMuteDTMF:(NSButton *)sender
......@@ -81,8 +76,9 @@
- (IBAction)pathControlSingleClick:(id)sender {
// Select that chosen component of the path.
[self.recordingsPathControl setURL:[[self.recordingsPathControl clickedPathComponentCell] URL]];
Media::RecordingModel::instance().setRecordPath(QString::fromNSString([self.recordingsPathControl.URL path]));
NSArray* pathComponentArray = [self pathComponentArrayWithCurrentUrl:[[self.recordingsPathControl clickedPathComponentCell] URL].path];
[recordingsPathControl setPathComponentCells:pathComponentArray];
Media::RecordingModel::instance().setRecordPath(QString::fromNSString([self.recordingsPathControl.URL path]));
}
- (IBAction)chooseOutput:(id)sender {
......@@ -104,29 +100,30 @@
/*
Assemble a set of custom cells to display into an array to pass to the path control.
*/
- (NSArray *)pathComponentArray
- (NSArray *)pathComponentArrayWithCurrentUrl:(NSString *) url
{
NSMutableArray *pathComponentArray = [[NSMutableArray alloc] init];
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSURL* desktopURL = [fileManager URLForDirectory:NSDesktopDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
NSURL* documentsURL = [fileManager URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
NSURL* userURL = [fileManager URLForDirectory:NSUserDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
NSURL* downloadURL = [fileManager URLForDirectory:NSDownloadsDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
NSPathComponentCell *componentCell;
// Use utility method to obtain a NSPathComponentCell based on icon, title and URL.
componentCell = [self componentCellForType:kGenericFolderIcon withTitle:@"Desktop" URL:desktopURL];
[pathComponentArray addObject:componentCell];
componentCell = [self componentCellForType:kGenericFolderIcon withTitle:@"Documents" URL:documentsURL];
componentCell = [self componentCellForType:kGenericFolderIcon withTitle:@"Downloads" URL:downloadURL];
[pathComponentArray addObject:componentCell];
componentCell = [self componentCellForType:kUserFolderIcon withTitle:NSUserName() URL:userURL];
[pathComponentArray addObject:componentCell];
return pathComponentArray;
NSString * downloads = [downloadURL path];
if([url isEqualToString:downloads]) {
return pathComponentArray;
}
if(![url isEqualToString:@""]) {
NSString * name = [url componentsSeparatedByString:@"/"].lastObject;
if(!name) {
return pathComponentArray;
}
componentCell = [self componentCellForType:kGenericFolderIcon withTitle:name URL:[NSURL URLWithString: url]];
[pathComponentArray addObject:componentCell];
return pathComponentArray;
}
}
/*
......@@ -173,6 +170,13 @@
return YES;
}
- (BOOL) panel:(id)sender shouldEnableURL:(NSURL*)url {
if(!appSandboxed()) {
return YES;
}
return isUrlAccessibleFromSandbox(url);
}
#pragma mark - NSMenuDelegate methods
- (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel
......
......@@ -21,6 +21,6 @@
#import <Cocoa/Cocoa.h>
#import "LrcModelsSProtocol.h"
@interface GeneralPrefsVC : NSViewController <LrcModelsSProtocol>
@interface GeneralPrefsVC : NSViewController <LrcModelsSProtocol, NSOpenSavePanelDelegate>
@end
......@@ -41,6 +41,7 @@
#import "Constants.h"
#import "views/NSImage+Extensions.h"
#import "delegates/ImageManipulationDelegate.h"
#import "utils.h"
@interface GeneralPrefsVC () {
__unsafe_unretained IBOutlet NSTextField* historyChangedLabel;
......@@ -146,6 +147,7 @@
[panel setAllowsMultipleSelection:NO];
[panel setCanChooseDirectories:YES];
[panel setCanChooseFiles:NO];
panel.delegate = self;
if ([panel runModal] != NSFileHandlingPanelOKButton) return;
if ([[panel URLs] lastObject] == nil) return;
NSString * path = [[[[panel URLs] lastObject] path] stringByAppendingString:@"/"];
......@@ -271,4 +273,13 @@
}
}
#pragma mark - NSOpenSavePanelDelegate delegate methods
- (BOOL) panel:(id)sender shouldEnableURL:(NSURL*)url {
if(!appSandboxed()) {
return YES;
}
return isUrlAccessibleFromSandbox(url);
}
@end
......@@ -104,3 +104,36 @@ setVideoAutoQuality(bool autoQuality, std::string accountId)
codecModel << CodecModel::EditAction::SAVE;
}
}
static inline bool isUrlAccessibleFromSandbox(NSURL* url)
{
NSFileManager* fileManager = [[NSFileManager alloc] init];
NSArray* urlPathsMusic = [fileManager URLsForDirectory:NSMusicDirectory
inDomains:NSUserDomainMask];
NSArray* urlPathsPictures = [fileManager URLsForDirectory:NSPicturesDirectory
inDomains:NSUserDomainMask];
NSArray* urlPathsDownloads = [fileManager URLsForDirectory:NSDownloadsDirectory
inDomains:NSUserDomainMask];
NSArray* urlPathsMovies = [fileManager URLsForDirectory:NSMoviesDirectory
inDomains:NSUserDomainMask];
NSArray* availablePaths = [[[urlPathsMusic arrayByAddingObjectsFromArray: urlPathsPictures] arrayByAddingObjectsFromArray: urlPathsDownloads] arrayByAddingObjectsFromArray: urlPathsMovies];
if([availablePaths containsObject:url]) {
return YES;
}
for (NSURL* availableUrl in availablePaths) {
if ([url.path containsString:availableUrl.path]) {
return YES;
}
}
return NO;
}
static inline bool appSandboxed()
{
NSString* bundleID = [[NSBundle mainBundle] bundleIdentifier];
NSDictionary* environment = [[NSProcessInfo processInfo] environment];
if(environment[bundleID]) {
return YES;
}
return NO;
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="11201" systemVersion="15G1004" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<?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">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11201"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14109"/>
<capability name="Alignment constraints to the first baseline" minToolsVersion="6.0"/>
<capability name="Alignment constraints with different attributes" minToolsVersion="5.1"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="AudioPrefsVC">
......@@ -144,7 +144,7 @@
</constraints>
<pathCell key="cell" selectable="YES" editable="YES" alignment="left" pathStyle="popUp" id="SPB-Bz-vDM">
<font key="font" metaFont="system"/>
<url key="url" string="file://localhost/Applications/"/>
<url key="url" string="file:///Downloads"/>
</pathCell>
<connections>
<action selector="pathControlSingleClick:" target="-2" id="jyC-qZ-OzW"/>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment