~ubuntu-branches/ubuntu/trusty/liblastfm/trusty-proposed

« back to all changes in this revision

Viewing changes to .pc/fix_q_os_x11.patch/src/InternetConnectionMonitor.cpp

  • Committer: Package Import Robot
  • Author(s): John Stamp
  • Date: 2013-09-04 11:06:05 UTC
  • Revision ID: package-import@ubuntu.com-20130904110605-r04wb1tyuns31vij
Tags: 1.0.8-2
Check Q_OS_UNIX definition instead of nonexistent Q_OS_X11.  This fixes
the cache and config paths that broke in the last upload.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright 2009 Last.fm Ltd. 
 
3
      - Primarily authored by Max Howell, Jono Cole and Doug Mansell
 
4
 
 
5
   This file is part of liblastfm.
 
6
 
 
7
   liblastfm 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 3 of the License, or
 
10
   (at your option) any later version.
 
11
 
 
12
   liblastfm 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 liblastfm.  If not, see <http://www.gnu.org/licenses/>.
 
19
*/
 
20
 
 
21
#include "InternetConnectionMonitor.h"
 
22
#include "linux/LNetworkConnectionMonitor.h"
 
23
#include "mac/MNetworkConnectionMonitor.h"
 
24
#include "win/WNetworkConnectionMonitor.h"
 
25
#include "NetworkConnectionMonitor.h"
 
26
#include "ws.h"
 
27
 
 
28
#include <QDebug>
 
29
 
 
30
class lastfm::InternetConnectionMonitorPrivate
 
31
{
 
32
public:
 
33
    bool m_up;
 
34
    NetworkConnectionMonitor* m_networkMonitor;
 
35
};
 
36
 
 
37
lastfm::InternetConnectionMonitor::InternetConnectionMonitor( QObject *parent )
 
38
                                 : QObject( parent )
 
39
                                 , d( new InternetConnectionMonitorPrivate)
 
40
{
 
41
    d->m_up = true;
 
42
    d->m_networkMonitor = createNetworkConnectionMonitor();
 
43
 
 
44
    if ( d->m_networkMonitor )
 
45
    {
 
46
        connect( d->m_networkMonitor, SIGNAL( networkUp() ), this, SLOT( onNetworkUp() ) );
 
47
        connect( d->m_networkMonitor, SIGNAL( networkDown() ), this, SLOT( onNetworkDown() ) );
 
48
    }
 
49
 
 
50
    connect( nam(), SIGNAL( finished( QNetworkReply* ) ), this, SLOT( onFinished( QNetworkReply* ) ) );
 
51
}
 
52
 
 
53
lastfm::InternetConnectionMonitor::~InternetConnectionMonitor()
 
54
{
 
55
    delete d;
 
56
}
 
57
 
 
58
bool lastfm::InternetConnectionMonitor::isDown() const
 
59
{
 
60
    return !d->m_up;
 
61
}
 
62
 
 
63
bool lastfm::InternetConnectionMonitor::isUp() const
 
64
{
 
65
    return d->m_up;
 
66
}
 
67
 
 
68
void
 
69
lastfm::InternetConnectionMonitor::onFinished( QNetworkReply* reply )
 
70
{
 
71
    if( reply->attribute( QNetworkRequest::SourceIsFromCacheAttribute).toBool() ) return;
 
72
 
 
73
    switch( reply->error() )
 
74
    {
 
75
        case QNetworkReply::NoError:
 
76
            if ( !d->m_up )
 
77
            {
 
78
                d->m_up = true;
 
79
                emit up();
 
80
                emit connectivityChanged( d->m_up );
 
81
                qDebug() << "Internet connection is reachable :)";
 
82
            }
 
83
            break;
 
84
        case QNetworkReply::HostNotFoundError:
 
85
        case QNetworkReply::TimeoutError:
 
86
        case QNetworkReply::ProxyConnectionRefusedError:
 
87
        case QNetworkReply::ProxyConnectionClosedError:
 
88
        case QNetworkReply::ProxyNotFoundError:
 
89
        case QNetworkReply::ProxyTimeoutError:
 
90
        case QNetworkReply::ProxyAuthenticationRequiredError:
 
91
            if ( d->m_up )
 
92
            {
 
93
                d->m_up = false;
 
94
                emit down();
 
95
                emit connectivityChanged( d->m_up );
 
96
            }
 
97
            break;
 
98
        default:
 
99
            break;
 
100
    }
 
101
}
 
102
 
 
103
void
 
104
lastfm::InternetConnectionMonitor::onNetworkUp()
 
105
{
 
106
#ifdef Q_OS_MAC
 
107
    // We don't need to check on mac as the
 
108
    // check is done as part of the reach api
 
109
    d->m_up = true;
 
110
 
 
111
    emit up();
 
112
    emit connectivityChanged( d->m_up );
 
113
    qDebug() << "Internet connection is reachable :)";
 
114
#else
 
115
    qDebug() << "Network seems to be up again. Let's try if there's internet connection!";
 
116
    nam()->head( QNetworkRequest( QUrl( "http://www.last.fm/" ) ) );
 
117
#endif
 
118
}
 
119
 
 
120
void
 
121
lastfm::InternetConnectionMonitor::onNetworkDown()
 
122
{
 
123
    qDebug() << "Internet is unreachable :(";
 
124
    d->m_up = false;
 
125
    emit down();
 
126
    emit connectivityChanged( d->m_up );
 
127
}
 
128
 
 
129
lastfm::NetworkConnectionMonitor*
 
130
lastfm::InternetConnectionMonitor::createNetworkConnectionMonitor()
 
131
{
 
132
    NetworkConnectionMonitor* ncm = 0;
 
133
 
 
134
#ifdef Q_OS_X11
 
135
    ncm = new LNetworkConnectionMonitor( this );
 
136
#elif defined(Q_OS_WIN) && ! defined __MINGW32__
 
137
    ncm = new WNetworkConnectionMonitor( this );
 
138
#elif defined(Q_OS_MAC)
 
139
    ncm = new MNetworkConnectionMonitor( this );
 
140
#endif
 
141
 
 
142
    return ncm;
 
143
}