Skip to content

Commit

Permalink
Load Mudlet maps from Client.Map as well (Mudlet#3104)
Browse files Browse the repository at this point in the history
* Load Mudlet maps from Client.Map as well

* Close dialog properly in Mudlet maps

* Invert wrong 'if' check

* Save downloaded map in correct format

Not always as XML

* Don't unlock mutex twice
  • Loading branch information
vadi2 authored Sep 30, 2019
1 parent 2c37acd commit 56abb71
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 19 deletions.
67 changes: 50 additions & 17 deletions src/TMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2189,7 +2189,11 @@ void TMap::downloadMap(const QString& remoteUrl, const QString& localFileName)
}

if (localFileName.isEmpty()) {
mLocalMapFileName = mudlet::getMudletPath(mudlet::profileXmlMapPathFileName, mProfileName);
if (url.toString().endsWith(QLatin1String("xml"))) {
mLocalMapFileName = mudlet::getMudletPath(mudlet::profileXmlMapPathFileName, mProfileName);
} else {
mLocalMapFileName = mudlet::getMudletPath(mudlet::profileMapPathFileName, mProfileName, QStringLiteral("map.dat"));
}
} else {
mLocalMapFileName = localFileName;
}
Expand All @@ -2208,7 +2212,7 @@ void TMap::downloadMap(const QString& remoteUrl, const QString& localFileName)
mpNetworkReply = mpNetworkAccessManager->get(request);
// Using zero for both min and max values should cause the bar to oscillate
// until the first update
mpProgressDialog = new QProgressDialog(tr("Downloading XML map file for use in %1...",
mpProgressDialog = new QProgressDialog(tr("Downloading map file for use in %1...",
"%1 is the name of the current Mudlet profile")
.arg(mProfileName), tr("Abort"), 0, 0);
mpProgressDialog->setWindowTitle(tr("Map download", "This is a title of a progress window."));
Expand Down Expand Up @@ -2359,6 +2363,24 @@ void TMap::slot_downloadError(QNetworkReply::NetworkError error)

void TMap::slot_replyFinished(QNetworkReply* reply)
{
auto cleanup = [this, reply](){
reply->deleteLater();
mpNetworkReply = Q_NULLPTR;

// We don't delete the progress dialog until here as we now use it to inform
// about post-download operations

mpProgressDialog->deleteLater();
mpProgressDialog = Q_NULLPTR; // Must reset this so it can be reused

mLocalMapFileName.clear();
mExpectedFileSize = 0;

// We have finished with the XMLimporter so must release the lock on it
mXmlImportMutex.unlock();
};


if (reply != mpNetworkReply) {
qWarning() << "TMap::slot_replyFinished( QNetworkReply * ) ERROR - received argument was not the expected stored pointer.";
}
Expand All @@ -2385,14 +2407,37 @@ void TMap::slot_replyFinished(QNetworkReply* reply)
file.flush();
file.close();

if (!file.fileName().endsWith(QStringLiteral("xml"), Qt::CaseInsensitive)) {
auto pHost = mpHost;
if (!pHost) {
cleanup();
return;
}

QString infoMsg = tr("[ INFO ] - ... map downloaded and stored, now parsing it...");
postMessage(infoMsg);
if (pHost->mpConsole->loadMap(file.fileName())) {
TEvent mapDownloadEvent {};
mapDownloadEvent.mArgumentList.append(QStringLiteral("sysMapDownloadEvent"));
mapDownloadEvent.mArgumentTypeList.append(ARGUMENT_TYPE_STRING);
pHost->raiseEvent(mapDownloadEvent);
} else {
QString alertMsg = tr("[ ERROR ] - Map download problem, failure in parsing destination file:\n%1.").arg(file.fileName());
postMessage(alertMsg);
}

cleanup();
return;
}

if (file.open(QFile::ReadOnly | QFile::Text)) {
QString infoMsg = tr("[ INFO ] - ... map downloaded and stored, now parsing it...");
postMessage(infoMsg);

Host* pHost = mpHost;
if (!pHost) {
qWarning() << "TMap::slot_replyFinished( QNetworkReply * ) ERROR - NULL Host pointer - something is really wrong!";
mXmlImportMutex.unlock();
cleanup();
return;
}

Expand All @@ -2407,7 +2452,7 @@ void TMap::slot_replyFinished(QNetworkReply* reply)

if (readXmlMapFile(file)) {
TEvent mapDownloadEvent {};
mapDownloadEvent.mArgumentList.append(QLatin1String("sysMapDownloadEvent"));
mapDownloadEvent.mArgumentList.append(QStringLiteral("sysMapDownloadEvent"));
mapDownloadEvent.mArgumentTypeList.append(ARGUMENT_TYPE_STRING);
pHost->raiseEvent(mapDownloadEvent);
} else {
Expand All @@ -2423,20 +2468,8 @@ void TMap::slot_replyFinished(QNetworkReply* reply)
}
}
}
reply->deleteLater();
mpNetworkReply = Q_NULLPTR;

// We don't delete the progress dialog until here as we now use it to inform
// about post-download operations

mpProgressDialog->deleteLater();
mpProgressDialog = Q_NULLPTR; // Must reset this so it can be reused

mLocalMapFileName.clear();
mExpectedFileSize = 0;

// We have finished with the XMLimporter so must release the lock on it
mXmlImportMutex.unlock();
cleanup();
}

void TMap::reportStringToProgressDialog(const QString text)
Expand Down
4 changes: 2 additions & 2 deletions src/mudlet.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ class mudlet : public QMainWindow, public Ui::main_window
// Takes two extra arguments (profile name, mapFileName) that returns
// the pathFile name for any map file:
profileMapPathFileName,
// Takes one extra argument (profile name) that returns the pathFile
// name for the downloaded IRE Server provided XML map:
// Takes one extra argument (profile name) that returns the file
// location for the downloaded MMP map:
profileXmlMapPathFileName,
// Takes two extra arguments (profile name, data item) that gives a
// path file name for, typically a data item stored as a single item
Expand Down

0 comments on commit 56abb71

Please sign in to comment.