1 /***************************************************************************
2 * Copyright (C) 2004 by Marco Gulino *
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. *
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. *
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"
29 #include <QDragEnterEvent>
30 #include <kinstance.h>
33 #include <QtGui/QWidget>
35 #include <kstandarddirs.h>
38 #include <dcopclient.h>
39 #include <kmessagebox.h>
40 #include <kpushbutton.h>
41 #include <kiconloader.h>
45 #include <kapplication.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
)
59 void amarokWidget::dragEnterEvent(QDragEnterEvent
* event
)
61 event
->accept( K3URLDrag::canDecode(event
) );
64 void amarokWidget::dropEvent(QDropEvent
* event
)
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
)
76 bool amarokWidget::eventFilter( QObject
*, QEvent
*e
)
78 if(e
->type() < QEvent::DragEnter
|| e
->type() > QEvent::Drop
)
80 QApplication::sendEvent(this, e
);
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
& ) ) );
143 UniversalAmarok::~UniversalAmarok()
149 #include "universalamarok.moc"
152 // FIXME: is this referenced from anywhere ??!
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 ??!
166 KDE_EXPORT
bool add_konqsidebar_universalamarok(QString
* fn
, QString
* param
, QMap
<QString
,QString
> *map
)
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");
183 \fn UniversalAmarok::updateBrowser()
185 void UniversalAmarok::updateBrowser(const QString
& file
)
187 if (! (QFile::exists(file
) ) )
194 if( f_file
.open(QIODevice::ReadOnly
) )
196 QTextStream
stream( &f_file
);
197 stream
.setCodec( "UTF8" );
199 while ( !stream
.atEnd() ) {
200 line
= stream
.readLine(); // line of text excluding '\n'
201 text
+= QString("\n") + line
;
203 text
=text
.replace("<img id='current_box-largecover-image' ", "<img id='current_box-largecover-image' width=70 height=70 ");
205 browser
->write(text
);
208 browser
->openUrl(file
);
213 \fn UniversalAmarok::updateStatus()
215 void UniversalAmarok::updateStatus()
218 vol_slider
->setValue( playerStub
->getVolume() );
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") {
248 playlistStub
->playMedia(url
);
253 \fn UniversalAmarok::checkForAmarok()
255 void UniversalAmarok::checkForAmarok()
257 if(!amarokDCOP
->isApplicationRegistered("amarok"))
262 void UniversalAmarok::noAmarokRunning() {
263 QString m_HTMLSource
="";
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!" ) +
272 "<div id='introduction_box-body' class='box-body'>"
274 i18n( "To run Amarok, just click on the link below: "
277 "<a href='run:amarok' class='button'>" + i18n( "Run Amarok..." ) + "</a>"
283 browser
->write( m_HTMLSource
);
287 void UniversalAmarok::runAmarok() {
288 KToolInvocation::kdeinitExecWait("amarok");
293 void UniversalAmarok::volChanged(int vol
)
296 playerStub
->setVolume(vol
);
299 void UniversalAmarok::showIntroduction()
301 QString m_HTMLSource
="";
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!" ) +
310 "<div id='introduction_box-body' class='box-body'>"
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."
317 "<a href='show:collectionSetup' class='button'>" + i18n( "Build Collection..." ) + "</a>"
322 kDebug() << m_HTMLSource
;
324 browser
->write( m_HTMLSource
);