File: OnlineServices.cpp

package info (click to toggle)
fotowall 0.9-8
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 4,280 kB
  • sloc: cpp: 23,275; makefile: 51; sh: 25
file content (177 lines) | stat: -rw-r--r-- 6,259 bytes parent folder | download | duplicates (3)
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
172
173
174
175
176
177
/***************************************************************************
 *                                                                         *
 *   This file is part of the Fotowall project,                            *
 *       http://www.enricoros.com/opensource/fotowall                      *
 *                                                                         *
 *   Copyright (C) 2009 by Enrico Ros <enrico.ros@gmail.com>               *
 *                                                                         *
 *   This program 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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "OnlineServices.h"

#include "Shared/MetaXmlReader.h"
#include "App.h"
#include "Settings.h"
#include "VersionCheckDialog.h"

#include <QDate>
#include <QDesktopServices>
#include <QMessageBox>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QTimer>


// const strings
#define TUTORIAL_URL QUrl("http://fosswire.com/post/2008/09/fotowall-make-wallpaper-collages-from-your-photos/")
#define TUTORIAL_STRING "Peter walks you through how to use Foto"
#define FOTOWALL_WEBSITE_STRING "http://www.enricoros.com/opensource/fotowall/"
#define ENRICO_BLOG_STRING "http://www.enricoros.com/blog/tag/fotowall/"



OnlineServices::OnlineServices(QNetworkAccessManager * nam, QObject * parent)
  : QObject(parent)
  , m_nam(nam)
  , m_haveTutorial(false)
{
    // set the global reference
    App::onlineServices = this;

#if !defined(NO_UPDATE_CHECK)
    // start-up operations
    autoUpdate();
#endif
}

OnlineServices::~OnlineServices()
{
    // unset the global reference
    App::onlineServices = 0;
}

bool OnlineServices::checkForTutorial()
{
    // if altready found, don't check again
    if (m_haveTutorial)
        return true;

    // try to get the tutorial page (note, multiple QNAMs will be deleted on app closure)
    QNetworkReply * reply = m_nam->get(QNetworkRequest(TUTORIAL_URL));
    connect(reply, SIGNAL(finished()), this, SLOT(slotCheckTutorialReply()));
    return false;
}

void OnlineServices::openWebpage()
{
    // if not fetched, queue a post-operation
    MetaXml::Connector * conn = MetaXml::Connector::instance();
    if (!conn->hasDone()) {
        if (m_postOps.isEmpty()) {
            connect(conn, SIGNAL(fetched()), this, SLOT(slotConnectorFinished()));
            connect(conn, SIGNAL(fetchError(const QString &)), this, SLOT(slotConnectorFinished()));
        }
        m_postOps.append(OpenWebsite);
        return;
    }

    // if already fetched, grab the url and go
    if (conn->reader()) {
        foreach (const MetaXml::Website & site, conn->reader()->websites) {
            if (site.type == MetaXml::Website::HomePage) {
                QDesktopServices::openUrl(QUrl(site.url));
                return;
            }
        }
    }
    QDesktopServices::openUrl(QUrl(FOTOWALL_WEBSITE_STRING));
}

void OnlineServices::openBlog()
{
    // if not fetched, queue a post-operation
    MetaXml::Connector * conn = MetaXml::Connector::instance();
    if (!conn->hasDone()) {
        if (m_postOps.isEmpty()) {
            connect(conn, SIGNAL(fetched()), this, SLOT(slotConnectorFinished()));
            connect(conn, SIGNAL(fetchError(const QString &)), this, SLOT(slotConnectorFinished()));
        }
        m_postOps.append(OpenBlog);
        return;
    }

    // ask for confirmation
    if (QMessageBox::question(0, tr("Opening Fotowall's author Blog"), tr("This is the blog of the main author of Fotowall.\nDo you want to open the web page?"), QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
        return;

    // if already fetched, grab the url and go
    if (conn->reader()) {
        foreach (const MetaXml::Website & site, conn->reader()->websites) {
            if (site.type == MetaXml::Website::Blog) {
                QDesktopServices::openUrl(QUrl(site.url));
                return;
            }
        }
    }
    QDesktopServices::openUrl(QUrl(ENRICO_BLOG_STRING));
}

void OnlineServices::openTutorial()
{
    int answer = QMessageBox::question(0, tr("Opening the Web Tutorial"), tr("The Tutorial is provided on Fosswire by Peter Upfold.\nIt's about Fotowall 0.2 a rather old version.\nDo you want to open the web page?"), QMessageBox::Yes, QMessageBox::No);
    if (answer == QMessageBox::Yes)
        QDesktopServices::openUrl(TUTORIAL_URL);
}

void OnlineServices::checkForUpdates()
{
    VersionCheckDialog vcd;
    vcd.exec();
    App::settings->setValue("Fotowall/LastUpdateCheck", QDate::currentDate());
}

void OnlineServices::autoUpdate()
{
    // find out the time of the last update check
    QDate lastCheck = App::settings->value("Fotowall/LastUpdateCheck").toDate();
    if (lastCheck.isNull()) {
        App::settings->setValue("Fotowall/LastUpdateCheck", QDate::currentDate());
        return;
    }

    // check for updates 30 days after the last one
    if (lastCheck.daysTo(QDate::currentDate()) > 30)
        QTimer::singleShot(2000, this, SLOT(checkForUpdates()));
}

void OnlineServices::slotCheckTutorialReply()
{
    // set the m_haveTutorial variable if the tutorial was found & is valid
    QNetworkReply * reply = static_cast<QNetworkReply *>(sender());
    if (reply->error() != QNetworkReply::NoError)
        return;
    QString htmlCode = reply->readAll();
    bool haveTutorial = htmlCode.contains(TUTORIAL_STRING, Qt::CaseInsensitive);

    // notify the listeners about any change
    if (haveTutorial != m_haveTutorial) {
        m_haveTutorial = haveTutorial;
        emit tutorialFound(m_haveTutorial);
    }
}

void OnlineServices::slotConnectorFinished()
{
    // perform post-ops now that the connector has finished
    while (!m_postOps.isEmpty()) {
        switch (m_postOps.takeFirst()) {
            case OpenWebsite:   openWebpage();  break;
            case OpenBlog:      openBlog();     break;
        }
    }
}