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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
|
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QMessageBox>
#include <QGridLayout>
#include <QPushButton>
#include <QFileDialog>
#include <QDir>
#include <QTreeView>
#include <QStandardItemModel>
#include <QStandardItem>
#include <QFileInfo>
#include <QBrush>
#include <QLabel>
#include <QDateTime>
#include <QSettings>
#include <QHeaderView>
#include <QStatusBar>
#include <QRegularExpression>
#include <QToolBar>
#include <QMenuBar>
#include <QClipboard>
#include <QCompleter>
#include <QDirModel>
#include <QProgressBar>
#include <QApplication>
#include "torrentwidget.h"
#include "filesorter.h"
#include "torrentparser.h"
#include "torrentdisplay.h"
#include "globals.h"
TorrentWidget::TorrentWidget(QWidget *parent) : QWidget(parent), mExt("*.torrent") {
setupGui();
}
TorrentWidget::~TorrentWidget(){
writeHeaderData();
writeSettings();
}
void TorrentWidget::setupGui(){
mDir = new QLineEdit;
QCompleter *torDirCompl = new QCompleter(this);
torDirCompl->setModel(new QDirModel(torDirCompl));
mDir->setCompleter(torDirCompl);
connect(mDir, &QLineEdit::returnPressed, this, &TorrentWidget::setDir);
QPushButton *selDirB = new QPushButton(tr("Browse..."));
connect(selDirB, &QPushButton::clicked, this, &TorrentWidget::selectDir);
mSearchTorrents = new QLineEdit;
connect(mSearchTorrents, &QLineEdit::returnPressed, this, &TorrentWidget::searchFile);
QPushButton *doSearchTorrentsB = new QPushButton(tr("Search"));
connect(doSearchTorrentsB, &QPushButton::clicked, this, &TorrentWidget::searchFile);
QLabel *searchL = new QLabel(tr("&Search"));
searchL->setBuddy(mSearchTorrents);
QGridLayout *buttonL = new QGridLayout;
buttonL->addWidget(new QLabel(tr("Directory")), 0, 0);
buttonL->addWidget(mDir, 0, 1);
buttonL->addWidget(selDirB, 0, 2);
buttonL->addWidget(searchL, 1, 0);
buttonL->addWidget(mSearchTorrents, 1, 1);
buttonL->addWidget(doSearchTorrentsB, 1, 2);
mFileView = new QTreeView;
mFileView->setSortingEnabled(true);
mFileView->setSelectionBehavior(QAbstractItemView::SelectRows);
mFileView->setSelectionMode(QAbstractItemView::ExtendedSelection);
mFileView->setUniformRowHeights(true);
mModel = new QStandardItemModel;
mProxy = new FileSorter;
mProxy->setSourceModel(mModel);
mFileView->setModel(mProxy);
QLabel *fileL = new QLabel(tr("Found:"));
readSettings();
mToolBar = new QToolBar;
mToolBar->setIconSize(QSize(16, 16));
mMenuBar = new QMenuBar;
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(mMenuBar);
mainLayout->addWidget(mToolBar);
mainLayout->addLayout(buttonL);
mainLayout->addWidget(fileL);
mainLayout->addWidget(mFileView);
setLayout(mainLayout);
mTorrentDisplay = new TorrentDisplay(this);
connect(mFileView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &TorrentWidget::fileSelectionChanged);
createActions();
connect(mFileView, &QTreeView::doubleClicked, this, &TorrentWidget::torrentInfo);
}
void TorrentWidget::createActions(){
mRefreshA = new QAction(QIcon(":/refresh.png"), tr("Refresh"), this);
mRefreshA->setShortcut(tr("CTRL+R"));
connect(mRefreshA, &QAction::triggered, this, &TorrentWidget::gatherData);
mDeleteA = new QAction(QIcon(":/delete.png"), tr("Delete..."), this);
mDeleteA->setShortcut(Qt::Key_Delete);
connect(mDeleteA, &QAction::triggered, this, &TorrentWidget::deleteFiles);
mMoveA = new QAction(QIcon(":/diaper.png"), tr("Move..."), this);
mMoveA->setShortcut(tr("CTRL+M"));
connect(mMoveA, &QAction::triggered, this, &TorrentWidget::moveFiles);
mTorrentInfoA = new QAction(QIcon(":/huge_bra.png"), tr("Torrent Info..."), this);
mTorrentInfoA->setShortcut(Qt::CTRL + Qt::Key_Return);
connect(mTorrentInfoA, &QAction::triggered, this, &TorrentWidget::torrentInfo);
mSelDirA = new QAction(QIcon(":/folder.png"), tr("Select folder..."), this);
mSelDirA->setShortcut(tr("CTRL+O"));
connect(mSelDirA, &QAction::triggered, this, &TorrentWidget::selectDir);
mCopyFnToClipA = new QAction(QIcon(":/edit-copy.png"), tr("Filename to Clip"), this);
mCopyFnToClipA->setShortcut(tr("CTRL+C"));
connect(mCopyFnToClipA, &QAction::triggered, this, &TorrentWidget::copyToClipboard);
mGuessSubtitleA = new QAction(QIcon(":/hourglass_figure.png"), tr("Guess subtitle"), this);
mGuessSubtitleA->setShortcut(tr("CTRL+SHIFT+C"));
connect(mGuessSubtitleA, &QAction::triggered, this, &TorrentWidget::guessSubtitle);
QMenu *fileMenu = new QMenu(tr("&File"));
fileMenu->addAction(mSelDirA);
fileMenu->addAction(mRefreshA);
fileMenu->addSeparator();
fileMenu->addAction(Globals::instance()->action(Globals::QuitAction));
QMenu *editMenu = new QMenu(tr("&Edit"));
editMenu->addAction(mCopyFnToClipA);
editMenu->addAction(mGuessSubtitleA);
editMenu->addAction(mMoveA);
editMenu->addAction(mDeleteA);
editMenu->addSeparator();
editMenu->addAction(mTorrentInfoA);
editMenu->addSeparator();
editMenu->addAction(Globals::instance()->action(Globals::ConfigAction));
mMenuBar->addMenu(fileMenu);
mMenuBar->addMenu(editMenu);
mToolBar->addAction(mRefreshA);
mToolBar->addAction(mSelDirA);
mToolBar->addAction(createSeparator());
mToolBar->addAction(mCopyFnToClipA);
mToolBar->addAction(mGuessSubtitleA);
mToolBar->addAction(mMoveA);
mToolBar->addAction(mDeleteA);
mToolBar->addAction(createSeparator());
mToolBar->addAction(mTorrentInfoA);
mToolBar->addAction(createSeparator());
mToolBar->addAction(Globals::instance()->action(Globals::ConfigAction));
addActions(QList<QAction*>() << mRefreshA << mSelDirA << createSeparator() << mCopyFnToClipA << mGuessSubtitleA << mMoveA << mDeleteA << createSeparator() << mTorrentInfoA);
}
void TorrentWidget::gatherData(){
rememberSelection();
if(isVisible()){
writeHeaderData();
}
QSqlDatabase db = QSqlDatabase::database("shemovdb");
if(!db.isOpen()){
db.open();
}
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);
mProgressBar->reset();
mProgressBar->setMinimum(0);
mProgressBar->setMaximum(fl.count() - 1);
if(fl.count() > 100){
QString msg = QString(tr("<p>Looking at %1 files could take a while</p><p>Do you really want to continue</p>")).arg(QString::number(fl.count()));
int retval = QMessageBox::question(this, tr("Continue"), msg);
if(retval == QMessageBox::No){
return;
}
}
int count = 0;
mFileView->setSortingEnabled(false);
mModel->clear();
QStandardItem *root = mModel->invisibleRootItem();
mModel->setHorizontalHeaderLabels(QStringList() << QChar(0x26A7) << tr("Name") << tr("Created"));
QBrush redBrush(Qt::red);
QBrush greenBrush(Qt::darkGreen);
for(QFileInfo fi : fl){
QList<QStandardItem*> fData;
for(int i = 0; i < ColumnCount; ++i){
QStandardItem *item = new QStandardItem;
item->setEditable(false);
if(i == CreatedColumn){
item->setFont(QFont("courier new"));
}
fData << item;
}
int result = 0;
q.bindValue(":fn", fi.fileName());
q.exec();
while(q.next()){
result = q.value(0).toInt();
}
fData[NameColumn]->setText(fi.fileName());
fData[NameColumn]->setData(fi.absoluteFilePath(), FullPathRole);
fData[CreatedColumn]->setText(fi.birthTime().toString(Qt::RFC2822Date));
fData[CreatedColumn]->setData(fi.birthTime(), CreatedRole);
if(result){
fData[IconColumn]->setIcon(QIcon(":/huge_bra.png"));
fData[IconColumn]->setData(Present, PresentRole);
fData[NameColumn]->setForeground(greenBrush);
}else{
fData[IconColumn]->setIcon(QIcon(":/gaping_ass.png"));
fData[IconColumn]->setData(NotPresent, PresentRole);
fData[NameColumn]->setForeground(redBrush);
}
root->appendRow(fData);
mProgressBar->setValue(++count);
}
qApp->processEvents();
mFileView->setSortingEnabled(true);
readHeaderData();
restoreSelection();
}
void TorrentWidget::deleteFiles(){
QModelIndexList sel = mFileView->selectionModel()->selectedRows(NameColumn);
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);
if(res == QMessageBox::Yes){
int ctr = 0;
QStringList removed;
for(const QModelIndex &i : sel){
QString fp = i.data(FullPathRole).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);
}
removed << i.data().toString();
}
for(const QString& f : removed){
QList<QStandardItem*> res = mModel->findItems(f, Qt::MatchExactly, NameColumn);
if(!res.isEmpty()){
mModel->removeRow(res.at(0)->row());
}
}
QString msg = QString(tr("Successfully deleted %1 of %2 file(s)")).arg(QString::number(ctr)).arg(QString::number(sel.count()));
emit statusMessage(msg);
emit freeSpaceChanged(mDir->text());
}
}
void TorrentWidget::moveFiles(){
QModelIndexList sel = mFileView->selectionModel()->selectedRows(NameColumn);
if(sel.isEmpty()){
return;
}
QSettings s;
QString sDir = s.value("dirs/movedir", QDir::homePath()).toString();
QString dir = QFileDialog::getExistingDirectory(this, tr("Select directory"), sDir);
if(!dir.isEmpty()){
s.setValue("dirs/movedir", dir);
int ctr = 0;
QStringList removed;
for(const QModelIndex &i : sel){
QFileInfo fp(i.data(FullPathRole).toString());
QString newfn = QString("%1/%2").arg(dir).arg(fp.fileName());
bool ok = QFile::rename(fp.absoluteFilePath(), newfn);
if(ok){
++ctr;
}else{
QFileInfo nf(newfn);
if(nf.exists() && nf.isFile()){
//don't care if it's the same file or not...
QFile::remove(fp.absoluteFilePath());
++ctr;
}
}
removed << i.data().toString();
}
for(const QString& f : removed){
QList<QStandardItem*> res = mModel->findItems(f, Qt::MatchExactly, NameColumn);
if(!res.isEmpty()){
mModel->removeRow(res.at(0)->row());
}
}
QString msg = QString(tr("Successfully moved %1 of %2 file(s)")).arg(QString::number(ctr)).arg(QString::number(sel.count()));
emit statusMessage(msg);
emit freeSpaceChanged(mDir->text());
}
}
void TorrentWidget::torrentInfo(){
QModelIndexList sel = mFileView->selectionModel()->selectedRows(NameColumn);
if(sel.isEmpty()){
return;
}
QString fn = sel.first().data(FullPathRole).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 TorrentWidget::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(FullPathRole).toString();
TorrentParser p(fn);
emit statusMessage(QString(tr("parsing %1").arg(fn)));
QList<QVariant> torrentData = p.parse();
QStringList files = p.files(torrentData);
for(QString f : files){
QRegularExpressionMatch m = regex.match(f);
if(m.hasMatch()){
++success;
mFileView->selectionModel()->select(cur, QItemSelectionModel::Select | QItemSelectionModel::Rows);
if(success == 1){
mFileView->scrollTo(cur);
}
}
}
}
emit statusMessage(QString(tr("Found %1 file(s)").arg(QString::number(success))));
}
void TorrentWidget::keyPressEvent(QKeyEvent *e){
if(e->key() == Qt::Key_S && (e->modifiers() & Qt::ControlModifier)){
mSearchTorrents->setFocus();
mSearchTorrents->selectAll();
goto exit;
}
if(e->key() == Qt::Key_D && (e->modifiers() & Qt::ControlModifier)){
mDir->setFocus();
mDir->selectAll();
goto exit;
}
if(e->key() == Qt::Key_F && (e->modifiers() & Qt::ControlModifier)){
mFileView->setFocus();
goto exit;
}
if(mFileView->hasFocus()){
if(e->key() == Qt::Key_Return){
}
}
return QWidget::keyPressEvent(e);
exit:
e->accept();
}
void TorrentWidget::contextMenuEvent(QContextMenuEvent *e){
QMenu contextMenu(this);
contextMenu.addActions(actions());
contextMenu.exec(e->globalPos());
}
void TorrentWidget::selectDir(){
QSettings s;
QString sDir = s.value("dirs/torrentdir", QDir::homePath()).toString();
QString dir = QFileDialog::getExistingDirectory(this, tr("Select directory"), sDir);
if(!dir.isEmpty()){
mDir->setText(QDir::toNativeSeparators(dir));
setDir();
}
}
void TorrentWidget::setDir(){
QSettings s;
s.setValue("dirs/torrentdir", mDir->text());
gatherData();
emit freeSpaceChanged(mDir->text());
}
void TorrentWidget::copyToClipboard(){
QModelIndex cur = mFileView->selectionModel()->currentIndex();
QClipboard *clip = QApplication::clipboard();
clip->setText(cur.data().toString());
}
void TorrentWidget::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));
emit selectionCountChanged(msg);
}
void TorrentWidget::selectFirst(){
if(mModel->rowCount() > 0){
QModelIndex idx = mProxy->index(0, NameColumn);
mFileView->selectionModel()->setCurrentIndex(idx, QItemSelectionModel::Rows);
}
}
void TorrentWidget::guessSubtitle(){
QModelIndex cur = mFileView->selectionModel()->currentIndex();
QClipboard *clip = QApplication::clipboard();
QString curName = cur.data().toString().toLower();
QRegExp re1(" *\\[.*\\] *");
re1.setMinimal(true);
curName.replace(re1, "");
QRegExp re2(" *\\(.*\\) *");
re2.setMinimal(true);
curName.replace(re2, "");
QRegExp re3("\\s+");
curName.replace(re3, " ");
curName.replace(".torrent", "");
curName = curName.trimmed();
QRegExp re4("^shemale\\s*-*");
curName.replace(re4, "");
QRegExp re5("(\\.mp\\d|rq|\\s*720p|\\s*1080p)");
curName.replace(re5, "");
curName = curName.trimmed();
clip->setText(curName);
QString msg = QString(tr("Copied \"%1\" to Clipboard")).arg(curName);
emit statusMessage(msg);
}
void TorrentWidget::readSettings(){
QSettings s;
QString dir = s.value("searchdir", QDir::toNativeSeparators(QDir::homePath())).toString();
mDir->setText(dir);
}
void TorrentWidget::writeSettings(){
QSettings s;
s.setValue("searchdir", mDir->text());
}
void TorrentWidget::rememberSelection(){
QModelIndexList sel = mFileView->selectionModel()->selectedRows(NameColumn);
mSelection.clear();
for(QModelIndex idx : sel){
mSelection << idx.data().toString();
}
}
void TorrentWidget::restoreSelection(){
QList<QStandardItem*> modelItems;
for(QString sel : mSelection){
modelItems << mModel->findItems(sel, Qt::MatchExactly, NameColumn);
}
for(QStandardItem *i : modelItems){
QModelIndex srcIdx = mModel->indexFromItem(i);
QModelIndex idx = mProxy->mapFromSource(srcIdx);
mFileView->selectionModel()->select(idx, QItemSelectionModel::Select | QItemSelectionModel::Rows);
}
}
QAction *TorrentWidget::createSeparator(){
QAction *retval = new QAction(this);
retval->setSeparator(true);
return retval;
}
void TorrentWidget::readHeaderData(){
QSettings s;
QByteArray headers = s.value("fileheaders").toByteArray();
if(!headers.isEmpty()){
mFileView->header()->restoreState(headers);
}
}
void TorrentWidget::writeHeaderData(){
QSettings s;
s.setValue("fileheaders", mFileView->header()->saveState());
}
|