1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QMessageBox>
#include <QGridLayout>
#include <QPushButton>
#include <QFileDialog>
#include <QDir>
#include <QLineEdit>
#include <QTreeView>
#include <QStandardItemModel>
#include <QStandardItem>
#include <QFileInfo>
#include <QBrush>
#include <QLabel>
#include <QDateTime>
#include <QSettings>
#include <QHeaderView>
#include <QStatusBar>
#include <QApplication>
#include <QRegularExpression>
#include <QDebug>
#include "shemovcleaner.h"
#include "filesorter.h"
#include "torrentparser.h"
#include "torrentdisplay.h"
ShemovCleaner::ShemovCleaner(QWidget *parent) : QMainWindow(parent), mExt("*.torrent") {
QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL", "shemovdb");
db.setHostName("hadante.d-tor.org");
db.setUserName("shemov");
db.setPassword("shemov");
db.setDatabaseName("shemov2");
if(!db.open()){
QMessageBox::critical(0, tr("Error"), tr("Could not open database!"));
}
setupGui();
gatherData();
}
ShemovCleaner::~ShemovCleaner(){
writeHeaderData();
writeSettings();
}
void ShemovCleaner::setupGui(){
qApp->setWindowIcon(QIcon(":/clean_tampon.png"));
mDir = new QLineEdit;
mSelDir = new QPushButton(tr("Browse..."));
connect(mSelDir, SIGNAL(clicked()), this, SLOT(selectDir()));
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(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);
mFileView->selectionModel();
mFileView->setSelectionBehavior(QAbstractItemView::SelectRows);
mFileView->setSelectionMode(QAbstractItemView::ExtendedSelection);
mModel = new QStandardItemModel;
mProxy = new FileSorter;
mProxy->setSourceModel(mModel);
mFileView->setModel(mProxy);
QLabel *fileL = new QLabel(tr("Found:"));
readSettings();
mDelete = new QPushButton(tr("Delete..."));
connect(mDelete, SIGNAL(clicked()), this, SLOT(deleteFiles()));
mMove = new QPushButton(tr("Move..."));
connect(mMove, SIGNAL(clicked()), this, SLOT(moveFiles()));
mInfo = new QPushButton(tr("Torrent..."));
connect(mInfo, SIGNAL(clicked()), this, SLOT(torrentInfo()));
QHBoxLayout *buttonL2 = new QHBoxLayout;
buttonL2->addStretch();
buttonL2->addWidget(mMove);
buttonL2->addWidget(mDelete);
buttonL2->addWidget(mInfo);
buttonL2->addStretch();
QWidget *mainWidget = new QWidget;
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(buttonL);
mainLayout->addWidget(fileL);
mainLayout->addWidget(mFileView);
mainLayout->addLayout(buttonL2);
mainWidget->setLayout(mainLayout);
setMinimumWidth(800);
setMinimumHeight(600);
setCentralWidget(mainWidget);
createStatusbar();
mTorrentDisplay = new TorrentDisplay(this);
connect(mFileView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(fileSelectionChanged(QItemSelection,QItemSelection)));
}
void ShemovCleaner::createStatusbar(){
QLabel *l1 = new QLabel(tr("Sel."));
mSelected = new QLabel("000/000");
mSelected->setFrameStyle(QFrame::Panel | QFrame::Sunken);
statusBar()->addPermanentWidget(l1);
statusBar()->addPermanentWidget(mSelected);
}
void ShemovCleaner::gatherData(){
QSqlDatabase db = QSqlDatabase::database("shemovdb");
QSqlQuery q(db);
q.prepare("SELECT COUNT(*) FROM metadata WHERE tsubject = :fn");
QDir d(mDir->text());
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"));
QBrush redBrush(Qt::red);
QBrush greenBrush(Qt::darkGreen);
foreach(QFileInfo fi, fl){
int result = 0;
q.bindValue(":fn", fi.fileName());
q.exec();
while(q.next()){
result = q.value(0).toInt();
}
QStandardItem *i1;
QStandardItem *i2 = new QStandardItem(fi.fileName());
QStandardItem *i3 = new QStandardItem(fi.created().toString(Qt::RFC2822Date));
if(result){
i1 = new QStandardItem(QIcon(":/huge_bra.png"), QString());
i1->setData(1);
i2->setForeground(greenBrush);
}else{
i1 = new QStandardItem(QIcon(":/gaping_ass.png"), QString());
i1->setData(0);
i2->setForeground(redBrush);
}
i2->setData(fi.absoluteFilePath());
root->appendRow(QList<QStandardItem*>() << i1 << i2 << i3);
}
readHeaderData();
}
void ShemovCleaner::deleteFiles(){
QModelIndexList sel = mFileView->selectionModel()->selectedRows(1);
if(sel.isEmpty()){
return;
}
QString q = QString(tr("Really delete %1 file(s)?")).arg(QString::number(sel.count()));
int res = QMessageBox::question(this, tr("Delete files..."), q);
int ctr = 0;
if(res == QMessageBox::Yes){
foreach(QModelIndex i, sel){
QString fp = i.data(Qt::UserRole + 1).toString();
bool ok = QFile::remove(fp);
if(ok){
++ctr;
}else{
QFileInfo fi(fp);
QString msg = QString(tr("Shit: %1 vanished from under us!")).arg(fi.fileName());
QMessageBox::critical(this, tr("Error"), msg);
}
}
QString msg = QString(tr("Successfully deleted %1 of %2 file(s)")).arg(QString::number(ctr)).arg(QString::number(sel.count()));
statusBar()->showMessage(msg);
gatherData();
}
}
void ShemovCleaner::moveFiles(){
QModelIndexList sel = mFileView->selectionModel()->selectedRows(1);
if(sel.isEmpty()){
return;
}
QString dir = QFileDialog::getExistingDirectory(this, tr("Select directory"), QDir::homePath());
if(!dir.isEmpty()){
int ctr = 0;
foreach(QModelIndex i, sel){
QFileInfo fp(i.data(Qt::UserRole + 1).toString());
QString newfn = QString("%1/%2").arg(dir).arg(fp.fileName());
bool ok = QFile::rename(fp.absoluteFilePath(), newfn);
if(ok){
++ctr;
}
}
QString msg = QString(tr("Successfully moved %1 of %2 file(s)")).arg(QString::number(ctr)).arg(QString::number(sel.count()));
statusBar()->showMessage(msg);
gatherData();
}
}
void ShemovCleaner::torrentInfo(){
QModelIndexList sel = mFileView->selectionModel()->selectedRows(1);
if(sel.isEmpty()){
return;
}
QString fn = sel.first().data(Qt::UserRole + 1).toString();
TorrentParser p(fn, this);
QList<QVariant> tData = p.parse();
tData.removeAll(QVariant());
tData.removeAll(0);
mTorrentDisplay->setData(tData, sel.first().data().toString());
mTorrentDisplay->show();
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));
}
void ShemovCleaner::fileSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected){
Q_UNUSED(selected);
Q_UNUSED(deselected);
int count = mFileView->selectionModel()->selectedRows().count();
int total = mModel->rowCount();
QString msg = QString("%1/%2").arg(QString::number(count)).arg(QString::number(total));
mSelected->setText(msg);
}
void ShemovCleaner::readSettings(){
QSettings s;
QString dir = s.value("searchdir", QDir::toNativeSeparators(QDir::homePath())).toString();
mDir->setText(dir);
}
void ShemovCleaner::writeSettings(){
QSettings s;
s.setValue("searchdir", mDir->text());
}
void ShemovCleaner::readHeaderData(){
QSettings s;
QByteArray headers = s.value("fileheaders").toByteArray();
if(!headers.isEmpty()){
mFileView->header()->restoreState(headers);
}
}
void ShemovCleaner::writeHeaderData(){
QSettings s;
s.setValue("fileheaders", mFileView->header()->saveState());
}
|