1 #include "joblistwidget.h"
2 #include "debug.h"
3 #include "restoredialog.h"
4
5 #include <QKeyEvent>
6 #include <QMessageBox>
7
JobListWidget(QWidget * parent)8 JobListWidget::JobListWidget(QWidget *parent) : QListWidget(parent)
9 {
10 _filter.setCaseSensitivity(Qt::CaseInsensitive);
11 _filter.setPatternSyntax(QRegExp::Wildcard);
12 connect(this, &QListWidget::itemActivated, [&](QListWidgetItem *item) {
13 emit displayJobDetails(static_cast<JobListWidgetItem *>(item)->job());
14 });
15 }
16
~JobListWidget()17 JobListWidget::~JobListWidget()
18 {
19 clear();
20 }
21
backupSelectedItems()22 void JobListWidget::backupSelectedItems()
23 {
24 if(selectedItems().isEmpty())
25 return;
26
27 auto confirm =
28 QMessageBox::question(this, tr("Confirm action"),
29 tr("Initiate backup for the %1 selected job(s)?")
30 .arg(selectedItems().count()));
31 if(confirm != QMessageBox::Yes)
32 return;
33
34 foreach(QListWidgetItem *item, selectedItems())
35 {
36 if(item->isSelected())
37 {
38 JobPtr job = static_cast<JobListWidgetItem *>(item)->job();
39 emit backupJob(job);
40 }
41 }
42 }
43
selectJob(JobPtr job)44 void JobListWidget::selectJob(JobPtr job)
45 {
46 if(!job)
47 {
48 DEBUG << "Null JobPtr passed.";
49 return;
50 }
51
52 for(int i = 0; i < count(); ++i)
53 {
54 JobListWidgetItem *jobItem = static_cast<JobListWidgetItem *>(item(i));
55 if(jobItem && (jobItem->job()->objectKey() == job->objectKey()))
56 {
57 clearSelection();
58 setCurrentItem(jobItem);
59 scrollToItem(currentItem(), QAbstractItemView::EnsureVisible);
60 break;
61 }
62 }
63 }
64
inspectJobByRef(QString jobRef)65 void JobListWidget::inspectJobByRef(QString jobRef)
66 {
67 if(!jobRef.isEmpty())
68 {
69 for(int i = 0; i < count(); ++i)
70 {
71 JobListWidgetItem *jobItem =
72 static_cast<JobListWidgetItem *>(item(i));
73 if(jobItem && (jobItem->job()->objectKey() == jobRef))
74 emit displayJobDetails(jobItem->job());
75 }
76 }
77 }
78
backupAllJobs()79 void JobListWidget::backupAllJobs()
80 {
81 for(int i = 0; i < count(); ++i)
82 {
83 JobPtr job = static_cast<JobListWidgetItem *>(item(i))->job();
84 emit backupJob(job);
85 }
86 }
87
backupItem()88 void JobListWidget::backupItem()
89 {
90 if(sender())
91 {
92 JobPtr job = qobject_cast<JobListWidgetItem *>(sender())->job();
93 if(job)
94 emit backupJob(job);
95 }
96 }
97
inspectItem()98 void JobListWidget::inspectItem()
99 {
100 if(sender())
101 {
102 emit displayJobDetails(qobject_cast<JobListWidgetItem *>(sender())->job());
103 }
104 }
105
restoreItem()106 void JobListWidget::restoreItem()
107 {
108 if(sender())
109 {
110 JobPtr job = qobject_cast<JobListWidgetItem *>(sender())->job();
111 if(!job->archives().isEmpty())
112 {
113 ArchivePtr archive = job->archives().first();
114 RestoreDialog *restoreDialog = new RestoreDialog(this, archive);
115 restoreDialog->show();
116 connect(restoreDialog, &RestoreDialog::accepted, [=] {
117 emit restoreArchive(restoreDialog->archive(),
118 restoreDialog->getOptions());
119 });
120 }
121 }
122 }
123
deleteItem()124 void JobListWidget::deleteItem()
125 {
126 execDeleteJob(qobject_cast<JobListWidgetItem *>(sender()));
127 }
128
execDeleteJob(JobListWidgetItem * jobItem)129 void JobListWidget::execDeleteJob(JobListWidgetItem *jobItem)
130 {
131 if(!jobItem)
132 {
133 DEBUG << "Null JobListWidgetItem passed.";
134 return;
135 }
136
137 JobPtr job = jobItem->job();
138 auto confirm = QMessageBox::question(this, tr("Confirm action"),
139 tr("Are you sure you want to "
140 "delete job \"%1\" "
141 "(this cannot be undone)?")
142 .arg(job->name()));
143 if(confirm != QMessageBox::Yes)
144 return;
145
146 bool purgeArchives = false;
147 if(!job->archives().isEmpty())
148 {
149 auto delArchives =
150 QMessageBox::question(this, tr("Confirm action"),
151 tr("Also delete %1 archives "
152 "belonging to this job "
153 "(this cannot be undone)?")
154 .arg(job->archives().count()));
155 if(delArchives == QMessageBox::Yes)
156 purgeArchives = true;
157 }
158 emit deleteJob(job, purgeArchives);
159 delete jobItem;
160 emit countChanged(count(), visibleItemsCount());
161 }
162
visibleItemsCount()163 int JobListWidget::visibleItemsCount()
164 {
165 int count = 0;
166 foreach(QListWidgetItem *item, findItems("*", Qt::MatchWildcard))
167 {
168 if(item && !item->isHidden())
169 count++;
170 }
171 return count;
172 }
173
setJobs(QMap<QString,JobPtr> jobs)174 void JobListWidget::setJobs(QMap<QString, JobPtr> jobs)
175 {
176 setUpdatesEnabled(false);
177 clear();
178 foreach(JobPtr job, jobs)
179 {
180 addJob(job);
181 }
182 setUpdatesEnabled(true);
183 }
184
addJob(JobPtr job)185 void JobListWidget::addJob(JobPtr job)
186 {
187 if(!job)
188 {
189 DEBUG << "Null JobPtr passed.";
190 return;
191 }
192
193 JobListWidgetItem *item = new JobListWidgetItem(job);
194 connect(item, &JobListWidgetItem::requestBackup, this,
195 &JobListWidget::backupItem);
196 connect(item, &JobListWidgetItem::requestInspect, this,
197 &JobListWidget::inspectItem);
198 connect(item, &JobListWidgetItem::requestRestore, this,
199 &JobListWidget::restoreItem);
200 connect(item, &JobListWidgetItem::requestDelete, this,
201 &JobListWidget::deleteItem);
202 insertItem(count(), item);
203 setItemWidget(item, item->widget());
204 item->setHidden(!job->name().contains(_filter));
205 emit countChanged(count(), visibleItemsCount());
206 }
207
inspectSelectedItem()208 void JobListWidget::inspectSelectedItem()
209 {
210 if(!selectedItems().isEmpty())
211 emit displayJobDetails(
212 static_cast<JobListWidgetItem *>(selectedItems().first())->job());
213 }
214
restoreSelectedItem()215 void JobListWidget::restoreSelectedItem()
216 {
217 if(!selectedItems().isEmpty())
218 {
219 JobPtr job =
220 static_cast<JobListWidgetItem *>(selectedItems().first())->job();
221 if(!job->archives().isEmpty())
222 {
223 ArchivePtr archive = job->archives().first();
224 RestoreDialog *restoreDialog = new RestoreDialog(this, archive);
225 restoreDialog->show();
226 connect(restoreDialog, &RestoreDialog::accepted, [=] {
227 emit restoreArchive(restoreDialog->archive(),
228 restoreDialog->getOptions());
229 });
230 }
231 }
232 }
233
deleteSelectedItem()234 void JobListWidget::deleteSelectedItem()
235 {
236 if(!selectedItems().isEmpty())
237 execDeleteJob(static_cast<JobListWidgetItem *>(selectedItems().first()));
238 }
239
setFilter(QString regex)240 void JobListWidget::setFilter(QString regex)
241 {
242 setUpdatesEnabled(false);
243 clearSelection();
244 _filter.setPattern(regex);
245 for(int i = 0; i < count(); ++i)
246 {
247 JobListWidgetItem *jobItem = static_cast<JobListWidgetItem *>(item(i));
248 if(jobItem)
249 {
250 if(jobItem->job()->name().contains(_filter))
251 jobItem->setHidden(false);
252 else
253 jobItem->setHidden(true);
254 }
255 }
256 setUpdatesEnabled(true);
257 emit countChanged(count(), visibleItemsCount());
258 }
259
keyPressEvent(QKeyEvent * event)260 void JobListWidget::keyPressEvent(QKeyEvent *event)
261 {
262 switch(event->key())
263 {
264 case Qt::Key_Delete:
265 deleteSelectedItem();
266 break;
267 case Qt::Key_Escape:
268 if(!selectedItems().isEmpty())
269 clearSelection();
270 else
271 QListWidget::keyPressEvent(event);
272 break;
273 default:
274 QListWidget::keyPressEvent(event);
275 }
276 }
277