2012-02-20 21:47:14 +00:00
|
|
|
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
|
2009-01-28 20:55:45 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include <QDir>
|
2018-03-22 17:07:10 +00:00
|
|
|
#include <QTimer>
|
2011-05-29 05:08:37 +00:00
|
|
|
#include "externalviewer.hh"
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2018-03-22 17:07:10 +00:00
|
|
|
ExternalViewer::ExternalViewer(
|
|
|
|
const char * data, int size, QString const & extension, QString const & viewerCmdLine_, QObject * parent ):
|
2009-01-28 20:55:45 +00:00
|
|
|
QObject( parent ),
|
|
|
|
tempFile( QDir::temp().filePath( QString( "gd-XXXXXXXX." ) + extension ) ),
|
|
|
|
viewer( this ),
|
2011-05-30 01:05:28 +00:00
|
|
|
viewerCmdLine( viewerCmdLine_ )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2018-03-22 17:07:10 +00:00
|
|
|
if ( !tempFile.open() || tempFile.write( data, size ) != size ) {
|
2009-01-28 20:55:45 +00:00
|
|
|
throw exCantCreateTempFile();
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
tempFileName = tempFile.fileName(); // For some reason it loses it after it was closed()
|
|
|
|
|
2013-03-15 20:22:49 +00:00
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
// Issue #239
|
|
|
|
tempFileName = QDir::toNativeSeparators( tempFileName );
|
|
|
|
#endif
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
tempFile.close();
|
|
|
|
|
2014-05-10 21:02:31 +00:00
|
|
|
qDebug( "%s", tempFile.fileName().toLocal8Bit().data() );
|
2009-04-12 23:53:13 +00:00
|
|
|
}
|
|
|
|
|
2022-01-09 08:35:07 +00:00
|
|
|
void ExternalViewer::start()
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( &viewer, &QProcess::finished, this, &QObject::deleteLater );
|
|
|
|
connect( &viewer, &QProcess::errorOccurred, this, &QObject::deleteLater );
|
2011-05-29 05:08:37 +00:00
|
|
|
|
2024-09-03 16:54:50 +00:00
|
|
|
QStringList args = QProcess::splitCommand( viewerCmdLine );
|
2011-05-30 01:05:28 +00:00
|
|
|
if ( !args.isEmpty() ) {
|
2024-09-03 16:54:50 +00:00
|
|
|
const QString program = args.takeFirst();
|
2011-05-30 01:05:28 +00:00
|
|
|
args.push_back( tempFileName );
|
|
|
|
viewer.start( program, args, QIODevice::NotOpen );
|
2024-09-03 16:54:50 +00:00
|
|
|
if ( !viewer.waitForStarted() ) {
|
2011-05-30 01:05:28 +00:00
|
|
|
throw exCantRunViewer( viewerCmdLine.toUtf8().data() );
|
2024-09-03 16:54:50 +00:00
|
|
|
}
|
2011-05-30 01:05:28 +00:00
|
|
|
}
|
2024-09-03 16:54:50 +00:00
|
|
|
else {
|
2011-05-30 01:05:28 +00:00
|
|
|
throw exCantRunViewer( tr( "the viewer program name is empty" ).toUtf8().data() );
|
2024-09-03 16:54:50 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2018-03-22 17:07:10 +00:00
|
|
|
|
|
|
|
bool ExternalViewer::stop()
|
|
|
|
{
|
|
|
|
if ( viewer.state() == QProcess::NotRunning ) {
|
|
|
|
return true;
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2018-03-22 17:07:10 +00:00
|
|
|
viewer.terminate();
|
2022-12-26 02:08:17 +00:00
|
|
|
QTimer::singleShot( 1000, &viewer, &QProcess::kill ); // In case terminate() fails.
|
2018-03-22 17:07:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalViewer::stopSynchronously()
|
|
|
|
{
|
|
|
|
// This implementation comes straight from QProcess::~QProcess().
|
|
|
|
if ( viewer.state() == QProcess::NotRunning ) {
|
|
|
|
return;
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2018-03-22 17:07:10 +00:00
|
|
|
viewer.kill();
|
|
|
|
viewer.waitForFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
void stopAndDestroySynchronously( ExternalViewer * viewer )
|
|
|
|
{
|
|
|
|
if ( !viewer ) {
|
|
|
|
return;
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2018-03-22 17:07:10 +00:00
|
|
|
viewer->stopSynchronously();
|
|
|
|
delete viewer;
|
|
|
|
}
|