| 165 | } |
| 166 | |
| 167 | bool SettingsObject::setPathWithBookmark(const QString& id, const QString& path) |
| 168 | { |
| 169 | auto setting = getSetting(id); |
| 170 | if (!setting) { |
| 171 | qCritical() << QString("Error changing setting %1. Setting doesn't exist.").arg(id); |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | QDir dir(path); |
| 176 | if (!dir.exists()) { |
| 177 | qCritical() << QString("Error changing setting %1. Path doesn't exist.").arg(id); |
| 178 | return false; |
| 179 | } |
| 180 | QString absolutePath = dir.absolutePath(); |
| 181 | QString bookmarkId = id + "Bookmark"; |
| 182 | std::shared_ptr<Setting> bookmarkSetting = getSetting(bookmarkId); |
| 183 | // there is no need to use bookmarks if the default value is used or the directory is within the data directory (already can access) |
| 184 | if (path == setting->defValue().toString() || absolutePath.startsWith(QDir::current().absolutePath())) { |
| 185 | bookmarkSetting->reset(); |
| 186 | return true; |
| 187 | } |
| 188 | QByteArray bytes = m_sandboxedFileAccess.pathToSecurityScopedBookmark(absolutePath); |
| 189 | if (bytes.isEmpty()) { |
| 190 | qCritical() << QString("Failed to create bookmark for %1 - no access?").arg(id); |
| 191 | // TODO: show an alert to the user asking them to reselect the directory |
| 192 | return false; |
| 193 | } |
| 194 | auto oldBookmark = bookmarkSetting->get().toByteArray(); |
| 195 | m_sandboxedFileAccess.stopUsingSecurityScopedBookmark(oldBookmark); |
| 196 | if (!bytes.isEmpty() && bookmarkSetting) { |
| 197 | bookmarkSetting->set(bytes); |
| 198 | bool stale; |
| 199 | m_sandboxedFileAccess.startUsingSecurityScopedBookmark(bytes, stale); |
| 200 | // just created the bookmark, it shouldn't be stale |
| 201 | } |
| 202 | |
| 203 | setting->set(path); |
| 204 | return true; |
| 205 | } |
| 206 | #endif |
| 207 | |
| 208 | void SettingsObject::reset(const QString& id) const |