File indexing completed on 2024-04-14 14:18:01

0001 //  -*- c-basic-offset:4; indent-tabs-mode:nil -*-
0002 /*
0003     This file is part of the KDE libraries
0004     SPDX-FileCopyrightText: 2000, 2006 David Faure <faure@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-only
0007 */
0008 #ifndef __kbookmarkmanager_h
0009 #define __kbookmarkmanager_h
0010 
0011 #include <QDomDocument>
0012 #include <QObject>
0013 #include <QString>
0014 
0015 #include <memory>
0016 
0017 class KBookmarkManagerPrivate;
0018 
0019 #include "kbookmark.h"
0020 #if KBOOKMARKS_ENABLE_DEPRECATED_SINCE(5, 79)
0021 #include "kbookmarkowner.h" // for SC reasons
0022 #endif
0023 
0024 class KBookmarkGroup;
0025 class QDBusMessage;
0026 
0027 /**
0028  * @class KBookmarkManager kbookmarkmanager.h KBookmarkManager
0029  *
0030  * This class implements the reading/writing of bookmarks in XML.
0031  * The bookmarks file is read and written using the XBEL standard
0032  * (http://pyxml.sourceforge.net/topics/xbel/)
0033  *
0034  * A sample file looks like this :
0035  * \code
0036  * <xbel>
0037  *   <bookmark href="http://techbase.kde.org"><title>Developer Web Site</title></bookmark>
0038  *   <folder folded="no">
0039  *     <title>Title of this folder</title>
0040  *     <bookmark icon="kde" href="http://www.kde.org"><title>KDE Web Site</title></bookmark>
0041  *     <folder toolbar="yes">
0042  *       <title>My own bookmarks</title>
0043  *       <bookmark href="http://www.koffice.org"><title>KOffice Web Site</title></bookmark>
0044  *       <separator/>
0045  *       <bookmark href="http://www.kdevelop.org"><title>KDevelop Web Site</title></bookmark>
0046  *     </folder>
0047  *   </folder>
0048  * </xbel>
0049  * \endcode
0050  */
0051 class KBOOKMARKS_EXPORT KBookmarkManager : public QObject
0052 {
0053     Q_OBJECT
0054 private:
0055     /**
0056      * Creates a bookmark manager with a path to the bookmarks.  By
0057      * default, it will use the KDE standard dirs to find and create the
0058      * correct location.  If you are using your own app-specific
0059      * bookmarks directory, you must instantiate this class with your
0060      * own path <em>before</em> KBookmarkManager::managerForFile() is ever
0061      * called.
0062      *
0063      * @param bookmarksFile full path to the bookmarks file,
0064      * Use ~/.kde/share/apps/konqueror/bookmarks.xml for the konqueror bookmarks
0065      *
0066      * @param dbusObjectName a unique name that represents this bookmark collection,
0067      * usually your component (e.g. application) name. This is "konqueror" for the
0068      * konqueror bookmarks, "kfile" for KFileDialog bookmarks, etc.
0069      * The final D-Bus object path is /KBookmarkManager/dbusObjectName
0070      * An empty @p dbusObjectName disables the registration to D-Bus (used for temporary managers)
0071      */
0072     KBOOKMARKS_NO_EXPORT KBookmarkManager(const QString &bookmarksFile, const QString &dbusObjectName);
0073 
0074     /**
0075      * Creates a bookmark manager for an external file
0076      * (Using QFileSystemWatcher for change monitoring)
0077      * @since 4.1
0078      */
0079     KBOOKMARKS_NO_EXPORT explicit KBookmarkManager(const QString &bookmarksFile);
0080 
0081     /**
0082      * Creates a temp bookmark manager
0083      */
0084     KBOOKMARKS_NO_EXPORT KBookmarkManager();
0085 
0086 public:
0087     /**
0088      * Destructor
0089      */
0090     ~KBookmarkManager() override;
0091 
0092     /**
0093      * Check whether auto error handling is enabled.
0094      * If enabled, it will show an error dialog to the user when an
0095      * error occurs. It is turned on by default.
0096      * @return true if auto error handling is enabled, false otherwise
0097      * @note dialogs will only be displayed if the current thread is the gui thread
0098      * @since 4.6
0099      * @see setAutoErrorHandlingEnabled()
0100      */
0101     bool autoErrorHandlingEnabled() const;
0102 
0103     /**
0104      * Enable or disable auto error handling is enabled.
0105      * If enabled, it will show an error dialog to the user when an
0106      * error occurs. It is turned on by default.
0107      * If disabled, the application should react on the error() signal.
0108      * @param enable true to enable auto error handling, false to disable
0109      * @param parent the parent widget for the error dialogs, can be @c nullptr for
0110      *               top-level
0111      * @since 4.6
0112      * @see autoErrorHandlingEnabled()
0113      */
0114     void setAutoErrorHandlingEnabled(bool enable, QWidget *parent);
0115 
0116     /**
0117      * Set the update flag. Defaults to true.
0118      * @param update if true then KBookmarkManager will listen to D-Bus update requests.
0119      */
0120     void setUpdate(bool update);
0121 
0122     /**
0123      * Save the bookmarks to the given XML file on disk.
0124      * @param filename full path to the desired bookmarks file location
0125      * @param toolbarCache iff true save a cache of the toolbar folder, too
0126      * @return true if saving was successful
0127      */
0128     // KF6 TODO: Use an enum and not a bool
0129     bool saveAs(const QString &filename, bool toolbarCache = true) const;
0130 
0131     /**
0132      * Update access time stamps for a given url.
0133      * @param url the viewed url
0134      * @return true if any metadata was modified (bookmarks file is not saved automatically)
0135      */
0136     bool updateAccessMetadata(const QString &url);
0137 
0138     /*
0139      * NB. currently *unimplemented*
0140      *
0141      * Update favicon url for a given url.
0142      * @param url the viewed url
0143      * @param faviconurl the favicion url
0144      */
0145     void updateFavicon(const QString &url, const QString &faviconurl);
0146 
0147     /**
0148      * This will return the path that this manager is using to read
0149      * the bookmarks.
0150      * @internal
0151      * @return the path containing the bookmarks
0152      */
0153     QString path() const;
0154 
0155     /**
0156      * This will return the root bookmark.  It is used to iterate
0157      * through the bookmarks manually.  It is mostly used internally.
0158      *
0159      * @return the root (top-level) bookmark
0160      */
0161     KBookmarkGroup root() const;
0162 
0163     /**
0164      * This returns the root of the toolbar menu.
0165      * In the XML, this is the group with the attribute toolbar=yes
0166      *
0167      * @return the toolbar group
0168      */
0169     KBookmarkGroup toolbar();
0170 
0171     /**
0172      * @return the bookmark designated by @p address
0173      * @param address the address belonging to the bookmark you're looking for
0174      * @param tolerate when true tries to find the most tolerable bookmark position
0175      * @see KBookmark::address
0176      */
0177     KBookmark findByAddress(const QString &address);
0178 
0179     /**
0180      * Saves the bookmark file and notifies everyone.
0181      *
0182      **/
0183     void emitChanged();
0184 
0185     /**
0186      * Saves the bookmark file and notifies everyone.
0187      * @param group the parent of all changed bookmarks
0188      */
0189     void emitChanged(const KBookmarkGroup &group);
0190 
0191     /**
0192      * Save the bookmarks to an XML file on disk.
0193      * You should use emitChanged() instead of this function, it saves
0194      * and notifies everyone that the file has changed.
0195      * Only use this if you don't want the emitChanged signal.
0196      * @param toolbarCache iff true save a cache of the toolbar folder, too
0197      * @return true if saving was successful
0198      */
0199     // KF6 TODO: Use an enum and not a bool
0200     bool save(bool toolbarCache = true) const;
0201 
0202     void emitConfigChanged();
0203 
0204     /**
0205      * Set options with which slotEditBookmarks called keditbookmarks
0206      * this can be used to change the appearance of the keditbookmarks
0207      * in order to provide a slightly differing outer shell depending
0208      * on the bookmarks file / app which calls it.
0209      * @param caption the --caption string, for instance "Konsole"
0210      * @param browser iff false display no browser specific
0211      *            menu items in keditbookmarks :: --nobrowser
0212      */
0213     // KF6 TODO: Use an enum and not a bool
0214     void setEditorOptions(const QString &caption, bool browser);
0215 
0216     /**
0217      * This static function will return an instance of the
0218      * KBookmarkManager, responsible for the given @p bookmarksFile.
0219      * If you do not instantiate this class either
0220      * natively or in a derived class, then it will return an object
0221      * with the default behaviors.  If you wish to use different
0222      * behaviors, you <em>must</em> derive your own class and
0223      * instantiate it before this method is ever called.
0224      *
0225      * @param bookmarksFile full path to the bookmarks file,
0226      * Use ~/.kde/share/apps/konqueror/bookmarks.xml for the konqueror bookmarks
0227      *
0228      * @param dbusObjectName a unique name that represents this bookmark collection,
0229      * usually your component (e.g. application) name. This is "konqueror" for the
0230      * konqueror bookmarks, "kfile" for KFileDialog bookmarks, etc.
0231      * The final D-Bus object path is /KBookmarkManager/dbusObjectName
0232      * An empty @p dbusObjectName disables the registration to D-Bus (used for temporary managers)
0233      *
0234      */
0235     static KBookmarkManager *managerForFile(const QString &bookmarksFile, const QString &dbusObjectName);
0236 
0237     /**
0238      * Returns a KBookmarkManager, which will use QFileSystemWatcher for change detection
0239      * This is important when sharing bookmarks with other Desktops.
0240      * @param bookmarksFile full path to the bookmarks file
0241      * @since 4.1
0242      */
0243     static KBookmarkManager *managerForExternalFile(const QString &bookmarksFile);
0244 
0245     /**
0246      * only used for KBookmarkBar
0247      */
0248     static KBookmarkManager *createTempManager();
0249 
0250     /**
0251      * Returns a pointer to the user's main (konqueror) bookmark collection.
0252      */
0253     static KBookmarkManager *userBookmarksManager();
0254 
0255     /**
0256      * @internal
0257      */
0258     QDomDocument internalDocument() const;
0259 
0260 public Q_SLOTS:
0261     void slotEditBookmarks();
0262     void slotEditBookmarksAtAddress(const QString &address);
0263 
0264     /**
0265      * Reparse the whole bookmarks file and notify about the change
0266      * Doesn't send signal over D-Bus to the other Bookmark Managers
0267      * You probably want to use emitChanged()
0268      *
0269      */
0270     void notifyCompleteChange(const QString &caller);
0271 
0272 #ifndef KBOOKMARKS_NO_DBUS
0273     /**
0274      * Emit the changed signal for the group whose address is given
0275      * @see KBookmark::address()
0276      * Called by the process that saved the file after
0277      * a small change (new bookmark or new folder).
0278      * Does not send signal over D-Bus to the other Bookmark Managers
0279      * You probably want to call emitChanged()
0280      */
0281     void notifyChanged(const QString &groupAddress, const QDBusMessage &msg);
0282 #endif
0283 
0284     void notifyConfigChanged();
0285 
0286 Q_SIGNALS:
0287     /**
0288      * Signal send over D-Bus
0289      */
0290     void bookmarkCompleteChange(QString caller);
0291 
0292     /**
0293      * Signal send over D-Bus
0294      */
0295     void bookmarksChanged(QString groupAddress);
0296 
0297     /**
0298      * Signal send over D-Bus
0299      */
0300     void bookmarkConfigChanged();
0301 
0302     /**
0303      * Signals that the group (or any of its children) with the address
0304      * @p groupAddress (e.g. "/4/5")
0305      * has been modified by the caller @p caller.
0306      * connect to this
0307      */
0308     void changed(const QString &groupAddress, const QString &caller);
0309 
0310     /**
0311      * Signals that the config changed
0312      */
0313     void configChanged();
0314 
0315     /**
0316      * Emitted when an error occurs.
0317      * Contains the translated error message.
0318      * @since 4.6
0319      */
0320     void error(const QString &errorMessage);
0321 
0322 private Q_SLOTS:
0323     KBOOKMARKS_NO_EXPORT void slotFileChanged(const QString &path); // external bookmarks
0324 
0325 private:
0326     // consts added to avoid a copy-and-paste of internalDocument
0327     KBOOKMARKS_NO_EXPORT void parse() const;
0328     KBOOKMARKS_NO_EXPORT void init(const QString &dbusPath);
0329 
0330     KBOOKMARKS_NO_EXPORT void startKEditBookmarks(const QStringList &args);
0331 
0332 private:
0333     std::unique_ptr<KBookmarkManagerPrivate> const d;
0334 
0335     friend class KBookmarkGroup;
0336 };
0337 
0338 #endif