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"
|
2011-05-30 01:05:28 +00:00
|
|
|
#include "parsecmdline.hh"
|
2013-11-16 18:34:09 +00:00
|
|
|
#include "gddebug.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)
|
2022-01-09 08:35:07 +00:00
|
|
|
:
|
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();
|
|
|
|
|
|
|
|
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
|
|
|
GD_DPRINTF( "%s\n", 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
|
|
|
|
2011-05-30 01:05:28 +00:00
|
|
|
QStringList args = parseCommandLine( viewerCmdLine );
|
|
|
|
|
|
|
|
if ( !args.isEmpty() )
|
|
|
|
{
|
|
|
|
QString program = args.first();
|
|
|
|
args.pop_front();
|
|
|
|
args.push_back( tempFileName );
|
|
|
|
viewer.start( program, args, QIODevice::NotOpen );
|
|
|
|
if ( !viewer.waitForStarted() )
|
|
|
|
throw exCantRunViewer( viewerCmdLine.toUtf8().data() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw exCantRunViewer( tr( "the viewer program name is empty" ).toUtf8().data() );
|
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;
|
|
|
|
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;
|
|
|
|
viewer.kill();
|
|
|
|
viewer.waitForFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
void stopAndDestroySynchronously( ExternalViewer * viewer )
|
|
|
|
{
|
|
|
|
if( !viewer )
|
|
|
|
return;
|
|
|
|
viewer->stopSynchronously();
|
|
|
|
delete viewer;
|
|
|
|
}
|