diff --git a/include/core/network.h b/include/core/network.h index 346f53fe..1a093529 100644 --- a/include/core/network.h +++ b/include/core/network.h @@ -26,13 +26,16 @@ }); */ +#include "url.h" + #if __has_include() #include #include #include +#endif + #include #include -#endif #ifdef QT_NETWORK_LIB @@ -95,6 +98,7 @@ private: class Network { public: + static NetworkReplyData * get(Url::ID id); static NetworkReplyData * get(const QString &url); static NetworkReplyData * get(const QUrl &url); }; diff --git a/include/core/url.h b/include/core/url.h new file mode 100644 index 00000000..c6fb9100 --- /dev/null +++ b/include/core/url.h @@ -0,0 +1,33 @@ +#pragma once +#ifndef URL_H +#define URL_H + +#include +#include + +/* + Some basic URL functions. + This mostly exists to collect all our explicit URLs in one place, in case they ever need to be changed. +*/ + +namespace Url { + enum ID { + Manual, + ManualProjectFiles, + ManualProjectIdentifiers, + ManualPlugins, + Releases, + }; + const QUrl& get(ID id); + + // Open's the specified URL using the system's default browser. + // Returns true if the request was sent (but the operation was not necessarily successful). + bool open(const QUrl& url); + bool open(ID id); + + // Open's the specified local file using the system's default application. + // Returns true if the request was sent (but the operation was not necessarily successful). + bool openLocalFile(const QString& path); +}; + +#endif // URL_H diff --git a/porymap.pro b/porymap.pro index 317134d1..13af9324 100644 --- a/porymap.pro +++ b/porymap.pro @@ -70,6 +70,7 @@ SOURCES += src/config/keyvalueconfigbase.cpp \ src/core/parseutil.cpp \ src/core/tile.cpp \ src/core/tileset.cpp \ + src/core/url.cpp \ src/core/utility.cpp \ src/core/validator.cpp \ src/core/version.cpp \ @@ -194,6 +195,7 @@ HEADERS += include/config/keyvalueconfigbase.h \ include/core/parseutil.h \ include/core/tile.h \ include/core/tileset.h \ + include/core/url.h \ include/core/utility.h \ include/core/validator.h \ include/core/version.h \ diff --git a/src/core/network.cpp b/src/core/network.cpp index 00776b82..a49bddd7 100644 --- a/src/core/network.cpp +++ b/src/core/network.cpp @@ -23,6 +23,10 @@ QPointer NetworkAccessManager::instance() { return manager; } +NetworkReplyData * Network::get(Url::ID id) { + return Network::get(Url::get(id)); +} + NetworkReplyData * Network::get(const QString &url) { return Network::get(QUrl(url)); } diff --git a/src/core/url.cpp b/src/core/url.cpp new file mode 100644 index 00000000..513845f7 --- /dev/null +++ b/src/core/url.cpp @@ -0,0 +1,37 @@ +#include "url.h" + +#include + +const QUrl& Url::get(Url::ID id) { + switch (id) { + case ID::Manual: + {static const QUrl url("https://huderlem.github.io/porymap/"); return url;} + case ID::ManualProjectFiles: + {static const QUrl url("https://huderlem.github.io/porymap/manual/project-files.html#files"); return url;} + case ID::ManualProjectIdentifiers: + {static const QUrl url("https://huderlem.github.io/porymap/manual/project-files.html#identifiers"); return url;} + case ID::ManualPlugins: + {static const QUrl url("https://huderlem.github.io/porymap/manual/scripting-capabilities.html"); return url;} + + // We could use the URL ".../releases/latest" to retrieve less data, but this would run into problems if the + // most recent item on the releases page is not actually a new release (like the static windows build). + // By getting all releases we can also present a multi-version changelog of all changes since the host release. + case ID::Releases: + {static const QUrl url("https://api.github.com/repos/huderlem/porymap/releases"); return url;} + } + + static const QUrl empty; + return empty; +} + +bool Url::open(const QUrl& url) { + return QDesktopServices::openUrl(url); +} + +bool Url::open(Url::ID id) { + return open(get(id)); +} + +bool Url::openLocalFile(const QString& path) { + return open(QUrl::fromLocalFile(path)); +} diff --git a/src/editor.cpp b/src/editor.cpp index 9f74bab5..2c16b2eb 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -14,9 +14,9 @@ #include "validator.h" #include "message.h" #include "eventframes.h" +#include "url.h" #include -#include #include #include #include @@ -2355,7 +2355,7 @@ void Editor::openInTextEditor(const QString &path, int lineNum) { QString command = porymapConfig.textEditorGotoLine; if (command.isEmpty()) { // Open map scripts in the system's default editor. - QDesktopServices::openUrl(QUrl::fromLocalFile(path)); + Url::openLocalFile(path); } else { if (command.contains("%F")) { if (command.contains("%L")) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index c0556ad1..c932fa5f 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -30,9 +30,9 @@ #include "newlocationdialog.h" #include "loadingscreen.h" #include "version.h" +#include "url.h" #include -#include #include #include #include @@ -3026,12 +3026,11 @@ void MainWindow::on_actionOpen_Log_File_triggered() { } void MainWindow::on_actionOpen_Config_Folder_triggered() { - QDesktopServices::openUrl(QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation))); + Url::openLocalFile(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)); } void MainWindow::on_actionOpen_Manual_triggered() { - static const QUrl url("https://huderlem.github.io/porymap/"); - QDesktopServices::openUrl(url); + Url::open(Url::ID::Manual); } void MainWindow::on_actionPreferences_triggered() { diff --git a/src/ui/maplistmodels.cpp b/src/ui/maplistmodels.cpp index e3a1d7fe..c80ca353 100644 --- a/src/ui/maplistmodels.cpp +++ b/src/ui/maplistmodels.cpp @@ -4,7 +4,6 @@ #include "filterchildrenproxymodel.h" #include -#include #include #include #include diff --git a/src/ui/projectsettingseditor.cpp b/src/ui/projectsettingseditor.cpp index 34824306..886bab9d 100644 --- a/src/ui/projectsettingseditor.cpp +++ b/src/ui/projectsettingseditor.cpp @@ -5,10 +5,10 @@ #include "newdefinedialog.h" #include "utility.h" #include "eventfilters.h" +#include "url.h" #include #include -#include #include /* @@ -836,13 +836,11 @@ void ProjectSettingsEditor::dialogButtonClicked(QAbstractButton *button) { } void ProjectSettingsEditor::openFilesHelp() { - static const QUrl url("https://huderlem.github.io/porymap/manual/project-files.html#files"); - QDesktopServices::openUrl(url); + Url::open(Url::ID::ManualProjectFiles); } void ProjectSettingsEditor::openIdentifiersHelp() { - static const QUrl url("https://huderlem.github.io/porymap/manual/project-files.html#identifiers"); - QDesktopServices::openUrl(url); + Url::open(Url::ID::ManualProjectIdentifiers); } // Close event triggered by a project reload. User doesn't need any prompts, just close the window. diff --git a/src/ui/updatepromoter.cpp b/src/ui/updatepromoter.cpp index a9da1de5..55f401de 100644 --- a/src/ui/updatepromoter.cpp +++ b/src/ui/updatepromoter.cpp @@ -4,11 +4,11 @@ #include "log.h" #include "config.h" #include "version.h" +#include "url.h" #include #include #include -#include #include UpdatePromoter::UpdatePromoter(QWidget *parent) @@ -60,12 +60,7 @@ void UpdatePromoter::checkForUpdates() { this->resetDialog(); this->button_Retry->setEnabled(false); ui->label_Status->setText("Checking for updates..."); - - // We could use the URL ".../releases/latest" to retrieve less data, but this would run into problems if the - // most recent item on the releases page is not actually a new release (like the static windows build). - // By getting all releases we can also present a multi-version changelog of all changes since the host release. - static const QUrl url("https://api.github.com/repos/huderlem/porymap/releases"); - this->get(url); + this->get(Url::get(Url::ID::Releases)); } void UpdatePromoter::get(const QUrl &url) { @@ -186,7 +181,7 @@ void UpdatePromoter::dialogButtonClicked(QAbstractButton *button) { } else if (button == this->button_Retry) { this->checkForUpdates(); } else if (button == this->button_Downloads) { - QDesktopServices::openUrl(this->downloadUrl); + Url::open(this->downloadUrl); } }