diff options
author | Arno <arno@disconnect.de> | 2016-08-20 10:13:44 +0200 |
---|---|---|
committer | Arno <arno@disconnect.de> | 2016-08-20 10:13:44 +0200 |
commit | ddd1616f5c310c94214e0ba2629f174c4cf768f3 (patch) | |
tree | 3e8eb94565dead6859a9b3fbd37ce24ec1d39c53 | |
parent | d47427a2d51fcc7f8e1f8926a706e04ff01603ed (diff) | |
download | ShemovCleaner-ddd1616f5c310c94214e0ba2629f174c4cf768f3.tar.gz ShemovCleaner-ddd1616f5c310c94214e0ba2629f174c4cf768f3.tar.bz2 ShemovCleaner-ddd1616f5c310c94214e0ba2629f174c4cf768f3.zip |
Implement file search
Search for a regular expression in all files of all torrents and select
them.
-rw-r--r-- | shemovcleaner.cpp | 51 | ||||
-rw-r--r-- | shemovcleaner.h | 7 | ||||
-rw-r--r-- | torrentparser.cpp | 28 | ||||
-rw-r--r-- | torrentparser.h | 1 |
4 files changed, 71 insertions, 16 deletions
diff --git a/shemovcleaner.cpp b/shemovcleaner.cpp index c50e598..b33f526 100644 --- a/shemovcleaner.cpp +++ b/shemovcleaner.cpp @@ -17,6 +17,7 @@ #include <QHeaderView> #include <QStatusBar> #include <QApplication> +#include <QRegularExpression> #include <QDebug> @@ -25,7 +26,7 @@ #include "torrentparser.h" #include "torrentdisplay.h" -ShemovCleaner::ShemovCleaner(QWidget *parent) : QMainWindow(parent){ +ShemovCleaner::ShemovCleaner(QWidget *parent) : QMainWindow(parent), mExt("*.torrent") { QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL", "shemovdb"); db.setHostName("hadante.d-tor.org"); db.setUserName("shemov"); @@ -47,19 +48,21 @@ ShemovCleaner::~ShemovCleaner(){ void ShemovCleaner::setupGui(){ qApp->setWindowIcon(QIcon(":/clean_tampon.png")); mDir = new QLineEdit; - mDir->setText("C:\\Users\\am\\Downloads"); - mExt = new QLineEdit; - mExt->setText("*.torrent"); mSelDir = new QPushButton(tr("Browse...")); connect(mSelDir, SIGNAL(clicked()), this, SLOT(selectDir())); - mSelExt = new QPushButton(tr("Go!")); - connect(mSelExt, SIGNAL(clicked()), this, SLOT(gatherData())); + + mSearchTorrents = new QLineEdit; + connect(mSearchTorrents, SIGNAL(returnPressed()), this, SLOT(searchFile())); + mDoSearchTorrents = new QPushButton(tr("Search")); + connect(mDoSearchTorrents, SIGNAL(clicked()), this, SLOT(searchFile())); QGridLayout *buttonL = new QGridLayout; - buttonL->addWidget(mDir, 0, 0); - buttonL->addWidget(mSelDir, 0, 1); - buttonL->addWidget(mExt, 1, 0); - buttonL->addWidget(mSelExt, 1, 1); + buttonL->addWidget(new QLabel(tr("Directory")), 0, 0); + buttonL->addWidget(mDir, 0, 1); + buttonL->addWidget(mSelDir, 0, 2); + buttonL->addWidget(new QLabel(tr("Search file")), 1, 0); + buttonL->addWidget(mSearchTorrents, 1, 1); + buttonL->addWidget(mDoSearchTorrents, 1, 2); mFileView = new QTreeView; mFileView->setSortingEnabled(true); @@ -116,7 +119,7 @@ void ShemovCleaner::gatherData(){ QSqlQuery q(db); q.prepare("SELECT COUNT(*) FROM metadata WHERE tsubject = :fn"); QDir d(mDir->text()); - QFileInfoList fl = d.entryInfoList(QStringList() << mExt->text(), QDir::Files, QDir::Name); + QFileInfoList fl = d.entryInfoList(QStringList() << mExt, QDir::Files, QDir::Name); mModel->clear(); QStandardItem *root = mModel->invisibleRootItem(); mModel->setHorizontalHeaderLabels(QStringList() << QChar(0x26A7) << tr("Name") << tr("Created")); @@ -210,6 +213,29 @@ void ShemovCleaner::torrentInfo(){ return; } +void ShemovCleaner::searchFile(){ + mFileView->selectionModel()->clear(); + int count = mProxy->rowCount(); + QRegularExpression regex(mSearchTorrents->text(), QRegularExpression::CaseInsensitiveOption); + int success = 0; + for(int i = 0; i < count; ++i){ + QModelIndex cur = mProxy->index(i, 1); + QString fn = cur.data(Qt::UserRole + 1).toString(); + TorrentParser p(fn); + statusBar()->showMessage(QString(tr("parsing %1").arg(fn))); + QList<QVariant> torrentData = p.parse(); + QStringList files = p.files(torrentData); + foreach(QString f, files){ + QRegularExpressionMatch m = regex.match(f); + if(m.hasMatch()){ + ++success; + mFileView->selectionModel()->select(cur, QItemSelectionModel::Select | QItemSelectionModel::Rows); + } + } + } + statusBar()->showMessage(QString(tr("Found %1 file(s)").arg(QString::number(success)))); +} + void ShemovCleaner::selectDir(){ QString dir = QFileDialog::getExistingDirectory(this, tr("Select directory"), QDir::homePath()); mDir->setText(QDir::toNativeSeparators(dir)); @@ -228,14 +254,11 @@ void ShemovCleaner::readSettings(){ QSettings s; QString dir = s.value("searchdir", QDir::toNativeSeparators(QDir::homePath())).toString(); mDir->setText(dir); - QString ext = s.value("extension", "*.torrent").toString(); - mExt->setText(ext); } void ShemovCleaner::writeSettings(){ QSettings s; s.setValue("searchdir", mDir->text()); - s.setValue("extension", mExt->text()); } void ShemovCleaner::readHeaderData(){ diff --git a/shemovcleaner.h b/shemovcleaner.h index fe02fc6..80ccddb 100644 --- a/shemovcleaner.h +++ b/shemovcleaner.h @@ -3,6 +3,7 @@ #include <QMainWindow> #include <QItemSelection> +#include <QString> class QPushButton; class QLineEdit; @@ -25,6 +26,7 @@ class ShemovCleaner : public QMainWindow { void deleteFiles(); void moveFiles(); void torrentInfo(); + void searchFile(); private: void setupGui(); @@ -34,12 +36,13 @@ class ShemovCleaner : public QMainWindow { void readSettings(); void writeSettings(); QLineEdit *mDir; - QLineEdit *mExt; QPushButton *mSelDir; - QPushButton *mSelExt; + QLineEdit *mSearchTorrents; + QPushButton *mDoSearchTorrents; QPushButton *mMove; QPushButton *mDelete; QPushButton *mInfo; + QString mExt; QLabel *mSelected; QStandardItemModel *mModel; FileSorter *mProxy; diff --git a/torrentparser.cpp b/torrentparser.cpp index 8e09681..cd2fde4 100644 --- a/torrentparser.cpp +++ b/torrentparser.cpp @@ -20,6 +20,34 @@ const QList<QVariant> TorrentParser::parse(){ return retval; } +const QStringList TorrentParser::files(QList<QVariant> data){ + QStringList retval; + foreach(QVariant cur, data){ + if(cur.canConvert<QVariantHash>()){ + QVariantHash h = cur.toHash(); + QHash<QString, QVariant>::const_iterator it = h.constBegin(); + while(it != h.constEnd()){ + if(it.key() == "info"){ + QVariantHash infoD = it.value().toHash(); + if(infoD.contains("files")){ + QVariantList fileList = infoD.value("files").toList(); + foreach(QVariant fn, fileList){ + QVariantHash e = fn.toHash(); + QVariantList path = e.value("path").toList(); + retval << path.last().toString(); + } + }else{ + retval << infoD.value("name").toString(); + } + break; + } + ++it; + } + } + } + return retval; +} + const QVariant TorrentParser::parseObject(){ QChar cur = mData.at(mPos); if(cur == 'i'){ diff --git a/torrentparser.h b/torrentparser.h index e1f8ef7..8a99d22 100644 --- a/torrentparser.h +++ b/torrentparser.h @@ -13,6 +13,7 @@ class TorrentParser : public QObject { public: TorrentParser(const QString file, QObject *parent = 0); const QList<QVariant> parse(); + const QStringList files(QList<QVariant> data); private: const QVariant parseObject(); |