From ca88a5fe6239dfd4bd789fe26651ae5348b95547 Mon Sep 17 00:00:00 2001 From: Tsu Jan Date: Thu, 15 Aug 2024 23:02:39 +0330 Subject: [PATCH] Use our own bookmarks file The new file is `~/.local/share/libfm-qt/bookmarks`. If it doesn't exist or is empty, as soon as a libfm-qt based app is launched, the GTK3 file `~/.config/gtk-3.0/bookmarks` will be consulted for backward compatibility, then the new file will be created and the GTK3 file will be ignored. Closes https://github.com/lxqt/libfm-qt/issues/1009 NOTE: The portal file dialogs will see the change after a reboot (or after restarting user portal services). --- src/core/bookmarks.cpp | 36 +++++++++++++++++++++--------------- src/core/bookmarks.h | 2 +- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/core/bookmarks.cpp b/src/core/bookmarks.cpp index 2034d330..28c8416d 100644 --- a/src/core/bookmarks.cpp +++ b/src/core/bookmarks.cpp @@ -9,11 +9,11 @@ namespace Fm { std::weak_ptr Bookmarks::globalInstance_; static inline CStrPtr get_legacy_bookmarks_file(void) { - return CStrPtr{g_build_filename(g_get_home_dir(), ".gtk-bookmarks", nullptr)}; + return CStrPtr{g_build_filename(g_get_user_config_dir(), "gtk-3.0", "bookmarks", nullptr)}; } static inline CStrPtr get_new_bookmarks_file(void) { - return CStrPtr{g_build_filename(g_get_user_config_dir(), "gtk-3.0", "bookmarks", nullptr)}; + return CStrPtr{g_build_filename(g_get_user_data_dir(), "libfm-qt", "bookmarks", nullptr)}; } BookmarkItem::BookmarkItem(const FilePath& path, const QString name): @@ -74,14 +74,17 @@ Bookmarks::Bookmarks(QObject* parent): QObject(parent), idle_handler{false} { - /* trying the gtk-3.0 first and use it if it exists */ + /* trying our config file first */ auto fpath = get_new_bookmarks_file(); file_ = FilePath::fromLocalPath(fpath.get()); - load(); - if(items_.empty()) { /* not found, use legacy file */ + load(file_); + if(items_.empty()) { /* not found, check the legacy, gtk-3.0 file */ fpath = get_legacy_bookmarks_file(); - file_ = FilePath::fromLocalPath(fpath.get()); - load(); + auto gtkFile = FilePath::fromLocalPath(fpath.get()); + load(gtkFile); + if(!items_.empty()) { + queueSave(); + } } mon = GObjectPtr{g_file_monitor_file(file_.gfile().get(), G_FILE_MONITOR_NONE, nullptr, nullptr), false}; if(mon) { @@ -161,18 +164,21 @@ void Bookmarks::save() { } idle_handler = false; // G_UNLOCK(bookmarks); - GError* err = nullptr; - if(!g_file_replace_contents(file_.gfile().get(), buf.c_str(), buf.length(), nullptr, - FALSE, G_FILE_CREATE_NONE, nullptr, nullptr, &err)) { - g_critical("%s", err->message); - g_error_free(err); + CStrPtr libfmDataDir{g_build_filename(g_get_user_data_dir(), "libfm-qt", nullptr)}; + if(g_mkdir_with_parents(libfmDataDir.get(), 0755) == 0) { + GError* err = nullptr; + if(!g_file_replace_contents(file_.gfile().get(), buf.c_str(), buf.length(), nullptr, + FALSE, G_FILE_CREATE_NONE, nullptr, nullptr, &err)) { + g_critical("%s", err->message); + g_error_free(err); + } } /* we changed bookmarks list, let inform who interested in that */ Q_EMIT changed(); } -void Bookmarks::load() { - auto fpath = file_.localPath(); +void Bookmarks::load(const FilePath& path) { + auto fpath = path.localPath(); FILE* f; char buf[1024]; /* load the file */ @@ -205,7 +211,7 @@ void Bookmarks::load() { void Bookmarks::onFileChanged(GFileMonitor* /*mon*/, GFile* /*gf*/, GFile* /*other*/, GFileMonitorEvent /*evt*/) { // reload the bookmarks items_.clear(); - load(); + load(file_); Q_EMIT changed(); } diff --git a/src/core/bookmarks.h b/src/core/bookmarks.h index 3459f198..0034795b 100644 --- a/src/core/bookmarks.h +++ b/src/core/bookmarks.h @@ -68,7 +68,7 @@ private Q_SLOTS: void save(); private: - void load(); + void load(const FilePath& path); void queueSave(); static void _onFileChanged(GFileMonitor* mon, GFile* gf, GFile* other, GFileMonitorEvent evt, Bookmarks* _this) {