add more spacing
[personal-kdebase.git] / runtime / renamedlgplugins / audio / audiopreview.cpp
blob4d71679fdd1506c0f5402b6f0087d4420a58ca43
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
7 of the License.
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.
20 #include <QLabel>
21 #include <QPixmap>
23 #include <kfilemetainfo.h>
24 #include <klocale.h>
25 #include <kmimetype.h>
26 #include <kurl.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)
37 : KVBox( parent )
39 m_isTempFile = false;
40 pic = 0;
41 m_player = 0L;
42 description = 0;
43 setSpacing( 0 );
44 if( url.isValid() && url.isLocalFile() ) {
45 m_localFile = url.path();
46 pic = new QLabel(this);
47 pic->setPixmap(KIO::pixmapForUrl( url ));
48 pic->adjustSize();
49 initView( mimeType );
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&)));
55 pic = label;
56 } else {
57 description = new QLabel(this );
58 description->setText(i18n("Unable to load audio file") );
62 AudioPreview::~AudioPreview()
64 if ( m_isTempFile )
65 KIO::NetAccess::removeTempFile( m_localFile );
67 delete m_player;
70 void AudioPreview::initView( const QString& mimeType )
72 KUrl url = KUrl::fromPath( m_localFile );
73 pic->setText( QString() );
74 pic->setPixmap(KIO::pixmapForUrl( url ));
75 pic->adjustSize();
77 KFileMetaInfo info(m_localFile);
78 KMimeType::Ptr mimeptr = KMimeType::mimeType(mimeType);
80 QString desc;
81 if (info.isValid())
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();
104 if (length/60 < 10)
105 desc.append("0");
106 desc.append(QString("%1:").arg(length/60, 0, 10));
107 if (length%60 < 10)
108 desc.append("0");
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 );
116 if ( m_player )
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()) )
127 m_isTempFile = true;
128 initView( KMimeType::findByPath( m_localFile )->name() );
132 #include <audiopreview.moc>