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

« back to all changes in this revision

Viewing changes to src/DiagnosticsDialog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Pedro Fragoso
  • Date: 2007-12-31 09:49:54 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20071231094954-ix1amvcsj9pk61ya
Tags: 1:1.4.1.57486.dfsg-1ubuntu1
* Merge from Debian unstable (LP: #180254), remaining changes:
  - debian/rules;
    - Added dh_icons
  - Modify Maintainer value to match Debian-Maintainer-Field Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
*   Copyright (C) 2005 - 2007 by                                          *
 
3
*      Jono Cole, Last.fm Ltd <jono@last.fm>                              *
 
4
*      Christian Muehlhaeuser, Last.fm Ltd <chris@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 "Radio.h"
 
23
#include "WebService/Request.h"
 
24
#include "Scrobbler-1.2.h"
 
25
#include "DiagnosticsDialog.h"
 
26
#include "LastFmSettings.h"
 
27
#include "container.h"
 
28
#include "lastfmapplication.h"
 
29
#include "libFingerprint/FingerprintCollector.h"
 
30
 
 
31
#include <QClipboard>
 
32
 
 
33
 
 
34
static void smallFontise( QWidget* w )
 
35
{
 
36
    #ifdef WIN32
 
37
    return; //small fonts look wrong on Windows
 
38
    #endif
 
39
 
 
40
    QFont f = w->font();
 
41
    #if defined LINUX
 
42
        f.setPointSize( f.pointSize() - 2 );
 
43
    #else
 
44
        f.setPointSize( 10 );
 
45
    #endif
 
46
    w->setFont( f );
 
47
}
 
48
 
 
49
 
 
50
DiagnosticsDialog::DiagnosticsDialog( QWidget *parent )
 
51
        : QDialog( parent )
 
52
{
 
53
    ui.setupUi( this );
 
54
 
 
55
    #ifdef HIDE_RADIO
 
56
    ui.radioGroupBox->setVisible( false );
 
57
    #endif
 
58
 
 
59
    // not possible to do this with designer, and varies by platform
 
60
    smallFontise( ui.cachedTracksLabel );
 
61
    smallFontise( ui.cachedTracksTitle );
 
62
    smallFontise( ui.fingerprintedTracksTitle );
 
63
 
 
64
    #ifndef LINUX
 
65
    // Qt 4.3.1 b0rked? as this is specified in Designer, but only works on Linux :(
 
66
    foreach ( QGroupBox* b, findChildren<QGroupBox*>() )
 
67
    {
 
68
        b->layout()->setMargin( 9 ); //Qt ignores our setting in Designer :(
 
69
        #ifdef WIN32
 
70
        if ( QSysInfo::WindowsVersion != QSysInfo::WV_VISTA )
 
71
            b->setFlat( false );
 
72
        #endif
 
73
    }
 
74
    #endif
 
75
 
 
76
    #ifdef Q_OS_MAC
 
77
    layout()->setMargin( 7 );
 
78
    delete ui.line;
 
79
    ui.cachedTracksList->setAttribute( Qt::WA_MacShowFocusRect, false );
 
80
    ui.vboxLayout1->setSpacing( 18 );
 
81
    #endif
 
82
 
 
83
    ui.httpBufferLabel->setMinimumWidth( ui.httpBufferProgress->fontMetrics().width( "100.0k" ) );
 
84
 
 
85
    ui.httpBufferProgress->setMaximum( 500000 );
 
86
    ui.decodedBufferProgress->setMaximum( 100000 );
 
87
    ui.outputBufferProgress->setMaximum( 100000 );
 
88
 
 
89
    connect( ui.closeButton, SIGNAL( clicked()), SLOT( close() ) );
 
90
    connect( qApp, SIGNAL( event( int, QVariant ) ), SLOT( onAppEvent( int, QVariant ) ) );
 
91
    connect( &The::scrobbler(), SIGNAL( status( int, QVariant ) ), SLOT( onScrobblerEvent() ) );
 
92
    connect( ui.viewLogButton,  SIGNAL( clicked() ), &The::container(), SLOT( onAltShiftL() ) );
 
93
    connect( ui.viewHelperLogButton, SIGNAL( clicked() ), &The::container(), SLOT( onAltShiftH() ) );
 
94
    connect( ui.refreshButton, SIGNAL( clicked() ), SLOT( onRefresh() ) );
 
95
    connect( ui.copyToClipboardButton, SIGNAL( clicked() ), SLOT( onCopyToClipboard() ) );
 
96
 
 
97
    // Fingerprint collector
 
98
    ui.fpQueueSizeLabel->setText( "0" );
 
99
    connect( The::app().m_fpCollector, SIGNAL( trackFingerprintingStarted( TrackInfo ) ),
 
100
             this,                     SLOT( onTrackFingerprintingStarted( TrackInfo ) ),
 
101
             Qt::QueuedConnection );
 
102
    connect( The::app().m_fpCollector, SIGNAL( trackFingerprinted( TrackInfo ) ),
 
103
             this,                     SLOT( onTrackFingerprinted( TrackInfo ) ),
 
104
             Qt::QueuedConnection );
 
105
    connect( The::app().m_fpCollector, SIGNAL( cantFingerprintTrack( TrackInfo, QString ) ),
 
106
             this,                     SLOT( onCantFingerprintTrack( TrackInfo, QString ) ),
 
107
             Qt::QueuedConnection );
 
108
}
 
109
 
 
110
 
 
111
DiagnosticsDialog::~DiagnosticsDialog()
 
112
{}
 
113
 
 
114
 
 
115
void
 
116
DiagnosticsDialog::show()
 
117
{
 
118
    //initialize the progress bars to 0
 
119
    onDecodedBufferSizeChanged( 0 );
 
120
    onHttpBufferSizeChanged( 0 );
 
121
    onOutputBufferSizeChanged( 0 );
 
122
 
 
123
    connect( &The::audioController().m_thread, SIGNAL( httpBufferSizeChanged( int ) ),
 
124
        this,                              SLOT( onHttpBufferSizeChanged( int ) ),
 
125
        Qt::QueuedConnection );
 
126
 
 
127
    connect( &The::audioController().m_thread, SIGNAL( decodedBufferSizeChanged( int ) ),
 
128
        this,                              SLOT( onDecodedBufferSizeChanged( int ) ),
 
129
        Qt::QueuedConnection );
 
130
 
 
131
    connect( &The::audioController().m_thread, SIGNAL( outputBufferSizeChanged( int ) ),
 
132
        this,                              SLOT( onOutputBufferSizeChanged( int ) ),
 
133
        Qt::QueuedConnection );
 
134
 
 
135
    onRefresh();
 
136
 
 
137
    QDialog::show();
 
138
}
 
139
 
 
140
 
 
141
void 
 
142
DiagnosticsDialog::close()
 
143
{
 
144
    disconnect( &The::audioController().m_thread, SIGNAL( httpBufferSizeChanged( int ) ),
 
145
        this,                                     SLOT( onHttpBufferSizeChanged( int ) ) );
 
146
 
 
147
    disconnect( &The::audioController().m_thread, SIGNAL( decodedBufferSizeChanged( int ) ),
 
148
        this,                                     SLOT( onDecodedBufferSizeChanged( int ) ) );
 
149
 
 
150
    disconnect( &The::audioController().m_thread, SIGNAL( outputBufferSizeChanged( int ) ),
 
151
        this,                                     SLOT( onOutputBufferSizeChanged( int ) ) );
 
152
 
 
153
    QDialog::accept();
 
154
}
 
155
 
 
156
 
 
157
void
 
158
DiagnosticsDialog::onHttpBufferSizeChanged(int bufferSize)
 
159
{
 
160
    ui.httpBufferProgress->setValue( bufferSize );
 
161
    ui.httpBufferLabel->setText( QString::number( bufferSize / 1000.0f, 'f', 1 ) + "k" );
 
162
}
 
163
 
 
164
 
 
165
void
 
166
DiagnosticsDialog::onDecodedBufferSizeChanged(int bufferSize)
 
167
{
 
168
    ui.decodedBufferProgress->setValue( bufferSize );
 
169
    ui.decodedBufferLabel->setText( QString::number( bufferSize / 1000.0f, 'f', 1 ) + "k" );
 
170
}
 
171
 
 
172
 
 
173
void
 
174
DiagnosticsDialog::onOutputBufferSizeChanged(int bufferSize)
 
175
{
 
176
    ui.outputBufferProgress->setValue( bufferSize );
 
177
    ui.outputBufferLabel->setText( QString::number( bufferSize / 1000.0f, 'f', 1 ) + "k" );
 
178
}
 
179
 
 
180
 
 
181
void
 
182
DiagnosticsDialog::onAppEvent( int event, const QVariant& /* data */ )
 
183
{
 
184
    switch ( event )
 
185
    {
 
186
        case Event::ScrobblePointReached:
 
187
        {
 
188
            populateCacheList( The::settings().currentUser().username() );
 
189
        }
 
190
        break;
 
191
 
 
192
        default:
 
193
        break;
 
194
    }
 
195
}
 
196
 
 
197
 
 
198
void
 
199
DiagnosticsDialog::onScrobblerEvent()
 
200
{
 
201
    QString const username = The::settings().currentUser().username();
 
202
 
 
203
    QString submissionStatus = tr( "OK" );
 
204
    if ( The::scrobbler().lastError( username ) != Scrobbler::NoError )
 
205
    {
 
206
        submissionStatus = tr( "Error: " ) + Scrobbler::errorDescription( The::scrobbler().lastError( username ) );
 
207
    }
 
208
    else
 
209
        ui.lastConnectionStatusLabel->setText( QDateTime::currentDateTime().toString( "d/M/yyyy h:mm" ) );
 
210
 
 
211
    ui.submissionServerStatusLabel->setText( submissionStatus );
 
212
    populateCacheList( The::settings().currentUser().username() );
 
213
}
 
214
 
 
215
 
 
216
void
 
217
DiagnosticsDialog::radioHandshakeReturn( Request* req )
 
218
{
 
219
    Handshake* handshake = static_cast<Handshake*>( req );
 
220
    if ( handshake->failed() )
 
221
    {
 
222
        ui.radioServerStatusLabel->setText( tr( "Error: " ) + handshake->errorMessage() );
 
223
    } else
 
224
    {
 
225
        ui.radioServerStatusLabel->setText( tr( "OK" ) );
 
226
    }
 
227
}
 
228
 
 
229
 
 
230
void 
 
231
DiagnosticsDialog::populateCacheList( const QString& username )
 
232
{
 
233
    ScrobbleCache scrobbleCache( username );
 
234
 
 
235
    QList<QTreeWidgetItem *> items;
 
236
    const QList<TrackInfo>& cachedTracks = scrobbleCache.tracks();
 
237
    for( QList<TrackInfo>::const_iterator i = cachedTracks.begin(); i != cachedTracks.end(); i++ )
 
238
    {
 
239
        if ( i->isScrobbled() )
 
240
            items.append( new QTreeWidgetItem( (QTreeWidget*)0,
 
241
                          QStringList() << i->artist() << i->track() << i->album() ) );
 
242
    }
 
243
 
 
244
    ui.cachedTracksList->clear();
 
245
    ui.cachedTracksList->insertTopLevelItems( 0, items );
 
246
 
 
247
    if (scrobbleCache.tracks().isEmpty())
 
248
        ui.cachedTracksLabel->setText( tr( "The cache is empty" ) );
 
249
    else
 
250
        ui.cachedTracksLabel->setText( tr( "%n cached track(s)", "", items.count() ) );
 
251
}
 
252
 
 
253
 
 
254
void 
 
255
DiagnosticsDialog::onRefresh()
 
256
{
 
257
    LastFmUserSettings& user = The::settings().currentUser();
 
258
 
 
259
    QString username = user.username();
 
260
    QString password = user.password();
 
261
    QString version = The::settings().version();
 
262
 
 
263
    // Not strictly necessary. Will refresh itself via signal, too.
 
264
    onScrobblerEvent();
 
265
 
 
266
//     ui.submissionServerStatusLabel->setText( tr( "Checking.." ) );
 
267
    ui.radioServerStatusLabel->setText( tr( "Checking.." ) );
 
268
 
 
269
    Handshake* radioHandshake = new Handshake;
 
270
    radioHandshake->setUsername( username );
 
271
    radioHandshake->setPassword( password );
 
272
    radioHandshake->setVersion( version );
 
273
    radioHandshake->setLanguage( The::settings().appLanguage() );
 
274
    radioHandshake->start();
 
275
 
 
276
    connect( radioHandshake, SIGNAL( result( Request* ) ),
 
277
        this,           SLOT  ( radioHandshakeReturn( Request* ) ),
 
278
        Qt::QueuedConnection );
 
279
 
 
280
    populateCacheList( username );
 
281
}
 
282
 
 
283
 
 
284
void
 
285
DiagnosticsDialog::onCopyToClipboard()
 
286
{
 
287
    QClipboard *clipboard = QApplication::clipboard();
 
288
    QString clipboardText;
 
289
 
 
290
    //TODO should read "Last successful submission" - that's what it actually shows at least
 
291
    clipboardText.append( tr( "Last successful connection: " ) + ui.lastConnectionStatusLabel->text() + "\n\n" );
 
292
    clipboardText.append( tr( "Submission Server: " ) + ui.submissionServerStatusLabel->text() + "\n" );
 
293
    clipboardText.append( ui.cachedTracksLabel->text() + ":\n\n" );
 
294
 
 
295
    // Iterate through cached tracks list and add to clipboard contents
 
296
    for(int row = 0; row < ui.cachedTracksList->topLevelItemCount(); row++)
 
297
    {
 
298
        QTreeWidgetItem *rowData = ui.cachedTracksList->topLevelItem( row );
 
299
        for(int col = 0; col < rowData->columnCount(); col++)
 
300
        {
 
301
            clipboardText.append( rowData->data( col, Qt::DisplayRole ).toString() );
 
302
            clipboardText.append( "\t:\t" );
 
303
        }
 
304
        //remove trailing seperators
 
305
        clipboardText.chop(3);
 
306
        clipboardText.append( "\n" );
 
307
    }
 
308
 
 
309
    #ifndef HIDE_RADIO
 
310
    clipboardText.append("\n" + tr( "Radio Server: " ) + ui.radioServerStatusLabel->text() + "\n" );
 
311
    #endif
 
312
 
 
313
    clipboard->setText( clipboardText );
 
314
}
 
315
 
 
316
 
 
317
void
 
318
DiagnosticsDialog::onTrackFingerprintingStarted( TrackInfo track )
 
319
{
 
320
    ui.fpCurrentTrackLabel->setText( track.toString() );
 
321
    ui.fpQueueSizeLabel->setText( QString::number( The::app().m_fpCollector->queueSize() ) );
 
322
}
 
323
 
 
324
 
 
325
void
 
326
DiagnosticsDialog::onTrackFingerprinted( TrackInfo track  )
 
327
{
 
328
    ui.fpCurrentTrackLabel->setText( "" );
 
329
    ui.fpQueueSizeLabel->setText( QString::number( The::app().m_fpCollector->queueSize() ) );
 
330
    
 
331
    new QTreeWidgetItem( ui.fingerprintedTracksList, QStringList() << track.artist() << track.track() << track.album() );
 
332
}
 
333
 
 
334
 
 
335
void
 
336
DiagnosticsDialog::onCantFingerprintTrack( TrackInfo /* track */, QString /* reason */ )
 
337
{
 
338
    ui.fpCurrentTrackLabel->setText( "" );
 
339
    ui.fpQueueSizeLabel->setText( QString::number( The::app().m_fpCollector->queueSize() ) );
 
340
}