]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/tray-monitor/filesmodel.h
Add restore wizard to the tray monitor.
[bacula/bacula] / bacula / src / qt-console / tray-monitor / filesmodel.h
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2017 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    This notice must be preserved when any source code is
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 #ifndef FILESMODEL_H
20 #define FILESMODEL_H
21 #include <QStandardItemModel>
22 #include <QFileIconProvider>
23 #include <QMimeData>
24 #include "task.h"
25 enum {
26     PathIdRole = Qt::UserRole+1,
27     FilenameIdRole = Qt::UserRole+2,
28     FileIdRole = Qt::UserRole+3,
29     JobIdRole = Qt::UserRole+4,
30     LStatRole = Qt::UserRole+5,
31     PathRole = Qt::UserRole+6,
32     TypeRole = Qt::UserRole+7
33 };
34
35 enum {
36     TYPEROLE_DIRECTORY = 0,
37     TYPEROLE_FILE
38 };
39
40 class DirectoryItem : public QStandardItem
41 {
42 public:
43     DirectoryItem() : QStandardItem()
44     {
45         /* explicit set the data, so it can be serialized in Mime Data for D&D */
46         setData(QVariant(TYPEROLE_DIRECTORY), TypeRole);
47     }
48     QVariant data(int role = Qt::UserRole + 1) const
49     {
50         if (role == Qt::DecorationRole) {
51             QFileIconProvider provider;
52             return provider.icon(QFileIconProvider::Folder);
53         }
54
55         return QStandardItem::data(role);
56     }
57 };
58
59 class FileItem : public QStandardItem
60 {
61 public:
62     FileItem() : QStandardItem()
63     {
64         /* explicit set the data, so it can be serialized in Mime Data for D&D */
65         setData(QVariant(TYPEROLE_FILE), TypeRole);
66     }
67     enum {
68         FILE_TYPE = UserType +2
69     };
70
71     QVariant data(int role = Qt::UserRole + 1) const
72     {
73         if (role == Qt::DecorationRole) {
74             QFileIconProvider provider;
75             return provider.icon(QFileIconProvider::File);
76         }
77
78         return QStandardItem::data(role);
79     }
80 };
81
82 #define BATCH_SIZE 100
83 class FileSourceModel : public QStandardItemModel
84 {
85 public:
86     FileSourceModel() : QStandardItemModel(),
87         m_cursor(0),
88         m_batchSize(BATCH_SIZE),
89         m_canFetchMore(true)
90     {}
91
92     bool canFetchMore(const QModelIndex &parent) const
93     {
94         Q_UNUSED(parent)
95         return false/*m_canFetchMore*/;
96     }
97     void fetchMore(const QModelIndex &parent)
98     {
99         Q_UNUSED(parent)
100     }
101
102 public slots:
103     void taskComplete(task *t)
104     {
105         // increase cursor
106         // update canfetch
107         // deletelater
108         t->deleteLater();
109     }
110
111 private:
112     u_int64_t       m_cursor;
113     u_int64_t       m_batchSize;
114     bool            m_canFetchMore;
115 };
116
117 class FileDestModel : public QStandardItemModel
118 {
119     bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const
120     {
121         Q_UNUSED(action)
122         Q_UNUSED(row)
123         Q_UNUSED(column)
124         Q_UNUSED(parent)
125
126         if (data->hasFormat("application/x-qstandarditemmodeldatalist")) {
127             QByteArray encoded = data->data("application/x-qabstractitemmodeldatalist");
128             QDataStream stream(&encoded, QIODevice::ReadOnly);
129
130             while (!stream.atEnd())
131             {
132                 int row, col;
133                 QMap<int,  QVariant> roleDataMap;
134                 stream >> row >> col >> roleDataMap;
135
136                 /* do something with the data */
137                 int type = roleDataMap[TypeRole].toInt();
138
139                 switch(type) {
140                 case TYPEROLE_DIRECTORY:
141                 case TYPEROLE_FILE:
142                     break;
143                 default:
144                     return false;
145                 }
146             }
147             return true;
148         }
149         return false;
150     }
151
152     bool dropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent)
153     {
154         Q_UNUSED(action)
155         Q_UNUSED(row)
156         Q_UNUSED(column)
157         Q_UNUSED(parent)
158
159         QByteArray encoded = data->data("application/x-qabstractitemmodeldatalist");
160         QDataStream stream(&encoded, QIODevice::ReadOnly);
161
162         while (!stream.atEnd())
163         {
164             int row, col;
165             QMap<int,  QVariant> roleDataMap;
166             stream >> row >> col >> roleDataMap;
167
168             QStandardItem *item;
169             /* do something with the data */
170             int type = roleDataMap[TypeRole].toInt();
171
172             switch(type) {
173             case TYPEROLE_DIRECTORY:
174                 item = new DirectoryItem();
175                 break;
176             case TYPEROLE_FILE:
177                 item = new FileItem();
178                 break;
179             default:
180                 return false;
181             }
182
183             item->setData(roleDataMap[PathIdRole], PathIdRole);
184             item->setData(roleDataMap[FilenameIdRole], FilenameIdRole);
185             item->setData(roleDataMap[FileIdRole], FileIdRole);
186             item->setData(roleDataMap[JobIdRole], JobIdRole);
187             item->setData(roleDataMap[LStatRole], LStatRole);
188             item->setData(roleDataMap[PathRole], PathRole);
189             item->setData(roleDataMap[PathRole], Qt::DisplayRole);
190
191             appendRow(item);
192         }
193         return true;
194     }
195 };
196
197 #endif // FILESMODEL_H