Mercurial > dedupe
comparison DataController.cpp @ 0:a3834af36579
Working with memory backend.
| author | Tom Fredrik Blenning Klaussen <bfg@blenning.no> |
|---|---|
| date | Mon, 20 Aug 2012 15:49:48 +0200 |
| parents | |
| children | aae83c0a771d |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:a3834af36579 |
|---|---|
| 1 #include "DataController.hpp" | |
| 2 | |
| 3 #include "MemoryDbLink.hpp" | |
| 4 | |
| 5 #include "PermissionException.hpp" | |
| 6 #include "DataController.hpp" | |
| 7 | |
| 8 #include <QtGui/QApplication> | |
| 9 #include <QtCore/QDir> | |
| 10 | |
| 11 #include <QtCore/QDebug> | |
| 12 #include <QtCore/QTimer> | |
| 13 #include <QtCore/QCryptographicHash> | |
| 14 #include <QtCore/QDateTime> | |
| 15 | |
| 16 #include <QtGui/QMainWindow> | |
| 17 #include <QtGui/QTreeWidget> | |
| 18 #include <QtGui/QHeaderView> | |
| 19 #include <QtGui/QMenuBar> | |
| 20 #include <QtGui/QToolBar> | |
| 21 #include <QtGui/QDoubleSpinBox> | |
| 22 #include <QtGui/QLabel> | |
| 23 #include <QtGui/QHBoxLayout> | |
| 24 #include <QtGui/QProgressBar> | |
| 25 | |
| 26 #include "EditDistance.hpp" | |
| 27 | |
| 28 #include <cassert> | |
| 29 void findFiles(const QDir& dir, FileDbLink& dblink) | |
| 30 { | |
| 31 /* | |
| 32 QProgressDialog progressDialog(this); | |
| 33 progressDialog.setCancelButtonText(tr("&Cancel")); | |
| 34 progressDialog.setRange(0, files.size()); | |
| 35 progressDialog.setWindowTitle(tr("Find Files")); | |
| 36 */ | |
| 37 | |
| 38 foreach(QString filename, dir.entryList(QDir::NoDotAndDotDot | QDir::Dirs)) { | |
| 39 filename = dir.absoluteFilePath(filename); | |
| 40 findFiles(QDir(filename), dblink); | |
| 41 } | |
| 42 | |
| 43 foreach(QString filename, dir.entryList(QDir::Files)) { | |
| 44 filename = dir.absoluteFilePath(filename); | |
| 45 try { | |
| 46 dblink.updateIfModified(filename); | |
| 47 } | |
| 48 catch (const PermissionException& e) { | |
| 49 qDebug()<<e.toString(); | |
| 50 } | |
| 51 catch (Exception& e) { | |
| 52 qDebug()<<e.toString(); | |
| 53 exit(1); | |
| 54 } | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 QTreeWidgetItem* DataController::createItem(const FileDbLink::DBInfo& info) | |
| 59 { | |
| 60 QTreeWidgetItem* item = new QTreeWidgetItem(); | |
| 61 item->setData(0, Qt::DisplayRole, info.name()); | |
| 62 item->setData(0, 32, info.path()); | |
| 63 item->setData(0, 33, info.name()); | |
| 64 | |
| 65 item->setData(1, Qt::DisplayRole, info.size()); | |
| 66 item->setData(2, Qt::DisplayRole, info.mtime()); | |
| 67 item->setData(3, Qt::DisplayRole, info.checksum().toHex()); | |
| 68 return item; | |
| 69 } | |
| 70 | |
| 71 void DataController::delayPopulate() | |
| 72 { | |
| 73 populateDelay->start(); | |
| 74 } | |
| 75 | |
| 76 | |
| 77 void DataController::populate() | |
| 78 { | |
| 79 populate(nameFilter->isChecked(), sizeFilter->isChecked(), mtimeFilter->isChecked(), | |
| 80 checksumFilter->isChecked(), (100 - editCutoffSpin->value()) * .01); | |
| 81 } | |
| 82 | |
| 83 void DataController::populate(bool showNameDups, bool showSizeDups, | |
| 84 bool showMTimeDups, bool showCheckSumDups, | |
| 85 float editDistanceCutoff) | |
| 86 { | |
| 87 tw->clear(); | |
| 88 | |
| 89 const QList<QSharedPointer<FileDbLink::DBInfo> > elems = dblink->sortOn(FileDbLink::EDIT, true); | |
| 90 | |
| 91 QProgressBar bar; | |
| 92 | |
| 93 bar.resize(200,25); | |
| 94 bar.setValue(0); | |
| 95 bar.setMinimum(0); | |
| 96 bar.setMaximum(elems.size()); | |
| 97 bar.show(); | |
| 98 | |
| 99 connect(this, SIGNAL(populateProgress(int)), &bar, SLOT(setValue(int))); | |
| 100 | |
| 101 int n = 0; | |
| 102 | |
| 103 tw->setUpdatesEnabled(false); | |
| 104 | |
| 105 foreach(QSharedPointer<FileDbLink::DBInfo> line, elems) { | |
| 106 QTreeWidgetItem* item = 0; | |
| 107 bool anyAdded = false; | |
| 108 | |
| 109 if (showNameDups) { | |
| 110 QTreeWidgetItem* topLevelItem = 0; | |
| 111 foreach(QSharedPointer<FileDbLink::DBInfo> dup, elems) { | |
| 112 if(dup != line && line->name() == dup->name() ) { | |
| 113 if (!topLevelItem) { | |
| 114 topLevelItem = new QTreeWidgetItem(); | |
| 115 topLevelItem->setData(0, Qt::DisplayRole, "Name"); | |
| 116 if(!item) | |
| 117 item = createItem(*line); | |
| 118 item->addChild(topLevelItem); | |
| 119 anyAdded = true; | |
| 120 } | |
| 121 topLevelItem->addChild(createItem(*dup)); | |
| 122 } | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 if (showSizeDups) { | |
| 127 QTreeWidgetItem* topLevelItem = 0; | |
| 128 foreach(QSharedPointer<FileDbLink::DBInfo> dup, elems) { | |
| 129 if(dup != line && line->size() == dup->size() ) { | |
| 130 if (!topLevelItem) { | |
| 131 topLevelItem = new QTreeWidgetItem(); | |
| 132 topLevelItem->setData(0, Qt::DisplayRole, "Size"); | |
| 133 if(!item) | |
| 134 item = createItem(*line); | |
| 135 item->addChild(topLevelItem); | |
| 136 anyAdded = true; | |
| 137 } | |
| 138 topLevelItem->addChild(createItem(*dup)); | |
| 139 } | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 if (showMTimeDups) { | |
| 144 QTreeWidgetItem* topLevelItem = 0; | |
| 145 foreach(QSharedPointer<FileDbLink::DBInfo> dup, elems) { | |
| 146 if(dup != line && line->mtime() == dup->mtime() ) { | |
| 147 if (!topLevelItem) { | |
| 148 topLevelItem = new QTreeWidgetItem(); | |
| 149 topLevelItem->setData(0, Qt::DisplayRole, "MTime"); | |
| 150 if(!item) | |
| 151 item = createItem(*line); | |
| 152 item->addChild(topLevelItem); | |
| 153 anyAdded = true; | |
| 154 } | |
| 155 topLevelItem->addChild(createItem(*dup)); | |
| 156 } | |
| 157 } | |
| 158 } | |
| 159 | |
| 160 if (showCheckSumDups) { | |
| 161 QTreeWidgetItem* topLevelItem = 0; | |
| 162 foreach(QSharedPointer<FileDbLink::DBInfo> dup, elems) { | |
| 163 if(dup != line && line->checksum() == dup->checksum() ) { | |
| 164 if (!topLevelItem) { | |
| 165 topLevelItem = new QTreeWidgetItem(); | |
| 166 topLevelItem->setData(0, Qt::DisplayRole, "Checksum"); | |
| 167 if(!item) | |
| 168 item = createItem(*line); | |
| 169 item->addChild(topLevelItem); | |
| 170 anyAdded = true; | |
| 171 } | |
| 172 topLevelItem->addChild(createItem(*dup)); | |
| 173 } | |
| 174 } | |
| 175 } | |
| 176 | |
| 177 if (editDistanceCutoff < 1.0) { | |
| 178 QTreeWidgetItem* topLevelItem = 0; | |
| 179 QMultiMap<int, QSharedPointer<FileDbLink::DBInfo> > oList; | |
| 180 | |
| 181 int absoluteCutoff = line->name().length() * editDistanceCutoff; | |
| 182 foreach(QSharedPointer<FileDbLink::DBInfo> dup, elems) { | |
| 183 if(dup != line) { | |
| 184 int distance = EditDistance::Compute(line->name(), dup->name()); | |
| 185 | |
| 186 if (distance <= absoluteCutoff) { | |
| 187 oList.insert(distance, dup); | |
| 188 } | |
| 189 } | |
| 190 } | |
| 191 | |
| 192 if (oList.size() > 0 ) { | |
| 193 topLevelItem = new QTreeWidgetItem(); | |
| 194 topLevelItem->setData(0, Qt::DisplayRole, "Editdistance"); | |
| 195 if(!item) | |
| 196 item = createItem(*line); | |
| 197 item->addChild(topLevelItem); | |
| 198 anyAdded = true; | |
| 199 } | |
| 200 | |
| 201 foreach(QSharedPointer<FileDbLink::DBInfo> dup, oList.values()) { | |
| 202 topLevelItem->addChild(createItem(*dup)); | |
| 203 } | |
| 204 } | |
| 205 | |
| 206 | |
| 207 | |
| 208 if (item) | |
| 209 tw->addTopLevelItem(item); | |
| 210 emit populateProgress(++n); | |
| 211 if (n % 64 == 0) { | |
| 212 QCoreApplication::processEvents(); | |
| 213 } | |
| 214 } | |
| 215 tw->setUpdatesEnabled(true); | |
| 216 | |
| 217 } | |
| 218 | |
| 219 | |
| 220 DataController::DataController() : showFullPath(false) | |
| 221 { | |
| 222 populateDelay = new QTimer(this); | |
| 223 populateDelay->setSingleShot(true); | |
| 224 populateDelay->setInterval(500); | |
| 225 connect(populateDelay, SIGNAL(timeout()), this, SLOT(populate())); | |
| 226 | |
| 227 dblink = new MemoryDbLink(); | |
| 228 | |
| 229 findFiles(QDir("."), *dblink); | |
| 230 | |
| 231 mw = new QMainWindow(); | |
| 232 QMenuBar* mb = new QMenuBar(); | |
| 233 | |
| 234 QMenu* menu = mb->addMenu("&View"); | |
| 235 QAction* action = menu->addAction("Show full path"); | |
| 236 action->setCheckable(true); | |
| 237 connect(action, SIGNAL(toggled(bool)), this, SLOT(setShowFullPath(bool))); | |
| 238 | |
| 239 mw->setMenuBar(mb); | |
| 240 | |
| 241 QToolBar* filterBar = new QToolBar("Filters"); | |
| 242 | |
| 243 nameFilter = filterBar->addAction("Name"); | |
| 244 nameFilter->setCheckable(true); | |
| 245 connect(nameFilter, SIGNAL(toggled(bool)), this, SLOT(delayPopulate())); | |
| 246 | |
| 247 sizeFilter = filterBar->addAction("Size"); | |
| 248 sizeFilter->setCheckable(true); | |
| 249 connect(sizeFilter, SIGNAL(toggled(bool)), this, SLOT(delayPopulate())); | |
| 250 | |
| 251 mtimeFilter = filterBar->addAction("MTime"); | |
| 252 mtimeFilter->setCheckable(true); | |
| 253 connect(mtimeFilter, SIGNAL(toggled(bool)), this, SLOT(delayPopulate())); | |
| 254 | |
| 255 checksumFilter = filterBar->addAction("Checksum"); | |
| 256 checksumFilter->setCheckable(true); | |
| 257 connect(checksumFilter, SIGNAL(toggled(bool)), this, SLOT(delayPopulate())); | |
| 258 | |
| 259 QWidget* widget = new QWidget(); | |
| 260 QLayout* layout = new QHBoxLayout(); | |
| 261 layout->setContentsMargins(0,0,0,0); | |
| 262 widget->setLayout(layout); | |
| 263 layout->addWidget(new QLabel("Edit distance", widget)); | |
| 264 editCutoffSpin = new QSpinBox(widget); | |
| 265 layout->addWidget(editCutoffSpin); | |
| 266 editCutoffSpin->setMinimum(0); | |
| 267 editCutoffSpin->setMaximum(100); | |
| 268 editCutoffSpin->setValue(70); | |
| 269 editCutoffSpin->setSingleStep(10); | |
| 270 editCutoffSpin->setSuffix("%"); | |
| 271 connect(editCutoffSpin, SIGNAL(valueChanged(int)), this, SLOT(delayPopulate())); | |
| 272 filterBar->addWidget(widget); | |
| 273 | |
| 274 mw->addToolBar(filterBar); | |
| 275 | |
| 276 tw = new QTreeWidget(mw); | |
| 277 mw->setCentralWidget(tw); | |
| 278 tw->setEditTriggers(QAbstractItemView::NoEditTriggers); | |
| 279 | |
| 280 tw->setHeaderLabels(QString("Path;Size;Date;Checksum").split(";")); | |
| 281 | |
| 282 populate(); | |
| 283 | |
| 284 tw->setSortingEnabled(true); | |
| 285 tw->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); | |
| 286 tw->setSelectionBehavior(QAbstractItemView::SelectRows); | |
| 287 //tw->resizeColumnsToContents(); | |
| 288 mw->resize(800,800); | |
| 289 mw->show(); | |
| 290 | |
| 291 | |
| 292 } | |
| 293 | |
| 294 DataController::~DataController() | |
| 295 { | |
| 296 } | |
| 297 | |
| 298 | |
| 299 void DataController::cellDoubleClicked(int row, int column) | |
| 300 { | |
| 301 if(column == 0) { | |
| 302 toggleShowFullPath(); | |
| 303 } | |
| 304 } | |
| 305 | |
| 306 | |
| 307 void DataController::setShowFullPath(bool showFullPath) | |
| 308 { | |
| 309 this->showFullPath = showFullPath; | |
| 310 | |
| 311 tw->setSortingEnabled(false); | |
| 312 | |
| 313 int role = (showFullPath)?32:33; | |
| 314 for (int row = 0; row < tw->topLevelItemCount(); ++row) { | |
| 315 QTreeWidgetItem* pathItem = tw->topLevelItem(row); | |
| 316 pathItem->setData(0, Qt::DisplayRole, pathItem->data(0,role)); | |
| 317 } | |
| 318 tw->setSortingEnabled(true); | |
| 319 } | |
| 320 | |
| 321 bool DataController::toggleShowFullPath() | |
| 322 { | |
| 323 bool showFullPath = ! this->showFullPath; | |
| 324 setShowFullPath(showFullPath); | |
| 325 return showFullPath; | |
| 326 } |
