refactor: normalize url fn & cleanup

Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com>
This commit is contained in:
Rachel Powers 2023-05-12 16:37:45 -07:00
parent 3e11d94829
commit b1ffc8ddab
4 changed files with 36 additions and 63 deletions

View File

@ -235,12 +235,12 @@ Application::Application(int &argc, char **argv) : QApplication(argc, argv)
m_instanceIdToShowWindowOf = parser.value("show"); m_instanceIdToShowWindowOf = parser.value("show");
for (auto url : parser.values("import")){ for (auto url : parser.values("import")){
addImportUrl(url); m_urlsToImport.append(normalizeImportUrl(url));
} }
// treat unspecified positional arguments as import urls // treat unspecified positional arguments as import urls
for (auto url : parser.positionalArguments()) { for (auto url : parser.positionalArguments()) {
addImportUrl(url); m_urlsToImport.append(normalizeImportUrl(url));
} }
// error if --launch is missing with --server or --profile // error if --launch is missing with --server or --profile
@ -345,12 +345,11 @@ Application::Application(int &argc, char **argv) : QApplication(argc, argv)
activate.command = "activate"; activate.command = "activate";
m_peerInstance->sendMessage(activate.serialize(), timeout); m_peerInstance->sendMessage(activate.serialize(), timeout);
if(!m_urlsToImport.isEmpty()) if (!m_urlsToImport.isEmpty()) {
{
for (auto url : m_urlsToImport) { for (auto url : m_urlsToImport) {
ApplicationMessage import; ApplicationMessage import;
import.command = "import"; import.command = "import";
import.args.insert("path", url.toString()); import.args.insert("url", url.toString());
m_peerInstance->sendMessage(import.serialize(), timeout); m_peerInstance->sendMessage(import.serialize(), timeout);
} }
} }
@ -1071,27 +1070,16 @@ void Application::messageReceived(const QByteArray& message)
auto & command = received.command; auto & command = received.command;
if(command == "activate") if (command == "activate") {
{
showMainWindow(); showMainWindow();
} } else if (command == "import") {
else if(command == "import") QString url = received.args["url"];
{ if (url.isEmpty()) {
QString path = received.args["path"];
if(path.isEmpty())
{
qWarning() << "Received" << command << "message without a zip path/URL."; qWarning() << "Received" << command << "message without a zip path/URL.";
return; return;
} }
auto local_file = QFileInfo(path); m_mainWindow->processURLs({ normalizeImportUrl(url) });
if (local_file.exists()) { } else if (command == "launch") {
m_mainWindow->processURLs({ QUrl::fromLocalFile(local_file.absoluteFilePath()) });
} else {
m_mainWindow->processURLs({ QUrl::fromUserInput(path) });
}
}
else if(command == "launch")
{
QString id = received.args["id"]; QString id = received.args["id"];
QString server = received.args["server"]; QString server = received.args["server"];
QString profile = received.args["profile"]; QString profile = received.args["profile"];
@ -1131,9 +1119,7 @@ void Application::messageReceived(const QByteArray& message)
serverObject, serverObject,
accountObject accountObject
); );
} } else {
else
{
qWarning() << "Received invalid message" << message; qWarning() << "Received invalid message" << message;
} }
} }
@ -1741,13 +1727,12 @@ void Application::triggerUpdateCheck()
} }
} }
void Application::addImportUrl(QString const& url) QUrl Application::normalizeImportUrl(QString const& url)
{ {
auto local_file = QFileInfo(url); auto local_file = QFileInfo(url);
if (local_file.exists()){ if (local_file.exists()) {
m_urlsToImport.append(QUrl::fromLocalFile(local_file.absoluteFilePath())); return QUrl::fromLocalFile(local_file.absoluteFilePath());
} else { } else {
m_urlsToImport.append(QUrl::fromUserInput(url)); return QUrl::fromUserInput(url);
} }
} }

View File

@ -211,7 +211,7 @@ public:
int suitableMaxMem(); int suitableMaxMem();
void addImportUrl(QString const& url); QUrl normalizeImportUrl(QString const& url);
signals: signals:
void updateAllowedChanged(bool status); void updateAllowedChanged(bool status);

View File

@ -90,7 +90,7 @@ void InstanceImportTask::executeTask()
setStatus(tr("Downloading modpack:\n%1").arg(m_sourceUrl.toString())); setStatus(tr("Downloading modpack:\n%1").arg(m_sourceUrl.toString()));
m_downloadRequired = true; m_downloadRequired = true;
downloadFromUrl(); downloadFromUrl();
} }
} }

View File

@ -102,16 +102,13 @@ void ImportPage::openedImpl()
void ImportPage::updateState() void ImportPage::updateState()
{ {
if(!isOpened) if (!isOpened) {
{
return; return;
} }
if(ui->modpackEdit->hasAcceptableInput()) if (ui->modpackEdit->hasAcceptableInput()) {
{
QString input = ui->modpackEdit->text(); QString input = ui->modpackEdit->text();
auto url = QUrl::fromUserInput(input); auto url = QUrl::fromUserInput(input);
if(url.isLocalFile()) if (url.isLocalFile()) {
{
// FIXME: actually do some validation of what's inside here... this is fake AF // FIXME: actually do some validation of what's inside here... this is fake AF
QFileInfo fi(input); QFileInfo fi(input);
@ -120,15 +117,12 @@ void ImportPage::updateState()
// mrpack is a modrinth pack // mrpack is a modrinth pack
bool isMRPack = fi.suffix() == "mrpack"; bool isMRPack = fi.suffix() == "mrpack";
if(fi.exists() && (isZip || isMRPack)) if (fi.exists() && (isZip || isMRPack)) {
{
QFileInfo fi(url.fileName()); QFileInfo fi(url.fileName());
dialog->setSuggestedPack(fi.completeBaseName(), new InstanceImportTask(url,this)); dialog->setSuggestedPack(fi.completeBaseName(), new InstanceImportTask(url, this));
dialog->setSuggestedIcon("default"); dialog->setSuggestedIcon("default");
} }
} } else if (url.scheme() == "curseforge") {
else if (url.scheme() == "curseforge")
{
// need to find the download link for the modpack // need to find the download link for the modpack
// format of url curseforge://install?addonId=IDHERE&fileId=IDHERE // format of url curseforge://install?addonId=IDHERE&fileId=IDHERE
QUrlQuery query(url); QUrlQuery query(url);
@ -139,12 +133,9 @@ void ImportPage::updateState()
req->addNetAction( req->addNetAction(
Net::Download::makeByteArray(QUrl(QString("https://api.curseforge.com/v1/mods/%1/files/%2").arg(addonId, fileId)), array)); Net::Download::makeByteArray(QUrl(QString("https://api.curseforge.com/v1/mods/%1/files/%2").arg(addonId, fileId)), array));
connect(req.get(), &NetJob::finished, [array] { connect(req.get(), &NetJob::finished, [array] { delete array; });
delete array; connect(req.get(), &NetJob::failed, this,
}); [this](QString reason) { CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show(); });
connect(req.get(), &NetJob::failed, this, [this](QString reason){
CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show();
});
connect(req.get(), &NetJob::succeeded, this, [this, array, addonId, fileId] { connect(req.get(), &NetJob::succeeded, this, [this, array, addonId, fileId] {
qDebug() << "Returned CFURL Json:\n" << array->toStdString().c_str(); qDebug() << "Returned CFURL Json:\n" << array->toStdString().c_str();
auto doc = Json::requireDocument(*array); auto doc = Json::requireDocument(*array);
@ -156,12 +147,15 @@ void ImportPage::updateState()
auto dl_url = QUrl( auto dl_url = QUrl(
Json::ensureString(Json::ensureObject(Json::ensureObject(doc.object()), "data"), "downloadUrl", "", "downloadUrl")); Json::ensureString(Json::ensureObject(Json::ensureObject(doc.object()), "data"), "downloadUrl", "", "downloadUrl"));
if (!dl_url.isValid()) { if (!dl_url.isValid()) {
CustomMessageBox::selectable(this, tr("Error"), tr("The modpack is blocked ! Please download it manually"), QMessageBox::Critical)->show(); CustomMessageBox::selectable(this, tr("Error"), tr("The modpack is blocked ! Please download it manually"),
QMessageBox::Critical)
->show();
return; return;
} }
QFileInfo dl_file(dl_url.fileName()); QFileInfo dl_file(dl_url.fileName());
QString pack_name = Json::ensureString(Json::ensureObject(Json::ensureObject(doc.object()), "data"), "displayName", dl_file.completeBaseName(), "displayName"); QString pack_name = Json::ensureString(Json::ensureObject(Json::ensureObject(doc.object()), "data"), "displayName",
dl_file.completeBaseName(), "displayName");
QMap<QString, QString> extra_info; QMap<QString, QString> extra_info;
extra_info.insert("pack_id", addonId); extra_info.insert("pack_id", addonId);
@ -169,7 +163,7 @@ void ImportPage::updateState()
dialog->setSuggestedPack(pack_name, new InstanceImportTask(dl_url, this, std::move(extra_info))); dialog->setSuggestedPack(pack_name, new InstanceImportTask(dl_url, this, std::move(extra_info)));
dialog->setSuggestedIcon("default"); dialog->setSuggestedIcon("default");
} else { } else {
CustomMessageBox::selectable(this, tr("Error"), tr("This url isn't a valid modpack !"), QMessageBox::Critical)->show(); CustomMessageBox::selectable(this, tr("Error"), tr("This url isn't a valid modpack !"), QMessageBox::Critical)->show();
} }
@ -178,24 +172,18 @@ void ImportPage::updateState()
dlUrlDialod.setSkipButton(true, tr("Abort")); dlUrlDialod.setSkipButton(true, tr("Abort"));
dlUrlDialod.execWithTask(req.get()); dlUrlDialod.execWithTask(req.get());
return; return;
} } else {
else if (input.endsWith("?client=y")) {
{
if(input.endsWith("?client=y")) {
input.chop(9); input.chop(9);
input.append("/file"); input.append("/file");
url = QUrl::fromUserInput(input); url = QUrl::fromUserInput(input);
} }
// hook, line and sinker. // hook, line and sinker.
QFileInfo fi(url.fileName()); QFileInfo fi(url.fileName());
dialog->setSuggestedPack(fi.completeBaseName(), new InstanceImportTask(url,this)); dialog->setSuggestedPack(fi.completeBaseName(), new InstanceImportTask(url, this));
dialog->setSuggestedIcon("default"); dialog->setSuggestedIcon("default");
} }
} } else {
else
{
dialog->setSuggestedPack(); dialog->setSuggestedPack();
} }
} }