| 119 | |
| 120 | #ifdef Q_OS_MACOS |
| 121 | QString SettingsObject::getPathFromBookmark(const QString& id) |
| 122 | { |
| 123 | auto setting = getSetting(id); |
| 124 | if (!setting) { |
| 125 | qCritical() << QString("Error changing setting %1. Setting doesn't exist.").arg(id); |
| 126 | return ""; |
| 127 | } |
| 128 | |
| 129 | // there is no need to use bookmarks if the default value is used or the directory is within the data directory (already can access) |
| 130 | if (setting->get() == setting->defValue() || |
| 131 | QDir(setting->get().toString()).absolutePath().startsWith(QDir::current().absolutePath())) { |
| 132 | return setting->get().toString(); |
| 133 | } |
| 134 | |
| 135 | auto bookmarkId = id + "Bookmark"; |
| 136 | auto bookmarkSetting = getSetting(bookmarkId); |
| 137 | if (!bookmarkSetting) { |
| 138 | qCritical() << QString("Error changing setting %1. Bookmark setting doesn't exist.").arg(id); |
| 139 | return ""; |
| 140 | } |
| 141 | |
| 142 | QByteArray bookmark = bookmarkSetting->get().toByteArray(); |
| 143 | if (bookmark.isEmpty()) { |
| 144 | qDebug() << "Creating bookmark for" << id << "at" << setting->get().toString(); |
| 145 | setPathWithBookmark(id, setting->get().toString()); |
| 146 | return setting->get().toString(); |
| 147 | } |
| 148 | bool stale; |
| 149 | QUrl url = m_sandboxedFileAccess.securityScopedBookmarkToURL(bookmark, stale); |
| 150 | if (url.isValid()) { |
| 151 | if (stale) { |
| 152 | setting->set(url.path()); |
| 153 | bookmarkSetting->set(bookmark); |
| 154 | } |
| 155 | |
| 156 | m_sandboxedFileAccess.startUsingSecurityScopedBookmark(bookmark, stale); |
| 157 | // already did a stale check, no need to do it again |
| 158 | |
| 159 | // convert to relative path to current directory if `url` is a descendant of the current directory |
| 160 | QDir currentDir = QDir::current().absolutePath(); |
| 161 | return url.path().startsWith(currentDir.absolutePath()) ? currentDir.relativeFilePath(url.path()) : url.path(); |
| 162 | } |
| 163 | |
| 164 | return setting->get().toString(); |
| 165 | } |
| 166 | |
| 167 | bool SettingsObject::setPathWithBookmark(const QString& id, const QString& path) |
| 168 | { |