Menu

[r39]: / trunk / mainwindow.cpp  Maximize  Restore  History

Download this file

235 lines (210 with data), 7.8 kB

  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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#ifdef Q_OS_WIN
QString GetWinVer() {
// http://web3.codeproject.com/Messages/4823136/Re-Another-Way.aspx
NTSTATUS (WINAPI *RtlGetVersion)(LPOSVERSIONINFOEXW);
OSVERSIONINFOEXW osInfo;
*(FARPROC*)&RtlGetVersion = GetProcAddress(GetModuleHandleA("ntdll"), "RtlGetVersion");
QString result;
if (NULL != RtlGetVersion) {
osInfo.dwOSVersionInfoSize = sizeof(osInfo);
RtlGetVersion(&osInfo);
result = "Windows ";
if(osInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) result += "NT ";
result += QString::number(osInfo.dwMajorVersion) + "." + QString::number(osInfo.dwMinorVersion);
}
return result;
}
#endif
QString GetOS() {
QString OS;
#ifdef Q_OS_LINUX
OS = "X11; Linux";
#endif
#ifdef Q_OS_FREEBSD
OS = "X11; FreeBSD";
#endif
#ifdef Q_OS_WIN
OS = GetWinVer();
#endif
#ifdef Q_OS_MAC
OS = "Macintosh; Mac OSX";
#endif
return OS;
}
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
this->setWindowTitle(APPNAME);
this->setAcceptDrops(true);
ui->checkEncode->setChecked(true);
ui->btnGoFile->setText("Encode");
ui->checkEncodeText->setChecked(true);
ui->btnGoText->setText("Encode");
QTimer::singleShot(200, this, SLOT(CheckUpdates()));
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::UpdateProgress(qint64 Position, qint64 Size) {
QString pos = QString::number(Position);
QString size = QString::number(Size);
QString msg = "["+ QString::number(CurrentIndex) + "/" + QString::number(FileCount) + "] Processing " + pos + " of " + size + " bytes";
ui->statusBar->showMessage(msg);
}
void MainWindow::FileComplete(int index, int count) {
CurrentIndex = index;
FileCount = count;
}
void MainWindow::OperationComplete() {
ui->statusBar->showMessage("Done");
SetControlsActive(true);
if(ui->btnGoFile->text() == "Stop") {
ui->btnGoFile->setEnabled(true);
if(ui->checkEncode->isChecked()) {
ui->btnGoFile->setText("Encode");
}
else {
ui->btnGoFile->setText("Decode");
}
}
}
void MainWindow::on_btnGoFile_clicked(){
if(ui->btnGoFile->text() == "Stop") {
worker->DoStop();
ui->btnGoFile->setEnabled(false);
return;
}
SetControlsActive(false);
ui->btnGoFile->setText("Stop");
QThread* thread = new QThread;
//WorkThread* worker = new WorkThread();
worker = new WorkThread();
CurrentIndex = 1;
FileCount = 0;
int i;
for(i = 0; i < ui->listFiles->count(); i++) {
QListWidgetItem *item = ui->listFiles->item(i);
QFileInfo fi(item->text());
if(fi.exists()) {
worker->AddFile(item->text());
FileCount++;
}
}
if(ui->checkEncode->isChecked()) {
worker->SetMode(wmEncode);
}
else {
worker->SetMode(wmDecode);
}
worker->SetOutputPath(ui->textOutputDirectory->text());
worker->moveToThread(thread);
connect(thread, SIGNAL(started()), worker, SLOT(Process()));
connect(worker, SIGNAL(Finished()), thread, SLOT(quit()));
connect(worker, SIGNAL(Finished()), worker, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
connect(worker, SIGNAL(UpdateProgress(qint64,qint64)), this, SLOT(UpdateProgress(qint64,qint64)));
connect(worker, SIGNAL(FileComplete(int,int)), this, SLOT(FileComplete(int,int)));
connect(worker, SIGNAL(Finished()), this, SLOT(OperationComplete()));
thread->start();
}
void MainWindow::on_checkEncode_toggled(bool checked) {
if(checked) {
ui->btnGoFile->setText("Encode");
}
}
void MainWindow::on_checkDecode_toggled(bool checked) {
if(checked) {
ui->btnGoFile->setText("Decode");
}
}
void MainWindow::on_btnAddFile_clicked() {
QStringList filenames = QFileDialog::getOpenFileNames(this, "Select files", "", "*.*");
if(filenames.isEmpty()) return;
ui->listFiles->addItems(filenames);
}
void MainWindow::on_btnDeleteFile_clicked() {
qDeleteAll(ui->listFiles->selectedItems());
}
void MainWindow::on_btnClearList_clicked() {
ui->listFiles->clear();
}
void MainWindow::on_btnPickDir_clicked() {
QString outputdir = QFileDialog::getExistingDirectory(this, "Select directory");
if(outputdir.isEmpty()) return;
ui->textOutputDirectory->setText(outputdir);
}
void MainWindow::on_checkEncodeText_toggled(bool checked) {
if(checked) {
ui->btnGoText->setText("Encode");
}
}
void MainWindow::on_checkDecodeText_toggled(bool checked) {
if(checked) {
ui->btnGoText->setText("Decode");
}
}
void MainWindow::on_btnGoText_clicked() {
QByteArray ba;
ba.append(ui->textInput->toPlainText());
if(ui->checkEncodeText->isChecked()) {
ui->textOutput->setPlainText(ba.toBase64());
}
else {
ui->textOutput->setPlainText(ba.fromBase64(ba));
}
}
void MainWindow::on_btnClearText_clicked() {
ui->textInput->clear();
ui->textOutput->clear();
}
void MainWindow::SetControlsActive(bool m) {
//ui->btnGoFile->setEnabled(m);
ui->btnAddFile->setEnabled(m);
ui->btnClearList->setEnabled(m);
ui->btnDeleteFile->setEnabled(m);
ui->btnPickDir->setEnabled(m);
ui->textOutputDirectory->setEnabled(m);
ui->checkDecode->setEnabled(m);
ui->checkEncode->setEnabled(m);
}
void MainWindow::on_btnAbout_clicked() {
QString html;
html = "<p><b style=\"font-size: 14pt\">"+QString(APPNAME)+"</b> "+QString(APPVER)+"<br>\n";
html.append("&copy;2016, 2017 <a href="/?originalUrl=https%3A%2F%2Fsourceforge.net%2F%253C%2Fspan">\"https://www.matthewhipkin.co.uk\" style=\"color: #FF0000\">Matthew Hipkin</a><br>\n");
html.append("<p>A base64 file encoder/decoder.</p>");
html.append("<p><a href="/?originalUrl=https%3A%2F%2Fsourceforge.net%2F%253C%2Fspan">\"https://twitter.com/hippy2094\"><img src="/?originalUrl=https%3A%2F%2Fsourceforge.net%2F%253C%2Fspan">\":/images/logo_twitter_25px.png\"></a> <a href="/?originalUrl=https%3A%2F%2Fsourceforge.net%2F%253C%2Fspan">\"https://sourceforge.net/projects/base64-binary/\"><img src="/?originalUrl=https%3A%2F%2Fsourceforge.net%2F%253C%2Fspan">\":/images/sourceforge_logo_small.png\"></a></p>");
QMessageBox::about(this,"About "+QString(APPNAME),html);
}
void MainWindow::CheckUpdates() {
QString ua;
ua = "Mozilla/5.0 (compatible; "+GetOS()+"; "+APPNAME+" "+APPVER+" ("+QString::number(CURRVER)+"))";
QNetworkAccessManager nam;
QUrl url("http://www.matthewhipkin.co.uk/bin64.txt");
QNetworkRequest req(url);
req.setRawHeader("User-Agent",QByteArray(ua.toStdString().c_str()));
QNetworkReply* reply = nam.get(req);
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
QByteArray bytes = reply->readAll();
QString t = QString::fromUtf8(bytes);
if(t.trimmed().toInt() > CURRVER) {
labelUpdate = new QLabel(this);
labelUpdate->setText("<p>A new version is available, <a href="/?originalUrl=https%3A%2F%2Fsourceforge.net%2F%253C%2Fspan">\"https://www.matthewhipkin.co.uk\">click here</a> to get it!");
labelUpdate->setVisible(true);
labelUpdate->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:1, x2:0, y2:0, stop:0 rgba(255, 204, 204, 255), stop:1 rgba(255, 255, 255, 255)); }");
labelUpdate->setTextInteractionFlags(Qt::TextBrowserInteraction);
labelUpdate->setOpenExternalLinks(true);
ui->centralWidget->layout()->addWidget(labelUpdate);
}
}
void MainWindow::dragEnterEvent(QDragEnterEvent *e) {
e->accept();
}
void MainWindow::dropEvent(QDropEvent *e) {
foreach(QUrl url, e->mimeData()->urls()) {
QString filename = url.toLocalFile();
ui->listFiles->addItem(filename);
}
}
MongoDB Logo MongoDB