-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScrobSocket.cpp
171 lines (138 loc) · 4.17 KB
/
ScrobSocket.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
Copyright 2005-2009 Last.fm Ltd.
- Primarily authored by Max Howell, Jono Cole and Doug Mansell
This file is part of the Last.fm Desktop Application Suite.
lastfm-desktop is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
lastfm-desktop is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with lastfm-desktop. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ScrobSocket.h"
#include <QThread>
#include <QHostAddress>
#include <QUrl>
#include <QTextStream>
#ifdef WIN32
#include "common/c++/win/scrobSubPipeName.cpp"
#endif
ScrobSocket::ScrobSocket( const QString& clientId, QObject* parent )
: QLocalSocket( parent )
, m_bInConnect( false )
, m_clientId( clientId )
{
connect( this, SIGNAL(readyRead()), SLOT(onReadyRead()) );
connect( this, SIGNAL(error( QLocalSocket::LocalSocketError )), SLOT(onError( QLocalSocket::LocalSocketError )) );
connect( this, SIGNAL(connected()), SLOT(onConnected()) );
connect( this, SIGNAL(disconnected()), SLOT(onDisconnected()) );
transmit( "INIT c=" + clientId + "\n" );
}
ScrobSocket::~ScrobSocket()
{
if (!m_track.isNull()) stop();
}
void
ScrobSocket::transmit( const QString& data )
{
m_msgQueue.enqueue( data );
qDebug() << "Connection state == " << state();
if( state() == QAbstractSocket::UnconnectedState ) {
doConnect();
}
}
void
ScrobSocket::onConnected()
{
if( !m_msgQueue.empty() )
{
qDebug() << m_msgQueue.head().trimmed();
write( m_msgQueue.takeFirst().toUtf8());
}
}
void
ScrobSocket::doConnect()
{
if (!m_bInConnect) {
#ifdef WIN32
std::string s;
DWORD r = scrobSubPipeName( &s );
if (r != 0) throw std::runtime_error( formatWin32Error( r ) );
QString const name = QString::fromStdString( s );
#else
QString const name = "lastfm_scrobsub";
#endif
// avoid stack-overflow connect/disconnect loop with m_bInConnect
m_bInConnect = true;
connectToServer( name );
m_bInConnect = false;
}
}
void
ScrobSocket::onDisconnected()
{
if( !m_msgQueue.empty())
doConnect();
}
void
ScrobSocket::onError( QLocalSocket::LocalSocketError error )
{
switch (error)
{
case SocketTimeoutError:
// TODO look, really we should store at least one start message forever
// then if last time we didn't connect and this time it's a pause we
// send the start first
m_msgQueue.clear();
break;
case PeerClosedError:
// expected
break;
case ConnectionRefusedError: // happens if client isn't running
break;
default: // may as well
qDebug() << lastfm::qMetaEnumString<QAbstractSocket>( error, "SocketError" );
}
}
static inline QString encodeAmp( QString data )
{
return data.replace( '&', "&&" );
}
void
ScrobSocket::start( const lastfm::Track& t )
{
m_track = t;
transmit( "START c=" + m_clientId + "&"
"a=" + encodeAmp( t.artist() ) + "&"
"t=" + encodeAmp( t.title() ) + "&"
"b=" + encodeAmp( t.album() ) + "&" // todo: and when album.isNull?
"l=" + QString::number( t.duration() ) + "&"
"p=" + encodeAmp( t.url().path() ) + '\n' );
}
void
ScrobSocket::pause()
{
transmit( "PAUSE c=" + m_clientId + "\n" );
}
void
ScrobSocket::resume()
{
transmit( "RESUME c=" + m_clientId + "\n" );
}
void
ScrobSocket::stop()
{
transmit( "STOP c=" + m_clientId + "\n" );
}
void
ScrobSocket::onReadyRead()
{
QByteArray bytes = readAll();
if (bytes != "OK\n")
qWarning() << bytes.trimmed();
disconnectFromServer();
}