1/* This file is part of the KDE libraries
2 Copyright (C) 2003 Carsten Pfeiffer <pfeiffer@kde.org>
3 Copyright (C) 2006 Matthias Kretz <kretz@kde.org>
4
5 library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation, version 2.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "kfileaudiopreview.h"
21
22#include <QtGui/QCheckBox>
23#include <QtGui/QLayout>
24#include <QtGui/QGroupBox>
25
26#include <kglobal.h>
27#include <kconfig.h>
28#include <klocale.h>
29#include <kmimetype.h>
30#include <kpluginfactory.h>
31#include <kpluginloader.h>
32
33#include <config-kfile.h>
34
35#include <phonon/mediaobject.h>
36#include <phonon/audiooutput.h>
37#include <phonon/path.h>
38#include <phonon/backendcapabilities.h>
39#include <phonon/videowidget.h>
40#include "mediacontrols.h"
41#include <kconfiggroup.h>
42
43K_PLUGIN_FACTORY( KFileAudioPreviewFactory, registerPlugin<KFileAudioPreview>(); )
44K_EXPORT_PLUGIN( KFileAudioPreviewFactory )
45
46
47///////////////////////////////////////////////////////////////////
48///////////////////////////////////////////////////////////////////
49
50using namespace Phonon;
51
52class KFileAudioPreview::Private
53{
54public:
55 Private()
56 : player( 0 )
57 , audioOutput( 0 )
58 , videoWidget( 0 )
59 {
60 }
61
62 MediaObject* player;
63 AudioOutput* audioOutput;
64 VideoWidget* videoWidget;
65 MediaControls* controls;
66};
67
68
69KFileAudioPreview::KFileAudioPreview( QWidget *parent, const QVariantList & )
70 : KPreviewWidgetBase( parent )
71 , d( new Private )
72{
73 KGlobal::locale()->insertCatalog("kfileaudiopreview4");
74
75 setSupportedMimeTypes(BackendCapabilities::availableMimeTypes());
76
77 d->audioOutput = new AudioOutput(Phonon::NoCategory, this);
78
79 d->videoWidget = new VideoWidget( this );
80 d->videoWidget->hide();
81
82 d->controls = new MediaControls( this );
83 d->controls->setEnabled( false );
84 d->controls->setAudioOutput( d->audioOutput );
85
86 m_autoPlay = new QCheckBox( i18n("Play &automatically"), this );
87 KConfigGroup config( KGlobal::config(), ConfigGroup );
88 m_autoPlay->setChecked( config.readEntry( "Autoplay sounds", true ) );
89 connect( m_autoPlay, SIGNAL(toggled(bool)), SLOT(toggleAuto(bool)) );
90
91 QVBoxLayout* layout = new QVBoxLayout(this);
92 layout->setMargin(0);
93 layout->addWidget(d->videoWidget);
94 layout->addWidget(d->controls);
95 layout->addWidget(m_autoPlay, 0, Qt::AlignHCenter);
96 layout->addStretch();
97}
98
99KFileAudioPreview::~KFileAudioPreview()
100{
101 KConfigGroup config( KGlobal::config(), ConfigGroup );
102 config.writeEntry( "Autoplay sounds", m_autoPlay->isChecked() );
103
104 delete d;
105}
106
107void KFileAudioPreview::stateChanged( Phonon::State newstate, Phonon::State oldstate )
108{
109 if( oldstate == Phonon::LoadingState && newstate != Phonon::ErrorState )
110 d->controls->setEnabled( true );
111}
112
113void KFileAudioPreview::showPreview( const KUrl &url )
114{
115 d->controls->setEnabled(false);
116 if (!d->player) {
117 d->player = new MediaObject(this);
118 Phonon::createPath(d->player, d->videoWidget);
119 Phonon::createPath(d->player, d->audioOutput);
120 connect(d->player, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
121 SLOT(stateChanged(Phonon::State,Phonon::State)));
122 d->videoWidget->setVisible(d->player->hasVideo());
123 connect(d->player, SIGNAL(hasVideoChanged(bool)), d->videoWidget, SLOT(setVisible(bool)));
124 d->controls->setMediaObject(d->player);
125 }
126 d->player->setCurrentSource(url);
127
128 if( m_autoPlay->isChecked() )
129 d->player->play();
130}
131
132void KFileAudioPreview::clearPreview()
133{
134 if( d->player )
135 {
136 delete d->player;
137 d->player = 0;
138 d->controls->setEnabled( false );
139 }
140}
141
142void KFileAudioPreview::toggleAuto( bool on )
143{
144 if( !d->player )
145 return;
146
147 if( on && d->controls->isEnabled() )
148 d->player->play();
149 else
150 d->player->stop();
151}
152
153#include "kfileaudiopreview.moc"
154