libcalamaresui: factor out the pastebin functionality

- While called from the ViewManager (to post the debug log)
   this isn't really part of the ViewManager itself, so factor
   out the pasting code into its own file.
This commit is contained in:
Adriaan de Groot 2019-07-30 15:09:25 +02:00
parent 8f7d183a0c
commit d49ddf6463
4 changed files with 144 additions and 72 deletions

View File

@ -16,6 +16,7 @@ set( calamaresui_SOURCES
utils/CalamaresUtilsGui.cpp utils/CalamaresUtilsGui.cpp
utils/DebugWindow.cpp utils/DebugWindow.cpp
utils/ImageRegistry.cpp utils/ImageRegistry.cpp
utils/Paste.cpp
utils/qjsonmodel.cpp utils/qjsonmodel.cpp
utils/qjsonitem.cpp utils/qjsonitem.cpp

View File

@ -21,23 +21,23 @@
#include "ViewManager.h" #include "ViewManager.h"
#include "utils/Logger.h"
#include "viewpages/BlankViewStep.h" #include "viewpages/BlankViewStep.h"
#include "viewpages/ViewStep.h" #include "viewpages/ViewStep.h"
#include "Branding.h"
#include "ExecutionViewStep.h" #include "ExecutionViewStep.h"
#include "JobQueue.h" #include "JobQueue.h"
#include "utils/Retranslator.h"
#include "Branding.h"
#include "Settings.h" #include "Settings.h"
#include "utils/Logger.h"
#include "utils/Paste.h"
#include "utils/Retranslator.h"
#include <QApplication> #include <QApplication>
#include <QBoxLayout> #include <QBoxLayout>
#include <QFile> #include <QFile>
#include <QMessageBox> #include <QMessageBox>
#include <QMetaObject> #include <QMetaObject>
#include <QRegularExpression>
#include <QTcpSocket>
#include <QUrl>
namespace Calamares namespace Calamares
{ {
@ -193,9 +193,7 @@ ViewManager::insertViewStep( int before, ViewStep* step )
void void
ViewManager::onInstallationFailed( const QString& message, const QString& details ) ViewManager::onInstallationFailed( const QString& message, const QString& details )
{ {
bool shouldOfferWebPaste = true; // TODO: config var bool shouldOfferWebPaste = true; // TODO: config var
QString ficheHost = "termbin.com"; // TODO: config var
quint16 fichePort = 9999; // TODO: config var
cError() << "Installation failed:"; cError() << "Installation failed:";
cDebug() << "- message:" << message; cDebug() << "- message:" << message;
@ -205,8 +203,6 @@ quint16 fichePort = 9999; // TODO: config var
? tr( "Setup Failed" ) ? tr( "Setup Failed" )
: tr( "Installation Failed" ); : tr( "Installation Failed" );
QString pasteMsg = tr( "Would you like to paste the install log to the web?" ); 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 = "<p>" + message + "</p>"; QString text = "<p>" + message + "</p>";
if ( !details.isEmpty() ) if ( !details.isEmpty() )
text += "<p>" + details + "</p>"; text += "<p>" + details + "</p>";
@ -235,7 +231,7 @@ quint16 fichePort = 9999; // TODO: config var
cDebug() << "Calamares will quit when the dialog closes."; cDebug() << "Calamares will quit when the dialog closes.";
connect( msgBox, &QMessageBox::buttonClicked, connect( msgBox, &QMessageBox::buttonClicked,
[msgBox, ficheHost, fichePort, pasteUrlFmt, pasteUrlTitle] ( QAbstractButton* button ) [msgBox] ( QAbstractButton* button )
{ {
if ( button->text() != tr( "&Yes" ) ) if ( button->text() != tr( "&Yes" ) )
{ {
@ -243,69 +239,15 @@ quint16 fichePort = 9999; // TODO: config var
return; return;
} }
QFile pasteSourceFile( Logger::logFile() ); // TODO: host and port should be configurable
if ( !pasteSourceFile.open( QIODevice::ReadOnly | QIODevice::Text ) ) 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"; pasteUrlMsg = tr( "The upload was unsuccessful. No web-paste was done." );
return;
} }
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(); QMessageBox* pasteUrlMsgBox = new QMessageBox();
pasteUrlMsgBox->setIcon( QMessageBox::Critical ); pasteUrlMsgBox->setIcon( QMessageBox::Critical );
pasteUrlMsgBox->setWindowTitle( pasteUrlTitle ); pasteUrlMsgBox->setWindowTitle( pasteUrlTitle );

View File

@ -0,0 +1,99 @@
/* === This file is part of Calamares - <https://github.com/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 <http://www.gnu.org/licenses/>.
*/
#include "Paste.h"
#include "utils/Logger.h"
#include <QFile>
#include <QRegularExpression>
#include <QTcpSocket>
#include <QUrl>
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

View File

@ -0,0 +1,30 @@
/* === This file is part of Calamares - <https://github.com/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 <http://www.gnu.org/licenses/>.
*/
#ifndef UTILS_PASTE_H
#define UTILS_PASTE_H
class QObject;
class QString;
namespace CalamaresUtils
{
QString pastebin( QObject* parent, const QString& ficheHost, int fichePort );
}
#endif