1 /* This file is part of the KDE project
2 Copyright (C) 2003 Fabian Wolf <fabianw@gmx.net>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public
6 License as published by the Free Software Foundation; version 2
9 This program 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 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
23 #include <kfilemetainfo.h>
25 #include <kmimetype.h>
28 #include <kio/netaccess.h>
29 #include <kurllabel.h>
30 #include <kmediaplayer/player.h>
31 #include <kservicetypetrader.h>
32 #include <ksqueezedtextlabel.h>
34 #include "audiopreview.h"
36 AudioPreview::AudioPreview( QWidget
*parent
, const KUrl
&url
, const QString
&mimeType
)
44 if( url
.isValid() && url
.isLocalFile() ) {
45 m_localFile
= url
.path();
46 pic
= new QLabel(this);
47 pic
->setPixmap(KIO::pixmapForUrl( url
));
50 } else if( !url
.isLocalFile() ) {
51 KUrlLabel
*label
= new KUrlLabel( this );
52 label
->setText(i18n("This audio file is not stored\non the local host.\nClick on this label to load it.\n" ) );
53 label
->setUrl( url
.prettyUrl() );
54 connect(label
, SIGNAL(leftClickedUrl(const QString
&)), SLOT(downloadFile(const QString
&)));
57 description
= new QLabel(this );
58 description
->setText(i18n("Unable to load audio file") );
62 AudioPreview::~AudioPreview()
65 KIO::NetAccess::removeTempFile( m_localFile
);
70 void AudioPreview::initView( const QString
& mimeType
)
72 KUrl url
= KUrl::fromPath( m_localFile
);
73 pic
->setText( QString() );
74 pic
->setPixmap(KIO::pixmapForUrl( url
));
77 KFileMetaInfo
info(m_localFile
);
78 KMimeType::Ptr mimeptr
= KMimeType::mimeType(mimeType
);
83 if (mimeptr
->is("audio/mpeg") || mimeptr
->is("application/ogg"))
85 // following 3 infos might be very long; make sure they get squeezed
86 KSqueezedTextLabel
*sl
;
88 sl
= new KSqueezedTextLabel(this);
89 sl
->setText(i18n("Artist: %1", info
.item("Artist").value().toString()));
91 sl
= new KSqueezedTextLabel(this);
92 sl
->setText(i18n("Title: %1", info
.item("Title").value().toString()));
94 sl
= new KSqueezedTextLabel(this);
95 sl
->setText(i18n("Comment: %1", info
.item("Comment").value().toString()));
97 desc
.append(i18nc("Bitrate: 160 kbits/s", "Bitrate: %1 %2\n", info
.item("Bitrate").value().toString(), info
.item("Bitrate").suffix() ));
99 desc
.append(i18n("Sample rate: %1 %2\n", info
.item("Sample Rate").value().toString(), info
.item("Sample Rate").suffix() ));
100 desc
.append(i18n("Length: "));
102 /* Calculate length in mm:ss format */
103 int length
= info
.item("Length").value().toInt();
106 desc
.append(QString("%1:").arg(length
/60, 0, 10));
109 desc
.append(QString("%1\n").arg(length
%60, 0, 10));
112 description
= new QLabel(this);
113 description
->setText( desc
);
114 description
->adjustSize();
115 m_player
= KServiceTypeTrader::createInstanceFromQuery
<KMediaPlayer::Player
>( "KMediaPlayer/Player", QString(), this );
118 static_cast<KParts::ReadOnlyPart
*>(m_player
)->openUrl( url
);
119 m_player
->widget()->show();
123 void AudioPreview::downloadFile( const QString
& url
)
125 if( KIO::NetAccess::download( KUrl( url
), m_localFile
, window()) )
128 initView( KMimeType::findByPath( m_localFile
)->name() );
132 #include <audiopreview.moc>