Make plasma libs build.
[amarok.git] / src / konquisidebar / universalamarok.cpp
blobaf0eb00c8550ce54bc1d970a470594e1eacb24b7
1 /***************************************************************************
2 * Copyright (C) 2004 by Marco Gulino *
3 * marco@Paganini *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "universalamarok.h"
22 #include "amarokdcopiface_stub.h"
25 #include <QLabel>
26 //Added by qt3to4:
27 #include <QDropEvent>
28 #include <QEvent>
29 #include <QDragEnterEvent>
30 #include <kinstance.h>
31 #include <klocale.h>
32 #include <QString>
33 #include <QtGui/QWidget>
34 #include <kglobal.h>
35 #include <kstandarddirs.h>
36 #include <QLayout>
37 #include <QTimer>
38 #include <dcopclient.h>
39 #include <kmessagebox.h>
40 #include <kpushbutton.h>
41 #include <kiconloader.h>
42 #include <QDateTime>
43 #include <QFileInfo>
44 #include <ktoolbar.h>
45 #include <kapplication.h>
46 #include <QSlider>
47 #include <kdebug.h>
48 #include <k3urldrag.h>
49 #include <khtmlview.h>
50 #include <ktoolinvocation.h>
52 #define HTML_FILE KGlobal::dirs()->saveLocation( "data", "amarok/", true ) + "contextbrowser.html"
54 amarokWidget::amarokWidget( QWidget * parent, const char * name, Qt::WFlags f )
55 : KVBox(parent, name, f)
57 setAcceptDrops(true);
59 void amarokWidget::dragEnterEvent(QDragEnterEvent* event)
61 event->accept( K3URLDrag::canDecode(event) );
64 void amarokWidget::dropEvent(QDropEvent* event)
66 KUrl::List urlList;
67 if( K3URLDrag::decode(event, urlList) )
69 KUrl::List::iterator it;
70 KUrl::List::iterator end( urlList.end() );
71 for (it = urlList.begin(); it != end; ++it )
72 emit emitURL(*it);
76 bool amarokWidget::eventFilter( QObject *, QEvent *e )
78 if(e->type() < QEvent::DragEnter || e->type() > QEvent::Drop )
79 return false;
80 QApplication::sendEvent(this, e);
81 return true;
84 UniversalAmarok::UniversalAmarok(KInstance *inst,QObject *parent,QWidget *widgetParent, QString &desktopName, const char* name):
85 KonqSidebarPlugin(inst,parent,widgetParent,desktopName,name)
87 KIconLoader::global()->addAppDir( "amarok" );
88 widget = new amarokWidget( widgetParent );
89 // widgetParent->resize(580,300);
90 KToolBar *topBar = new KToolBar( widget, "Topbar" );
91 topBar->setIconSize(16);
92 topBar->insertButton( "today", 0, SIGNAL( clicked() ), this, SLOT( currentTrack() ) );
93 topBar->insertButton( "document", 0, SIGNAL( clicked() ), this, SLOT( lyrics() ) );
94 topBar->insertButton( "personal", 0, SIGNAL( clicked() ), this, SLOT( wiki() ) );
96 browser = new KHTMLPart(widget, "widget-browser");
97 //browser=new KHTMLPart(widget);
98 kDebug() << "parentPart() << " << browser->parentPart();
99 browser->setDNDEnabled( true );
100 browser->setEncoding( "utf8", true );
101 updateBrowser( HTML_FILE );
102 browser->view()->installEventFilter( widget );
103 amarokDCOP = new DCOPClient();
104 amarokDCOP->attach();
106 playerStub = new AmarokPlayerInterface_stub( amarokDCOP, "amarok", "player");
107 playlistStub = new AmarokPlaylistInterface_stub( amarokDCOP, "amarok", "playlist");
108 contextStub = new AmarokContextBrowserInterface_stub (amarokDCOP, "amarok", "contextbrowser");
110 KToolBar* toolBar=new KToolBar(widget, "PlayerControls");
112 toolBar->setIconSize(16);
113 toolBar->insertButton( "player_start",0, SIGNAL( clicked() ), this, SLOT( sendPrev() ) );
114 toolBar->insertButton( "player_play", 0, SIGNAL( clicked() ), this, SLOT( sendPlay() ) );
115 toolBar->insertButton( "player_pause",0, SIGNAL( clicked() ), this, SLOT( sendPause() ) );
116 toolBar->insertButton( "player_stop", 0, SIGNAL( clicked() ), this, SLOT( sendStop() ) );
117 toolBar->insertButton( "player_end", 0, SIGNAL( clicked() ), this, SLOT( sendNext() ) );
119 toolBar->insertSeparator();
120 toolBar->insertButton( "arts", 0, SIGNAL( clicked() ), this, SLOT( sendMute() ) );
122 vol_slider = new QSlider(0,100,1,0,Qt::Horizontal, toolBar,"volume");
123 vol_slider->setLineStep(2);
125 connect(vol_slider, SIGNAL( valueChanged(int) ), this, SLOT(volChanged(int ) ) );
126 toolBar->insertWidget(1,2, vol_slider);
128 fileInfo = new QFileInfo(HTML_FILE);
129 QTimer *t = new QTimer( this );
131 connect( t, SIGNAL(timeout()), SLOT(updateStatus() ) );
132 t->start( 2000, false );
133 kDebug() << "Connecting widget signal";
135 connect( widget, SIGNAL( emitURL( const KUrl &)),
136 this, SLOT( openUrlRequest( const KUrl &) ) );
137 connect( browser->browserExtension(), SIGNAL( openUrlRequest( const KUrl &, const KParts::URLArgs & ) ),
138 this, SLOT( openUrlRequest( const KUrl & ) ) );
139 widget->show();
143 UniversalAmarok::~UniversalAmarok()
145 delete fileInfo;
149 #include "universalamarok.moc"
152 // FIXME: is this referenced from anywhere ??!
154 extern "C"
156 KDE_EXPORT void* create_konqsidebar_universalamarok(KInstance *instance,QObject *par,QWidget *widp,QString &desktopname,const char *name)
158 KGlobal::locale()->insertCatalog( "amarok" );
159 return new UniversalAmarok(instance,par,widp,desktopname,name);
163 // FIXME: Is this referenced from anywhere ??!
164 extern "C"
166 KDE_EXPORT bool add_konqsidebar_universalamarok(QString* fn, QString* param, QMap<QString,QString> *map)
168 Q_UNUSED(param);
170 map->insert ("Type", "Link");
171 map->insert ("URL", "");
172 map->insert ("Icon", "amarok");
173 map->insert ("Name", i18n ("Amarok"));
174 map->insert ("Open", "true");
175 map->insert ("X-KDE-KonqSidebarModule","konqsidebar_universalamarok");
176 fn->setLatin1 ("amarok.desktop");
177 return true;
183 \fn UniversalAmarok::updateBrowser()
185 void UniversalAmarok::updateBrowser(const QString& file)
187 if (! (QFile::exists(file) ) )
189 showIntroduction();
190 return;
192 QString text;
193 QFile f_file(file);
194 if( f_file.open(QIODevice::ReadOnly) )
196 QTextStream stream( &f_file );
197 stream.setCodec( "UTF8" );
198 QString line;
199 while ( !stream.atEnd() ) {
200 line = stream.readLine(); // line of text excluding '\n'
201 text += QString("\n") + line;
202 } f_file.close();
203 text=text.replace("<img id='current_box-largecover-image' ", "<img id='current_box-largecover-image' width=70 height=70 ");
204 browser->begin();
205 browser->write(text);
206 browser->end();
207 } else
208 browser->openUrl(file);
213 \fn UniversalAmarok::updateStatus()
215 void UniversalAmarok::updateStatus()
217 checkForAmarok();
218 vol_slider->setValue( playerStub->getVolume() );
219 fileInfo->refresh();
220 if( fileInfo->lastModified() != fileDT )
222 updateBrowser( HTML_FILE );
223 fileDT=fileInfo->lastModified();
229 \fn UniversalAmarok::getCurrentPlaying()
231 QString UniversalAmarok::getCurrentPlaying()
233 return playerStub->nowPlaying();
238 \fn UniversalAmarok::openUrlRequest( const KUrl &url )
240 void UniversalAmarok::openUrlRequest( const KUrl &url )
242 if( ! url.isValid() ) return;
243 if (url.url() == "run:amarok") {
244 runAmarok();
245 return;
247 checkForAmarok();
248 playlistStub->playMedia(url);
253 \fn UniversalAmarok::checkForAmarok()
255 void UniversalAmarok::checkForAmarok()
257 if(!amarokDCOP->isApplicationRegistered("amarok"))
258 noAmarokRunning();
262 void UniversalAmarok::noAmarokRunning() {
263 QString m_HTMLSource="";
264 m_HTMLSource.append(
265 "<html>"
266 "<div id='introduction_box' class='box'>"
267 "<div id='introduction_box-header' class='box-header'>"
268 "<span id='introduction_box-header-title' class='box-header-title'>"
269 + i18n( "Amarok is not running!" ) +
270 "</span>"
271 "</div>"
272 "<div id='introduction_box-body' class='box-body'>"
273 "<p>" +
274 i18n( "To run Amarok, just click on the link below: "
276 "</p>"
277 "<a href='run:amarok' class='button'>" + i18n( "Run Amarok..." ) + "</a>"
278 "</div>"
279 "</div>"
280 "</html>"
282 browser->begin();
283 browser->write( m_HTMLSource );
284 browser->end();
287 void UniversalAmarok::runAmarok() {
288 KToolInvocation::kdeinitExecWait("amarok");
293 void UniversalAmarok::volChanged(int vol)
295 checkForAmarok();
296 playerStub->setVolume(vol);
299 void UniversalAmarok::showIntroduction()
301 QString m_HTMLSource="";
302 m_HTMLSource.append(
303 "<html>"
304 "<div id='introduction_box' class='box'>"
305 "<div id='introduction_box-header' class='box-header'>"
306 "<span id='introduction_box-header-title' class='box-header-title'>"
307 + i18n( "Hello Amarok user!" ) +
308 "</span>"
309 "</div>"
310 "<div id='introduction_box-body' class='box-body'>"
311 "<p>" +
312 i18n( "This is the Context Browser: "
313 "it shows you contextual information about the currently playing track. "
314 "In order to use this feature of Amarok, you need to build a Collection."
316 "</p>"
317 "<a href='show:collectionSetup' class='button'>" + i18n( "Build Collection..." ) + "</a>"
318 "</div>"
319 "</div>"
320 "</html>"
322 kDebug() << m_HTMLSource;
323 browser->begin();
324 browser->write( m_HTMLSource );
325 browser->end();