summaryrefslogtreecommitdiffstats
path: root/interfaces/kmediaplayer/kfileaudiopreview/kfileaudiopreview.cpp
blob: 2998af9f58660be6aac0d6763780a73d4887b6b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "kfileaudiopreview.h"

#include <tqcheckbox.h>
#include <tqhbox.h>
#include <tqlayout.h>
#include <tqvgroupbox.h>

#include <kglobal.h>
#include <kconfig.h>
#include <klibloader.h>
#include <klocale.h>
#include <kmediaplayer/player.h>
#include <kmimetype.h>
#include <kparts/componentfactory.h>

#include <kplayobjectfactory.h>

#include <config-kfile.h>

class KFileAudioPreviewFactory : public KLibFactory
{
protected:
    virtual TQObject *createObject( TQObject *parent, const char *name,
                           const char *className, const TQStringList & args)
    {
        Q_UNUSED(className);
        Q_UNUSED(args);
        return TQT_TQOBJECT(new KFileAudioPreview( dynamic_cast<TQWidget*>( parent ), name ));
    }
};

K_EXPORT_COMPONENT_FACTORY( kfileaudiopreview, KFileAudioPreviewFactory )


///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////


class KFileAudioPreview::KFileAudioPreviewPrivate
{
public:
    KFileAudioPreviewPrivate( TQWidget *parent )
    {
        player = KParts::ComponentFactory::createInstanceFromQuery<KMediaPlayer::Player>( "KMediaPlayer/Player", TQString(), TQT_TQOBJECT(parent) );
    }

    ~KFileAudioPreviewPrivate()
    {
        delete player;
    }

    KMediaPlayer::Player *player;
};


KFileAudioPreview::KFileAudioPreview( TQWidget *parent, const char *name )
    : KPreviewWidgetBase( parent, name )
{
    TDEGlobal::locale()->insertCatalogue("kfileaudiopreview");    

    TQStringList formats = KDE::PlayObjectFactory::mimeTypes();
    // ###
    TQStringList::ConstIterator it = formats.begin();
    for ( ; it != formats.end(); ++it )
        m_supportedFormats.insert( *it, (void*) 1 );

    TQVGroupBox *box = new TQVGroupBox( i18n("Media Player"), this );
    TQVBoxLayout *layout = new TQVBoxLayout( this );
    layout->addWidget( box );

    (void) new TQWidget( box ); // spacer

    d = new KFileAudioPreviewPrivate( 0L ); // not box -- being reparented anyway
    if ( d->player ) // only if there actually is a component...
    {
        setSupportedMimeTypes( formats );
        KMediaPlayer::View *view = d->player->view();
        view->setEnabled( false );

        // if we have access to the video widget, show it above the player
        // So, reparent first the video widget, then the view.
        if ( view->videoWidget() )
        {
            TQHBox *frame = new TQHBox( box );
            frame->setFrameStyle( TQFrame::Panel | TQFrame::Sunken );
            frame->setSizePolicy( TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Expanding ) );
            view->videoWidget()->reparent( frame, TQPoint(0,0) );
        }

        view->reparent( box, TQPoint(0,0) );
    }

    m_autoPlay = new TQCheckBox( i18n("Play &automatically"), box );
    TDEConfigGroup config( TDEGlobal::config(), ConfigGroup );
    m_autoPlay->setChecked( config.readBoolEntry( "Autoplay sounds", true ) );
    connect( m_autoPlay, TQT_SIGNAL(toggled(bool)), TQT_SLOT(toggleAuto(bool)) );
}

KFileAudioPreview::~KFileAudioPreview()
{
    TDEConfigGroup config( TDEGlobal::config(), ConfigGroup );
    config.writeEntry( "Autoplay sounds", m_autoPlay->isChecked() );

    delete d;
}

void KFileAudioPreview::showPreview( const KURL &url )
{
    if ( !d->player || !url.isValid() )
        return;

    KMimeType::Ptr mt = KMimeType::findByURL( url );
    bool supported = m_supportedFormats.find( mt->name() );
    d->player->view()->setEnabled( supported );
    if ( !supported )
        return;

    static_cast<KParts::ReadOnlyPart*>(d->player)->openURL( url );
    if ( m_autoPlay->isChecked() )
        d->player->play();
}

void KFileAudioPreview::clearPreview()
{
    if ( d->player )
    {
        d->player->stop();
        d->player->closeURL();
    }
}

void KFileAudioPreview::toggleAuto( bool on )
{
    if ( !d->player )
        return;

    if ( on && m_currentURL.isValid() && d->player->view()->isEnabled() )
        d->player->play();
    else
        d->player->stop();
}

void KFileAudioPreview::virtual_hook( int, void* )
{}

#include "kfileaudiopreview.moc"