diff options
-rw-r--r-- | src/libs/installer/globals.cpp | 57 | ||||
-rw-r--r-- | src/libs/installer/globals.h | 2 | ||||
-rw-r--r-- | src/libs/installer/metadatajob.cpp | 24 |
3 files changed, 77 insertions, 6 deletions
diff --git a/src/libs/installer/globals.cpp b/src/libs/installer/globals.cpp index b6bf0ca3e..3fd084768 100644 --- a/src/libs/installer/globals.cpp +++ b/src/libs/installer/globals.cpp @@ -1,6 +1,6 @@ /************************************************************************** ** -** Copyright (C) 2023 The Qt Company Ltd. +** Copyright (C) 2024 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the Qt Installer Framework. @@ -30,6 +30,14 @@ #include "globals.h" +#if defined(Q_OS_MACOS) || defined(Q_OS_LINUX) +#include <termios.h> +#include <unistd.h> +#elif defined(Q_OS_WIN) +#include <conio.h> +#endif +#include <iostream> + const char IFW_SERVER[] = "ifw.server"; const char IFW_INSTALLER_INSTALLLOG[] = "ifw.installer.installlog"; const char IFW_DEVELOPER_BUILD[] = "ifw.developer.build"; @@ -119,5 +127,52 @@ QString enumToString(const QMetaObject& metaObject, const char *enumerator, int return value; } +void askForCredentials(QString *username, QString *password, const QString &usernameTitle, const QString &passwordTitle) +{ + std::string usernameStdStr; + std::string passwordStdStr; + + std::cout << qPrintable(usernameTitle); + std::cin >> usernameStdStr; + + std::cout << qPrintable(passwordTitle); +#if defined(Q_OS_MACOS) || defined(Q_OS_LINUX) + termios oldTerm; + termios term; + + // Turn off echoing + tcgetattr(STDIN_FILENO, &oldTerm); + term = oldTerm; + term.c_lflag &= ~ECHO; + tcsetattr(STDIN_FILENO, TCSANOW, &term); + + std::cin >> passwordStdStr; + + // Clear input buffer + std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); + + // Restore old attributes + tcsetattr(STDIN_FILENO, TCSANOW, &oldTerm); +#elif defined(Q_OS_WIN) + char ch; + while ((ch = _getch()) != '\r') { // Return key + if (ch == '\b') { // Backspace key + if (!passwordStdStr.empty()) + passwordStdStr.pop_back(); + } else { + passwordStdStr.push_back(ch); + } + } + // Clear input buffer + int c; + while ((c = getchar()) != '\n' && c != EOF); + +#endif + std::cout << "\n"; + + *username = username->fromStdString(usernameStdStr); + *password = password->fromStdString(passwordStdStr); +} + } // namespace QInstaller diff --git a/src/libs/installer/globals.h b/src/libs/installer/globals.h index 3b3f4e3ab..2d119048b 100644 --- a/src/libs/installer/globals.h +++ b/src/libs/installer/globals.h @@ -59,6 +59,8 @@ QSet<T> toQSet(const C<T> &container) return QSet<T>(container.begin(), container.end()); } +void askForCredentials(QString *username, QString *password, const QString &usernameTitle, const QString &passwordTitle); + } // QInstaller #endif // GLOBALS_H diff --git a/src/libs/installer/metadatajob.cpp b/src/libs/installer/metadatajob.cpp index 641f2da62..536449ebd 100644 --- a/src/libs/installer/metadatajob.cpp +++ b/src/libs/installer/metadatajob.cpp @@ -41,6 +41,7 @@ #include <QtConcurrent> #include <QtMath> #include <QRandomGenerator> +#include <QApplication> namespace QInstaller { @@ -520,13 +521,25 @@ void MetadataJob::xmlTaskFinished() } } else if (e.type() == AuthenticationRequiredException::Type::Server) { qCWarning(QInstaller::lcInstallerInstallLog) << e.message(); - ServerAuthenticationDialog dlg(e.message(), e.taskItem()); - if (dlg.exec() == QDialog::Accepted) { + QString username; + QString password; + if (m_core->isCommandLineInstance()) { + qCDebug(QInstaller::lcInstallerInstallLog) << "Server Requires Authentication"; + qCDebug(QInstaller::lcInstallerInstallLog) << "You need to supply a username and password to access this site."; + askForCredentials(&username, &password, QLatin1String("Username: "), QLatin1String("Password: ")); + } else { + ServerAuthenticationDialog dlg(e.message(), e.taskItem()); + if (dlg.exec() == QDialog::Accepted) { + username = dlg.user(); + password = dlg.password(); + } + } + if (!username.isEmpty()) { Repository original = e.taskItem().value(TaskRole::UserRole) .value<Repository>(); Repository replacement = original; - replacement.setUsername(dlg.user()); - replacement.setPassword(dlg.password()); + replacement.setUsername(username); + replacement.setPassword(password); Settings &s = m_core->settings(); QSet<Repository> temporaries = s.temporaryRepositories(); @@ -580,8 +593,9 @@ void MetadataJob::xmlTaskFinished() // No new metadata packages to fetch, still need to update the cache // for refreshed repositories. startUpdateCacheTask(); - } + } } else if (status == XmlDownloadRetry) { + reset(); QMetaObject::invokeMethod(this, "doStart", Qt::QueuedConnection); } else { reset(); |