comparison DataController.cpp @ 5:5e4985407feb

Add commandline tool updateDeDupe. Fix removal of removed files from DB.
author Tom Fredrik Blenning Klaussen <bfg@blenning.no>
date Wed, 22 Aug 2012 00:41:15 +0200
parents f489b0c9bf99
children d6fdca3bf24e
comparison
equal deleted inserted replaced
4:f489b0c9bf99 5:5e4985407feb
8 #include "PermissionException.hpp" 8 #include "PermissionException.hpp"
9 #include "DataController.hpp" 9 #include "DataController.hpp"
10 10
11 #include <QtGui/QApplication> 11 #include <QtGui/QApplication>
12 #include <QtCore/QDir> 12 #include <QtCore/QDir>
13 #include <QtCore/QUrl>
13 14
14 #include <QtCore/QDebug> 15 #include <QtCore/QDebug>
15 #include <QtCore/QTimer> 16 #include <QtCore/QTimer>
16 #include <QtCore/QCryptographicHash> 17 #include <QtCore/QCryptographicHash>
17 #include <QtCore/QDateTime> 18 #include <QtCore/QDateTime>
18 19
19 #include <QtGui/QMainWindow> 20 #include <QtGui/QMainWindow>
21 #include <QtGui/QMessageBox>
20 #include <QtGui/QDesktopServices> 22 #include <QtGui/QDesktopServices>
21 #include <QtGui/QTreeWidget> 23 #include <QtGui/QTreeWidget>
22 #include <QtGui/QHeaderView> 24 #include <QtGui/QHeaderView>
23 #include <QtGui/QMenuBar> 25 #include <QtGui/QMenuBar>
24 #include <QtGui/QToolBar> 26 #include <QtGui/QToolBar>
34 foreach(QString filename, dir.entryList(QDir::NoDotAndDotDot | QDir::Dirs)) { 36 foreach(QString filename, dir.entryList(QDir::NoDotAndDotDot | QDir::Dirs)) {
35 filename = dir.absoluteFilePath(filename); 37 filename = dir.absoluteFilePath(filename);
36 findFiles(QDir(filename), list); 38 findFiles(QDir(filename), list);
37 } 39 }
38 40
39 foreach(QString filename, dir.entryList(QDir::Files)) { 41 foreach(QString filename, dir.entryList(QDir::Files | QDir::NoSymLinks)) {
40 list << dir.absoluteFilePath(filename); 42 list << dir.absoluteFilePath(filename);
41 } 43 }
42 } 44 }
43 45
44 QStringList DataController::findFiles(const QDir& dir) 46 QStringList DataController::findFiles(const QDir& dir)
50 52
51 void DataController::findFiles(const QDir& dir, FileDBLink& dblink) 53 void DataController::findFiles(const QDir& dir, FileDBLink& dblink)
52 { 54 {
53 QStringList list = findFiles(dir); 55 QStringList list = findFiles(dir);
54 56
55 QProgressBar bar;
56
57 QDateTime last = QDateTime::currentDateTime(); 57 QDateTime last = QDateTime::currentDateTime();
58 58
59 bar.resize(200,25); 59 dblink.keepOnlyFromPrefix(dir.path(), list);
60 bar.setValue(0); 60
61 bar.setMinimum(0); 61 std::auto_ptr<QProgressBar> bar;
62 bar.setMaximum(list.size()); 62
63 bar.show(); 63 progressMax = list.size();
64 64
65 connect(this, SIGNAL(populateProgress(int)), &bar, SLOT(setValue(int))); 65 if (showGUI) {
66 bar = std::auto_ptr<QProgressBar>(new QProgressBar());
67
68 bar->resize(200,25);
69 bar->setValue(0);
70 bar->setMinimum(0);
71 bar->setMaximum(progressMax);
72 bar->show();
73
74 connect(this, SIGNAL(populateProgress(int)), bar.get(), SLOT(setValue(int)));
75 }
66 76
67 int n = 0; 77 int n = 0;
68 foreach(QString filename, findFiles(dir)) { 78 foreach(QString filename, findFiles(dir)) {
69 try { 79 try {
70 dblink.updateIfModified(filename); 80 dblink.updateIfModified(filename);
269 } 279 }
270 tw->setUpdatesEnabled(true); 280 tw->setUpdatesEnabled(true);
271 281
272 } 282 }
273 283
284 void DataController::contextMenuRequested(const QPoint& point)
285 {
286 contextMenuItem = tw->itemAt(point);
287 if (!contextMenu) {
288 contextMenu = new QMenu(tw);
289 QAction* deleteAction = contextMenu->addAction("Delete");
290 connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteFile()));
291 }
292 contextMenu->popup(tw->mapToGlobal(point));
293 }
294
295
274 void DataController::setDir(const QDir& dir) 296 void DataController::setDir(const QDir& dir)
275 { 297 {
276 this->dir = dir.absolutePath(); 298 this->dir = dir.absolutePath();
277 } 299 }
278 300
334 filterBar->addWidget(widget); 356 filterBar->addWidget(widget);
335 357
336 mw->addToolBar(filterBar); 358 mw->addToolBar(filterBar);
337 359
338 tw = new QTreeWidget(mw); 360 tw = new QTreeWidget(mw);
361 tw->setContextMenuPolicy( Qt::CustomContextMenu);
362 connect(tw, SIGNAL(customContextMenuRequested (const QPoint&)),
363 this, SLOT(contextMenuRequested(const QPoint&)));
364 connect(tw, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)),
365 this, SLOT(itemDoubleClicked(QTreeWidgetItem*, int)));
366
339 mw->setCentralWidget(tw); 367 mw->setCentralWidget(tw);
340 tw->setEditTriggers(QAbstractItemView::NoEditTriggers); 368 tw->setEditTriggers(QAbstractItemView::NoEditTriggers);
341 369
342 tw->setHeaderLabels(QString("Path;Size;Date;Checksum").split(";")); 370 tw->setHeaderLabels(QString("Path;Size;Date;Checksum").split(";"));
343 371
358 DataController::DataController(bool showGUI) 386 DataController::DataController(bool showGUI)
359 { 387 {
360 setup(QString(), QString(), showGUI); 388 setup(QString(), QString(), showGUI);
361 } 389 }
362 390
391 #include <iostream>
392
393 void DataController::progressUpdate(int p)
394 {
395 QString str;
396 if (p == 0)
397 str.sprintf("Progress %6.2f%%", p * 100.0 / progressMax);
398 else
399 str.sprintf("\b\b\b\b\b\b%6.2f%%", p * 100.0 / progressMax);
400 std::cout<<str.toStdString();
401 std::cout.flush();
402 }
403
404
405 void DataController::deleteFile()
406 {
407 QString path = contextMenuItem->data(0, 32).toString();
408 QMessageBox::StandardButton button =
409 QMessageBox::question(tw, "Confirm delete",
410 QString("Do you really want to delete \"%1\"?").arg(path),
411 QMessageBox::Cancel | QMessageBox::Ok,
412 QMessageBox::Cancel);
413 if (button == QMessageBox::Ok) {
414 QFile file(path);
415 if (file.remove()) {
416 dblink->deleteFileFromDB(path);
417 populate();
418 }
419 else {
420 QMessageBox::warning(tw, "Delete failed", QString("Could not delete \"%1\"?").arg(file.fileName()));
421 }
422 }
423 }
424
425
426 void DataController::itemDoubleClicked (QTreeWidgetItem * item, int column)
427 {
428 QUrl url = QUrl::fromLocalFile(item->data(0, 32).toString());
429 QDesktopServices::openUrl(url);
430 }
431
432
363 void DataController::setup(const QString& dbpath_in, const QString& searchPath_in, bool showGUI) 433 void DataController::setup(const QString& dbpath_in, const QString& searchPath_in, bool showGUI)
364 { 434 {
435 this->showGUI = showGUI;
436
437 contextMenu = 0;
438
439 connect(this, SIGNAL(populateProgress(int)), this, SLOT(progressUpdate(int)));
440
365 QString dbpath; 441 QString dbpath;
366 if (dbpath_in.size() > 0) { 442 if (dbpath_in.size() > 0) {
367 dbpath = dbpath_in; 443 dbpath = dbpath_in;
368 } 444 }
369 else { 445 else {
404 { 480 {
405 this->showFullPath = showFullPath; 481 this->showFullPath = showFullPath;
406 482
407 tw->setSortingEnabled(false); 483 tw->setSortingEnabled(false);
408 484
409 int role = (showFullPath)?32:33; 485 int role = (showFullPath) ? 32 : 33;
410 for (int row = 0; row < tw->topLevelItemCount(); ++row) { 486 for (int row = 0; row < tw->topLevelItemCount(); ++row) {
411 QTreeWidgetItem* pathItem = tw->topLevelItem(row); 487 QTreeWidgetItem* pathItem = tw->topLevelItem(row);
412 pathItem->setData(0, Qt::DisplayRole, pathItem->data(0,role)); 488 pathItem->setData(0, Qt::DisplayRole, pathItem->data(0, role));
489 setShowFullPath(pathItem, showFullPath);
413 } 490 }
414 tw->setSortingEnabled(true); 491 tw->setSortingEnabled(true);
492 }
493
494 void DataController::setShowFullPath(QTreeWidgetItem* item, bool showFullPath)
495 {
496 int role = (showFullPath) ? 32 : 33;
497 for (int row = 0; row < item->childCount(); ++row) {
498 QTreeWidgetItem* child = item->child(row);
499 QVariant data = child->data(0,role);
500 if (data.isValid())
501 child->setData(0, Qt::DisplayRole, data);
502 setShowFullPath(child, showFullPath);
503 }
415 } 504 }
416 505
417 bool DataController::toggleShowFullPath() 506 bool DataController::toggleShowFullPath()
418 { 507 {
419 bool showFullPath = ! this->showFullPath; 508 bool showFullPath = ! this->showFullPath;