1 /***************************************************************************
2 * This file is part of libmygpo-qt                                         *
3 * Copyright (c) 2010 - 2013 Stefan Derkits <stefan@derkits.at>             *
4 * Copyright (c) 2010 - 2011 Christian Wagner <christian.wagner86@gmx.at>   *
5 * Copyright (c) 2010 - 2011 Felix Winter <ixos01@gmail.com>                *
6 *                                                                          *
7 * This library is free software; you can redistribute it and/or            *
8 * modify it under the terms of the GNU Lesser General Public               *
9 * License as published by the Free Software Foundation; either             *
10 * version 2.1 of the License, or (at your option) any later version.       *
11 *                                                                          *
12 * This library 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 GNU        *
15 * Lesser General Public License for more details.                          *
16 *                                                                          *
17 * You should have received a copy of the GNU Lesser General Public         *
18 * License along with this library; if not, write to the Free Software      *
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 *
20 * USA                                                                      *
21 ***************************************************************************/
22 
23 #include "Settings_p.h"
24 
25 #include "qjsonwrapper/Json.h"
26 
27 using namespace mygpo;
28 
SettingsPrivate(Settings * qq,QNetworkReply * reply)29 SettingsPrivate::SettingsPrivate( Settings* qq, QNetworkReply* reply ): q( qq ), m_reply( reply ), m_error( QNetworkReply::NoError )
30 {
31     QObject::connect( m_reply, SIGNAL( finished() ), this, SLOT( parseData() ) );
32     QObject::connect( m_reply, SIGNAL( error( QNetworkReply::NetworkError ) ), this, SLOT( error( QNetworkReply::NetworkError ) ) );
33 }
34 
~SettingsPrivate()35 SettingsPrivate::~SettingsPrivate()
36 {
37 }
38 
settings() const39 QVariant SettingsPrivate::settings() const
40 {
41     return m_settings;
42 }
43 
parse(const QVariant & data)44 bool SettingsPrivate::parse( const QVariant& data )
45 {
46     m_settings = data;
47     return true;
48 }
49 
parse(const QByteArray & data)50 bool SettingsPrivate::parse( const QByteArray& data )
51 {
52     bool ok;
53     QVariant variant = QJsonWrapper::parseJson( data, &ok );
54     if( ok )
55     {
56         ok = ( parse( variant ) );
57     }
58     return ok;
59 }
60 
parseData()61 void SettingsPrivate::parseData()
62 {
63     if( m_reply->error() == QNetworkReply::NoError )
64     {
65         if( parse( m_reply->readAll() ) )
66         {
67             emit q->finished();
68         }
69         else
70         {
71             emit q->parseError();
72         }
73     }
74     m_reply->deleteLater();
75 }
76 
77 
error(QNetworkReply::NetworkError error)78 void SettingsPrivate::error( QNetworkReply::NetworkError error )
79 {
80     this->m_error = error;
81     emit q->requestError( error );
82 }
83 
Settings(QNetworkReply * reply,QObject * parent)84 Settings::Settings( QNetworkReply* reply, QObject* parent ): QObject( parent ), d( new SettingsPrivate( this, reply ) )
85 {
86 
87 }
88 
~Settings()89 Settings::~Settings()
90 {
91     delete d;
92 }
93 
settings() const94 QVariant Settings::settings() const
95 {
96     return d->settings();
97 }
98