Back to index

scribus-ng  1.3.4.dfsg+svn20071115
upgradechecker.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 
00008 #include <qdir.h>
00009 #include <qdom.h>
00010 #include <qhttp.h>
00011 #include <qnetwork.h>
00012 #include <qwidget.h>
00013 #include <iostream>
00014 #include <cstdlib>
00015 
00016 #include "helpbrowser.h"
00017 #include "scpaths.h"
00018 #include "upgradechecker.h"
00019 #include "upgradechecker.moc"
00020 #include "prefsmanager.h"
00021 #include "scribuscore.h"
00022 
00023 #ifdef _WIN32
00024 #include <windows.h>
00025 #define sleep(t) Sleep(t*1000)
00026 #endif
00027 
00028 UpgradeChecker::UpgradeChecker()
00029  : QObject()
00030 {
00031        init();
00032        writeToConsole=true;
00033 }
00034 
00035 UpgradeChecker::~UpgradeChecker()
00036 {
00037        if(getter)
00038               delete getter;
00039 }
00040 
00041 void UpgradeChecker::init()
00042 {
00043        message="";
00044        getter=0;
00045        updates.clear();
00046        version=(VERSION);
00047        stability="unstable";
00048        QString versionStripped=version.lower();
00049        isCVS=versionStripped.contains("cvs");
00050        if (isCVS)
00051               versionStripped.remove("cvs");
00052        major=versionStripped.section('.',0,0).toInt();
00053        minor=versionStripped.section('.',1,1).toInt();
00054        revision1=versionStripped.section('.',2,2).toInt();
00055        revision2=versionStripped.section('.',3,4).toInt();
00056        #if defined(Q_OS_MAC)
00057        platform="MacOSX";
00058        #elif defined(Q_OS_WIN32)
00059        platform="Win32";
00060        #else
00061        platform="X11";
00062        #endif
00063 }
00064 
00065 
00066 bool UpgradeChecker::fetch()
00067 {
00068        QString filename("scribusversions.xml");
00069        tempFile=ScPaths::getTempFileDir()+filename;
00070 
00071        fin=false;
00072        
00073        QFile file(tempFile);
00074        if (getter)
00075               delete getter;
00076        getter=new QHttp();
00077        connect (getter, SIGNAL(done(bool)), this, SLOT(fileFinished(bool)));
00078        connect (getter, SIGNAL(requestStarted(int)), this, SLOT(reqStarted(int)));
00079        connect (getter, SIGNAL(requestFinished(int, bool)), this, SLOT(reqFinished(int, bool)));
00080        retrieveError=false;
00081        getter->setHost("www.scribus.net");
00082        if (retrieveError)
00083               return true;
00084        outputText("<b>"+ tr("Attempting to get the Scribus version update file")+"</b>");
00085        outputText( tr("(No data on your computer will be sent to an external location)"));
00086        if(!file.open(IO_ReadWrite))
00087               return true;
00088        getterID=getter->get("/downloads/"+filename, &file);
00089        
00090        int waitCount=0;
00091        while (!fin && waitCount<10 && !retrieveError)
00092        {
00093               qApp->processEvents();
00094               sleep(1);
00095               ++waitCount;
00096               if (writeToConsole)
00097                      std::cout << ". " << std::flush;
00098        }
00099        if (writeToConsole)
00100               std::cout << std::endl;
00101        getter->closeConnection();
00102        bool errorOccurred=false;
00103        if (waitCount>=10)
00104        {
00105               outputText("<b>"+ tr("Timed out when attempting to get update file.")+"</b>");
00106               errorOccurred=true;
00107        }
00108        if (retrieveError || getter->error()!=QHttp::NoError)
00109        {
00110               outputText("<b>"+ tr("Error when attempting to get update file: %1").arg(getter->errorString())+"</b>");
00111               errorOccurred=true;
00112        }
00113        if (errorOccurred)
00114        {
00115               file.close();
00116               file.remove();
00117               return true;
00118        }
00119 
00120        file.reset();
00121        process(file);
00122        file.close();
00123        file.remove();
00124        return false;
00125 }
00126 
00127 bool UpgradeChecker::process( QFile& dataFile )
00128 {
00129        
00130        QTextStream ts(&dataFile);
00131        ts.setEncoding(QTextStream::UnicodeUTF8);
00132        QString errorMsg;
00133        int eline;
00134        int ecol;
00135        QDomDocument doc( "scribusversions" );
00136        QString data(ts.read());
00137        if ( !doc.setContent( data, &errorMsg, &eline, &ecol )) 
00138        {
00139               if (data.lower().contains("404 not found"))
00140                      outputText("<b>"+ tr("File not found on server")+"</b>");
00141               else
00142                      outputText("<b>"+ tr("Could not open version file: %1\nError:%2 at line: %3, row: %4").arg(dataFile.name()).arg(errorMsg).arg(eline).arg(ecol)+"</b>");
00143               return false;
00144        }
00145        
00146        QDomElement docElem = doc.documentElement();
00147        QDomNode n = docElem.firstChild();
00148        while( !n.isNull() ) {
00149               QDomElement e = n.toElement();
00150               if( !e.isNull() ) {
00151                      if (e.tagName()=="release")
00152                      {
00153                             if (e.hasAttribute("stability") && e.hasAttribute("platform") && e.hasAttribute("version"))
00154                             {
00155                                    if (e.attribute("platform")==platform)
00156                                    {
00157                                           bool newVersion = false;
00158                                           QString verA(e.attribute("version"));
00159                                           QString verAStripped=verA.lower();
00160                                           bool verIsCVS=verAStripped.contains("cvs");
00161                                           if (verIsCVS)
00162                                                  verAStripped.remove("cvs");
00163                                           uint verMajor=verAStripped.section('.',0,0).toInt();
00164                                           uint verMinor=verAStripped.section('.',1,1).toInt();
00165                                           uint verRevsion1=verAStripped.section('.',2,2).toInt();
00166                                           uint verRevsion2=verAStripped.section('.',3,3).toInt();
00167                                           //If we found a release whe a user is running an old CVS version
00168                                           if (verMajor==major && verMinor==minor && verRevsion1==revision1 && verRevsion2==revision2 && isCVS && !verIsCVS && !updates.contains(verA))
00169                                                  newVersion = true;
00170                                           else
00171                                           //If we found a version that is not the same as what we are running
00172                                           if (!(verMajor==major && verMinor==minor && verRevsion1==revision1 && verRevsion2==revision2))
00173                                           {
00174                                                  if (
00175                                                         ((verMajor>major) ||
00176                                                         (verMajor==major && verMinor>minor) ||
00177                                                         (verMajor==major && verMinor==minor && verRevsion1>revision1) ||
00178                                                         (verMajor==major && verMinor==minor && verRevsion1==revision1 && verRevsion2>revision2))
00179                                                         && !updates.contains(verA)
00180                                                         )
00181                                                         newVersion = true;
00182                                           }
00183                                           if (newVersion)
00184                                           {
00185                                                  QString ver = verA;
00186                                                  QString link = e.attribute("link", "");
00187                                                  if (!link.isEmpty())
00188                                                  {
00189                                                         QString linkStr = QString("<a href=\"%1\">%2</a>").arg(link).arg(link);
00190                                                         ver = QString("%1 : %2").arg(verA).arg(linkStr);
00191                                                  }
00192                                                  updates.append(ver);
00193                                           }
00194                                    }
00195                             }
00196                      }
00197                      else
00198                      if (e.tagName()=="message")
00199                      {
00200                             message+=e.text();
00201                      }
00202               }
00203               n = n.nextSibling();
00204        }
00205        return true;
00206 }
00207 
00208 QStringList UpgradeChecker::upgradeData( )
00209 {
00210        return updates;
00211 }
00212 
00213 void UpgradeChecker::show(bool error)
00214 {
00215        if (error)
00216        {
00217               outputText("<b>"+ tr("An error occurred while looking for updates for Scribus, please check your internet connection.")+"</b>");
00218               return;
00219        }
00220        if (updates.isEmpty())
00221               outputText("<b>"+ tr("No updates are available for your version of Scribus %1").arg(version)+"</b>");
00222        else
00223        {
00224               outputText("<b>"+ tr("One or more updates for your version of Scribus (%1) are available:").arg(version)+"</b>");
00225               outputText( tr("This list may contain development versions."));
00226               for ( QStringList::Iterator it = updates.begin(); it != updates.end(); ++it )
00227                      outputText(*it);
00228               outputText("<b>"+ tr("Please visit www.scribus.net for details.")+"</b>");
00229        }
00230        outputText(message);
00231 }
00232 
00233 void UpgradeChecker::fileFinished(bool /*error*/)
00234 {
00235        fin=true;
00236 }
00237 
00238 void UpgradeChecker::fileStarted(bool /*error*/)
00239 {
00240 }
00241 
00242 void UpgradeChecker::reqStarted(int /*id*/)
00243 {
00244 }
00245 
00246 void UpgradeChecker::reqFinished(int /*id*/, bool error)
00247 {
00248        retrieveError=error;
00249 }
00250 
00251 void UpgradeChecker::outputText(QString text)
00252 {
00253        QString outText(text);
00254        outText.remove("<b>");
00255        outText.remove("</b>");
00256        outText.remove("<i>");
00257        outText.remove("</i>");
00258        outText.replace("<br>","\n");
00259        outText.replace("<br/>","\n");
00260        qDebug("%s", outText.local8Bit().data());
00261 }
00262 
00263 UpgradeCheckerGUI::UpgradeCheckerGUI(QWidget *widget)
00264  : UpgradeChecker(),
00265  outputWidget(widget)
00266 {
00267        writeToConsole=false;
00268 }
00269 
00270 UpgradeCheckerGUI::~UpgradeCheckerGUI()
00271 {
00272 }
00273 
00274 void UpgradeCheckerGUI::outputText(QString text)
00275 {
00276        TextBrowser* w=dynamic_cast<TextBrowser*>(outputWidget);
00277        if (w)
00278        {
00279               QString wText(w->text());
00280               wText.replace("\n","<br>");
00281               wText.remove("<qt>");
00282               wText.remove("</qt>");
00283               w->setText("<qt>"+wText+text+"<br>"+"</qt>");
00284        }      
00285 }