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
|
#include <QLineEdit>
#include <QPushButton>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QStandardItemModel>
#include <QStandardItem>
#include <QTreeView>
#include <QCompleter>
#include <QDirModel>
#include <QDir>
#include <QFileDialog>
#include <QLabel>
#include <QMimeDatabase>
#include <QMimeType>
#include <QFont>
#include <QSqlDatabase>
#include <QSqlQuery>
#include "filewidget.h"
#include "helper.h"
FileWidget::FileWidget(QWidget *parent) : QWidget(parent) {
setupGui();
}
void FileWidget::setupGui(){
mDir = new QLineEdit;
QCompleter *fsCompleter = new QCompleter(this);
QDirModel *dirModel = new QDirModel(this);
fsCompleter->setModel(dirModel);
fsCompleter->setCompletionMode(QCompleter::PopupCompletion);
mDir->setCompleter(fsCompleter);
mSelDir = new QPushButton(tr("Browse..."));
connect(mSelDir, SIGNAL(clicked()), this, SLOT(selectDir()));
QLabel *dirL = new QLabel(tr("&Directory"));
dirL->setBuddy(mDir);
mSearchFile = new QLineEdit;
connect(mSearchFile, SIGNAL(returnPressed()), this, SLOT(searchFile()));
mSearch = new QPushButton(tr("Search"));
connect(mSearch, SIGNAL(clicked()), this, SLOT(searchFile()));
QLabel *searchL = new QLabel(tr("&Search"));
searchL->setBuddy(mSearchFile);
QGridLayout *topL = new QGridLayout;
topL->addWidget(dirL, 0, 0);
topL->addWidget(mDir, 0, 1);
topL->addWidget(mSelDir, 0, 2);
topL->addWidget(searchL, 1, 0);
topL->addWidget(mSearchFile, 1, 1);
topL->addWidget(mSearch, 1, 2);
mFileView = new QTreeView;
mFileView->setSortingEnabled(true);
mFileView->setSelectionBehavior(QAbstractItemView::SelectRows);
mFileView->setSelectionMode(QAbstractItemView::ExtendedSelection);
mModel = new QStandardItemModel;
mFileView->setModel(mModel);
QSqlDatabase mDb = QSqlDatabase::database("shemovdb");
if(!mDb.isOpen()){
mDb.open();
}
mQOrigin = QSqlQuery(mDb);
mQOrigin.prepare("SELECT COUNT(*) FROM files_origin WHERE cmd5sum = :md5");
mQFiles = QSqlQuery(mDb);
mQFiles.prepare("SELECT COUNT(*) FROM files WHERE cmd5sum = :md5");
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(topL);
mainLayout->addWidget(mFileView);
setLayout(mainLayout);
gatherData();
}
void FileWidget::selectDir(){
QString dir = QFileDialog::getExistingDirectory(this, tr("Select directory"), QDir::homePath());
mDir->setText(QDir::toNativeSeparators(dir));
gatherData();
}
void FileWidget::gatherData(){
QDir d(mDir->text());
QFileInfoList fl = d.entryInfoList(QStringList() << "*", QDir::Files, QDir::Name);
mModel->clear();
QStandardItem *root = mModel->invisibleRootItem();
mModel->setHorizontalHeaderLabels(QStringList() << QChar(0x26A7) << tr("Name") << tr("MIME") << tr("MD5"));
QBrush redBrush(Qt::red);
QBrush greenBrush(Qt::darkGreen);
QMimeDatabase db;
foreach(QFileInfo fi, fl){
QMimeType mime = db.mimeTypeForFile(fi);
QStandardItem *i1;
QStandardItem *i2 = new QStandardItem(fi.fileName());
i2->setEditable(false);
QStandardItem *i4;
if(mime.name().startsWith("video")){
i1 = new QStandardItem(QIcon(":/huge_bra.png"), QString());
QString md5 = Helper::md5Sum(fi.absoluteFilePath());
i4 = new QStandardItem(md5);
i4->setFont(QFont("courier new"));
int haveFile = md5Count(md5);
if(haveFile){
i2->setForeground(greenBrush);
}else{
i2->setForeground(redBrush);
}
}else{
i1 = new QStandardItem(QIcon(":/gaping_ass.png"), QString());
i4 = new QStandardItem(tr("n/a"));
i4->setFont(QFont("courier new"));
}
i1->setEditable(false);
QStandardItem *i3 = new QStandardItem(mime.name());
i3->setEditable(false);
root->appendRow(QList<QStandardItem*>() << i1 << i2 << i3 << i4);
}
mFileView->resizeColumnToContents(0);
mFileView->resizeColumnToContents(1);
mFileView->resizeColumnToContents(2);
}
int FileWidget::md5Count(const QString &md5) {
mQOrigin.bindValue(":md5", md5);
mQOrigin.exec();
int retval = 0;
while(mQOrigin.next()){
retval = mQOrigin.value(0).toInt();
}
if(!retval){
mQFiles.bindValue(":md5", md5);
while(mQFiles.next()){
retval = mQFiles.value(0).toInt();
}
}
return retval;
}
void FileWidget::searchFile(){
}
|