Vidalia  0.2.17
UpdateProcess.h
Go to the documentation of this file.
00001 /*
00002 **  This file is part of Vidalia, and is subject to the license terms in the
00003 **  LICENSE file, found in the top level directory of this distribution. If you
00004 **  did not receive the LICENSE file with this file, you may obtain it from the
00005 **  Vidalia source package distributed by the Vidalia Project at
00006 **  http://www.torproject.org/projects/vidalia.html. No part of Vidalia, 
00007 **  including this file, may be copied, modified, propagated, or distributed 
00008 **  except according to the terms described in the LICENSE file.
00009 */
00010 
00011 #ifndef _UPDATEPROCESS_H
00012 #define _UPDATEPROCESS_H
00013 
00014 #include "PackageInfo.h"
00015 
00016 #include <QProcess>
00017 #include <QDateTime>
00018 #include <QStringList>
00019 #include <QUrl>
00020 
00021 
00022 class UpdateProcess : public QProcess
00023 {
00024   Q_OBJECT
00025 
00026 public:
00027   enum BundleInfo {
00028     TorBundleInfo,
00029   };
00030 
00031   /** Default constructor.
00032    */
00033   UpdateProcess(QObject *parent = 0);
00034 
00035   /** Begin a check for software updates that may be available for the
00036    * software package specified by <b>bi</b>.
00037    */
00038   void checkForUpdates(BundleInfo bi);
00039 
00040   /** Instructs the software update process to install previously downloaded
00041    * files for <b>bi</b>.
00042    */
00043   void installUpdates(BundleInfo bi);
00044 
00045   /** Returns true if the update process is currently in the middle of an
00046    * operation, such as checking for or installing updates.
00047    */
00048   bool isRunning() const;
00049 
00050   /** Sets the port to use as a SOCKS proxy to <b>port</b>. If <b>port</b> is
00051    * set to 0, then no SOCKS proxy will be used when checking for updates.
00052    */
00053   void setSocksPort(quint16 port);
00054 
00055   /** Return the time at which we should next check for available updates,
00056    * given the last we checked was at <b>lastCheckedAt</b>.
00057    */
00058   static QDateTime nextCheckForUpdates(const QDateTime &lastCheckedAt);
00059 
00060   /** Return true if we should check for available software udpates, given
00061    * the last time we checked was at <b>lastCheckedAt</b>. The returned
00062    * QDateTime will be in UTC.
00063    */
00064   static bool shouldCheckForUpdates(const QDateTime &lastCheckedAt);
00065 
00066   /** Returns the preferred interval (in seconds) between executions of the
00067    * Glider process to check for available software updates.
00068    */
00069   static int checkForUpdatesInterval();
00070 
00071   /** Returns the path and filename of the software update executable.
00072    */
00073   static QString updateExecutable();
00074 
00075   /** Returns the path in which the software update executable should write
00076    * all of its state information.
00077    */
00078   static QString updateRepositoryDir();
00079 
00080 signals:
00081   /** Emitted when the check for available software updates failed.
00082    * <b>errmsg</b> contains a human-readable description of the problem
00083    * encountered.
00084    */
00085   void checkForUpdatesFailed(QString errmsg);
00086 
00087   /** Emitted while an updated package download is in progress. <b>url</b> is
00088    * location of the update, <b>bytesReceived</b> is how many bytes have been
00089    * downloaded so far and <b>bytesTotal</b> is the total size of the package
00090    * being downloaded. */
00091   void downloadProgress(QString url, int bytesReceived, int bytesTotal);
00092 
00093   /** Emitted when updated software packages in bundle <b>bi</b> are
00094    * are available. <b>packages</b> contains a collection of PackageInfo objects
00095    * describing the updates available for installation.
00096    */
00097   void updatesAvailable(UpdateProcess::BundleInfo bi, PackageList packages);
00098 
00099   /** Emitted after all available updated packages have been successfully
00100    * installed.
00101    */
00102   void updatesInstalled(int nPackagesInstalled);
00103 
00104   /** Emitted when there is an error installing one or more updated software
00105    * packages. <b>errmsg</b> might even contain a useful description of the
00106    * error encountered (but don't bet the farm on it).
00107    */
00108   void installUpdatesFailed(QString errmsg);
00109 
00110 public slots:
00111     /** Cancels the currently running software update operation immediately. */
00112   void cancel();
00113 
00114 protected slots:
00115   /** Called when there is data to be read from the update process's stdout.
00116    * Reads and parses all available data.
00117    */
00118   void readStandardOutput();
00119 
00120   /** Called when there is data to be read from the update process's stderr.
00121    * Reads and parses all available data.
00122    */
00123   void readStandardError();
00124 
00125   /** Called when the underlying QProcess encounters an error.
00126    */
00127   void onError(QProcess::ProcessError error);
00128 
00129   /** Called when the auto-update process has terminated.
00130    */
00131   void onFinished(int exitCode, QProcess::ExitStatus exitStatus);
00132 
00133 protected:
00134   enum UpdateCommand {
00135     NoCommand,
00136     CheckForUpdates,
00137     InstallUpdates,
00138   };
00139 
00140   /** Converts a BundleInfo enum value to its proper Thandy-recognized URL
00141    * for the current OS and architecture. */
00142   QString bundleInfoToString(BundleInfo bundleInfo);
00143 
00144   /** Returns a PackageInfo object containing information about the updated
00145    * package specified by the /pkginfo/ URL in <b>package</b>.
00146    */
00147   static PackageInfo packageInfo(const QString &package);
00148 
00149   /** Returns a PackageInfo object populated with information extracted
00150    * from a Thandy-formatted XML document given by <b>xml</b>.
00151    */
00152   static PackageInfo packageInfoFromXml(const QByteArray &xml);
00153 
00154 private:
00155   /** Enum value of the current auto-update operation. */
00156   UpdateCommand _currentCommand;
00157 
00158   /** Enum value of the last bundle for which we performed some action
00159    * (e.g., check for updates, install an update, etc. */
00160   BundleInfo _currentBundle;
00161 
00162   /** List of packages that have available updates. */
00163   PackageList _packageList;
00164 
00165   /** Currently configured SOCKS port. */
00166   quint16 _socksPort;
00167 };
00168 
00169 #endif
00170