File indexing completed on 2024-04-28 17:05:55

0001 /*
0002     SPDX-FileCopyrightText: 2002 Shie Erlich <erlich@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2002 Rafi Yanai <yanai@users.sourceforge.net>
0004     SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #ifndef POPULARURLS_H
0010 #define POPULARURLS_H
0011 
0012 // QtCore
0013 #include <QHash>
0014 #include <QObject>
0015 #include <QUrl>
0016 // QtWidgets
0017 #include <QDialog>
0018 
0019 // the class holds a list of most popular links in a dual data structure
0020 // * linked list, with head and tail: for fast append/prepend support
0021 // * dictionary that maps urls to list nodes: to save the need to iterate
0022 //   over the list in order to find the correct node for each new url
0023 //
0024 // also, the class holds a maximum number of urls. two variables affect this:
0025 // * maxUrls - the num. of urls the user can see
0026 // * hardLimit - the actual number of urls kept.
0027 // when the number of urls reaches hardLimit, a garbage collection is done and
0028 // the bottom (hardLimit-maxUrls) entries are removed from the list
0029 typedef struct _UrlNode *UrlNodeP;
0030 typedef struct _UrlNode {
0031     UrlNodeP prev;
0032     QUrl url;
0033     int rank;
0034     UrlNodeP next;
0035 } UrlNode;
0036 
0037 class PopularUrlsDlg;
0038 
0039 class PopularUrls : public QObject
0040 {
0041     Q_OBJECT
0042 public:
0043     explicit PopularUrls(QObject *parent = nullptr);
0044     ~PopularUrls() override;
0045     void save();
0046     void load();
0047     void addUrl(const QUrl &url);
0048     QList<QUrl> getMostPopularUrls(int max);
0049 
0050 public slots:
0051     void showDialog();
0052 
0053 protected:
0054     // NOTE: the following methods append/insert/remove a node to the list
0055     // but NEVER free memory or allocate memory!
0056     void appendNode(UrlNodeP node);
0057     void insertNode(UrlNodeP node, UrlNodeP after);
0058     void removeNode(UrlNodeP node);
0059     void relocateIfNeeded(UrlNodeP node);
0060     void clearList();
0061     void dumpList();
0062     void decreaseRanks();
0063 
0064 private:
0065     UrlNodeP head, tail;
0066     QHash<QString, UrlNode *> ranks; // actually holds UrlNode*
0067     int count;
0068     static const int maxUrls = 30;
0069     PopularUrlsDlg *dlg;
0070 };
0071 
0072 class KrTreeWidget;
0073 class KTreeWidgetSearchLine;
0074 class QModelIndex;
0075 
0076 class PopularUrlsDlg : public QDialog
0077 {
0078     Q_OBJECT
0079 public:
0080     PopularUrlsDlg();
0081     ~PopularUrlsDlg() override;
0082     void run(QList<QUrl> list); // use this to open the dialog
0083     inline int result() const
0084     {
0085         return selection;
0086     } // returns index 0 - topmost, or -1
0087 
0088 protected slots:
0089     void slotVisibilityChanged();
0090     void slotItemSelected(const QModelIndex &);
0091 
0092 private:
0093     KrTreeWidget *urls;
0094     KTreeWidgetSearchLine *search;
0095     int selection;
0096 };
0097 
0098 #endif