Krita Source Code Documentation
Loading...
Searching...
No Matches
KisMacosSecurityBookmarkManager Class Reference

#include <KisMacosSecurityBookmarkManager.h>

+ Inheritance diagram for KisMacosSecurityBookmarkManager:

Classes

class  Private
 

Public Types

enum  SecurityBookmarkType { File = 0 , Directory }
 

Public Slots

void addBookmarkAndCheckParentDir (const QUrl &url)
 
void slotCreateBookmark (const QString &path)
 

Public Member Functions

void createBookmarkFromPath (const QString &path, const QString &refpath, SecurityBookmarkType type=SecurityBookmarkType::File)
 
bool isSandboxed ()
 
 KisMacosSecurityBookmarkManager ()
 
void loadSecurityScopedResources ()
 
bool parentDirHasPermissions (const QString &path)
 
bool requestAccessToDir (const QString &path)
 
void startAccessingSecurityScopedResources ()
 
void stopAccessingSecurityScopedResources ()
 
 ~KisMacosSecurityBookmarkManager ()
 

Static Public Member Functions

static KisMacosSecurityBookmarkManagerinstance ()
 

Private Member Functions

QUrl decodeBookmarkToURL (QString encodedPath)
 
void loadKeysFromArray (SecurityBookmarkType)
 
QString securityBookmarkTypeToString (const SecurityBookmarkType)
 

Private Attributes

const QScopedPointer< Privatem_d
 

Detailed Description

Definition at line 15 of file KisMacosSecurityBookmarkManager.h.

Member Enumeration Documentation

◆ SecurityBookmarkType

Constructor & Destructor Documentation

◆ KisMacosSecurityBookmarkManager()

◆ ~KisMacosSecurityBookmarkManager()

KisMacosSecurityBookmarkManager::~KisMacosSecurityBookmarkManager ( )

Member Function Documentation

◆ addBookmarkAndCheckParentDir

void KisMacosSecurityBookmarkManager::addBookmarkAndCheckParentDir ( const QUrl & url)
slot

Definition at line 236 of file KisMacosSecurityBookmarkManager.mm.

237{
238 const QString path = url.toDisplayString(QUrl::None);
239 qDebug() << "1 inserting to sandbox" << url << path;
240 if (!parentDirHasPermissions(path)) {
241 // we can't force the user to select a directory in particular
242 // we add the bookmark even if the file root directory was selected
243 createBookmarkFromPath(path, QString());
244
245 requestAccessToDir(path);
246 }
247}
void createBookmarkFromPath(const QString &path, const QString &refpath, SecurityBookmarkType type=SecurityBookmarkType::File)

References createBookmarkFromPath(), parentDirHasPermissions(), and requestAccessToDir().

◆ createBookmarkFromPath()

void KisMacosSecurityBookmarkManager::createBookmarkFromPath ( const QString & path,
const QString & refpath,
SecurityBookmarkType type = SecurityBookmarkType::File )

Creates a bookmark security scope for diven path

Parameters
pathFile path returned from NSOpenPanel
refpathIf non empty creates the security key relative to the reference path. This is useful for documents referring to other documents

Definition at line 86 of file KisMacosSecurityBookmarkManager.mm.

87{
88 if(path.isEmpty()) {
89 return;
90 }
91 NSURLBookmarkCreationOptions
92 options = m_d->entitlements.hasEntitlement(KisMacosEntitlements::Entitlements::BookmarkScopeApp) ? NSURLBookmarkCreationWithSecurityScope : 0;
93 NSError *err = nil;
94
95 NSString *pathEscaped = [path.toNSString() stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
96 NSURL *url = [NSURL URLWithString:pathEscaped];
97
98 NSURL *refurl = nil;
99 if (!refpath.isEmpty()) {
100 NSString *refpathEscaped = [refpath.toNSString() stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
101 refurl = [NSURL URLWithString:refpathEscaped];
102 }
103
104 // working with paths as strings
105 // this assumes user used NSOpenPanel on the path itself
106
107 // Create bookmark
108 NSData *bookmark = [url bookmarkDataWithOptions: options includingResourceValuesForKeys: nil relativeToURL: refurl error:&err];
109 NSString *bookmarkString = [bookmark base64EncodedStringWithOptions: NSDataBase64Encoding64CharacterLineLength];
110 NSLog(@" path: %@\n err: %@", pathEscaped, err);
111
112 if (!err) {
113 [url startAccessingSecurityScopedResource];
114
115 QString base64Data = QString::fromNSString(bookmarkString);
116 // write to file
117 const QString configPath = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation);
118 QSettings kritarc(configPath + QStringLiteral("/securitybookmarkrc"), QSettings::NativeFormat);
119
120 // get array current size
121 int size = kritarc.beginReadArray(securityBookmarkTypeToString(type));
122 kritarc.endArray();
123
124 kritarc.beginWriteArray(securityBookmarkTypeToString(type));
125 kritarc.setArrayIndex(size);
126 kritarc.setValue("path",path);
127 kritarc.setValue("base64", base64Data);
128 kritarc.endArray();
129
130 // Finally add to hashmap
131 m_d->securedFiles[path] = base64Data;
132 }
133
134 return;
135}
QString securityBookmarkTypeToString(const SecurityBookmarkType)
int size(const Forest< T > &forest)
Definition KisForest.h:1232

References KisMacosEntitlements::BookmarkScopeApp, m_d, and securityBookmarkTypeToString().

◆ decodeBookmarkToURL()

QUrl KisMacosSecurityBookmarkManager::decodeBookmarkToURL ( QString encodedPath)
private

Definition at line 157 of file KisMacosSecurityBookmarkManager.mm.

157 {
158 NSString *bookmarkString = encodedPath.toNSString();
159 NSData *decodedBookmark = [[NSData alloc] initWithBase64EncodedString: bookmarkString options:NSDataBase64DecodingIgnoreUnknownCharacters];
160
161 NSError *err = nil;
162
163 // Resolve the decoded bookmark data into a security-scoped URL.
164 NSURL *url =[NSURL URLByResolvingBookmarkData: decodedBookmark options: NSURLBookmarkResolutionWithSecurityScope relativeToURL: nil bookmarkDataIsStale: nil error:&err];
165 return QUrl::fromNSURL(url);
166}

◆ instance()

KisMacosSecurityBookmarkManager * KisMacosSecurityBookmarkManager::instance ( )
static

Definition at line 57 of file KisMacosSecurityBookmarkManager.mm.

58{
59 return s_instance;
60}

◆ isSandboxed()

bool KisMacosSecurityBookmarkManager::isSandboxed ( )

Definition at line 259 of file KisMacosSecurityBookmarkManager.mm.

260{
261 return m_d->entitlements.sandbox();
262}

References m_d.

◆ loadKeysFromArray()

void KisMacosSecurityBookmarkManager::loadKeysFromArray ( SecurityBookmarkType arrayKey)
private

Definition at line 137 of file KisMacosSecurityBookmarkManager.mm.

138{
139 const QString configPath = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation);
140 QSettings kritarc(configPath + QStringLiteral("/securitybookmarkrc"), QSettings::NativeFormat);
141
142 int size = kritarc.beginReadArray(securityBookmarkTypeToString(arrayKey));
143 for (int i = 0; i < size; i++) {
144 kritarc.setArrayIndex(i);
145 QString key = kritarc.value("path").toString();
146 m_d->securedFiles[key] = kritarc.value("base64").toString();
147 }
148 kritarc.endArray();
149}

References m_d, and securityBookmarkTypeToString().

◆ loadSecurityScopedResources()

void KisMacosSecurityBookmarkManager::loadSecurityScopedResources ( )

Loads security scoped files to internal dictionary

Definition at line 151 of file KisMacosSecurityBookmarkManager.mm.

References Directory, File, and loadKeysFromArray().

◆ parentDirHasPermissions()

bool KisMacosSecurityBookmarkManager::parentDirHasPermissions ( const QString & path)
Returns
Return true if file is contained within a directory with permissions previously granted by NSOpenPanel

Definition at line 74 of file KisMacosSecurityBookmarkManager.mm.

75{
76 bool contained = false;
77 Q_FOREACH(QString key, m_d->securedFiles.keys()) {
78 if(path.contains(key)) {
79 contained = true;
80 break;
81 }
82 }
83 return contained;
84}

References m_d.

◆ requestAccessToDir()

bool KisMacosSecurityBookmarkManager::requestAccessToDir ( const QString & path)

Shows NSOpenPanel and saves the selected directory for future access

Returns
TRUE if selected path is same as input path. users can still select any directory they want, but we specifically requested they give us permission to a specific directory

Definition at line 184 of file KisMacosSecurityBookmarkManager.mm.

185{
186 bool fileSelected = false;
187 QUrl fileURL = QUrl(path);
188
189 NSString *nsStdPath = path.toNSString();
190#ifdef KIS_STANDALONE
191 NSAlert *alert = [[NSAlert alloc] init];
192 [alert setAlertStyle:NSAlertStyleInformational];
193 [alert setMessageText:[NSString stringWithFormat:@"The file %@ is located in a directory where the application has no permission, please give the permission to the container folder or a higher one to allow krita to save temporary backups next to your file", [nsStdPath lastPathComponent]]];
194 [alert runModal];
195
196
197 NSOpenPanel *panel = [NSOpenPanel openPanel];
198 panel.canChooseFiles = false;
199 panel.canChooseDirectories = true;
200 NSURL *startLoc = [[NSURL alloc] initFileURLWithPath:nsStdPath ];
201 panel.directoryURL = startLoc;
202
203 if ([panel runModal] == NSModalResponseOK) {
204 static NSURL *fileUrl = [panel URL];
205 NSString *pathString = [fileUrl absoluteString];
206
207 QString filepath = QString::fromNSString(pathString);
209 fileSelected = true;
210 }
211
212#else
213 QMessageBox msgBox;
214 msgBox.setText(i18n("The file %1 is located in a directory where the application has no permissions, please give krita permission to this directory or a higher one to allow krita to save temporary backups next to your file", fileURL.fileName()));
215 msgBox.setInformativeText(i18n("The directory you select will grant krita permissions to all files and directories contained in it"));
216 msgBox.setStandardButtons(QMessageBox::Ok);
217 msgBox.setDefaultButton(QMessageBox::Ok);
218 int ret = msgBox.exec();
219
220 QUrl dirUrl = QFileDialog::getExistingDirectoryUrl(0, QString(), QUrl(path));
221
222 if (!dirUrl.isEmpty()) {
223 QString filepath = dirUrl.toDisplayString(QUrl::None);
225 fileSelected = true;
226 }
227#endif
228 return fileSelected;
229}
void init(QOpenGLContext *ctx)

References createBookmarkFromPath(), and Directory.

◆ securityBookmarkTypeToString()

QString KisMacosSecurityBookmarkManager::securityBookmarkTypeToString ( const SecurityBookmarkType type)
private

Definition at line 231 of file KisMacosSecurityBookmarkManager.mm.

232{
233 return QMetaEnum::fromType<SecurityBookmarkType>().valueToKey(type);
234}

◆ slotCreateBookmark

void KisMacosSecurityBookmarkManager::slotCreateBookmark ( const QString & path)
slot

Definition at line 249 of file KisMacosSecurityBookmarkManager.mm.

250{
251 // necessary convert to get proper location
252 QUrl url = QUrl::fromLocalFile(path);
253 QString pathString = url.toDisplayString();
254 if (!m_d->securedFiles.contains(pathString)) {
255 createBookmarkFromPath(pathString, QString());
256 }
257}

References createBookmarkFromPath(), and m_d.

◆ startAccessingSecurityScopedResources()

void KisMacosSecurityBookmarkManager::startAccessingSecurityScopedResources ( )

starts access to all registered security bookmarks

Definition at line 168 of file KisMacosSecurityBookmarkManager.mm.

169{
170 Q_FOREACH(QString encodedPath, m_d->securedFiles.values()) {
171 NSURL *url = decodeBookmarkToURL(encodedPath).toNSURL();
172 [url startAccessingSecurityScopedResource];
173 }
174}

References decodeBookmarkToURL(), and m_d.

◆ stopAccessingSecurityScopedResources()

void KisMacosSecurityBookmarkManager::stopAccessingSecurityScopedResources ( )

stops access to all started security bookmarks

Definition at line 176 of file KisMacosSecurityBookmarkManager.mm.

177{
178 Q_FOREACH(QString encodedPath, m_d->securedFiles.values()) {
179 NSURL *url = decodeBookmarkToURL(encodedPath).toNSURL();
180 [url stopAccessingSecurityScopedResource];
181 }
182}

References decodeBookmarkToURL(), and m_d.

Member Data Documentation

◆ m_d

const QScopedPointer<Private> KisMacosSecurityBookmarkManager::m_d
private

Definition at line 90 of file KisMacosSecurityBookmarkManager.h.


The documentation for this class was generated from the following files: