all repos — mgba @ 54413a8fd13cae1eff8618d306f440ac9e408695

mGBA Game Boy Advance Emulator

src/platform/qt/DatDownloadView.cpp (view raw)

 1/* Copyright (c) 2013-2015 Jeffrey Pfau
 2 *
 3 * This Source Code Form is subject to the terms of the Mozilla Public
 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 6#include "DatDownloadView.h"
 7
 8#include "GBAApp.h"
 9
10#include <QFile>
11#include <QNetworkAccessManager>
12
13extern "C" {
14#include "gba/context/config.h"
15#include "util/version.h"
16}
17
18using namespace QGBA;
19
20DatDownloadView::DatDownloadView(QWidget* parent)
21	: QDialog(parent)
22	, m_reply(nullptr)
23{
24	m_ui.setupUi(this);
25
26	m_netman = new QNetworkAccessManager(this);
27	connect(m_netman, SIGNAL(finished(QNetworkReply*)), this, SLOT(finished(QNetworkReply*)));
28	connect(m_ui.dialogButtonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(buttonPressed(QAbstractButton*)));
29}
30
31void DatDownloadView::start() {
32	if (m_reply) {
33		return;
34	}
35	QNetworkRequest request(QUrl("https://raw.githubusercontent.com/mgba-emu/mgba/master/res/nointro.dat"));
36	request.setHeader(QNetworkRequest::UserAgentHeader, QString("%1 %2").arg(projectName).arg(projectVersion));
37	m_reply = m_netman->get(request);
38	connect(m_reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(downloadProgress(qint64, qint64)));
39	connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(errored(QNetworkReply::NetworkError)));
40}
41
42void DatDownloadView::finished(QNetworkReply* reply) {
43	if (!m_reply) {
44		return;
45	}
46	char path[PATH_MAX];
47	GBAConfigDirectory(path, sizeof(path));
48	QFile outfile(QString::fromUtf8(path) + "/nointro.dat");
49	outfile.open(QIODevice::WriteOnly);
50	outfile.write(m_reply->readAll());
51	GBAApp::app()->reloadGameDB();
52
53	m_reply->deleteLater();
54	m_reply = nullptr;
55	setAttribute(Qt::WA_DeleteOnClose);
56	close();
57}
58
59void DatDownloadView::downloadProgress(qint64 read, qint64 size) {
60	if (size < 0) {
61		return;
62	}
63	m_ui.progressBar->setMaximum(size);
64	m_ui.progressBar->setValue(read);
65}
66
67void DatDownloadView::errored(QNetworkReply::NetworkError) {
68	m_ui.status->setText(tr("An error occurred"));
69	m_reply->deleteLater();
70	m_reply = nullptr;
71}
72
73void DatDownloadView::buttonPressed(QAbstractButton* button) {
74	switch (m_ui.dialogButtonBox->standardButton(button)) {
75	case QDialogButtonBox::Cancel:
76		if (m_reply) {
77			m_reply->abort();
78		}
79	 	break;
80	default:
81		break;
82	}
83}