1 /*
2     SPDX-FileCopyrightText: 2007 Joris Guisson <joris.guisson@gmail.com>
3     SPDX-FileCopyrightText: 2007 Ivan Vasic <ivasic@gmail.com>
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #ifndef KTQUEUEMANAGERMODEL_H
8 #define KTQUEUEMANAGERMODEL_H
9 
10 #include <QAbstractTableModel>
11 #include <QList>
12 
13 #include <torrent/queuemanager.h>
14 
15 class QMimeData;
16 
17 namespace bt
18 {
19 class TorrentInterface;
20 }
21 
22 namespace kt
23 {
24 class QueueManager;
25 
26 /**
27  * @author Joris Guisson
28  *
29  * Model for the QM
30  */
31 class QueueManagerModel : public QAbstractTableModel
32 {
33     Q_OBJECT
34 public:
35     QueueManagerModel(QueueManager *qman, QObject *parent);
36     ~QueueManagerModel() override;
37 
38     void setShowUploads(bool on);
39     void setShowDownloads(bool on);
40     void setShowNotQueued(bool on);
41 
42     int rowCount(const QModelIndex &parent) const override;
43     int columnCount(const QModelIndex &parent) const override;
44     QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
45     QVariant data(const QModelIndex &index, int role) const override;
46     bool removeRows(int row, int count, const QModelIndex &parent) override;
47     bool insertRows(int row, int count, const QModelIndex &parent) override;
48     Qt::ItemFlags flags(const QModelIndex &index) const override;
49     Qt::DropActions supportedDropActions() const override;
50     QStringList mimeTypes() const override;
51     QMimeData *mimeData(const QModelIndexList &indexes) const override;
52     bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;
53 
54     /**
55      * Move items one row up
56      * @param row The row of the item
57      * @param count The number of items to move
58      */
59     void moveUp(int row, int count);
60 
61     /**
62      * Move items one row down
63      * @param row The row of the item
64      * @param count The number of items to move
65      */
66     void moveDown(int row, int count);
67 
68     /**
69      * Move items to the top
70      * @param row The row of the item
71      * @param count The number of items to move
72      */
73     void moveTop(int row, int count);
74 
75     /**
76      * Move items to the bottom
77      * @param row The row of the item
78      * @param count The number of items to move
79      */
80     void moveBottom(int row, int count);
81 
82     /**
83      * Update the model
84      */
85     void update();
86 
87     /**
88         Given a search text find a matching torrent
89     */
90     QModelIndex find(const QString &text);
91 
92 public Q_SLOTS:
93     void onTorrentAdded(bt::TorrentInterface *tc);
94     void onTorrentRemoved(bt::TorrentInterface *tc);
95     void onQueueOrdered();
96     void onTorrentStatusChanged(bt::TorrentInterface *tc);
97 
98 private:
99     struct Item {
100         bt::TorrentInterface *tc;
101         bt::Int64 stalled_time;
102 
103         bool operator<(const Item &item) const
104         {
105             return tc->getPriority() < item.tc->getPriority();
106         }
107     };
108 
109     bool visible(const bt::TorrentInterface *tc);
110     void updateQueue();
111     void swapItems(int a, int b);
112     void dumpQueue();
113     void updatePriorities();
114     void softReset();
115 
116 private:
117     QueueManager *qman;
118     QList<Item> queue;
119     mutable QList<int> dragged_items;
120     QString search_text;
121 
122     bool show_uploads;
123     bool show_downloads;
124     bool show_not_queud;
125 };
126 
127 }
128 
129 #endif
130