~ubuntu-branches/ubuntu/lucid/lastfm/lucid

« back to all changes in this revision

Viewing changes to src/Helper/controlinterface.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Devid Filoni
  • Date: 2008-07-14 16:46:20 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20080714164620-cattx7c83ihhnk4l
Tags: upstream-1.5.1.31879.dfsg
ImportĀ upstreamĀ versionĀ 1.5.1.31879.dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
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>                          *
5
 
 *                                                                         *
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.                                   *
10
 
 *                                                                         *
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.                          *
15
 
 *                                                                         *
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
 
 ***************************************************************************/
21
 
 
22
 
#include "controlinterface.h"
23
 
 
24
 
#include "MooseCommon.h"
25
 
#include "logger.h"
26
 
#include "LastFmSettings.h"
27
 
 
28
 
#include <QCoreApplication>
29
 
#include <QWaitCondition>
30
 
#include <QStringList>
31
 
#include <QTcpServer>
32
 
#include <QTcpSocket>
33
 
 
34
 
ControlInterface::ControlInterface( QCoreApplication* parent )
35
 
                : m_parent( parent ),
36
 
                  m_clientSocket( 0 )
37
 
{
38
 
    m_serverSocket = new QTcpServer( this );
39
 
 
40
 
    connect( m_serverSocket, SIGNAL( newConnection() ), this, SLOT( clientConnect() ) );
41
 
 
42
 
    if ( !m_serverSocket->listen( QHostAddress::LocalHost, The::settings().helperControlPort() ) )
43
 
        m_serverSocket->listen( QHostAddress::LocalHost );
44
 
 
45
 
    The::settings().setHelperControlPort( m_serverSocket->serverPort() );
46
 
}
47
 
 
48
 
 
49
 
void
50
 
ControlInterface::clientConnect()
51
 
{
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;
56
 
 
57
 
    m_clientSocket = m_serverSocket->nextPendingConnection();
58
 
 
59
 
    connect( m_clientSocket, SIGNAL( readyRead() ), SLOT( clientRequest() ) );
60
 
}
61
 
 
62
 
 
63
 
void
64
 
ControlInterface::clientRequest()
65
 
{
66
 
    QMutexLocker locker( &m_mutex );
67
 
    QString request = QString::fromUtf8( m_clientSocket->readAll() );
68
 
 
69
 
    LOGL( 3, "clientRequest (old instance): " << request );
70
 
 
71
 
    if ( !request.isEmpty() )
72
 
    {
73
 
        if ( request.contains( "--quit" ) )
74
 
        {
75
 
            LOGL( 3, "old instance calling parent->quit" );
76
 
 
77
 
            m_parent->quit();
78
 
        }
79
 
 
80
 
        m_clientSocket->flush();
81
 
    }
82
 
 
83
 
    m_clientSocket->close();
84
 
    m_clientSocket->deleteLater();
85
 
    m_clientSocket = 0;
86
 
}
87
 
 
88
 
 
89
 
bool
90
 
ControlInterface::sendToInstance( const QString& data )
91
 
{
92
 
    LOGL( 3, "sendToInstance (new instance): " << data );
93
 
 
94
 
    QTcpSocket socket;
95
 
    socket.connectToHost( QHostAddress::LocalHost, The::settings().helperControlPort() );
96
 
 
97
 
    if ( socket.waitForConnected( 500 ) )
98
 
    {
99
 
        if ( data.length() > 0 )
100
 
        {
101
 
            QByteArray utf8Data = data.toUtf8();
102
 
            socket.write( utf8Data, utf8Data.length() );
103
 
            socket.flush();
104
 
        }
105
 
 
106
 
        socket.close();
107
 
        return true;
108
 
    }
109
 
    else
110
 
    {
111
 
        LOGL( 1, "sendToInstance failed, SocketError: " << socket.error() );
112
 
    }
113
 
 
114
 
    return false;
115
 
}