From ec8ee53c76804afb270357b8463f0c2d53035953 Mon Sep 17 00:00:00 2001 From: Mateo Pidal Date: Sun, 2 May 2021 03:55:27 -0300 Subject: [PATCH] Replaced username writing with a proper dropdown selection saved in a file and ordered by last used --- application/LaunchController.cpp | 59 +++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/application/LaunchController.cpp b/application/LaunchController.cpp index dcdee4af..61063f62 100644 --- a/application/LaunchController.cpp +++ b/application/LaunchController.cpp @@ -37,15 +37,66 @@ void LaunchController::login() JavaCommon::checkJVMArgs(m_instance->settings()->get("JvmArgs").toString(), m_parentWidget); // Mojang account login bypass - bool ok = false; + //bool ok = false; + QString usedname = "Player"; - QString name = QInputDialog::getText(m_parentWidget, tr("Player name"), + QString name; /*= QInputDialog::getText(m_parentWidget, tr("Player name"), tr("Choose your offline mode player name."), QLineEdit::Normal, "Player", &ok); - if (!ok) - { + */ + QFile namesFile; + namesFile.setFileName("names.txt"); + if(!namesFile.exists()){ + namesFile.open(QIODevice::WriteOnly | QIODevice::Text); + namesFile.write("Player"); + namesFile.close(); + qDebug() << "Wrote default \"names.txt\" since it didn't exist"; + } + namesFile.open(QIODevice::ReadOnly | QIODevice::Text); + + QInputDialog nameSelector; + QStringList names; + + while(!namesFile.atEnd()){ + names += namesFile.readLine().simplified(); + } + namesFile.close(); + + //names << "Test1" << "Test2" << "New Entry..."; + //nameSelector.setOption(QInputDialog::UseListViewForComboBoxItems); //In case we want to use a list instead of a combobox + nameSelector.setComboBoxItems(names); + nameSelector.setComboBoxEditable(true); + nameSelector.setWindowTitle("Select Username..."); + + if(nameSelector.exec() == QDialog::Accepted){ + name = nameSelector.textValue(); + namesFile.open(QIODevice::WriteOnly | QIODevice::Text); + namesFile.write(name.toStdString().c_str()); + if(!names.contains(name)){ + for(int i = 0; i < names.count(); i++){ + namesFile.write("\n"); namesFile.write(names[i].toStdString().c_str()); + } + qDebug() << "Wrote " << name << " to \"names.txt\" since it didn't exist before"; + } else { + for(int i = 0; i < names.count(); i++){ //TODO: Improve efficiency or find a better way to do this + if(names[i] != name){ + namesFile.write("\n"); namesFile.write(names[i].toStdString().c_str()); + } + qDebug() << "Reordered \"names.txt\""; + } + } + namesFile.flush(); + namesFile.close(); + } else { return; } + qDebug() << "Username Selected: " << name; + + /*if (!ok) + { + return; + }*/ + if (name.length()) { usedname = name;