diff --git a/src/libcalamaresui/CMakeLists.txt b/src/libcalamaresui/CMakeLists.txt index f6c8c9ac7..703a8bbe1 100644 --- a/src/libcalamaresui/CMakeLists.txt +++ b/src/libcalamaresui/CMakeLists.txt @@ -16,6 +16,7 @@ set( calamaresui_SOURCES utils/CalamaresUtilsGui.cpp utils/DebugWindow.cpp utils/ImageRegistry.cpp + utils/Paste.cpp utils/qjsonmodel.cpp utils/qjsonitem.cpp diff --git a/src/libcalamaresui/ViewManager.cpp b/src/libcalamaresui/ViewManager.cpp index ae146d0bd..b1334ecfa 100644 --- a/src/libcalamaresui/ViewManager.cpp +++ b/src/libcalamaresui/ViewManager.cpp @@ -21,23 +21,23 @@ #include "ViewManager.h" -#include "utils/Logger.h" #include "viewpages/BlankViewStep.h" #include "viewpages/ViewStep.h" + +#include "Branding.h" #include "ExecutionViewStep.h" #include "JobQueue.h" -#include "utils/Retranslator.h" -#include "Branding.h" #include "Settings.h" +#include "utils/Logger.h" +#include "utils/Paste.h" +#include "utils/Retranslator.h" + #include #include #include #include #include -#include -#include -#include namespace Calamares { @@ -193,9 +193,7 @@ ViewManager::insertViewStep( int before, ViewStep* step ) void ViewManager::onInstallationFailed( const QString& message, const QString& details ) { -bool shouldOfferWebPaste = true; // TODO: config var -QString ficheHost = "termbin.com"; // TODO: config var -quint16 fichePort = 9999; // TODO: config var + bool shouldOfferWebPaste = true; // TODO: config var cError() << "Installation failed:"; cDebug() << "- message:" << message; @@ -205,8 +203,6 @@ quint16 fichePort = 9999; // TODO: config var ? tr( "Setup Failed" ) : tr( "Installation Failed" ); QString pasteMsg = tr( "Would you like to paste the install log to the web?" ); - QString pasteUrlFmt = tr( "Install log posted to:\n%1" ); - QString pasteUrlTitle = tr( "Install Log Paste URL" ); QString text = "

" + message + "

"; if ( !details.isEmpty() ) text += "

" + details + "

"; @@ -235,7 +231,7 @@ quint16 fichePort = 9999; // TODO: config var cDebug() << "Calamares will quit when the dialog closes."; connect( msgBox, &QMessageBox::buttonClicked, - [msgBox, ficheHost, fichePort, pasteUrlFmt, pasteUrlTitle] ( QAbstractButton* button ) + [msgBox] ( QAbstractButton* button ) { if ( button->text() != tr( "&Yes" ) ) { @@ -243,69 +239,15 @@ quint16 fichePort = 9999; // TODO: config var return; } - QFile pasteSourceFile( Logger::logFile() ); - if ( !pasteSourceFile.open( QIODevice::ReadOnly | QIODevice::Text ) ) + // TODO: host and port should be configurable + QString pasteUrlMsg = CalamaresUtils::pastebin( msgBox, QStringLiteral( "termbin.com" ), 9999 ); + + QString pasteUrlTitle = tr( "Install Log Paste URL" ); + if ( pasteUrlMsg.isEmpty() ) { - cError() << "Could not open log file"; - return; + pasteUrlMsg = tr( "The upload was unsuccessful. No web-paste was done." ); } - QByteArray pasteData; - while ( !pasteSourceFile.atEnd() ) - { - pasteData += pasteSourceFile.readLine(); - } - - QTcpSocket* socket = new QTcpSocket(msgBox); - socket->connectToHost( ficheHost, fichePort ); - - if ( !socket->waitForConnected() ) - { - cError() << "Could not connect to paste server"; - socket->close(); - return; - } - - cDebug() << "Connected to paste server"; - - socket->write( pasteData ); - - if ( !socket->waitForBytesWritten() ) - { - cError() << "Could not write to paste server"; - socket->close(); - return; - } - - cDebug() << "Paste data written to paste server"; - - if ( !socket->waitForReadyRead() ) - { - cError() << "No data from paste server"; - socket->close(); - return; - } - - cDebug() << "Reading response from paste server"; - - char resp[1024]; resp[0] = '\0'; - qint64 nBytesRead = socket->readLine(resp, 1024); - socket->close(); - - QUrl pasteUrl = QUrl( QString( resp ).trimmed(), QUrl::StrictMode ); - QString pasteUrlStr = pasteUrl.toString() ; - QRegularExpression pasteUrlRegex( "^http[s]?://" + ficheHost ); - QString pasteUrlMsg = QString( pasteUrlFmt ).arg( pasteUrlStr ); - - if ( nBytesRead < 8 || !pasteUrl.isValid() || - !pasteUrlRegex.match( pasteUrlStr ).hasMatch() ) - { - cError() << "No data from paste server"; - return; - } - - cDebug() << pasteUrlMsg; - QMessageBox* pasteUrlMsgBox = new QMessageBox(); pasteUrlMsgBox->setIcon( QMessageBox::Critical ); pasteUrlMsgBox->setWindowTitle( pasteUrlTitle ); diff --git a/src/libcalamaresui/utils/Paste.cpp b/src/libcalamaresui/utils/Paste.cpp new file mode 100644 index 000000000..7833d18d6 --- /dev/null +++ b/src/libcalamaresui/utils/Paste.cpp @@ -0,0 +1,99 @@ +/* === This file is part of Calamares - === + * + * Copyright 2019, Bill Auger + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#include "Paste.h" + +#include "utils/Logger.h" + +#include +#include +#include +#include + +namespace CalamaresUtils +{ + +QString +pastebin( QObject* parent, const QString& ficheHost, int fichePort ) +{ + QString pasteUrlFmt = parent->tr( "Install log posted to:\n%1" ); + QFile pasteSourceFile( Logger::logFile() ); + if ( !pasteSourceFile.open( QIODevice::ReadOnly | QIODevice::Text ) ) + { + cError() << "Could not open log file"; + return QString(); + } + + QByteArray pasteData; + while ( !pasteSourceFile.atEnd() ) + { + pasteData += pasteSourceFile.readLine(); + } + + QTcpSocket* socket = new QTcpSocket( parent ); + socket->connectToHost( ficheHost, fichePort ); + + if ( !socket->waitForConnected() ) + { + cError() << "Could not connect to paste server"; + socket->close(); + return QString(); + } + + cDebug() << "Connected to paste server"; + + socket->write( pasteData ); + + if ( !socket->waitForBytesWritten() ) + { + cError() << "Could not write to paste server"; + socket->close(); + return QString(); + } + + cDebug() << "Paste data written to paste server"; + + if ( !socket->waitForReadyRead() ) + { + cError() << "No data from paste server"; + socket->close(); + return QString(); + } + + cDebug() << "Reading response from paste server"; + + char resp[ 1024 ]; + resp[ 0 ] = '\0'; + qint64 nBytesRead = socket->readLine( resp, 1024 ); + socket->close(); + + QUrl pasteUrl = QUrl( QString( resp ).trimmed(), QUrl::StrictMode ); + QString pasteUrlStr = pasteUrl.toString(); + QRegularExpression pasteUrlRegex( "^http[s]?://" + ficheHost ); + QString pasteUrlMsg = QString( pasteUrlFmt ).arg( pasteUrlStr ); + + if ( nBytesRead < 8 || !pasteUrl.isValid() || !pasteUrlRegex.match( pasteUrlStr ).hasMatch() ) + { + cError() << "No data from paste server"; + return QString(); + } + + cDebug() << "Paste server results:" << pasteUrlMsg; + return pasteUrlMsg; +} +} // namespace CalamaresUtils diff --git a/src/libcalamaresui/utils/Paste.h b/src/libcalamaresui/utils/Paste.h new file mode 100644 index 000000000..4def700fd --- /dev/null +++ b/src/libcalamaresui/utils/Paste.h @@ -0,0 +1,30 @@ +/* === This file is part of Calamares - === + * + * Copyright 2019, Bill Auger + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#ifndef UTILS_PASTE_H +#define UTILS_PASTE_H + +class QObject; +class QString; + +namespace CalamaresUtils +{ +QString pastebin( QObject* parent, const QString& ficheHost, int fichePort ); +} + +#endif