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

« back to all changes in this revision

Viewing changes to src/libLastFmTools/WebService.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
 
 *      Christian Muehlhaeuser, Last.fm Ltd <chris@last.fm>                *
4
 
 *      Erik Jaelevik, Last.fm Ltd <erik@last.fm>                          *
5
 
 *      Max Howell, Last.fm Ltd <max@last.fm>                              *
6
 
 *                                                                         *
7
 
 *   This program is free software; you can redistribute it and/or modify  *
8
 
 *   it under the terms of the GNU General Public License as published by  *
9
 
 *   the Free Software Foundation; either version 2 of the License, or     *
10
 
 *   (at your option) any later version.                                   *
11
 
 *                                                                         *
12
 
 *   This program is distributed in the hope that it will be useful,       *
13
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15
 
 *   GNU General Public License for more details.                          *
16
 
 *                                                                         *
17
 
 *   You should have received a copy of the GNU General Public License     *
18
 
 *   along with this program; if not, write to the                         *
19
 
 *   Free Software Foundation, Inc.,                                       *
20
 
 *   51 Franklin Steet, Fifth Floor, Boston, MA  02111-1307, USA.          *
21
 
 ***************************************************************************/
22
 
 
23
 
#include <QMessageBox>
24
 
 
25
 
#include "logger.h"
26
 
#include "Settings.h"
27
 
#include "WebService/Request.h"
28
 
#include "WebService.h"
29
 
 
30
 
 
31
 
WebService::WebService( QObject* parent ) :
32
 
        QObject( parent ),
33
 
        m_isAutoDetectedProxy( false ),
34
 
                m_proxyPort( 0 ),
35
 
        m_isSubscriber( false )
36
 
{
37
 
    LOGL( 3, "Initialising Web Service" );
38
 
    
39
 
    //TODO needs to be a setting, to manually set the proxy or automatically
40
 
    autoDetectProxy();
41
 
}
42
 
 
43
 
void
44
 
WebService::requestResult( Request *r )
45
 
{
46
 
    Q_ASSERT( r );
47
 
   
48
 
    switch (r->type())
49
 
    {
50
 
        case TypeHandshake:
51
 
        { 
52
 
            //TODO mxcl before I refactored, the handshake success if block was still
53
 
            // executed, but the result code was set to an error, I considered this a
54
 
            // bug and hopefully I'll remember to ask someone if that is the case or not
55
 
    
56
 
            Handshake *handshake = static_cast<Handshake*>(r);
57
 
 
58
 
            if (handshake->succeeded()) {
59
 
                m_username = handshake->username();
60
 
                m_password = handshake->password();
61
 
                m_streamUrl = handshake->streamUrl(); // legacy
62
 
                m_isSubscriber = handshake->isSubscriber();
63
 
            }
64
 
            
65
 
            if (handshake->isMessage())
66
 
                QMessageBox::information( 
67
 
                        qApp->activeWindow(), 
68
 
                        tr( "Last.fm Information" ),
69
 
                        handshake->message() );            
70
 
            
71
 
            emit handshakeResult( handshake );
72
 
            
73
 
            break;
74
 
        }
75
 
 
76
 
        // these macros makes the code more readable
77
 
        #define CASE( T ) case Type##T: { T##Request *request = static_cast<T##Request*>(r);
78
 
        #define break } break
79
 
 
80
 
        CASE( ChangeStation )
81
 
            StationUrl url = request->stationUrl();
82
 
            QString name = request->stationName();
83
 
 
84
 
          #ifndef HIDE_RADIO
85
 
            if (r->succeeded())
86
 
            {
87
 
                UserSettings &user = The::settings().currentUser();
88
 
 
89
 
                // Don't store previews (i.e. the ones that have XSPF)
90
 
                if (!request->hasXspf())
91
 
                {
92
 
                    user.setResumeStation( url );
93
 
                }
94
 
            }
95
 
          #endif
96
 
        
97
 
            emit changeStationResult( request );
98
 
            
99
 
            if (r->succeeded())
100
 
                emit stationChanged( url, name );
101
 
            break;
102
 
       
103
 
        CASE( SetTag )
104
 
            emit setTagResult( request );
105
 
            break;
106
 
        
107
 
        default:
108
 
            ;
109
 
    }
110
 
    
111
 
    /// these are emitted only for the currentUsername()
112
 
    switch (r->type()) 
113
 
    {
114
 
        CASE( Love )
115
 
            emit loved( request->track() );
116
 
            break;
117
 
        CASE( UnLove )
118
 
            emit unloved( request->track() );
119
 
            break;
120
 
        CASE( Ban )
121
 
            emit banned( request->track() );
122
 
            break;
123
 
        CASE( UnBan )
124
 
            emit unbanned( request->track() );
125
 
            break;
126
 
        CASE( UnListen )
127
 
            emit unlistened( request->track() );
128
 
            break;
129
 
        CASE( UserTags )
130
 
            if (request->username() == currentUsername())
131
 
                emit userTags( request->tags() );
132
 
            break;
133
 
        CASE( DeleteFriend )
134
 
            emit friendDeleted( request->deletedUsername() );
135
 
            break;
136
 
        CASE( Friends )
137
 
            emit friends( request->usernames() );
138
 
            break;
139
 
        CASE( Neighbours )
140
 
            emit neighbours( request->usernames() );
141
 
            break;
142
 
        CASE( RecentTracks )
143
 
            emit recentTracks( request->tracks() );
144
 
            break;
145
 
        CASE( RecentlyLovedTracks )
146
 
            emit recentLovedTracks( request->tracks() );
147
 
            break;
148
 
        CASE( RecentlyBannedTracks )
149
 
            emit recentBannedTracks( request->tracks() );
150
 
            break;
151
 
            
152
 
        default:
153
 
            ;            
154
 
    }
155
 
   
156
 
    #undef CASE
157
 
    #undef break
158
 
 
159
 
    if (r->failed())
160
 
        emit failure( r );
161
 
    else        
162
 
        emit success( r );
163
 
 
164
 
    emit result( r );
165
 
 
166
 
    
167
 
    if (r->autoDelete())
168
 
        // do last in case one of the signals is queued
169
 
        r->deleteLater();
170
 
}
171
 
 
172
 
QString
173
 
WebService::challengeString()
174
 
{
175
 
    uint const unixTime = QDateTime::currentDateTime().toTime_t();
176
 
    return QString::number( unixTime );
177
 
}
178
 
 
179
 
 
180
 
#ifdef Q_WS_MAC
181
 
    #include "CFStringToQString.h"
182
 
    #include <SystemConfiguration/SystemConfiguration.h> 
183
 
#endif
184
 
 
185
 
void
186
 
WebService::autoDetectProxy()
187
 
{
188
 
    Q_DEBUG_BLOCK;
189
 
 
190
 
    #ifdef Q_WS_MAC
191
 
        CFNumberRef enableNum;
192
 
        int enable;
193
 
 
194
 
        // Get the dictionary.
195
 
        CFDictionaryRef proxyDict = SCDynamicStoreCopyProxies( NULL );
196
 
        bool result = (proxyDict != NULL);
197
 
    
198
 
        // Get the enable flag.  This isn't a CFBoolean, but a CFNumber.
199
 
        if (result) {
200
 
            enableNum = (CFNumberRef) CFDictionaryGetValue( proxyDict, kSCPropNetProxiesHTTPEnable );
201
 
            result = (enableNum != NULL) && (CFGetTypeID(enableNum) == CFNumberGetTypeID());
202
 
        }
203
 
        
204
 
        if (result)
205
 
            result = CFNumberGetValue( enableNum, kCFNumberIntType, &enable ) && (enable != 0);
206
 
        
207
 
        // Get the proxy host.  DNS names must be in ASCII.  If you 
208
 
        // put a non-ASCII character  in the "Secure Web Proxy"
209
 
        // field in the Network preferences panel, the CFStringGetCString
210
 
        // function will fail and this function will return false.
211
 
        
212
 
        CFStringRef hostStr;
213
 
        
214
 
        if (result) {
215
 
            hostStr = (CFStringRef) CFDictionaryGetValue( proxyDict, kSCPropNetProxiesHTTPProxy );
216
 
            result = (hostStr != NULL) && (CFGetTypeID(hostStr) == CFStringGetTypeID());
217
 
        }
218
 
        if (result)
219
 
            m_proxyHost = CFStringToQString( hostStr );
220
 
 
221
 
 
222
 
    ////// Get the proxy port.
223
 
        int portInt;
224
 
        CFNumberRef portNum;
225
 
        
226
 
        if (result) {
227
 
            portNum = (CFNumberRef) CFDictionaryGetValue( proxyDict, kSCPropNetProxiesHTTPPort );
228
 
            result = (portNum != NULL) && (CFGetTypeID(portNum) == CFNumberGetTypeID());
229
 
        }
230
 
        if (result)
231
 
            result = CFNumberGetValue( portNum, kCFNumberIntType, &portInt );
232
 
        
233
 
        if (result)
234
 
            m_proxyPort = portInt;
235
 
    
236
 
    ////// Set that we found the proxy information
237
 
        m_isAutoDetectedProxy = true;
238
 
    
239
 
    ////// Clean up.
240
 
        
241
 
        if (proxyDict != NULL)
242
 
            CFRelease( proxyDict );
243
 
    #endif
244
 
    
245
 
//     qDebug() << m_proxyHost << ':' << m_proxyPort;
246
 
}