1 /***************************************************************************
2  *                                                                         *
3  *   This program is free software; you can redistribute it and/or modify  *
4  *   it under the terms of the GNU General Public License as published by  *
5  *   the Free Software Foundation; either version 3 of the License, or     *
6  *   (at your option) any later version.                                   *
7  *                                                                         *
8  ***************************************************************************/
9 
10 #pragma once
11 
12 #include <QAbstractItemModel>
13 #include <QSortFilterProxyModel>
14 #include <QString>
15 #include <QPixmap>
16 #include <QList>
17 #include <QHash>
18 #include <QStringList>
19 #include <QRegExp>
20 
21 #include "dcpp/stdinc.h"
22 #include "dcpp/SearchResult.h"
23 #include "dcpp/SearchManager.h"
24 
25 class SearchProxyModel: public QSortFilterProxyModel {
26 Q_OBJECT
27 public:
QSortFilterProxyModel(parent)28     SearchProxyModel(QObject *parent = NULL): QSortFilterProxyModel(parent){}
~SearchProxyModel()29     virtual ~SearchProxyModel(){}
30 
31     virtual void sort(int column, Qt::SortOrder order);
32 };
33 
34 static const unsigned COLUMN_SF_COUNT          = 0;
35 static const unsigned COLUMN_SF_FILENAME       = 1;
36 static const unsigned COLUMN_SF_EXTENSION      = 2;
37 static const unsigned COLUMN_SF_SIZE           = 3;
38 static const unsigned COLUMN_SF_ESIZE          = 4;
39 static const unsigned COLUMN_SF_TTH            = 5;
40 static const unsigned COLUMN_SF_PATH           = 6;
41 static const unsigned COLUMN_SF_NICK           = 7;
42 static const unsigned COLUMN_SF_FREESLOTS      = 8;
43 static const unsigned COLUMN_SF_ALLSLOTS       = 9;
44 static const unsigned COLUMN_SF_IP             = 10;
45 static const unsigned COLUMN_SF_HUB            = 11;
46 static const unsigned COLUMN_SF_HOST           = 12;
47 
48 class SearchListException{
49     public:
50 
51     enum Type{
52         Sort=0,
53         Add,
54         Unkn
55     };
56 
57         SearchListException();
58         SearchListException(const SearchListException&);
59         SearchListException(const QString& message, Type type);
60         virtual ~SearchListException();
61 
62         SearchListException &operator=(const SearchListException&);
63 
64         QString message;
65         Type type;
66 };
67 
68 class SearchItem
69 {
70 
71 public:
72     SearchItem(const QList<QVariant> &data, SearchItem *parent = 0);
73     virtual ~SearchItem();
74 
75     void appendChild(SearchItem *child);
76 
77     SearchItem *child(int row);
78     int childCount() const;
79     int columnCount() const;
80     QVariant data(int column) const;
81     int row() const;
82     SearchItem *parent() const;
83     bool exists(const QString &user_cid) const;
84 
85     unsigned count;
86 
87     QString cid;
88 
89     bool isDir;
90 
91     QList<SearchItem*> childItems;
92 private:
93 
94     QList<QVariant> itemData;
95     SearchItem *parentItem;
96 };
97 
98 class SearchModel : public QAbstractItemModel
99 {
100     Q_OBJECT
101     typedef QMap<QString, QVariant> VarMap;
102 public:
103 
104     SearchModel(QObject *parent = 0);
105     ~SearchModel();
106 
107     /** */
108     QVariant data(const QModelIndex &, int) const;
109     /** */
110     Qt::ItemFlags flags(const QModelIndex &) const;
111     /** */
112     QVariant headerData(int section, Qt::Orientation, int role = Qt::DisplayRole) const;
113     /** */
114     QModelIndex index(int, int, const QModelIndex &parent = QModelIndex()) const;
115     /** */
116     QModelIndex parent(const QModelIndex &index) const;
117     /** */
118     int rowCount(const QModelIndex &parent = QModelIndex()) const;
119     /** */
120     int columnCount(const QModelIndex &parent = QModelIndex()) const;
121     /** */
122     bool hasChildren(const QModelIndex &parent) const;
123     /** sort list */
124     virtual void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
125 
126     /** */
127     QModelIndex createIndexForItem(SearchItem*);
128 
129     void setFilterRole(int);
130 
131     /** */
132     bool addResult(
133             const QString &file,
134             qulonglong size,
135             const QString &tth,
136             const QString &path,
137             const QString &nick,
138             const int free_slots,
139             const int all_slots,
140             const QString &ip,
141             const QString &hub,
142             const QString &host,
143             const QString &cid,
144             const bool isDir);
145 
146     /** */
147     int getSortColumn() const;
148     /** */
149     void setSortColumn(int);
150     /** */
151     Qt::SortOrder getSortOrder() const;
152     /** */
153     void setSortOrder(Qt::SortOrder);
154 
155     /** Clear model and redraw view*/
156     void clearModel();
157     /** */
158     void removeItem(const SearchItem*);
159 
160     /** */
161     void repaint();
162 
163 public Q_SLOTS:
164     /** */
165     bool addResultPtr(const VarMap&);
166 
167 private:
168     /** */
169     bool okToFind(const SearchItem*);
170     /** */
171     int filterRole;
172     /** */
173     int sortColumn;
174     /** */
175     Qt::SortOrder sortOrder;
176     /** */
177     SearchItem *rootItem;
178     /** */
179     QHash<QString, SearchItem*> tths;
180 
181     void reset();
182 };
183