Subversion Repositories Scribus

Rev

Rev 25064 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5855 cbradney 1
/*
2
For general Scribus (>=1.3.2) copyright and licensing information please refer
3
to the COPYING file provided with the program. Following this notice may exist
4
a copyright and/or license notice that predates the release of Scribus 1.3.2
5
for which a new license (GPL+exception) is in place.
6
*/
7
 
16546 jghali 8
#include <QApplication>
10926 cbradney 9
#include <QDebug>
10108 cbradney 10
#include <QDir>
10223 cbradney 11
#include <QDomDocument>
18438 craig 12
#include <QNetworkRequest>
10613 cbradney 13
#include <QTextCodec>
9724 cbradney 14
#include <QTextStream>
15
#include <QWidget>
10223 cbradney 16
 
22698 jghali 17
#include <chrono>
18
#include <cstdlib>
5855 cbradney 19
#include <iostream>
22698 jghali 20
#include <thread>
5855 cbradney 21
 
25158 craig 22
#include "api/api_application.h"
5917 jghali 23
#include "scpaths.h"
5855 cbradney 24
#include "upgradechecker.h"
25
 
22600 craig 26
UpgradeChecker::UpgradeChecker()
5855 cbradney 27
{
23656 jghali 28
        m_stability = "unstablesvn";
24486 craig 29
        m_isSVN = ScribusAPI::isSVN();
30
        major = ScribusAPI::getVersionMajor();
31
        minor = ScribusAPI::getVersionMinor();
32
        m_patchLevel = ScribusAPI::getVersionPatch();
33
        m_versionSuffix = ScribusAPI::getVersionSuffix().toLower().remove("svn").toInt();
23702 jghali 34
#if defined(Q_OS_MAC)
23656 jghali 35
        m_platform = "MacOSX";
23702 jghali 36
#elif defined(Q_OS_WIN32)
23656 jghali 37
        m_platform = "Win32";
23702 jghali 38
#else
23656 jghali 39
        m_platform = "X11";
23702 jghali 40
#endif
5855 cbradney 41
}
42
 
10926 cbradney 43
void UpgradeChecker::fetch()
5855 cbradney 44
{
45
        QString filename("scribusversions.xml");
23656 jghali 46
        m_tempFile = ScPaths::tempFileDir()+filename;
5866 cbradney 47
 
22730 jghali 48
        m_fin = false;
19161 craig 49
 
22730 jghali 50
        m_file = new QFile(m_tempFile);
51
        m_networkManager = new QNetworkAccessManager(this);
52
        if (m_networkManager != nullptr && m_file != nullptr)
5855 cbradney 53
        {
13430 cbradney 54
                outputText( tr("No data on your computer will be sent to an external location"));
25064 jghali 55
                QApplication::processEvents();
22730 jghali 56
                if (m_file->open(QIODevice::ReadWrite))
10926 cbradney 57
                {
17543 craig 58
                        QString hostname("services.scribus.net");
23657 jghali 59
                        QString filepath("/" + filename);
21951 craig 60
                        QUrl fileURL(QString("https://%1%2").arg(hostname, filepath));
23657 jghali 61
                        outputText("<b>" + tr("Attempting to get the Scribus version update file:") + "</b>");
18438 craig 62
                        outputText(fileURL.toString());
63
 
64
                        QNetworkRequest networkRequest(fileURL);
20691 craig 65
                        m_networkReply = m_networkManager->get(networkRequest);
66
                        connect(m_networkReply, SIGNAL(finished()), SLOT(downloadFinished()));
67
                        connect(m_networkReply, SIGNAL(readyRead()), SLOT(downloadReadyRead()));
18438 craig 68
 
10926 cbradney 69
                        int waitCount=0;
20691 craig 70
                        while (!m_fin && waitCount<20)
10926 cbradney 71
                        {
22698 jghali 72
                                std::this_thread::sleep_for(std::chrono::seconds(1));
73
                                ++waitCount;
74
                                if (m_writeToConsole)
75
                                        std::cout << ". " << std::flush;
76
                                outputText( ".", true );
25064 jghali 77
                                QApplication::processEvents();
10926 cbradney 78
                        }
20691 craig 79
                        if (m_writeToConsole)
22698 jghali 80
                                std::cout << std::endl;
81
                        if (waitCount >= 20)
23657 jghali 82
                                outputText("<b>" + tr("Timed out when attempting to get update file.") + "</b>");
20925 jghali 83
                        m_file->close();
10926 cbradney 84
                }
20925 jghali 85
                m_file->remove();
5855 cbradney 86
        }
20925 jghali 87
        delete m_file;
23656 jghali 88
        m_file = nullptr;
18438 craig 89
        outputText( tr("Finished") );
20691 craig 90
        m_networkReply->deleteLater();
91
        m_networkManager->deleteLater();
5855 cbradney 92
}
93
 
18438 craig 94
void UpgradeChecker::downloadFinished()
95
{
20691 craig 96
        if (m_networkReply->error())
97
                outputText(QString("Failed: %1").arg(qPrintable(m_networkReply->errorString())));
18438 craig 98
        else
99
        {
20925 jghali 100
                m_file->reset();
18438 craig 101
                process();
22730 jghali 102
                m_fin = true;
23657 jghali 103
                show(m_networkReply->error() != QNetworkReply::NoError);
18438 craig 104
        }
105
}
106
 
107
void UpgradeChecker::downloadReadyRead()
108
{
20925 jghali 109
        m_file->write(m_networkReply->readAll());
18438 craig 110
}
111
 
10926 cbradney 112
bool UpgradeChecker::process()
5855 cbradney 113
{
20925 jghali 114
        if (!m_file)
10926 cbradney 115
                return false;
20925 jghali 116
        QTextStream ts(m_file);
24834 jghali 117
        ts.setEncoding(QStringConverter::Utf8);
5855 cbradney 118
        QString errorMsg;
119
        int eline;
120
        int ecol;
24486 craig 121
        QDomDocument doc("scribusversions");
10589 cbradney 122
        QString data(ts.readAll());
22730 jghali 123
        if (!doc.setContent( data, &errorMsg, &eline, &ecol ))
5855 cbradney 124
        {
21942 craig 125
                if (data.contains("404 not found", Qt::CaseInsensitive))
23657 jghali 126
                        outputText("<b>" + tr("File not found on server") + "</b>");
5855 cbradney 127
                else
23657 jghali 128
                        outputText("<b>" + tr("Could not open version file: %1\nError:%2 at line: %3, row: %4").arg(m_file->fileName(), errorMsg).arg(eline).arg(ecol) + "</b>");
5855 cbradney 129
                return false;
130
        }
5866 cbradney 131
 
5855 cbradney 132
        QDomElement docElem = doc.documentElement();
22730 jghali 133
        for (QDomNode n = docElem.firstChild(); !n.isNull(); n = n.nextSibling())
22724 jghali 134
        {
5855 cbradney 135
                QDomElement e = n.toElement();
22730 jghali 136
                if (e.isNull())
137
                        continue;
138
                if (e.tagName() == "release")
22724 jghali 139
                {
22730 jghali 140
                        if (!e.hasAttribute("stability") || !e.hasAttribute("platform") || !e.hasAttribute("version"))
141
                                continue;
142
                        if (e.attribute("platform") != m_platform)
143
                                continue;
144
 
145
                        bool newVersion = false;
146
                        QString verA(e.attribute("version"));
147
                        QString verAStripped = verA.toLower();
148
                        bool verIsCVS = verAStripped.contains("cvs");
149
                        if (verIsCVS)
150
                                verAStripped.remove("cvs");
23657 jghali 151
                        uint verMajor = verAStripped.section('.', 0, 0).toInt();
152
                        uint verMinor = verAStripped.section('.', 1, 1).toInt();
153
                        uint verRevsion1 = verAStripped.section('.', 2, 2).toInt();
154
                        uint verRevsion2 = verAStripped.section('.', 3, 3).toInt();
24665 jghali 155
                        //If we found a release when a user is running an old CVS version
24486 craig 156
                        if (verMajor == major && verMinor == minor && verRevsion1 == m_patchLevel && verRevsion2 == m_versionSuffix && m_isSVN && !verIsCVS && !m_updates.contains(verA))
22730 jghali 157
                                newVersion = true;
24486 craig 158
                        else if (!(verMajor == major && verMinor == minor && verRevsion1 == m_patchLevel && verRevsion2 == m_versionSuffix))
5855 cbradney 159
                        {
22730 jghali 160
                                //If we found a version that is not the same as what we are running
161
                                if (((verMajor > major) ||
162
                                        (verMajor == major && verMinor > minor) ||
24486 craig 163
                                        (verMajor == major && verMinor == minor && verRevsion1 > m_patchLevel) ||
164
                                        (verMajor == major && verMinor == minor && verRevsion1 == m_patchLevel && verRevsion2 > m_versionSuffix))
22730 jghali 165
                                        && !m_updates.contains(verA))
5855 cbradney 166
                                {
22730 jghali 167
                                        newVersion = true;
5855 cbradney 168
                                }
169
                        }
22730 jghali 170
                        if (newVersion)
171
                        {
172
                                QString ver(verA);
173
                                QString link(e.attribute("link", ""));
174
                                if (!link.isEmpty())
22600 craig 175
                                {
22730 jghali 176
                                        QString linkStr = QString("<a href=\"%1\">%2</a>").arg(link, link);
177
                                        ver = QString("%1 : %2").arg(verA, linkStr);
22600 craig 178
                                }
22730 jghali 179
                                m_updates.append(ver);
180
                        }
5855 cbradney 181
                }
22730 jghali 182
                else if (e.tagName() == "message")
183
                {
184
                        m_message += e.text();
185
                }
5855 cbradney 186
        }
187
        return true;
188
}
189
 
25064 jghali 190
QStringList UpgradeChecker::upgradeData() const
5855 cbradney 191
{
20691 craig 192
        return m_updates;
5855 cbradney 193
}
194
 
195
void UpgradeChecker::show(bool error)
196
{
10926 cbradney 197
        outputText("<br/>");
5855 cbradney 198
        if (error)
199
        {
23657 jghali 200
                outputText("<b>" + tr("An error occurred while looking for updates for Scribus, please check your internet connection.") + "</b>");
5855 cbradney 201
                return;
202
        }
20691 craig 203
        if (m_updates.isEmpty())
24486 craig 204
                outputText("<b>" + tr("No updates are available for your version of Scribus %1").arg(ScribusAPI::getVersion()) + "</b>");
5925 cbradney 205
        else
5855 cbradney 206
        {
24486 craig 207
                outputText("<b>" + tr("One or more updates for your version of Scribus (%1) are available:").arg(ScribusAPI::getVersion()) + "</b>");
10926 cbradney 208
                outputText( tr("This list may contain development/unstable versions."));
20691 craig 209
                for ( QStringList::Iterator it = m_updates.begin(); it != m_updates.end(); ++it )
5925 cbradney 210
                        outputText(*it);
23657 jghali 211
                outputText("<b>" + tr("Please visit www.scribus.net for details.") + "</b>");
212
                outputText("<b>" + tr("If you have installed Scribus from a package management system, for example on a Linux-based operating system, your package manager may have this upgrade available.") + "</b>");
5855 cbradney 213
        }
20691 craig 214
        outputText(m_message);
5855 cbradney 215
}
216
 
22600 craig 217
void UpgradeChecker::outputText(const QString& text, bool /*noLineFeed*/)
5855 cbradney 218
{
5933 cbradney 219
        QString outText(text);
220
        outText.remove("<b>");
221
        outText.remove("</b>");
222
        outText.remove("<i>");
223
        outText.remove("</i>");
224
        outText.replace("<br>","\n");
225
        outText.replace("<br/>","\n");
10926 cbradney 226
        qDebug() << outText.toLocal8Bit().data();
5924 cbradney 227
}
228
 
18438 craig 229
 
230
void UpgradeChecker::reportError(const QString& s)
5924 cbradney 231
{
20691 craig 232
        if (!m_errorReported)
18438 craig 233
        {
234
                outputText("<br/><b>"+ tr("Error: %1").arg(s)+"</b>");
20691 craig 235
                m_errorReported=true;
18438 craig 236
        }
237
}
238
 
20197 craig 239
UpgradeCheckerGUI::UpgradeCheckerGUI(QTextBrowser *tb) : UpgradeChecker()
18438 craig 240
{
25064 jghali 241
        m_outputWidget = tb;
242
        m_writeToConsole = false;
5924 cbradney 243
}
244
 
22600 craig 245
void UpgradeCheckerGUI::outputText(const QString& text, bool noLineFeed)
5924 cbradney 246
{
20691 craig 247
        QTextBrowser* w=m_outputWidget;
22600 craig 248
        if (!w)
249
                return;
250
 
251
        QString wText(w->toPlainText());
252
        wText.replace("\n","<br>");
253
        wText.remove("<qt>");
254
        wText.remove("</qt>");
255
        if (noLineFeed)
256
                w->setHtml("<qt>"+wText+text+"</qt>");
257
        else
258
                w->setHtml("<qt>"+wText+text+"<br>"+"</qt>");
5923 cbradney 259
}
10926 cbradney 260
 
261