Use desktop services to open all types of files but the audio ones.

This commit is contained in:
Konstantin Isakov 2009-02-08 14:40:26 +00:00
parent e3bb365473
commit 71c736f35f
2 changed files with 44 additions and 43 deletions

View file

@ -168,7 +168,6 @@ void ArticleView::linkClicked( QUrl const & url )
} }
} }
QString contentType; QString contentType;
if ( !found && !articleNetMgr.getResource( url, data, contentType ) ) if ( !found && !articleNetMgr.getResource( url, data, contentType ) )
@ -177,13 +176,9 @@ void ArticleView::linkClicked( QUrl const & url )
return; return;
} }
// Decide the viewer
QString program, extension;
if ( url.scheme() == "gdau" ) if ( url.scheme() == "gdau" )
{ {
#ifdef Q_OS_WIN32 #ifdef Q_OS_WIN32
// Windows-only: use system PlaySound function // Windows-only: use system PlaySound function
if ( winWavData.size() ) if ( winWavData.size() )
@ -194,51 +189,54 @@ void ArticleView::linkClicked( QUrl const & url )
PlaySoundA( &winWavData.front(), 0, PlaySoundA( &winWavData.front(), 0,
SND_ASYNC | SND_MEMORY | SND_NODEFAULT | SND_NOWAIT ); SND_ASYNC | SND_MEMORY | SND_NODEFAULT | SND_NOWAIT );
#else
return; // Use external viewer to play the file
#endif
program = "mplayer";
extension = "wav";
}
else
if ( url.path().endsWith( ".pdf", Qt::CaseInsensitive ) )
{
program = "evince";
extension = "pdf";
}
else
if ( url.path().endsWith( ".rtf", Qt::CaseInsensitive ) )
{
program = "oowriter";
extension = "rtf";
}
else
{
QMessageBox::critical( this, tr( "GoldenDict" ), tr( "Don't know how to handle the specified resource." ) );
return;
}
try
{
ExternalViewer * viewer = new ExternalViewer( this, data, extension, program );
try try
{ {
viewer->start(); ExternalViewer * viewer = new ExternalViewer( this, data, ".wav", "play" );
// Once started, it will erase itself try
{
viewer->start();
// Once started, it will erase itself
}
catch( ... )
{
delete viewer;
throw;
}
} }
catch( ... ) catch( ExternalViewer::Ex & e )
{ {
delete viewer; QMessageBox::critical( this, tr( "GoldenDict" ), tr( "Failed to run a player to play sound file: %1" ).arg( e.what() ) );
throw;
} }
#endif
return;
} }
catch( ExternalViewer::Ex & e )
// Create a temporary file
desktopOpenedTempFile.reset();
desktopOpenedTempFile = new QTemporaryFile( QDir::temp().filePath( "XXXXXX-" + url.path().section( '/', -1 ) ), this );
if ( !desktopOpenedTempFile->open() || desktopOpenedTempFile->write( &data.front(), data.size() ) != data.size() )
{ {
printf( "%s\n", e.what() ); QMessageBox::critical( this, tr( "GoldenDict" ), tr( "Failed to create temporary file." ) );
return;
} }
// For some reason it loses it after it was closed()
QString tempFileName = desktopOpenedTempFile->fileName();
desktopOpenedTempFile->close();
if ( !QDesktopServices::openUrl( QUrl::fromLocalFile( tempFileName ) ) )
QMessageBox::critical( this, tr( "GoldenDict" ), tr( "Failed to auto-open resource file, try opening manually: %1." ).arg( tempFileName ) );
} }
else else
if ( url.scheme() == "http" || url.scheme() == "https" || if ( url.scheme() == "http" || url.scheme() == "https" ||

View file

@ -27,6 +27,9 @@ class ArticleView: public QFrame
vector< char > winWavData; vector< char > winWavData;
#endif #endif
// For resources opened via desktop services
sptr< QTemporaryFile > desktopOpenedTempFile;
public: public:
/// The popupView flag influences contents of the context menus to be /// The popupView flag influences contents of the context menus to be
/// appropriate to the context of the view. /// appropriate to the context of the view.