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");
for (auto url : parser.values("import")){
addImportUrl(url);
m_urlsToImport.append(normalizeImportUrl(url));
}
// treat unspecified positional arguments as import urls
for (auto url : parser.positionalArguments()) {
addImportUrl(url);
m_urlsToImport.append(normalizeImportUrl(url));
}
// 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";
m_peerInstance->sendMessage(activate.serialize(), timeout);
if(!m_urlsToImport.isEmpty())
{
if (!m_urlsToImport.isEmpty()) {
for (auto url : m_urlsToImport) {
ApplicationMessage import;
import.command = "import";
import.args.insert("path", url.toString());
import.args.insert("url", url.toString());
m_peerInstance->sendMessage(import.serialize(), timeout);
}
}
@ -1071,27 +1070,16 @@ void Application::messageReceived(const QByteArray& message)
auto & command = received.command;
if(command == "activate")
{
if (command == "activate") {
showMainWindow();
}
else if(command == "import")
{
QString path = received.args["path"];
if(path.isEmpty())
{
} else if (command == "import") {
QString url = received.args["url"];
if (url.isEmpty()) {
qWarning() << "Received" << command << "message without a zip path/URL.";
return;
}
auto local_file = QFileInfo(path);
if (local_file.exists()) {
m_mainWindow->processURLs({ QUrl::fromLocalFile(local_file.absoluteFilePath()) });
} else {
m_mainWindow->processURLs({ QUrl::fromUserInput(path) });
}
}
else if(command == "launch")
{
m_mainWindow->processURLs({ normalizeImportUrl(url) });
} else if (command == "launch") {
QString id = received.args["id"];
QString server = received.args["server"];
QString profile = received.args["profile"];
@ -1131,9 +1119,7 @@ void Application::messageReceived(const QByteArray& message)
serverObject,
accountObject
);
}
else
{
} else {
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);
if (local_file.exists()) {
m_urlsToImport.append(QUrl::fromLocalFile(local_file.absoluteFilePath()));
return QUrl::fromLocalFile(local_file.absoluteFilePath());
} else {
m_urlsToImport.append(QUrl::fromUserInput(url));
return QUrl::fromUserInput(url);
}
}

View File

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

View File

@ -102,16 +102,13 @@ void ImportPage::openedImpl()
void ImportPage::updateState()
{
if(!isOpened)
{
if (!isOpened) {
return;
}
if(ui->modpackEdit->hasAcceptableInput())
{
if (ui->modpackEdit->hasAcceptableInput()) {
QString input = ui->modpackEdit->text();
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
QFileInfo fi(input);
@ -120,15 +117,12 @@ void ImportPage::updateState()
// mrpack is a modrinth pack
bool isMRPack = fi.suffix() == "mrpack";
if(fi.exists() && (isZip || isMRPack))
{
if (fi.exists() && (isZip || isMRPack)) {
QFileInfo fi(url.fileName());
dialog->setSuggestedPack(fi.completeBaseName(), new InstanceImportTask(url, this));
dialog->setSuggestedIcon("default");
}
}
else if (url.scheme() == "curseforge")
{
} else if (url.scheme() == "curseforge") {
// need to find the download link for the modpack
// format of url curseforge://install?addonId=IDHERE&fileId=IDHERE
QUrlQuery query(url);
@ -139,12 +133,9 @@ void ImportPage::updateState()
req->addNetAction(
Net::Download::makeByteArray(QUrl(QString("https://api.curseforge.com/v1/mods/%1/files/%2").arg(addonId, fileId)), array));
connect(req.get(), &NetJob::finished, [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::finished, [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::succeeded, this, [this, array, addonId, fileId] {
qDebug() << "Returned CFURL Json:\n" << array->toStdString().c_str();
auto doc = Json::requireDocument(*array);
@ -156,12 +147,15 @@ void ImportPage::updateState()
auto dl_url = QUrl(
Json::ensureString(Json::ensureObject(Json::ensureObject(doc.object()), "data"), "downloadUrl", "", "downloadUrl"));
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;
}
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;
extra_info.insert("pack_id", addonId);
@ -178,11 +172,7 @@ void ImportPage::updateState()
dlUrlDialod.setSkipButton(true, tr("Abort"));
dlUrlDialod.execWithTask(req.get());
return;
}
else
{
} else {
if (input.endsWith("?client=y")) {
input.chop(9);
input.append("/file");
@ -193,9 +183,7 @@ void ImportPage::updateState()
dialog->setSuggestedPack(fi.completeBaseName(), new InstanceImportTask(url, this));
dialog->setSuggestedIcon("default");
}
}
else
{
} else {
dialog->setSuggestedPack();
}
}