Rev 5880 | Rev 5923 | 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 | |||
5866 | cbradney | 8 | #include <qdir.h> |
5855 | cbradney | 9 | #include <qdom.h> |
10 | #include <qhttp.h> |
||
11 | #include <qnetwork.h> |
||
12 | #include <iostream> |
||
5857 | cbradney | 13 | #include <cstdlib> |
5855 | cbradney | 14 | |
5917 | jghali | 15 | #include "scpaths.h" |
5855 | cbradney | 16 | #include "upgradechecker.h" |
17 | #include "upgradechecker.moc" |
||
18 | #include "prefsmanager.h" |
||
19 | #include "scribuscore.h" |
||
20 | |||
5880 | jghali | 21 | #ifdef _WIN32 |
22 | #include <windows.h> |
||
23 | #define sleep Sleep |
||
24 | #endif |
||
5855 | cbradney | 25 | |
26 | UpgradeChecker::UpgradeChecker() |
||
27 | : QObject() |
||
28 | { |
||
29 | getter=0; |
||
30 | updates.clear(); |
||
31 | version=(VERSION); |
||
32 | stability="unstablecvs"; |
||
33 | QString versionStripped=version.lower(); |
||
34 | isCVS=versionStripped.contains("cvs"); |
||
35 | if (isCVS) |
||
36 | versionStripped.remove("cvs"); |
||
37 | major=versionStripped.section('.',0,0).toInt(); |
||
38 | minor=versionStripped.section('.',1,1).toInt(); |
||
39 | revision1=versionStripped.section('.',2,2).toInt(); |
||
40 | revision2=versionStripped.section('.',3,4).toInt(); |
||
41 | #if defined(Q_OS_MAC) |
||
42 | platform="MacOSX"; |
||
43 | #elif defined(Q_OS_WIN32) |
||
44 | platform="Win32"; |
||
45 | #else |
||
46 | platform="X11"; |
||
47 | #endif |
||
48 | } |
||
49 | |||
50 | UpgradeChecker::~UpgradeChecker() |
||
51 | { |
||
52 | } |
||
53 | |||
54 | bool UpgradeChecker::fetch() |
||
55 | { |
||
56 | QString filename("scribusversions.xml"); |
||
5866 | cbradney | 57 | //TODO fix for win32 and mac |
5917 | jghali | 58 | tempFile=ScPaths::getTempFileDir()+filename; |
5866 | cbradney | 59 | |
5855 | cbradney | 60 | fin=false; |
61 | |||
62 | QFile file(tempFile); |
||
63 | if (getter) |
||
64 | delete getter; |
||
65 | getter=new QHttp(); |
||
66 | connect (getter, SIGNAL(done(bool)), this, SLOT(fileFinished(bool))); |
||
67 | connect (getter, SIGNAL(requestStarted(int)), this, SLOT(reqStarted(int))); |
||
68 | connect (getter, SIGNAL(requestFinished(int, bool)), this, SLOT(reqFinished(int, bool))); |
||
69 | retrieveError=false; |
||
70 | getter->setHost("www.scribus.net"); |
||
71 | if (retrieveError) |
||
72 | return true; |
||
5866 | cbradney | 73 | qDebug("%s", tr("Attempting to get the Scribus version update file").local8Bit().data()); |
74 | qDebug("%s", tr("(No data on your computer will be sent to an external location)").local8Bit().data()); |
||
75 | if(!file.open(IO_ReadWrite)) |
||
5855 | cbradney | 76 | return true; |
5866 | cbradney | 77 | getterID=getter->get("/downloads/"+filename, &file); |
5855 | cbradney | 78 | |
79 | int waitCount=0; |
||
80 | while (!fin && waitCount<10 && !retrieveError) |
||
81 | { |
||
82 | qApp->processEvents(); |
||
83 | sleep(1); |
||
84 | ++waitCount; |
||
85 | std::cout << ". " << std::flush; |
||
86 | } |
||
87 | std::cout << std::endl; |
||
88 | getter->closeConnection(); |
||
5858 | cbradney | 89 | bool errorOccurred=false; |
5855 | cbradney | 90 | if (waitCount>=10) |
91 | { |
||
5866 | cbradney | 92 | qDebug("%s", tr("Timed out when attempting to get update file.").local8Bit().data()); |
5858 | cbradney | 93 | errorOccurred=true; |
5855 | cbradney | 94 | } |
95 | if (retrieveError || getter->error()!=QHttp::NoError) |
||
96 | { |
||
5866 | cbradney | 97 | qDebug("%s", tr("Error when attempting to get update file: %1").arg(getter->errorString()).local8Bit().data()); |
5858 | cbradney | 98 | errorOccurred=true; |
99 | } |
||
100 | if (errorOccurred) |
||
101 | { |
||
5866 | cbradney | 102 | file.close(); |
5858 | cbradney | 103 | file.remove(); |
5855 | cbradney | 104 | return true; |
105 | } |
||
5866 | cbradney | 106 | |
107 | file.reset(); |
||
108 | process(file); |
||
109 | file.close(); |
||
110 | file.remove(); |
||
5855 | cbradney | 111 | return false; |
112 | } |
||
113 | |||
5866 | cbradney | 114 | bool UpgradeChecker::process( QFile& dataFile ) |
5855 | cbradney | 115 | { |
5866 | cbradney | 116 | |
5855 | cbradney | 117 | QTextStream ts(&dataFile); |
118 | ts.setEncoding(QTextStream::UnicodeUTF8); |
||
119 | QString errorMsg; |
||
120 | int eline; |
||
121 | int ecol; |
||
122 | QDomDocument doc( "scribusversions" ); |
||
123 | QString data(ts.read()); |
||
124 | if ( !doc.setContent( data, &errorMsg, &eline, &ecol )) |
||
125 | { |
||
126 | if (data.lower().contains("404 not found")) |
||
5866 | cbradney | 127 | qDebug("%s", tr("File not found on server").local8Bit().data()); |
5855 | cbradney | 128 | else |
5866 | cbradney | 129 | qDebug("%s", tr("Could not open version file: %1\nError:%2 at line: %3, row: %4").arg(dataFile.name()).arg(errorMsg).arg(eline).arg(ecol).local8Bit().data()); |
5855 | cbradney | 130 | return false; |
131 | } |
||
5866 | cbradney | 132 | |
5855 | cbradney | 133 | QDomElement docElem = doc.documentElement(); |
134 | QDomNode n = docElem.firstChild(); |
||
135 | while( !n.isNull() ) { |
||
136 | QDomElement e = n.toElement(); |
||
137 | if( !e.isNull() ) { |
||
138 | if (e.tagName()=="release") |
||
139 | { |
||
140 | if (e.hasAttribute("stability") && e.hasAttribute("platform") && e.hasAttribute("version")) |
||
141 | { |
||
142 | if (e.attribute("platform")==platform) |
||
143 | { |
||
144 | QString verA(e.attribute("version")); |
||
145 | QString verAStripped=verA.lower(); |
||
146 | bool verIsCVS=verAStripped.contains("cvs"); |
||
147 | if (verIsCVS) |
||
148 | verAStripped.remove("cvs"); |
||
149 | uint verMajor=verAStripped.section('.',0,0).toInt(); |
||
150 | uint verMinor=verAStripped.section('.',1,1).toInt(); |
||
151 | uint verRevsion1=verAStripped.section('.',2,2).toInt(); |
||
152 | uint verRevsion2=verAStripped.section('.',3,3).toInt(); |
||
153 | if (!(verMajor==major && verMinor==minor && verRevsion1==revision1 && verRevsion2==revision2)) |
||
154 | { |
||
155 | if ( |
||
156 | (verMajor>major) || |
||
157 | (verMajor==major && verMinor>minor) || |
||
158 | (verMajor==major && verMinor==minor && verRevsion1>revision1) || |
||
159 | (verMajor==major && verMinor==minor && verRevsion1==revision1 && verRevsion2>revision2) |
||
160 | ) |
||
161 | updates.append(verA); |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | } |
||
167 | n = n.nextSibling(); |
||
168 | } |
||
169 | return true; |
||
170 | } |
||
171 | |||
172 | QStringList UpgradeChecker::upgradeData( ) |
||
173 | { |
||
174 | return updates; |
||
175 | } |
||
176 | |||
177 | void UpgradeChecker::show(bool error) |
||
178 | { |
||
179 | if (error) |
||
180 | { |
||
5866 | cbradney | 181 | qDebug("%s", tr("An error occurred while looking for updates for Scribus, please check your internet connection.").local8Bit().data()); |
5855 | cbradney | 182 | return; |
183 | } |
||
184 | if (updates.isEmpty()) |
||
185 | { |
||
5866 | cbradney | 186 | qDebug("%s", tr("No updates are available for your version of Scribus %1").arg(version).local8Bit().data()); |
5855 | cbradney | 187 | return; |
188 | } |
||
5866 | cbradney | 189 | qDebug("%s", tr("One or more updates for your version of Scribus (%1) are available:").arg(version).local8Bit().data()); |
5855 | cbradney | 190 | |
191 | for ( QStringList::Iterator it = updates.begin(); it != updates.end(); ++it ) |
||
5866 | cbradney | 192 | qDebug("%s", QString("%1").arg(*it).local8Bit().data()); |
5855 | cbradney | 193 | } |
194 | |||
5866 | cbradney | 195 | void UpgradeChecker::fileFinished(bool /*error*/) |
5855 | cbradney | 196 | { |
197 | fin=true; |
||
198 | } |
||
199 | |||
5866 | cbradney | 200 | void UpgradeChecker::fileStarted(bool /*error*/) |
5855 | cbradney | 201 | { |
202 | } |
||
203 | |||
5866 | cbradney | 204 | void UpgradeChecker::reqStarted(int /*id*/) |
5855 | cbradney | 205 | { |
206 | } |
||
207 | |||
5866 | cbradney | 208 | void UpgradeChecker::reqFinished(int /*id*/, bool error) |
5855 | cbradney | 209 | { |
210 | retrieveError=error; |
||
211 | } |