ファイルからMD5計算
Revision | 2b6d750c1137e78974bb18ef8b8ca1cbf0744add (tree) |
---|---|
Zeit | 2013-12-15 09:50:45 |
Autor | alucky4416 <alucky4416@user...> |
Commiter | alucky4416 |
CHG: CalcMD5 Change to QThread.
@@ -11,8 +11,10 @@ TEMPLATE = app | ||
11 | 11 | |
12 | 12 | |
13 | 13 | SOURCES += main.cpp\ |
14 | - mainwindow.cpp | |
14 | + mainwindow.cpp \ | |
15 | + thcalcmd5.cpp | |
15 | 16 | |
16 | -HEADERS += mainwindow.h | |
17 | +HEADERS += mainwindow.h \ | |
18 | + thcalcmd5.h | |
17 | 19 | |
18 | 20 | FORMS += mainwindow.ui |
@@ -12,6 +12,7 @@ | ||
12 | 12 | #include "mainwindow.h" |
13 | 13 | #include "ui_mainwindow.h" |
14 | 14 | |
15 | +#if 0 | |
15 | 16 | QString CalcMD5(QString filepath) |
16 | 17 | { |
17 | 18 | QString ret = ""; |
@@ -34,6 +35,7 @@ QString CalcMD5(QString filepath) | ||
34 | 35 | } |
35 | 36 | return ret; |
36 | 37 | } |
38 | +#endif | |
37 | 39 | |
38 | 40 | MainWindow::MainWindow(QWidget *parent) : |
39 | 41 | QMainWindow(parent), |
@@ -46,10 +48,22 @@ MainWindow::MainWindow(QWidget *parent) : | ||
46 | 48 | firstShow = true; |
47 | 49 | |
48 | 50 | setAcceptDrops(true); // available Drop file |
51 | + | |
52 | + thcalcmd5 = new ThCalcMD5(this); | |
53 | + connect(thcalcmd5, SIGNAL(notify_calc_end(QString)), | |
54 | + this, SLOT(slot_calc_end(QString))); | |
55 | + connect(thcalcmd5, SIGNAL(notify_calc_progress(int)), | |
56 | + this, SLOT(slot_calc_progress(int))); | |
57 | + thcalcmd5->start(); | |
58 | + | |
49 | 59 | } |
50 | 60 | |
51 | 61 | MainWindow::~MainWindow() |
52 | 62 | { |
63 | + thcalcmd5->stop(); | |
64 | + thcalcmd5->wait(); | |
65 | + delete thcalcmd5; | |
66 | + | |
53 | 67 | delete ui; |
54 | 68 | } |
55 | 69 |
@@ -125,7 +139,6 @@ void MainWindow::on_toolButton_Select_clicked() | ||
125 | 139 | } else { |
126 | 140 | ; |
127 | 141 | } |
128 | - ui->progressBar->setValue(100); | |
129 | 142 | } |
130 | 143 | } |
131 | 144 |
@@ -134,6 +147,13 @@ void MainWindow::on_pushButton_Start_clicked() | ||
134 | 147 | // QMessageBox::information(this, tr("information"), tr("StartButton clicked.")); |
135 | 148 | if (ui->lineEdit_FilePath->text().isEmpty()) return; |
136 | 149 | |
150 | + ui->lineEdit_ResultMD5->clear(); | |
151 | + ui->progressBar->setValue(0); | |
152 | + ui->pushButton_Abort->setEnabled(true); | |
153 | + | |
154 | + thcalcmd5->sendEvent_Start(ui->lineEdit_FilePath->text()); | |
155 | + | |
156 | +#if 0 | |
137 | 157 | QString result; |
138 | 158 | result = CalcMD5(ui->lineEdit_FilePath->text()); |
139 | 159 | if (result.isEmpty()) { |
@@ -143,11 +163,13 @@ void MainWindow::on_pushButton_Start_clicked() | ||
143 | 163 | ui->lineEdit_ResultMD5->setText(result); |
144 | 164 | on_lineEdit_VerifyMD5_textChanged(); |
145 | 165 | } |
166 | +#endif | |
146 | 167 | } |
147 | 168 | |
148 | 169 | void MainWindow::on_pushButton_Abort_clicked() |
149 | 170 | { |
150 | - QMessageBox::information(this, tr("information"), tr("AbortButton clicked.")); | |
171 | +// QMessageBox::information(this, tr("information"), tr("AbortButton clicked.")); | |
172 | + thcalcmd5->sendEvent_Stop(); | |
151 | 173 | } |
152 | 174 | |
153 | 175 | void MainWindow::on_lineEdit_VerifyMD5_textChanged() |
@@ -164,3 +186,21 @@ void MainWindow::on_lineEdit_VerifyMD5_textChanged() | ||
164 | 186 | } |
165 | 187 | |
166 | 188 | } |
189 | + | |
190 | +void MainWindow::slot_calc_end(QString result) | |
191 | +{ | |
192 | + if (result.isEmpty()) { | |
193 | + ; | |
194 | + } else { | |
195 | + ui->lineEdit_ResultMD5->clear(); | |
196 | + ui->lineEdit_ResultMD5->setText(result); | |
197 | + | |
198 | + on_lineEdit_VerifyMD5_textChanged(); | |
199 | + } | |
200 | + ui->pushButton_Abort->setEnabled(false); | |
201 | +} | |
202 | + | |
203 | +void MainWindow::slot_calc_progress(int progress) | |
204 | +{ | |
205 | + ui->progressBar->setValue(progress); | |
206 | +} |
@@ -1,6 +1,8 @@ | ||
1 | 1 | #ifndef MAINWINDOW_H |
2 | 2 | #define MAINWINDOW_H |
3 | 3 | |
4 | +#include "thcalcmd5.h" | |
5 | + | |
4 | 6 | #include <QMainWindow> |
5 | 7 | |
6 | 8 | namespace Ui { |
@@ -22,6 +24,9 @@ private slots: | ||
22 | 24 | void dragEnterEvent(QDragEnterEvent *event); |
23 | 25 | void dropEvent(QDropEvent *event); |
24 | 26 | |
27 | + void slot_calc_end(QString); | |
28 | + void slot_calc_progress(int); | |
29 | + | |
25 | 30 | void on_actionExit_triggered(); |
26 | 31 | |
27 | 32 | void on_actionAbout_triggered(); |
@@ -40,6 +45,8 @@ private: | ||
40 | 45 | Ui::MainWindow *ui; |
41 | 46 | |
42 | 47 | bool firstShow; |
48 | + | |
49 | + ThCalcMD5 *thcalcmd5; | |
43 | 50 | }; |
44 | 51 | |
45 | 52 | #endif // MAINWINDOW_H |
@@ -10,6 +10,18 @@ | ||
10 | 10 | <height>331</height> |
11 | 11 | </rect> |
12 | 12 | </property> |
13 | + <property name="minimumSize"> | |
14 | + <size> | |
15 | + <width>403</width> | |
16 | + <height>331</height> | |
17 | + </size> | |
18 | + </property> | |
19 | + <property name="maximumSize"> | |
20 | + <size> | |
21 | + <width>403</width> | |
22 | + <height>331</height> | |
23 | + </size> | |
24 | + </property> | |
13 | 25 | <property name="windowTitle"> |
14 | 26 | <string>QtCalcMD5</string> |
15 | 27 | </property> |
@@ -0,0 +1,106 @@ | ||
1 | +#include "thcalcmd5.h" | |
2 | + | |
3 | +#include <QDebug> | |
4 | + | |
5 | +#include <QFile> | |
6 | +#include <QCryptographicHash> | |
7 | + | |
8 | +ThCalcMD5::ThCalcMD5(QObject *parent) : | |
9 | + QThread(parent) | |
10 | +{ | |
11 | + stopped = false; | |
12 | + | |
13 | + EvtQue = new QQueue<EventData>(); | |
14 | +} | |
15 | + | |
16 | +ThCalcMD5::~ThCalcMD5() | |
17 | +{ | |
18 | + delete EvtQue; | |
19 | +} | |
20 | + | |
21 | +void ThCalcMD5::stop() | |
22 | +{ | |
23 | + stopped = true; | |
24 | +} | |
25 | + | |
26 | +void ThCalcMD5::run() | |
27 | +{ | |
28 | + EventData event; | |
29 | + int event_id; | |
30 | + int status = 0; // 0 = idle, 1 = exec | |
31 | + | |
32 | + QString result = ""; | |
33 | + QCryptographicHash hash(QCryptographicHash::Md5); | |
34 | + | |
35 | + QFile in; | |
36 | + char buf[8192]; | |
37 | + int bytesRead; | |
38 | + quint64 filesize = 0; | |
39 | + int totalread = 0; | |
40 | + | |
41 | + while (!stopped) { | |
42 | + if (EvtQue->isEmpty()) { | |
43 | + if (status == 1) { | |
44 | + if ((bytesRead = in.read(buf, sizeof(buf))) > 0) { | |
45 | + hash.addData(buf, bytesRead); | |
46 | + totalread += bytesRead; | |
47 | +// qDebug() << "Progress = " << (((double)totalread / (double)filesize) * 100.0); | |
48 | + emit notify_calc_progress(((double)totalread / (double)filesize) * 100.0); | |
49 | + } else { | |
50 | + result = hash.result().toHex(); | |
51 | + emit notify_calc_progress(100); | |
52 | + emit notify_calc_end(result); | |
53 | + in.close(); | |
54 | + status = 0; | |
55 | + } | |
56 | + } else { | |
57 | + msleep(100); | |
58 | + } | |
59 | + } else { | |
60 | + event = EvtQue->dequeue(); | |
61 | + event_id = event.id; | |
62 | + switch(event_id) { | |
63 | + case Ev_Start: | |
64 | + if (status == 0) { | |
65 | + in.setFileName(event.param); | |
66 | + if (!in.open(QIODevice::ReadOnly)) { | |
67 | + emit notify_calc_end(""); | |
68 | + return; | |
69 | + } | |
70 | + filesize = in.size(); | |
71 | + totalread = 0; | |
72 | + status = 1; | |
73 | + } | |
74 | + break; | |
75 | + case Ev_Stop: | |
76 | + if (status == 1) { | |
77 | + in.close(); | |
78 | + emit notify_calc_end(""); | |
79 | + status = 0; | |
80 | + } | |
81 | + break; | |
82 | + default: | |
83 | + break; | |
84 | + } | |
85 | + } | |
86 | + } | |
87 | + | |
88 | +} | |
89 | + | |
90 | +void ThCalcMD5::sendEvent_Start(QString filepath) | |
91 | +{ | |
92 | + EventData evt; | |
93 | + | |
94 | + evt.id = Ev_Start; | |
95 | + evt.param = filepath; | |
96 | + EvtQue->enqueue(evt); | |
97 | +} | |
98 | + | |
99 | +void ThCalcMD5::sendEvent_Stop() | |
100 | +{ | |
101 | + EventData evt; | |
102 | + | |
103 | + evt.id = Ev_Stop; | |
104 | + EvtQue->enqueue(evt); | |
105 | +} | |
106 | + |
@@ -0,0 +1,46 @@ | ||
1 | +#ifndef THCALCMD5_H | |
2 | +#define THCALCMD5_H | |
3 | + | |
4 | +#include <QThread> | |
5 | +#include <QQueue> | |
6 | + | |
7 | +typedef struct StEventData { | |
8 | + int id; // Event ID | |
9 | + QString param; // param data, LCD output string | |
10 | +} EventData; | |
11 | + | |
12 | +class ThCalcMD5 : public QThread | |
13 | +{ | |
14 | + Q_OBJECT | |
15 | +public: | |
16 | + explicit ThCalcMD5(QObject *parent = 0); | |
17 | + ~ThCalcMD5(); | |
18 | + void stop(); | |
19 | + | |
20 | + void sendEvent_Start(QString filepath); | |
21 | + void sendEvent_Stop(); | |
22 | + | |
23 | +signals: | |
24 | + void notify_calc_progress(int progress); | |
25 | + void notify_calc_end(QString md5); | |
26 | + | |
27 | +public slots: | |
28 | + | |
29 | +protected: | |
30 | + void run(); | |
31 | + | |
32 | +private: | |
33 | + volatile bool stopped; | |
34 | + | |
35 | + // Event ID | |
36 | + enum { | |
37 | + Ev_Nop = 0, | |
38 | + Ev_Start, | |
39 | + Ev_Stop | |
40 | + }; | |
41 | + QQueue<EventData> *EvtQue; // EventQueue | |
42 | + | |
43 | + | |
44 | +}; | |
45 | + | |
46 | +#endif // THCALCMD5_H |