mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-11-30 17:24:08 +00:00
278e05cbf3
External and internal audio players work similarly now. Fixes #950. * inherit a new ExternalAudioPlayer class from AudioPlayerInterface; * use an existing ExternalViewer class to implement ExternalAudioPlayer; * take (const char *, int) instead of std::vector<char> in ExternalViewer constructor to fit into AudioPlayerInterface; * extend ExternalViewer API to let ExternalAudioPlayer stop superseded audio player processes; * make AudioPlayerInterface::play() return an error message string to allow reporting immediate failures from derived classes; * Document AudioPlayerInterface API; * Document AudioPlayerFactory::player(); * use the common audio interface exclusively in ArticleView.
81 lines
2.3 KiB
C++
81 lines
2.3 KiB
C++
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
#include <QDir>
|
|
#include <QTimer>
|
|
#include "externalviewer.hh"
|
|
#include "parsecmdline.hh"
|
|
#include "gddebug.hh"
|
|
|
|
ExternalViewer::ExternalViewer( const char * data, int size,
|
|
QString const & extension, QString const & viewerCmdLine_,
|
|
QObject * parent)
|
|
throw( exCantCreateTempFile ):
|
|
QObject( parent ),
|
|
tempFile( QDir::temp().filePath( QString( "gd-XXXXXXXX." ) + extension ) ),
|
|
viewer( this ),
|
|
viewerCmdLine( viewerCmdLine_ )
|
|
{
|
|
if ( !tempFile.open() || tempFile.write( data, size ) != size )
|
|
throw exCantCreateTempFile();
|
|
|
|
tempFileName = tempFile.fileName(); // For some reason it loses it after it was closed()
|
|
|
|
#ifdef Q_OS_WIN32
|
|
// Issue #239
|
|
tempFileName = QDir::toNativeSeparators(tempFileName);
|
|
#endif
|
|
|
|
tempFile.close();
|
|
|
|
GD_DPRINTF( "%s\n", tempFile.fileName().toLocal8Bit().data() );
|
|
}
|
|
|
|
void ExternalViewer::start() throw( exCantRunViewer )
|
|
{
|
|
connect( &viewer, SIGNAL( finished( int, QProcess::ExitStatus ) ),
|
|
this, SLOT( deleteLater() ) );
|
|
connect( &viewer, SIGNAL( error( QProcess::ProcessError ) ),
|
|
this, SLOT( deleteLater() ) );
|
|
|
|
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() );
|
|
}
|
|
|
|
bool ExternalViewer::stop()
|
|
{
|
|
if( viewer.state() == QProcess::NotRunning )
|
|
return true;
|
|
viewer.terminate();
|
|
QTimer::singleShot( 1000, &viewer, SLOT( kill() ) ); // In case terminate() fails.
|
|
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;
|
|
}
|