From 9154228421d1a0409f2c3f09b408fbc996d161c5 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 9 Mar 2021 14:22:13 +0100 Subject: [PATCH 01/17] Changes: adjust description of pastebin --- CHANGES | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/CHANGES b/CHANGES index 07c5c8b9c..720908ca4 100644 --- a/CHANGES +++ b/CHANGES @@ -15,10 +15,9 @@ This release contains contributions from (alphabetically by first name): ## Core ## - Uploading your log files (in case of installation failure) has been - expanded and is now more - configurable. Users should still take care when uploading logs, - and distro's should configure a URL with no public viewing - for the uploading of those logs. (Thanks Anubhav) + expanded and is now more configurable. Users should still take care + when uploading logs, and distro's should configure a URL with + no public viewing of those logs. (Thanks Anubhav) ## Modules ## - A new QML-based *finishedq* module has been added. (Thanks Anke) From fc8830ae4a5d9442ecd1ff42aaab9123f86e5e09 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 9 Mar 2021 14:23:27 +0100 Subject: [PATCH 02/17] [libcalamaresui] Tidy logging --- src/libcalamaresui/utils/Paste.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libcalamaresui/utils/Paste.cpp b/src/libcalamaresui/utils/Paste.cpp index 27ae7f8f7..2c180bfa8 100644 --- a/src/libcalamaresui/utils/Paste.cpp +++ b/src/libcalamaresui/utils/Paste.cpp @@ -72,7 +72,7 @@ ficheLogUpload( QObject* parent ) return QString(); } - cDebug() << "Paste data written to paste server"; + cDebug() << Logger::SubEntry << "Paste data written to paste server"; if ( !socket->waitForReadyRead() ) { @@ -81,7 +81,7 @@ ficheLogUpload( QObject* parent ) return QString(); } - cDebug() << "Reading response from paste server"; + cDebug() << Logger::SubEntry << "Reading response from paste server"; char resp[ 1024 ]; resp[ 0 ] = '\0'; @@ -109,7 +109,7 @@ ficheLogUpload( QObject* parent ) return QString(); } - cDebug() << "Paste server results:" << pasteUrlMsg; + cDebug() << Logger::SubEntry << "Paste server results:" << pasteUrlMsg; return pasteUrlMsg; } } // namespace CalamaresUtils From bc2435eb7d9cfdc22a2d86c4b9218458b4d18e85 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 9 Mar 2021 14:23:52 +0100 Subject: [PATCH 03/17] [libcalamaresui] Apply coding style --- src/libcalamaresui/utils/Paste.cpp | 12 ++++++------ src/libcalamaresui/utils/Paste.h | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/libcalamaresui/utils/Paste.cpp b/src/libcalamaresui/utils/Paste.cpp index 2c180bfa8..ce03a77e3 100644 --- a/src/libcalamaresui/utils/Paste.cpp +++ b/src/libcalamaresui/utils/Paste.cpp @@ -12,13 +12,13 @@ #include "Branding.h" #include "utils/Logger.h" +#include +#include #include #include +#include #include #include -#include -#include -#include namespace CalamaresUtils { @@ -96,11 +96,11 @@ ficheLogUpload( QObject* parent ) if ( nBytesRead >= 8 && pasteUrl.isValid() && pasteUrlRegex.match( pasteUrlStr ).hasMatch() ) { QClipboard* clipboard = QApplication::clipboard(); - clipboard->setText(pasteUrlStr, QClipboard::Clipboard); + clipboard->setText( pasteUrlStr, QClipboard::Clipboard ); - if (clipboard->supportsSelection()) + if ( clipboard->supportsSelection() ) { - clipboard->setText(pasteUrlStr, QClipboard::Selection); + clipboard->setText( pasteUrlStr, QClipboard::Selection ); } } else diff --git a/src/libcalamaresui/utils/Paste.h b/src/libcalamaresui/utils/Paste.h index 29e73fc1c..cde1f1841 100644 --- a/src/libcalamaresui/utils/Paste.h +++ b/src/libcalamaresui/utils/Paste.h @@ -10,7 +10,7 @@ #ifndef UTILS_PASTE_H #define UTILS_PASTE_H -#include +#include class QObject; class QString; From 92e36558fa001137fc3e4591ab11c39075e2cff3 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 9 Mar 2021 14:25:40 +0100 Subject: [PATCH 04/17] [libcalamaresui] Remove unnecessary shadowing in lambda --- src/libcalamaresui/ViewManager.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libcalamaresui/ViewManager.cpp b/src/libcalamaresui/ViewManager.cpp index 4e53fb90b..184dceb32 100644 --- a/src/libcalamaresui/ViewManager.cpp +++ b/src/libcalamaresui/ViewManager.cpp @@ -186,11 +186,10 @@ ViewManager::onInstallationFailed( const QString& message, const QString& detail msgBox->show(); cDebug() << "Calamares will quit when the dialog closes."; - connect( msgBox, &QMessageBox::buttonClicked, [msgBox]( QAbstractButton* button ) { + connect( msgBox, &QMessageBox::buttonClicked, [msgBox, serverType]( QAbstractButton* button ) { if ( msgBox->buttonRole( button ) == QMessageBox::ButtonRole::YesRole ) { QString pasteUrlMsg; - QString serverType = Calamares::Branding::instance()->uploadServer( Calamares::Branding::Type ); if ( serverType == "fiche" ) { pasteUrlMsg = CalamaresUtils::ficheLogUpload( msgBox ); From 844831751d8b63157425471a90322026ed075cb8 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 9 Mar 2021 14:30:20 +0100 Subject: [PATCH 05/17] [libcalamaresui] Factor out the reading of the log file - this will be needed for other pastebins, too --- src/libcalamaresui/utils/Paste.cpp | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/libcalamaresui/utils/Paste.cpp b/src/libcalamaresui/utils/Paste.cpp index ce03a77e3..d2ef54a92 100644 --- a/src/libcalamaresui/utils/Paste.cpp +++ b/src/libcalamaresui/utils/Paste.cpp @@ -20,6 +20,23 @@ #include #include +/** @brief Reads the logfile, returns its contents. + * + * Returns an empty QByteArray() on any kind of error. + */ +static QByteArray +logFileContents() +{ + QFile pasteSourceFile( Logger::logFile() ); + if ( !pasteSourceFile.open( QIODevice::ReadOnly | QIODevice::Text ) ) + { + cError() << "Could not open log file"; + return QByteArray(); + } + // TODO: read the **last** 16kiB? + return pasteSourceFile.read( 16384 /* bytes */ ); +} + namespace CalamaresUtils { @@ -32,25 +49,18 @@ QStringList UploadServersList = { QString ficheLogUpload( QObject* parent ) { - const QString& ficheHost = Calamares::Branding::instance()->uploadServer( Calamares::Branding::URL ); quint16 fichePort = Calamares::Branding::instance()->uploadServer( Calamares::Branding::Port ).toInt(); QString pasteUrlFmt = parent->tr( "Install log posted to\n\n%1\n\nLink copied to clipboard" ); - QFile pasteSourceFile( Logger::logFile() ); - if ( !pasteSourceFile.open( QIODevice::ReadOnly | QIODevice::Text ) ) + QByteArray pasteData = logFileContents(); + if ( pasteData.isEmpty() ) { - cError() << "Could not open log file"; + // An error has already been logged return QString(); } - QByteArray pasteData; - while ( !pasteSourceFile.atEnd() ) - { - pasteData += pasteSourceFile.readLine(); - } - QTcpSocket* socket = new QTcpSocket( parent ); socket->connectToHost( ficheHost, fichePort ); From 260862fabc4527ad43db187e291d98c3379ea99f Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 9 Mar 2021 14:31:46 +0100 Subject: [PATCH 06/17] [libcalamaresui] Move the format-string closer to where it is used --- src/libcalamaresui/utils/Paste.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libcalamaresui/utils/Paste.cpp b/src/libcalamaresui/utils/Paste.cpp index d2ef54a92..e30553272 100644 --- a/src/libcalamaresui/utils/Paste.cpp +++ b/src/libcalamaresui/utils/Paste.cpp @@ -52,7 +52,6 @@ ficheLogUpload( QObject* parent ) const QString& ficheHost = Calamares::Branding::instance()->uploadServer( Calamares::Branding::URL ); quint16 fichePort = Calamares::Branding::instance()->uploadServer( Calamares::Branding::Port ).toInt(); - QString pasteUrlFmt = parent->tr( "Install log posted to\n\n%1\n\nLink copied to clipboard" ); QByteArray pasteData = logFileContents(); if ( pasteData.isEmpty() ) @@ -101,7 +100,9 @@ ficheLogUpload( QObject* parent ) QUrl pasteUrl = QUrl( QString( resp ).trimmed(), QUrl::StrictMode ); QString pasteUrlStr = pasteUrl.toString(); QRegularExpression pasteUrlRegex( "^http[s]?://" + ficheHost ); - QString pasteUrlMsg = QString( pasteUrlFmt ).arg( pasteUrlStr ); + + QString pasteUrlFmt = parent->tr( "Install log posted to\n\n%1\n\nLink copied to clipboard" ); + QString pasteUrlMsg = pasteUrlFmt.arg( pasteUrlStr ); if ( nBytesRead >= 8 && pasteUrl.isValid() && pasteUrlRegex.match( pasteUrlStr ).hasMatch() ) { From 1bf95eacb04082b3d89ecc2a1d8ca58746dba101 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 9 Mar 2021 14:33:14 +0100 Subject: [PATCH 07/17] [libcalamaresui] Tidy the logging some more --- src/libcalamaresui/utils/Paste.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libcalamaresui/utils/Paste.cpp b/src/libcalamaresui/utils/Paste.cpp index e30553272..c84c7bdb4 100644 --- a/src/libcalamaresui/utils/Paste.cpp +++ b/src/libcalamaresui/utils/Paste.cpp @@ -52,7 +52,6 @@ ficheLogUpload( QObject* parent ) const QString& ficheHost = Calamares::Branding::instance()->uploadServer( Calamares::Branding::URL ); quint16 fichePort = Calamares::Branding::instance()->uploadServer( Calamares::Branding::Port ).toInt(); - QByteArray pasteData = logFileContents(); if ( pasteData.isEmpty() ) { @@ -70,7 +69,7 @@ ficheLogUpload( QObject* parent ) return QString(); } - cDebug() << "Connected to paste server"; + cDebug() << "Connected to paste server" << ficheHost; socket->write( pasteData ); From 8af5fb5da530b5553ba4e8bf23ed87bad7f7a4e9 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 9 Mar 2021 14:38:43 +0100 Subject: [PATCH 08/17] [libcalamaresui] Simplify getting URL response - get a QByteArray rather than going through a char[] buffer - bytes-read is not important since the RE can only match if there **are** that many characters. --- src/libcalamaresui/utils/Paste.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/libcalamaresui/utils/Paste.cpp b/src/libcalamaresui/utils/Paste.cpp index c84c7bdb4..230199311 100644 --- a/src/libcalamaresui/utils/Paste.cpp +++ b/src/libcalamaresui/utils/Paste.cpp @@ -90,20 +90,17 @@ ficheLogUpload( QObject* parent ) } cDebug() << Logger::SubEntry << "Reading response from paste server"; - - char resp[ 1024 ]; - resp[ 0 ] = '\0'; - qint64 nBytesRead = socket->readLine( resp, 1024 ); + QByteArray responseText = socket->readLine( 1024 ); socket->close(); - QUrl pasteUrl = QUrl( QString( resp ).trimmed(), QUrl::StrictMode ); + QUrl pasteUrl = QUrl( QString( responseText ).trimmed(), QUrl::StrictMode ); QString pasteUrlStr = pasteUrl.toString(); QRegularExpression pasteUrlRegex( "^http[s]?://" + ficheHost ); QString pasteUrlFmt = parent->tr( "Install log posted to\n\n%1\n\nLink copied to clipboard" ); QString pasteUrlMsg = pasteUrlFmt.arg( pasteUrlStr ); - if ( nBytesRead >= 8 && pasteUrl.isValid() && pasteUrlRegex.match( pasteUrlStr ).hasMatch() ) + if ( pasteUrl.isValid() && pasteUrlRegex.match( pasteUrlStr ).hasMatch() ) { QClipboard* clipboard = QApplication::clipboard(); clipboard->setText( pasteUrlStr, QClipboard::Clipboard ); From f72436aa0a2d3c466e836b5a76e72e48a402540f Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 9 Mar 2021 14:41:58 +0100 Subject: [PATCH 09/17] [libcalamaresui] Drop RE-wrangling, compare hosts instead to detect valid paste URL --- src/libcalamaresui/utils/Paste.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/libcalamaresui/utils/Paste.cpp b/src/libcalamaresui/utils/Paste.cpp index 230199311..cdf8cc17b 100644 --- a/src/libcalamaresui/utils/Paste.cpp +++ b/src/libcalamaresui/utils/Paste.cpp @@ -15,7 +15,6 @@ #include #include #include -#include #include #include #include @@ -95,12 +94,11 @@ ficheLogUpload( QObject* parent ) QUrl pasteUrl = QUrl( QString( responseText ).trimmed(), QUrl::StrictMode ); QString pasteUrlStr = pasteUrl.toString(); - QRegularExpression pasteUrlRegex( "^http[s]?://" + ficheHost ); QString pasteUrlFmt = parent->tr( "Install log posted to\n\n%1\n\nLink copied to clipboard" ); QString pasteUrlMsg = pasteUrlFmt.arg( pasteUrlStr ); - if ( pasteUrl.isValid() && pasteUrlRegex.match( pasteUrlStr ).hasMatch() ) + if ( pasteUrl.isValid() && pasteUrl.host() == ficheHost ) { QClipboard* clipboard = QApplication::clipboard(); clipboard->setText( pasteUrlStr, QClipboard::Clipboard ); From 3c6683bd982f33bc9795bcb76859812a050d3303 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 9 Mar 2021 14:51:59 +0100 Subject: [PATCH 10/17] [libcalamaresui] Rip out untyped data about upload server This doesn't compile, but indicates the **type** information desired about the (a) upload server. --- src/libcalamaresui/Branding.h | 23 ++++++++++++++++------- src/libcalamaresui/utils/Paste.h | 5 +---- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/libcalamaresui/Branding.h b/src/libcalamaresui/Branding.h index 87f71e862..52c27bfea 100644 --- a/src/libcalamaresui/Branding.h +++ b/src/libcalamaresui/Branding.h @@ -83,13 +83,16 @@ public: }; Q_ENUM( StyleEntry ) - enum UploadServerEntry : short + /** @brief Supported log-upload servers. + * + * 'None' is here as a fallback. + */ + enum UploadServerType : short { - Type, - URL, - Port + None, + Fiche }; - Q_ENUM( UploadServerEntry ) + Q_ENUM( UploadServerType ) /** @brief Setting for how much the main window may expand. */ enum class WindowExpansion @@ -218,6 +221,13 @@ public: ///@brief Which navigation flavor is configured PanelFlavor navigationFlavor() const { return m_navigationFlavor; } + /** @brief Upload server configuration + * + * This is both the type (which may be none, in which case the URL + * is irrelevant and usually empty) and the URL for the upload. + */ + QPair< UploadServerType, QUrl > uploadServer() const { return m_uploadServer; } + /** * Creates a map called "branding" in the global storage, and inserts an * entry for each of the branding strings. This makes the branding @@ -234,7 +244,6 @@ public slots: QString styleString( StyleEntry styleEntry ) const; QString imagePath( ImageEntry imageEntry ) const; - QString uploadServer( UploadServerEntry uploadServerEntry ) const; PanelSide sidebarSide() const { return m_sidebarSide; } PanelSide navigationSide() const { return m_navigationSide; } @@ -252,7 +261,7 @@ private: QMap< QString, QString > m_strings; QMap< QString, QString > m_images; QMap< QString, QString > m_style; - QMap< QString, QString > m_uploadServer; + QPair< UploadServerType, QUrl > m_uploadServer; /* The slideshow can be done in one of two ways: * - as a sequence of images diff --git a/src/libcalamaresui/utils/Paste.h b/src/libcalamaresui/utils/Paste.h index cde1f1841..784f2f28a 100644 --- a/src/libcalamaresui/utils/Paste.h +++ b/src/libcalamaresui/utils/Paste.h @@ -10,10 +10,9 @@ #ifndef UTILS_PASTE_H #define UTILS_PASTE_H -#include +#include class QObject; -class QString; namespace CalamaresUtils { @@ -24,8 +23,6 @@ namespace CalamaresUtils */ QString ficheLogUpload( QObject* parent ); -extern QStringList UploadServersList; - } // namespace CalamaresUtils #endif From bce6f3f1b74a3b0246e13d3ef581db806063791e Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 9 Mar 2021 14:56:37 +0100 Subject: [PATCH 11/17] [libcalamaresui] Adjust paste code to desired API Still doesn't compile because consumers are not ready. --- src/libcalamaresui/utils/Paste.cpp | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/libcalamaresui/utils/Paste.cpp b/src/libcalamaresui/utils/Paste.cpp index cdf8cc17b..b65223a43 100644 --- a/src/libcalamaresui/utils/Paste.cpp +++ b/src/libcalamaresui/utils/Paste.cpp @@ -39,17 +39,10 @@ logFileContents() namespace CalamaresUtils { -QStringList UploadServersList = { - "fiche" - // In future more serverTypes can be added as Calamares support them - // "none" serverType is explicitly not mentioned here -}; - QString ficheLogUpload( QObject* parent ) { - const QString& ficheHost = Calamares::Branding::instance()->uploadServer( Calamares::Branding::URL ); - quint16 fichePort = Calamares::Branding::instance()->uploadServer( Calamares::Branding::Port ).toInt(); + auto [ type, serverUrl ] = Calamares::Branding::instance()->uploadServer(); QByteArray pasteData = logFileContents(); if ( pasteData.isEmpty() ) @@ -59,7 +52,7 @@ ficheLogUpload( QObject* parent ) } QTcpSocket* socket = new QTcpSocket( parent ); - socket->connectToHost( ficheHost, fichePort ); + socket->connectToHost( serverUrl.host(), serverUrl.port() ); if ( !socket->waitForConnected() ) { @@ -68,7 +61,7 @@ ficheLogUpload( QObject* parent ) return QString(); } - cDebug() << "Connected to paste server" << ficheHost; + cDebug() << "Connected to paste server" << serverUrl.host(); socket->write( pasteData ); @@ -98,7 +91,7 @@ ficheLogUpload( QObject* parent ) QString pasteUrlFmt = parent->tr( "Install log posted to\n\n%1\n\nLink copied to clipboard" ); QString pasteUrlMsg = pasteUrlFmt.arg( pasteUrlStr ); - if ( pasteUrl.isValid() && pasteUrl.host() == ficheHost ) + if ( pasteUrl.isValid() && pasteUrl.host() == serverUrl.host() ) { QClipboard* clipboard = QApplication::clipboard(); clipboard->setText( pasteUrlStr, QClipboard::Clipboard ); From efec12d001417980d0d4fe690a8069adc305a5f4 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 9 Mar 2021 15:24:02 +0100 Subject: [PATCH 12/17] [libcalamares] Read structured upload-server info - Use just type and url, since port can be specified in a URL. Note that we only use host and port, not the scheme (or the path, for that matter). - Factor out understanding the *uploadServer* key to a function. --- src/branding/default/branding.desc | 18 +++++++------- src/libcalamaresui/Branding.cpp | 38 +++++++++++++++++++++--------- src/libcalamaresui/Branding.h | 5 ++-- 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/branding/default/branding.desc b/src/branding/default/branding.desc index cd83b02f1..90f92b5f1 100644 --- a/src/branding/default/branding.desc +++ b/src/branding/default/branding.desc @@ -220,15 +220,13 @@ slideshowAPI: 2 # These options are to customize online uploading of logs to pastebins: -# - type : Defines the kind of pastebin service to be used.Currently -# it accepts two values: -# - none : disables the pastebin functionality -# - fiche : use fiche pastebin server -# - url : Defines the address of pastebin service to be used. -# Takes string as input -# - port : Defines the port number to be used to send logs. Takes -# integer as input +# - type : Defines the kind of pastebin service to be used. Currently +# it accepts two values: +# - none : disables the pastebin functionality +# - fiche : use fiche pastebin server +# - url : Defines the address of pastebin service to be used. +# Takes string as input. Important bits are the host and port, +# the scheme is not used. uploadServer : type : "fiche" - url : "termbin.com" - port : 9999 + url : "http://termbin.com:9999" diff --git a/src/libcalamaresui/Branding.cpp b/src/libcalamaresui/Branding.cpp index a71675e38..3668c0b4b 100644 --- a/src/libcalamaresui/Branding.cpp +++ b/src/libcalamaresui/Branding.cpp @@ -138,6 +138,32 @@ loadStrings( QMap< QString, QString >& map, } } +static Branding::UploadServerInfo +uploadServerFromMap( const QVariantMap& map ) +{ + using Type = Branding::UploadServerType; + // *INDENT-OFF* + // clang-format off + static const NamedEnumTable< Type > names { + { "none", Type::None }, + { "fiche", Type::Fiche } + }; + // clang-format on + // *INDENT-ON* + + QString typestring = map[ "type" ].toString(); + QString urlstring = map[ "url" ].toString(); + + if ( typestring.isEmpty() || urlstring.isEmpty() ) + { + return Branding::UploadServerInfo( Branding::UploadServerType::None, QUrl() ); + } + + bool bogus = false; // we don't care about type-name lookup success here + return Branding::UploadServerInfo( names.find( typestring, bogus ), + QUrl( urlstring, QUrl::ParsingMode::StrictMode ) ); +} + /** @brief Load the @p map with strings from @p config * * If os-release is supported (with KF5 CoreAddons >= 5.58) then @@ -227,11 +253,7 @@ Branding::Branding( const QString& brandingFilePath, QObject* parent ) } ); loadStrings( m_style, doc, "style", []( const QString& s ) -> QString { return s; } ); - const QVariantMap temp = CalamaresUtils::yamlMapToVariant( doc[ "uploadServer" ] ); - for ( auto it = temp.constBegin(); it != temp.constEnd(); ++it ) - { - m_uploadServer.insert( it.key(), it.value().toString() ); - } + m_uploadServer = uploadServerFromMap( CalamaresUtils::yamlMapToVariant( doc[ "uploadServer" ] ) ); } catch ( YAML::Exception& e ) { @@ -292,12 +314,6 @@ Branding::imagePath( Branding::ImageEntry imageEntry ) const return m_images.value( s_imageEntryStrings.value( imageEntry ) ); } -QString -Branding::uploadServer( Branding::UploadServerEntry uploadServerEntry ) const -{ - return m_uploadServer.value( s_uploadServerStrings.value( uploadServerEntry ) ); -} - QPixmap Branding::image( Branding::ImageEntry imageEntry, const QSize& size ) const { diff --git a/src/libcalamaresui/Branding.h b/src/libcalamaresui/Branding.h index 52c27bfea..831b2adec 100644 --- a/src/libcalamaresui/Branding.h +++ b/src/libcalamaresui/Branding.h @@ -226,7 +226,8 @@ public: * This is both the type (which may be none, in which case the URL * is irrelevant and usually empty) and the URL for the upload. */ - QPair< UploadServerType, QUrl > uploadServer() const { return m_uploadServer; } + using UploadServerInfo = QPair< UploadServerType, QUrl >; + UploadServerInfo uploadServer() const { return m_uploadServer; } /** * Creates a map called "branding" in the global storage, and inserts an @@ -261,7 +262,7 @@ private: QMap< QString, QString > m_strings; QMap< QString, QString > m_images; QMap< QString, QString > m_style; - QPair< UploadServerType, QUrl > m_uploadServer; + UploadServerInfo m_uploadServer; /* The slideshow can be done in one of two ways: * - as a sequence of images From 1ff854f05d66f03955406098be3549e1a6f69961 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 9 Mar 2021 15:32:46 +0100 Subject: [PATCH 13/17] [libcalamaresui] Push upload to a more abstract API - have a namespace Paste with just one entry point, which will handle untangling type &c. This doesn't compile, but indicates the direction to take the API --- src/libcalamaresui/ViewManager.cpp | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/libcalamaresui/ViewManager.cpp b/src/libcalamaresui/ViewManager.cpp index 184dceb32..30614fdfa 100644 --- a/src/libcalamaresui/ViewManager.cpp +++ b/src/libcalamaresui/ViewManager.cpp @@ -142,8 +142,7 @@ ViewManager::insertViewStep( int before, ViewStep* step ) void ViewManager::onInstallationFailed( const QString& message, const QString& details ) { - QString serverType = Calamares::Branding::instance()->uploadServer( Calamares::Branding::Type ); - bool shouldOfferWebPaste = CalamaresUtils::UploadServersList.contains( serverType ); + bool shouldOfferWebPaste = Calamares::Branding::instance()->uploadServer().first != Calamares::Branding::UploadServerType::None; cError() << "Installation failed:" << message; cDebug() << Logger::SubEntry << "- message:" << message; @@ -186,18 +185,10 @@ ViewManager::onInstallationFailed( const QString& message, const QString& detail msgBox->show(); cDebug() << "Calamares will quit when the dialog closes."; - connect( msgBox, &QMessageBox::buttonClicked, [msgBox, serverType]( QAbstractButton* button ) { + connect( msgBox, &QMessageBox::buttonClicked, [msgBox]( QAbstractButton* button ) { if ( msgBox->buttonRole( button ) == QMessageBox::ButtonRole::YesRole ) { - QString pasteUrlMsg; - if ( serverType == "fiche" ) - { - pasteUrlMsg = CalamaresUtils::ficheLogUpload( msgBox ); - } - else - { - pasteUrlMsg = QString(); - } + QString pasteUrlMsg = CalamaresUtils::Paste::doLogUpload(); QString pasteUrlTitle = tr( "Install Log Paste URL" ); if ( pasteUrlMsg.isEmpty() ) From 81badc36f44bd8618ffada5cab481be5bf387999 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 9 Mar 2021 15:42:21 +0100 Subject: [PATCH 14/17] [libcalamaresui] Implement abstract doLogUpload() API This is a "do the right thing" function, which then calls the implementation-specific code for each type. --- src/libcalamaresui/ViewManager.cpp | 5 +++-- src/libcalamaresui/utils/Paste.cpp | 30 +++++++++++++++++++++++------- src/libcalamaresui/utils/Paste.h | 5 ++++- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/libcalamaresui/ViewManager.cpp b/src/libcalamaresui/ViewManager.cpp index 30614fdfa..3d0fccb78 100644 --- a/src/libcalamaresui/ViewManager.cpp +++ b/src/libcalamaresui/ViewManager.cpp @@ -142,7 +142,8 @@ ViewManager::insertViewStep( int before, ViewStep* step ) void ViewManager::onInstallationFailed( const QString& message, const QString& details ) { - bool shouldOfferWebPaste = Calamares::Branding::instance()->uploadServer().first != Calamares::Branding::UploadServerType::None; + bool shouldOfferWebPaste + = Calamares::Branding::instance()->uploadServer().first != Calamares::Branding::UploadServerType::None; cError() << "Installation failed:" << message; cDebug() << Logger::SubEntry << "- message:" << message; @@ -188,7 +189,7 @@ ViewManager::onInstallationFailed( const QString& message, const QString& detail connect( msgBox, &QMessageBox::buttonClicked, [msgBox]( QAbstractButton* button ) { if ( msgBox->buttonRole( button ) == QMessageBox::ButtonRole::YesRole ) { - QString pasteUrlMsg = CalamaresUtils::Paste::doLogUpload(); + QString pasteUrlMsg = CalamaresUtils::Paste::doLogUpload( msgBox ); QString pasteUrlTitle = tr( "Install Log Paste URL" ); if ( pasteUrlMsg.isEmpty() ) diff --git a/src/libcalamaresui/utils/Paste.cpp b/src/libcalamaresui/utils/Paste.cpp index b65223a43..e6ac63f88 100644 --- a/src/libcalamaresui/utils/Paste.cpp +++ b/src/libcalamaresui/utils/Paste.cpp @@ -36,14 +36,10 @@ logFileContents() return pasteSourceFile.read( 16384 /* bytes */ ); } -namespace CalamaresUtils -{ -QString -ficheLogUpload( QObject* parent ) +static QString +ficheLogUpload( const QUrl& serverUrl, QObject* parent ) { - auto [ type, serverUrl ] = Calamares::Branding::instance()->uploadServer(); - QByteArray pasteData = logFileContents(); if ( pasteData.isEmpty() ) { @@ -110,4 +106,24 @@ ficheLogUpload( QObject* parent ) cDebug() << Logger::SubEntry << "Paste server results:" << pasteUrlMsg; return pasteUrlMsg; } -} // namespace CalamaresUtils + +QString +CalamaresUtils::Paste::doLogUpload( QObject* parent ) +{ + auto [ type, serverUrl ] = Calamares::Branding::instance()->uploadServer(); + if ( !serverUrl.isValid() ) + { + cWarning() << "Upload configure with invalid URL"; + return QString(); + } + + switch ( type ) + { + case Calamares::Branding::UploadServerType::None: + cWarning() << "No upload configured."; + return QString(); + case Calamares::Branding::UploadServerType::Fiche: + return ficheLogUpload( serverUrl, parent ); + } + return QString(); +} diff --git a/src/libcalamaresui/utils/Paste.h b/src/libcalamaresui/utils/Paste.h index 784f2f28a..6258e57a0 100644 --- a/src/libcalamaresui/utils/Paste.h +++ b/src/libcalamaresui/utils/Paste.h @@ -16,12 +16,15 @@ class QObject; namespace CalamaresUtils { +namespace Paste +{ /** @brief Send the current log file to a pastebin * * Returns the (string) URL that the pastebin gives us. */ -QString ficheLogUpload( QObject* parent ); +QString doLogUpload( QObject* parent ); +} // namespace Paste } // namespace CalamaresUtils From 846d6abaa83b36223fc43c03c6dd7cc1fbc2fe2a Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 9 Mar 2021 15:51:24 +0100 Subject: [PATCH 15/17] [libcalamaresui] Move message- and clipboard handling - The Paste API promises just a (string) URL back, not a whole message, so return just the URL from the abstract API and the concrete (fiche) implementation. - Set clipboard contents from the UI - Build (translated) message in the UI code --- src/libcalamaresui/ViewManager.cpp | 25 ++++++++++++++++++------- src/libcalamaresui/utils/Paste.cpp | 20 ++------------------ 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/src/libcalamaresui/ViewManager.cpp b/src/libcalamaresui/ViewManager.cpp index 3d0fccb78..b22d34c88 100644 --- a/src/libcalamaresui/ViewManager.cpp +++ b/src/libcalamaresui/ViewManager.cpp @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -189,16 +190,26 @@ ViewManager::onInstallationFailed( const QString& message, const QString& detail connect( msgBox, &QMessageBox::buttonClicked, [msgBox]( QAbstractButton* button ) { if ( msgBox->buttonRole( button ) == QMessageBox::ButtonRole::YesRole ) { - QString pasteUrlMsg = CalamaresUtils::Paste::doLogUpload( msgBox ); - - QString pasteUrlTitle = tr( "Install Log Paste URL" ); - if ( pasteUrlMsg.isEmpty() ) + QString pasteUrl = CalamaresUtils::Paste::doLogUpload( msgBox ); + QString pasteUrlMessage; + if ( pasteUrl.isEmpty() ) + { + pasteUrlMessage = tr( "The upload was unsuccessful. No web-paste was done." ); + } + else { - pasteUrlMsg = tr( "The upload was unsuccessful. No web-paste was done." ); + QClipboard* clipboard = QApplication::clipboard(); + clipboard->setText( pasteUrl, QClipboard::Clipboard ); + + if ( clipboard->supportsSelection() ) + { + clipboard->setText( pasteUrl, QClipboard::Selection ); + } + QString pasteUrlFmt = tr( "Install log posted to\n\n%1\n\nLink copied to clipboard" ); + pasteUrlMessage = pasteUrlFmt.arg( pasteUrl ); } - // TODO: make the URL clickable, or copy it to the clipboard automatically - QMessageBox::critical( nullptr, pasteUrlTitle, pasteUrlMsg ); + QMessageBox::critical( nullptr, tr( "Install Log Paste URL" ), pasteUrlMessage ); } QApplication::quit(); } ); diff --git a/src/libcalamaresui/utils/Paste.cpp b/src/libcalamaresui/utils/Paste.cpp index e6ac63f88..d723e182c 100644 --- a/src/libcalamaresui/utils/Paste.cpp +++ b/src/libcalamaresui/utils/Paste.cpp @@ -12,10 +12,7 @@ #include "Branding.h" #include "utils/Logger.h" -#include -#include #include -#include #include #include @@ -82,29 +79,16 @@ ficheLogUpload( const QUrl& serverUrl, QObject* parent ) socket->close(); QUrl pasteUrl = QUrl( QString( responseText ).trimmed(), QUrl::StrictMode ); - QString pasteUrlStr = pasteUrl.toString(); - - QString pasteUrlFmt = parent->tr( "Install log posted to\n\n%1\n\nLink copied to clipboard" ); - QString pasteUrlMsg = pasteUrlFmt.arg( pasteUrlStr ); - if ( pasteUrl.isValid() && pasteUrl.host() == serverUrl.host() ) { - QClipboard* clipboard = QApplication::clipboard(); - clipboard->setText( pasteUrlStr, QClipboard::Clipboard ); - - if ( clipboard->supportsSelection() ) - { - clipboard->setText( pasteUrlStr, QClipboard::Selection ); - } + cDebug() << Logger::SubEntry << "Paste server results:" << pasteUrl; + return pasteUrl.toString(); } else { cError() << "No data from paste server"; return QString(); } - - cDebug() << Logger::SubEntry << "Paste server results:" << pasteUrlMsg; - return pasteUrlMsg; } QString From 44ec8a7c0b75f56c89accd1391c8e748f23efb34 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 9 Mar 2021 17:22:48 +0100 Subject: [PATCH 16/17] [libcalamaresui] Improve testability - mark functions with STATICTEST so they can be compiled into a test - move logfile-reading so we can call the pastebin-upload functions with an arbitrary payload. --- src/libcalamaresui/utils/Paste.cpp | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/libcalamaresui/utils/Paste.cpp b/src/libcalamaresui/utils/Paste.cpp index d723e182c..ee54d265a 100644 --- a/src/libcalamaresui/utils/Paste.cpp +++ b/src/libcalamaresui/utils/Paste.cpp @@ -10,6 +10,7 @@ #include "Paste.h" #include "Branding.h" +#include "DllMacro.h" #include "utils/Logger.h" #include @@ -20,7 +21,7 @@ * * Returns an empty QByteArray() on any kind of error. */ -static QByteArray +STATICTEST QByteArray logFileContents() { QFile pasteSourceFile( Logger::logFile() ); @@ -34,16 +35,9 @@ logFileContents() } -static QString -ficheLogUpload( const QUrl& serverUrl, QObject* parent ) +STATICTEST QString +ficheLogUpload( const QByteArray& pasteData, QUrl& serverUrl, QObject* parent ) { - QByteArray pasteData = logFileContents(); - if ( pasteData.isEmpty() ) - { - // An error has already been logged - return QString(); - } - QTcpSocket* socket = new QTcpSocket( parent ); socket->connectToHost( serverUrl.host(), serverUrl.port() ); @@ -100,6 +94,18 @@ CalamaresUtils::Paste::doLogUpload( QObject* parent ) cWarning() << "Upload configure with invalid URL"; return QString(); } + if ( type == Calamares::Branding::UploadServerType::None ) + { + // Early return to avoid reading the log file + return QString(); + } + + QByteArray pasteData = logFileContents(); + if ( pasteData.isEmpty() ) + { + // An error has already been logged + return QString(); + } switch ( type ) { @@ -107,7 +113,7 @@ CalamaresUtils::Paste::doLogUpload( QObject* parent ) cWarning() << "No upload configured."; return QString(); case Calamares::Branding::UploadServerType::Fiche: - return ficheLogUpload( serverUrl, parent ); + return ficheLogUpload( pasteData, serverUrl, parent ); } return QString(); } From a1ed30382028211efa0925cf39e11d16a1d2c267 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 9 Mar 2021 17:55:10 +0100 Subject: [PATCH 17/17] [libcalamaresui] Add test for Paste This tests only the termbin ("fiche") paste by sending it a derpy fixed string. Prints the resulting URL, doesn't verify in particular. It'd be rude to run this test too often. --- src/libcalamaresui/CMakeLists.txt | 9 ++++ src/libcalamaresui/utils/Paste.cpp | 2 +- src/libcalamaresui/utils/TestPaste.cpp | 67 ++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/libcalamaresui/utils/TestPaste.cpp diff --git a/src/libcalamaresui/CMakeLists.txt b/src/libcalamaresui/CMakeLists.txt index f48008c2d..4afdc6614 100644 --- a/src/libcalamaresui/CMakeLists.txt +++ b/src/libcalamaresui/CMakeLists.txt @@ -111,3 +111,12 @@ foreach( subdir modulesystem utils viewpages widgets ) file( GLOB subdir_headers "${subdir}/*.h" ) install( FILES ${subdir_headers} DESTINATION include/libcalamares/${subdir} ) endforeach() + +calamares_add_test( + test_libcalamaresuipaste + SOURCES + utils/TestPaste.cpp + utils/Paste.cpp + LIBRARIES + calamaresui +) diff --git a/src/libcalamaresui/utils/Paste.cpp b/src/libcalamaresui/utils/Paste.cpp index ee54d265a..2d2f5404e 100644 --- a/src/libcalamaresui/utils/Paste.cpp +++ b/src/libcalamaresui/utils/Paste.cpp @@ -36,7 +36,7 @@ logFileContents() STATICTEST QString -ficheLogUpload( const QByteArray& pasteData, QUrl& serverUrl, QObject* parent ) +ficheLogUpload( const QByteArray& pasteData, const QUrl& serverUrl, QObject* parent ) { QTcpSocket* socket = new QTcpSocket( parent ); socket->connectToHost( serverUrl.host(), serverUrl.port() ); diff --git a/src/libcalamaresui/utils/TestPaste.cpp b/src/libcalamaresui/utils/TestPaste.cpp new file mode 100644 index 000000000..ff75be02d --- /dev/null +++ b/src/libcalamaresui/utils/TestPaste.cpp @@ -0,0 +1,67 @@ +/* === This file is part of Calamares - === + * + * SPDX-FileCopyrightText: 2021 Adriaan de Groot + * SPDX-License-Identifier: GPL-3.0-or-later + * + * + * Calamares is Free Software: see the License-Identifier above. + * + * + */ + +#include "Paste.h" + +#include "utils/Logger.h" + +#include +#include + +extern QByteArray logFileContents(); +extern QString ficheLogUpload( const QByteArray& pasteData, const QUrl& serverUrl, QObject* parent ); + +class TestPaste : public QObject +{ + Q_OBJECT + +public: + TestPaste() {} + ~TestPaste() override {} + +private Q_SLOTS: + void testGetLogFile(); + void testFichePaste(); +}; + +void +TestPaste::testGetLogFile() +{ + // This test assumes nothing **else** has set up logging yet + QByteArray b = logFileContents(); + QVERIFY( b.isEmpty() ); + + Logger::setupLogLevel( Logger::LOGDEBUG ); + Logger::setupLogfile(); + + b = logFileContents(); + QVERIFY( !b.isEmpty() ); +} + +void +TestPaste::testFichePaste() +{ + QString blabla( "the quick brown fox tested Calamares and found it rubbery" ); + QDateTime now = QDateTime::currentDateTime(); + + QByteArray d = ( blabla + now.toString() ).toUtf8(); + QString s = ficheLogUpload( d, QUrl( "http://termbin.com:9999" ), nullptr ); + + cDebug() << "Paste data to" << s; + QVERIFY( !s.isEmpty() ); +} + + +QTEST_GUILESS_MAIN( TestPaste ) + +#include "utils/moc-warnings.h" + +#include "TestPaste.moc"