1 /*****************************************************************************
2  * Copyright (C) 2002 Shie Erlich <erlich@users.sourceforge.net>             *
3  * Copyright (C) 2002 Rafi Yanai <yanai@users.sourceforge.net>               *
4  * Copyright (C) 2004-2019 Krusader Krew [https://krusader.org]              *
5  *                                                                           *
6  * This file is part of Krusader [https://krusader.org].                     *
7  *                                                                           *
8  * Krusader is free software: you can redistribute it and/or modify          *
9  * it under the terms of the GNU General Public License as published by      *
10  * the Free Software Foundation, either version 2 of the License, or         *
11  * (at your option) any later version.                                       *
12  *                                                                           *
13  * Krusader is distributed in the hope that it will be useful,               *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
16  * GNU General Public License for more details.                              *
17  *                                                                           *
18  * You should have received a copy of the GNU General Public License         *
19  * along with Krusader.  If not, see [http://www.gnu.org/licenses/].         *
20  *****************************************************************************/
21 
22 #ifndef POPULARURLS_H
23 #define POPULARURLS_H
24 
25 // QtCore
26 #include <QObject>
27 #include <QHash>
28 #include <QUrl>
29 // QtWidgets
30 #include <QDialog>
31 
32 // the class holds a list of most popular links in a dual data structure
33 // * linked list, with head and tail: for fast append/prepend support
34 // * dictionary that maps urls to list nodes: to save the need to iterate
35 //   over the list in order to find the correct node for each new url
36 //
37 // also, the class holds a maximum number of urls. two variables affect this:
38 // * maxUrls - the num. of urls the user can see
39 // * hardLimit - the actual number of urls kept.
40 // when the number of urls reaches hardLimit, a garbage collection is done and
41 // the bottom (hardLimit-maxUrls) entries are removed from the list
42 typedef struct _UrlNode* UrlNodeP;
43 typedef struct _UrlNode {
44     UrlNodeP prev;
45     QUrl url;
46     int rank;
47     UrlNodeP next;
48 } UrlNode;
49 
50 class PopularUrlsDlg;
51 
52 class PopularUrls : public QObject
53 {
54     Q_OBJECT
55 public:
56     explicit PopularUrls(QObject *parent = 0);
57     ~PopularUrls();
58     void save();
59     void load();
60     void addUrl(const QUrl& url);
61     QList<QUrl> getMostPopularUrls(int max);
62 
63 public slots:
64     void showDialog();
65 
66 protected:
67     // NOTE: the following methods append/insert/remove a node to the list
68     // but NEVER free memory or allocate memory!
69     void appendNode(UrlNodeP node);
70     void insertNode(UrlNodeP node, UrlNodeP after);
71     void removeNode(UrlNodeP node);
72     void relocateIfNeeded(UrlNodeP node);
73     void clearList();
74     void dumpList();
75     void decreaseRanks();
76 
77 private:
78     UrlNodeP head, tail;
79     QHash<QString, UrlNode *> ranks; // actually holds UrlNode*
80     int count;
81     static const int maxUrls = 30;
82     PopularUrlsDlg *dlg;
83 };
84 
85 class KrTreeWidget;
86 class KTreeWidgetSearchLine;
87 class QModelIndex;
88 
89 class PopularUrlsDlg: public QDialog
90 {
91     Q_OBJECT
92 public:
93     PopularUrlsDlg();
94     ~PopularUrlsDlg();
95     void run(QList<QUrl> list); // use this to open the dialog
result()96     inline int result() const {
97         return selection;
98     } // returns index 0 - topmost, or -1
99 
100 
101 protected slots:
102     void slotVisibilityChanged();
103     void slotItemSelected(const QModelIndex &);
104 
105 private:
106     KrTreeWidget *urls;
107     KTreeWidgetSearchLine *search;
108     int selection;
109 };
110 
111 #endif
112