File indexing completed on 2024-04-21 04:58:15

0001 /* This file is part of the KDE project
0002     SPDX-FileCopyrightText: 2000, 2001 Carsten Pfeiffer <pfeiffer@kde.org>
0003     SPDX-FileCopyrightText: 2007 David Faure <faure@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef KONQ_HISTORYMANAGER_H
0009 #define KONQ_HISTORYMANAGER_H
0010 
0011 #include <QObject>
0012 #include <QMap>
0013 #include <QStringList>
0014 
0015 #include <konqprivate_export.h>
0016 
0017 #include "konq_historyentry.h"
0018 #include "konq_historyprovider.h"
0019 
0020 class QTimer;
0021 class KBookmarkManager;
0022 class KCompletion;
0023 
0024 /**
0025  * This class maintains and manages a history of all URLs visited by one
0026  * Konqueror instance. Additionally it synchronizes the history with other
0027  * Konqueror instances via DBUS to keep one global and persistent history.
0028  *
0029  * It keeps the history in sync with one KCompletion object
0030  */
0031 class KONQUERORPRIVATE_EXPORT KonqHistoryManager : public KonqHistoryProvider
0032 {
0033     Q_OBJECT
0034 
0035 public:
0036     /**
0037      * Returns the KonqHistoryManager instance. This relies on a KonqHistoryManager instance
0038      * being created first!
0039      * This is a bit like "qApp": you can access the instance anywhere, but you need to
0040      * create it first.
0041      */
0042     static KonqHistoryManager *kself()
0043     {
0044         return static_cast<KonqHistoryManager *>(HistoryProvider::self());
0045     }
0046 
0047     explicit KonqHistoryManager(KBookmarkManager *bookmarkManager, QObject *parent = nullptr);
0048     ~KonqHistoryManager() override;
0049 
0050     /**
0051      * Adds a pending entry to the history. Pending means, that the entry is
0052      * not verified yet, i.e. it is not sure @p url does exist at all. You
0053      * probably don't know the title of the url in that case either.
0054      * Call @ref confirmPending() as soon you know the entry is good and should
0055      * be updated.
0056      *
0057      * If an entry with @p url already exists,
0058      * it will be updated (lastVisited date will become the current time
0059      * and the number of visits will be incremented).
0060      *
0061      * @param url The url of the history entry
0062      * @param typedUrl the string that the user typed, which resulted in url
0063      *                 Doesn't have to be a valid url, e.g. "slashdot.org".
0064      * @param title The title of the URL. If you don't know it (yet), you may
0065                     specify it in @ref confirmPending().
0066      */
0067     void addPending(const QUrl &url, const QString &typedUrl = QString(),
0068                     const QString &title = QString());
0069 
0070     /**
0071      * Confirms and updates the entry for @p url.
0072      */
0073     void confirmPending(const QUrl &url,
0074                         const QString &typedUrl = QString(),
0075                         const QString &title = QString());
0076 
0077     /**
0078      * Removes a pending url from the history, e.g. when the url does not
0079      * exist, or the user aborted loading.
0080      */
0081     void removePending(const QUrl &url);
0082 
0083     /**
0084      * @returns the KCompletion object.
0085      */
0086     KCompletion *completionObject() const
0087     {
0088         return m_pCompletion;
0089     }
0090 
0091     // HistoryProvider interface, let konq handle this
0092     /**
0093      * Reimplemented in such a way that all URLs that would be filtered
0094      * out normally (see @ref filterOut()) will still be added to the history.
0095      * By default, file:/ urls will be filtered out, but if they come thru
0096      * the HistoryProvider interface, they are added to the history.
0097      */
0098     void insert(const QString &) override;
0099     void remove(const QString &) override {}
0100     void clear() override {}
0101 
0102 private:
0103     /**
0104      * Loads the history and fills the completion object.
0105      */
0106     bool loadHistory();
0107 
0108     /**
0109      * Does the work for @ref addPending() and @ref confirmPending().
0110      *
0111      * Adds an entry to the history. If an entry with @p url already exists,
0112      * it will be updated (lastVisited date will become the current time
0113      * and the number of visits will be incremented).
0114      * @p pending means, the entry has not been "verified", it's been added
0115      * right after typing the url.
0116      * If @p pending is false, @p url will be removed from the pending urls
0117      * (if available) and NOT be added again in that case.
0118      */
0119     void addToHistory(bool pending, const QUrl &url,
0120                       const QString &typedUrl = QString(),
0121                       const QString &title = QString());
0122 
0123     /**
0124      * @returns true if the given @p url should be filtered out and not be
0125      * added to the history. By default, all local urls (url.isLocalFile())
0126      * will return true, as well as urls with an empty host.
0127      */
0128     virtual bool filterOut(const QUrl &url);
0129 
0130     void addToUpdateList(const QString &url);
0131 
0132     /**
0133      * The list of urls that is going to be emitted in slotEmitUpdated. Add
0134      * urls to it whenever you modify the list of history entries and start
0135      * m_updateTimer.
0136      */
0137     QStringList m_updateURLs;
0138 
0139 private Q_SLOTS:
0140     /**
0141      * Called by the updateTimer to emit the KParts::HistoryProvider::updated()
0142      * signal so that khtml can repaint the updated links.
0143      */
0144     void slotEmitUpdated();
0145 
0146     void slotCleared();
0147     void slotEntryRemoved(const KonqHistoryEntry &entry);
0148 
0149 private:
0150     void finishAddingEntry(const KonqHistoryEntry &entry, bool isSender) override;
0151     void clearPending();
0152 
0153     void addToCompletion(const QString &url, const QString &typedUrl, int numberOfTimesVisited = 1);
0154     void removeFromCompletion(const QString &url, const QString &typedUrl);
0155 
0156     /**
0157      * List of pending entries, which were added to the history, but not yet
0158      * confirmed (i.e. not yet added with pending = false).
0159      * Note: when removing an entry, you have to delete the KonqHistoryEntry
0160      * of the item you remove.
0161      */
0162     QMap<QString, KonqHistoryEntry *> m_pending;
0163 
0164     KCompletion *m_pCompletion; // the completion object we sync with
0165 
0166     /**
0167      * A timer that will emit the KParts::HistoryProvider::updated() signal
0168      * thru the slotEmitUpdated slot.
0169      */
0170     QTimer *m_updateTimer;
0171 
0172     KBookmarkManager *m_bookmarkManager;
0173 
0174     static const int s_historyVersion;
0175 };
0176 
0177 #endif // KONQ_HISTORY_H