diff 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
line wrap: on
line diff
--- a/DataController.cpp	Tue Aug 21 15:27:29 2012 +0200
+++ b/DataController.cpp	Wed Aug 22 00:41:15 2012 +0200
@@ -10,6 +10,7 @@
 
 #include <QtGui/QApplication>
 #include <QtCore/QDir>
+#include <QtCore/QUrl>
 
 #include <QtCore/QDebug>
 #include <QtCore/QTimer>
@@ -17,6 +18,7 @@
 #include <QtCore/QDateTime>
 
 #include <QtGui/QMainWindow>
+#include <QtGui/QMessageBox>
 #include <QtGui/QDesktopServices>
 #include <QtGui/QTreeWidget>
 #include <QtGui/QHeaderView>
@@ -36,7 +38,7 @@
     findFiles(QDir(filename), list);
   }
 
-  foreach(QString filename, dir.entryList(QDir::Files)) {
+  foreach(QString filename, dir.entryList(QDir::Files | QDir::NoSymLinks)) {
     list << dir.absoluteFilePath(filename);
   }
 }
@@ -52,17 +54,25 @@
 {
   QStringList list = findFiles(dir);
 
-  QProgressBar bar;
-
   QDateTime last = QDateTime::currentDateTime();
 
-  bar.resize(200,25);
-  bar.setValue(0);
-  bar.setMinimum(0);
-  bar.setMaximum(list.size());
-  bar.show();
+  dblink.keepOnlyFromPrefix(dir.path(), list);
+
+  std::auto_ptr<QProgressBar> bar;
+
+  progressMax = list.size();
+
+  if (showGUI) {
+    bar = std::auto_ptr<QProgressBar>(new QProgressBar());
 
-  connect(this, SIGNAL(populateProgress(int)), &bar, SLOT(setValue(int)));
+    bar->resize(200,25);
+    bar->setValue(0);
+    bar->setMinimum(0);
+    bar->setMaximum(progressMax);
+    bar->show();
+
+    connect(this, SIGNAL(populateProgress(int)), bar.get(), SLOT(setValue(int)));
+  }
 
   int n = 0;
   foreach(QString filename, findFiles(dir)) {
@@ -271,6 +281,18 @@
 
 }
 
+void DataController::contextMenuRequested(const QPoint& point)
+{
+  contextMenuItem = tw->itemAt(point);
+  if (!contextMenu) {
+    contextMenu = new QMenu(tw);
+    QAction* deleteAction = contextMenu->addAction("Delete");
+    connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteFile()));    
+  }
+  contextMenu->popup(tw->mapToGlobal(point));
+}
+
+
 void DataController::setDir(const QDir& dir)
 {
   this->dir = dir.absolutePath();
@@ -336,6 +358,12 @@
   mw->addToolBar(filterBar);
 
   tw = new QTreeWidget(mw);
+  tw->setContextMenuPolicy( Qt::CustomContextMenu);
+  connect(tw, SIGNAL(customContextMenuRequested (const QPoint&)),
+	  this, SLOT(contextMenuRequested(const QPoint&)));
+  connect(tw, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)),
+	  this, SLOT(itemDoubleClicked(QTreeWidgetItem*, int)));
+
   mw->setCentralWidget(tw);
   tw->setEditTriggers(QAbstractItemView::NoEditTriggers);
 
@@ -360,8 +388,56 @@
   setup(QString(), QString(), showGUI);
 }
 
+#include <iostream>
+
+void DataController::progressUpdate(int p)
+{
+  QString str;
+  if (p == 0)
+    str.sprintf("Progress %6.2f%%", p * 100.0 / progressMax);
+  else
+    str.sprintf("\b\b\b\b\b\b%6.2f%%", p * 100.0 / progressMax);
+  std::cout<<str.toStdString();
+  std::cout.flush();
+}
+
+
+void DataController::deleteFile()
+{
+  QString path = contextMenuItem->data(0, 32).toString();
+  QMessageBox::StandardButton button =
+    QMessageBox::question(tw, "Confirm delete",
+			  QString("Do you really want to delete \"%1\"?").arg(path),
+			  QMessageBox::Cancel | QMessageBox::Ok,
+			  QMessageBox::Cancel);
+  if (button ==  QMessageBox::Ok) {
+    QFile file(path);
+    if (file.remove()) {
+      dblink->deleteFileFromDB(path);
+      populate();
+    }
+    else {
+      QMessageBox::warning(tw, "Delete failed", QString("Could not delete \"%1\"?").arg(file.fileName()));
+    }
+  }
+}
+
+
+void DataController::itemDoubleClicked (QTreeWidgetItem * item, int column)
+{
+  QUrl url = QUrl::fromLocalFile(item->data(0, 32).toString());
+  QDesktopServices::openUrl(url);  
+}
+
+
 void DataController::setup(const QString& dbpath_in, const QString& searchPath_in, bool showGUI)
 {
+  this->showGUI = showGUI;
+
+  contextMenu = 0;
+
+  connect(this, SIGNAL(populateProgress(int)), this, SLOT(progressUpdate(int)));
+
   QString dbpath;
   if (dbpath_in.size() > 0) {
     dbpath = dbpath_in;
@@ -406,14 +482,27 @@
 
   tw->setSortingEnabled(false);
 
-  int role = (showFullPath)?32:33;
+  int role = (showFullPath) ? 32 : 33;
   for (int row = 0; row < tw->topLevelItemCount(); ++row) {
     QTreeWidgetItem* pathItem = tw->topLevelItem(row);
-    pathItem->setData(0, Qt::DisplayRole, pathItem->data(0,role));
+    pathItem->setData(0, Qt::DisplayRole, pathItem->data(0, role));
+    setShowFullPath(pathItem, showFullPath);
   }
   tw->setSortingEnabled(true);
 }
 
+void DataController::setShowFullPath(QTreeWidgetItem* item, bool showFullPath)
+{
+  int role = (showFullPath) ? 32 : 33;
+  for (int row = 0; row < item->childCount(); ++row) {
+    QTreeWidgetItem* child = item->child(row);
+    QVariant data = child->data(0,role);
+    if (data.isValid())
+      child->setData(0, Qt::DisplayRole, data);
+    setShowFullPath(child, showFullPath);
+  }
+}
+
 bool DataController::toggleShowFullPath()
 {
   bool showFullPath = ! this->showFullPath;