summaryrefslogtreecommitdiffstats
path: root/consistencycheck.cpp
blob: 15830933083d1cfd60104e3cb07e0bf6b6650e2d (plain)
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
/*
  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version
  2 of the License, or (at your option) any later version.
*/

#include <QtWidgets/QLabel>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QPlainTextEdit>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QVBoxLayout>
#include <QSqlQuery>
#include <QFileInfo>
#include <QSignalMapper>
#include <QTextCursor>
#include <QMutexLocker>
#include <QMutex>
#include <QTextCharFormat>
#include <QBrush>
#include <QTextBlock>
#include <QtWidgets/QCheckBox>
#include <QSettings>
#include <QDir>
#include <QtWidgets/QFileDialog>
#include <QtWidgets/QMessageBox>
#include <QtWidgets/QProgressBar>
#include <QtWidgets/QGroupBox>

#include "consistencycheck.h"
#include "helper.h"

ConsistencyCheck::ConsistencyCheck(QWidget *parent, Qt::WindowFlags f) : SmDialog(parent, f), mChecker(0){
	// setup widget
    // OK
    QGroupBox *okBox = new QGroupBox(tr("Ok"));
    QHBoxLayout *okLayout = new QHBoxLayout;
	mOkDisplay = new QPlainTextEdit;
	mOkDisplay->setReadOnly(true);
	mOkDisplay->setLineWrapMode(QPlainTextEdit::NoWrap);
    okLayout->addWidget(mOkDisplay);
    okBox->setLayout(okLayout);

    // Errors
    QGroupBox *errorBox = new QGroupBox(tr("Errors"));
    QHBoxLayout *errorLayout = new QHBoxLayout;
	mErrorDisplay = new QPlainTextEdit;
	mErrorDisplay->setReadOnly(true);
	mErrorDisplay->setLineWrapMode(QPlainTextEdit::NoWrap);
    errorLayout->addWidget(mErrorDisplay);
    errorBox->setLayout(errorLayout);

    // Progress
    QGroupBox *progressBox = new QGroupBox(tr("Progress"));
    QVBoxLayout *progressLayout = new QVBoxLayout;
    mProgress = new QProgressBar();
    progressLayout->addWidget(mProgress);
    mActivity = new QLabel(tr("Idle..."));
    progressLayout->addWidget(mActivity);
    progressBox->setLayout(progressLayout);

    // Buttons
	mCancelExit = new QPushButton(tr("Close"));
	mCheckDb = new QPushButton(tr("Check database"));
	mCheckFs = new QPushButton(tr("Check Filesystem"));
	mCleanup = new QPushButton(tr("Cleanup..."));
	connect(mCleanup, SIGNAL(clicked()), this, SLOT(cleanup()));
	mCleanup->setEnabled(false);
	QHBoxLayout *buttonLayout = new QHBoxLayout;
	buttonLayout->addWidget(mCheckDb);
	buttonLayout->addWidget(mCheckFs);
	buttonLayout->addWidget(mCleanup);
	buttonLayout->addStretch();
	buttonLayout->addWidget(mCancelExit);
	connect(mCancelExit, SIGNAL(clicked()), this, SLOT(accept()));

	// signal mapper
	QSignalMapper *mapper = new QSignalMapper(this);
	connect(mCheckDb, SIGNAL(clicked()), mapper, SLOT(map()));
	mapper->setMapping(mCheckDb, ConsistencyCheck::DbCheck);
	connect(mCheckFs, SIGNAL(clicked()), mapper, SLOT(map()));
	mapper->setMapping(mCheckFs, ConsistencyCheck::FsCheck);
	connect(mapper, SIGNAL(mapped(int)), this, SLOT(startChecker(int)));

	//misc
	mChecker = new ConsistencyChecker(this);
	connect(mChecker, SIGNAL(started()), this, SLOT(checkerStarted()));
	connect(mChecker, SIGNAL(finished()), this, SLOT(checkerFinished()));
	connect(mChecker, SIGNAL(consistencyMsg(QString)), this, SLOT(addMessage(QString)));
    connect(mChecker, SIGNAL(approxTotal(int)), this, SLOT(setProgressBarMax(int)));
    connect(mChecker, SIGNAL(progress(int)), this, SLOT(setProgress(int)));

	// main layout
	QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(okBox);
    mainLayout->addWidget(errorBox);
    mainLayout->addWidget(progressBox);
	mainLayout->addLayout(buttonLayout);
	setWindowTitle(tr("Checking consistency"));
	setLayout(mainLayout);
	setMinimumWidth(600);
}

ConsistencyCheck::~ConsistencyCheck(){
    mChecker->deleteLater();
}

void ConsistencyCheck::setProgressBarMax(int max){
    mProgress->reset();
    mProgress->setMaximum(max);
    QString activity = QString(tr("Expecting %1 files")).arg(QString::number(max));
    mActivity->setText(activity);
}

void ConsistencyCheck::setProgress(int value){
    if(value >= mProgress->maximum()){
        mProgress->setMaximum(value + 100);
        QString activity = QString(tr("Just got pregnant. Expecting %1 now.")).arg(QString::number(value + 100));
        mActivity->setText(activity);
    }
    mProgress->setValue(value);
}

void ConsistencyCheck::startChecker(int checkerType){
	if(mChecker->status() == ConsistencyChecker::Fail){
		return;
	}
	mOkDisplay->clear();
	mErrorDisplay->clear();
	switch(checkerType){
		case DbCheck:
			mChecker->setMode(ConsistencyCheck::DbCheck);
			break;
		case FsCheck:
			mChecker->setMode(ConsistencyCheck::FsCheck);
			;
			break;
		default:
			;
	}
	mChecker->check();
}

void ConsistencyCheck::addMessage(const QString &message){
	QTextCursor cursor;
	QTextCharFormat fmt;
	if(message.startsWith("OK")){
		cursor = QTextCursor(mOkDisplay->textCursor());
		fmt.setForeground(QBrush(Qt::green));
	}else{
		cursor = QTextCursor(mErrorDisplay->textCursor());
		fmt.setForeground(QBrush(Qt::red));
	}
	QTextBlock curBlock = cursor.block();
	if(!curBlock.text().isEmpty()){
		cursor.insertBlock();
	}
	cursor.setCharFormat(fmt);
	cursor.insertText(message);
}

void ConsistencyCheck::checkerStarted(){
	mCheckDb->setEnabled(false);
	mCheckFs->setEnabled(false);
	mCancelExit->setText(tr("Cancel"));
	mCancelExit->disconnect();
	connect(mCancelExit, SIGNAL(clicked()), this, SLOT(cancelChecker()));
}

void ConsistencyCheck::checkerFinished(){
    mProgress->setValue(mProgress->maximum());
    mActivity->setText(tr("Idle..."));
	mCheckDb->setEnabled(true);
	mCheckFs->setEnabled(true);
	mCancelExit->setText(tr("Close"));
	mCancelExit->disconnect();
	connect(mCancelExit, SIGNAL(clicked()), this, SLOT(accept()));
	mChecker->setCancel(false);
    if(!mChecker->strayFiles().isEmpty() || !mChecker->strayFileIds().isEmpty()){
		mCleanup->setEnabled(true);
	}
}

void ConsistencyCheck::cancelChecker(){
	mChecker->setCancel(true);
}

void ConsistencyCheck::cleanup(){
	QStringList strayFiles = mChecker->strayFiles();
	moveFiles(strayFiles);
	if(mChecker->mode() == DbCheck){
        QList<int> fileIds = mChecker->strayFileIds();
		if(!fileIds.isEmpty()){
            QString msg = QString(tr("Really delete %1 files from database?")).arg(QString::number(fileIds.count()));
			int retval = QMessageBox::critical(this, tr("Delete from database"), msg, QMessageBox::Yes | QMessageBox::No);
			if(retval == QMessageBox::Yes){
                mChecker->deleteFileIds(fileIds);
			}
		}
        QList<int> picIds = mChecker->strayPicIds();
        if(!picIds.isEmpty()){
            QString msg = QString(tr("Really delete %1 pictures from database?")).arg(QString::number(picIds.count()));
            int retval = QMessageBox::critical(this, tr("Delete from database"), msg, QMessageBox::Yes | QMessageBox::No);
            if(retval == QMessageBox::Yes){
                //mChecker->deleteIds(fileIds);
            }
        }
	}
	mCleanup->setEnabled(false);
}

void ConsistencyCheck::moveFiles(const QStringList &files){
	if(files.isEmpty()){
		return;
	}
	QSettings s;
	QString startDir = s.value("paths/selecteddir").toString();
	QString targetDir = QFileDialog::getExistingDirectory(this, tr("Move stray files to..."), startDir);
	if(!targetDir.isEmpty()){
		foreach(QString file, files){
			QFileInfo fi(file);
			QString tgt = QString("%1%2%3").arg(targetDir).arg(QDir::separator()).arg(fi.fileName());
			QFileInfo tfi(tgt);
			if(tfi.exists()){
				QString msg = QString(tr("Target %1 exists. Overwrite?")).arg(tfi.absoluteFilePath());
				int retval = QMessageBox::critical(this, tr("File exists"), msg, QMessageBox::Yes | QMessageBox::No);
				if(retval == QMessageBox::No){
					continue;
				}
			}
			QFile::rename(file, tgt);
		}
	}
}

ConsistencyChecker::ConsistencyChecker(QObject *parent) : QThread(parent), mCanceled(false), mMode(-1), mStatus(Ok){
	mDb = QSqlDatabase::cloneDatabase(QSqlDatabase::database("treedb"), "checkerDb");
	if(!mDb.open()){
		mStatus = Fail;
	}
	mFileQuery = new QSqlQuery(mDb);
	mFileQuery->prepare("SELECT COUNT(*) FROM files where cmd5sum = :md5");
}

ConsistencyChecker::~ConsistencyChecker(){
	delete mFileQuery;
	mDb.close();
}

void ConsistencyChecker::check(){
	if(!isRunning()){
		start();
	}
}

const QStringList ConsistencyChecker::strayFiles(){
	QMutexLocker locker(&mStrayFilesMutex);
	return mStrayFiles;
}

void ConsistencyChecker::clearStrayFiles(){
	QMutexLocker locker(&mStrayFilesMutex);
	mStrayFiles.clear();
}

const QList<int> ConsistencyChecker::strayFileIds(){
    QMutexLocker locker(&mStrayFileIdMutex);
    return mStrayFileIds;
}

const QList<int> ConsistencyChecker::strayPicIds(){
    QMutexLocker locker(&mStrayPicsIdMutex);
    return mStrayPicIds;
}

void ConsistencyChecker::clearStrayIds(){
    QMutexLocker locker(&mStrayFileIdMutex);
    mStrayFileIds.clear();
}

void ConsistencyChecker::setCancel(bool cancel){
	QMutexLocker locker(&mCancelMutex);
	mCanceled = cancel;
}

void ConsistencyChecker::deleteFileIds(const QList<int> &ids){
	QSqlQuery idQuery(mDb);
	idQuery.prepare("DELETE FROM files WHERE ifiles_id = :id");
	foreach(int id, ids){
		idQuery.bindValue(":id", id);
		idQuery.exec();
	}
}

void ConsistencyChecker::deletePicIds(const QList<int> &ids){
    QSqlQuery idQuery(mDb);
    idQuery.prepare("DELETE FROM pics WHERE ipicsid = :id");
    foreach(int id, ids){
        idQuery.bindValue(":id", id);
        idQuery.exec();
    }
}

void ConsistencyChecker::run(){
	clearStrayFiles();
	clearStrayIds();
	if(mMode == ConsistencyCheck::FsCheck){
		fsCheck();
	}
	if(mMode == ConsistencyCheck::DbCheck){
        dbCheck();
	}
}

void ConsistencyChecker::doDbCheckFiles(){
	QSqlQuery fileDbQuery = QSqlQuery("SELECT tfilename, cmd5sum, idvd, ifiles_id FROM files ORDER BY tfilename", mDb);
	while(fileDbQuery.next()){
        ++mCurCount;
        if(mCurCount % 50 == 0){
            emit progress(mCurCount);
        }
        QMutexLocker locker(&mCancelMutex);
		if(mCanceled){
			quit();
		}else{
			locker.unlock();
		}
		QString fileName = fileDbQuery.value(0).toString();
		QString md5sum = fileDbQuery.value(1).toString();
		int dvdNo = fileDbQuery.value(2).toInt();
		int fileId = fileDbQuery.value(3).toInt();
		QString fullPath = archivePath(fileName, md5sum);
		if(fullPath.isEmpty()){
			if(dvdNo != -1){
				emit consistencyMsg(QString(tr("OK: %1")).arg(fileName));
			}else{
                mStrayFileIdMutex.lock();
                mStrayFileIds << fileId;
                mStrayFileIdMutex.unlock();
				emit consistencyMsg(QString(tr("NOT FOUND: %1")).arg(fileName));
			}
		}else{
			if(dvdNo == -1){
				emit consistencyMsg(QString(tr("OK: %1")).arg(fileName));
			}else{
				QString mimeType = Helper::mimeType(fullPath);
				if(mimeType.startsWith("image")){
					emit consistencyMsg(QString(tr("OK: %1")).arg(fileName));
				}else{
					mStrayFilesMutex.lock();
					mStrayFiles << fullPath;
					mStrayFilesMutex.unlock();
					QString msg = QString(tr("FOUND: %1 (DVD: %2)")).arg(fullPath).arg(dvdNo);
					emit consistencyMsg(msg);
				}
			}
		}
	}
	quit();
}

void ConsistencyChecker::doDbCheckPics(){
    QSqlQuery picsQuery = QSqlQuery("SELECT tfilename, cmd5sum, ipicsid FROM pics ORDER BY tfilename", mDb);
    while(picsQuery.next()){
        ++mCurCount;
        if(mCurCount % 50 == 0){
            emit progress(mCurCount);
        }
        QMutexLocker locker(&mCancelMutex);
        if(mCanceled){
            quit();
        }else{
            locker.unlock();
        }
        QString fileName = picsQuery.value(0).toString();
        QString md5sum = picsQuery.value(1).toString();
        QString fullPath = archivePath(fileName, md5sum);
        if(fullPath.isEmpty()){
            mStrayPicsIdMutex.lock();
            mStrayPicIds << picsQuery.value(2).toInt();
            mStrayPicsIdMutex.unlock();
            emit consistencyMsg(QString(tr("NOT FOUND: %1")).arg(fileName));
        }else{
            emit consistencyMsg(QString(tr("OK: %1")).arg(fileName));
        }
    }
}

void ConsistencyChecker::dbCheck(){
    QSqlQuery numFilesQ("SELECT COUNT(*) FROM files", mDb);
    int numFiles;
    while(numFilesQ.next()){
        numFiles = numFilesQ.value(0).toInt();
    }
    QSqlQuery numPicsQ("SELECT COUNT(*) FROM pics", mDb);
    int numPics;
    while(numPicsQ.next()){
        numPics = numPicsQ.value(0).toInt();
    }
    emit approxTotal(numFiles + numPics);
    mCurCount = 0;
    doDbCheckFiles();
    doDbCheckPics();
    quit();
}

void ConsistencyChecker::fsCheck(){
	mStrayFilesMutex.lock();
	mStrayFiles.clear();
	mStrayFilesMutex.unlock();
    QVector<QString> md5sums;
    QSqlQuery picsmd5("SELECT cmd5sum FROM pics", mDb);
    while(picsmd5.next()){
        md5sums << picsmd5.value(0).toString();
    }
    QSqlQuery filesmd5("SELECT cmd5sum FROM files", mDb);
    while(filesmd5.next()){
        md5sums << filesmd5.value(0).toString();
    }
    emit approxTotal(md5sums.size());
    mCurCount = 0;
	QSettings s;
	QString startDir = s.value("paths/archivedir").toString();
	QFileInfo startFi(startDir);
    doFsCheck(startFi, md5sums);
	quit();
}

void ConsistencyChecker::doFsCheck(const QFileInfo &start, const QVector<QString> &md5sums){
	if(start.isDir()){
		QDir curDir(start.absoluteFilePath());
		foreach(QFileInfo fi, curDir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot)){
            mCancelMutex.lock();
            if(mCanceled){
                mCancelMutex.unlock();
                return;
            }
            mCancelMutex.unlock();
			if(fi.isDir()){
                doFsCheck(fi, md5sums);
            }else{
				QString md5sum = Helper::md5Sum(fi.absoluteFilePath());
                if(md5sums.contains(md5sum)){
                    emit consistencyMsg(QString(tr("OK: %1 -> %2")).arg(fi.fileName()).arg(md5sum));
                }else{
                    mStrayFilesMutex.lock();
                    mStrayFiles << fi.absoluteFilePath();
                    mStrayFilesMutex.unlock();
                    emit consistencyMsg(QString(tr("NOT FOUND: %1 -> %2")).arg(fi.fileName()).arg(md5sum));
                }
                ++mCurCount;
                if(mCurCount % 50 == 0){
                    emit progress(mCurCount);
                }
			}
		}
	}
}

QString ConsistencyChecker::archivePath(const QString &fileName, const QString &md5sum) const {
	QString filePath = Helper::createArchivePath(fileName, md5sum);
	QFileInfo fi(filePath);
	if(!fi.exists()){
		filePath = Helper::createArchivePath(fileName, md5sum, true);
		fi = QFileInfo(filePath);
		if(!fi.exists()){
			filePath.clear();
		}
	}
	return filePath;
}