1
/***************************************************************************
2
* Copyright (C) 2005 - 2007 by *
3
* Christian Muehlhaeuser, Last.fm Ltd <chris@last.fm> *
4
* Erik Jaelevik, Last.fm Ltd <erik@last.fm> *
6
* This program is free software; you can redistribute it and/or modify *
7
* it under the terms of the GNU General Public License as published by *
8
* the Free Software Foundation; either version 2 of the License, or *
9
* (at your option) any later version. *
11
* This program is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU General Public License for more details. *
16
* You should have received a copy of the GNU General Public License *
17
* along with this program; if not, write to the *
18
* Free Software Foundation, Inc., *
19
* 51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA. *
20
***************************************************************************/
22
#include "controlinterface.h"
24
#include "MooseCommon.h"
26
#include "LastFmSettings.h"
28
#include <QCoreApplication>
29
#include <QWaitCondition>
30
#include <QStringList>
34
ControlInterface::ControlInterface( QCoreApplication* parent )
38
m_serverSocket = new QTcpServer( this );
40
connect( m_serverSocket, SIGNAL( newConnection() ), this, SLOT( clientConnect() ) );
42
if ( !m_serverSocket->listen( QHostAddress::LocalHost, The::settings().helperControlPort() ) )
43
m_serverSocket->listen( QHostAddress::LocalHost );
45
The::settings().setHelperControlPort( m_serverSocket->serverPort() );
50
ControlInterface::clientConnect()
52
// Incoming connection from either a webbrowser or
53
// some other program like the firefox extension.
54
QMutexLocker locker( &m_mutex );
55
delete m_clientSocket;
57
m_clientSocket = m_serverSocket->nextPendingConnection();
59
connect( m_clientSocket, SIGNAL( readyRead() ), SLOT( clientRequest() ) );
64
ControlInterface::clientRequest()
66
QMutexLocker locker( &m_mutex );
67
QString request = QString::fromUtf8( m_clientSocket->readAll() );
69
LOGL( 3, "clientRequest (old instance): " << request );
71
if ( !request.isEmpty() )
73
if ( request.contains( "--quit" ) )
75
LOGL( 3, "old instance calling parent->quit" );
80
m_clientSocket->flush();
83
m_clientSocket->close();
84
m_clientSocket->deleteLater();
90
ControlInterface::sendToInstance( const QString& data )
92
LOGL( 3, "sendToInstance (new instance): " << data );
95
socket.connectToHost( QHostAddress::LocalHost, The::settings().helperControlPort() );
97
if ( socket.waitForConnected( 500 ) )
99
if ( data.length() > 0 )
101
QByteArray utf8Data = data.toUtf8();
102
socket.write( utf8Data, utf8Data.length() );
111
LOGL( 1, "sendToInstance failed, SocketError: " << socket.error() );