00001 #ifndef NFONTUPDATER_H
00002 #define NFONTUPDATER_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <QtGlobal>
00035 #include <QObject>
00036 #include <QString>
00037 #include <QStringList>
00038 #include <QUrl>
00039 #include <QVector>
00040 #include <QFile>
00041 #include <QHttp>
00042 #include <QFontDatabase>
00043
00044 class NFontUpdater : public QObject
00045 {
00046 Q_OBJECT
00047 Q_ENUMS(Source);
00048 Q_ENUMS(Step);
00049 Q_ENUMS(Error);
00050
00051 public:
00052 enum Source
00053 {
00054 FromNone = 0, FromServer, FromLocal,
00055 };
00056
00057 enum Step
00058 {
00059 Initialize = 0,
00060 CleanupDownloadDirectory,
00061 CheckNextPackageToDownload,
00062 DownloadCurrentPackage,
00063 CheckAllRequiredPackagesAvailable,
00064 RemoveFontSymlinks,
00065 RemoveFontFiles,
00066 CheckNextPackageToUnpack,
00067 UnpackCurrentPackage,
00068 RemoveCurrentPackage,
00069 MakeFontSymlinks,
00070 UpdateApplicationFont,
00071 Finish
00072 };
00073
00074 enum Error
00075 {
00076 NoError = 0,
00077 InvalidUrl,
00078 PackageNoFoundOnLocal,
00079 PackageNoFoundOnServer,
00080 LocalStorageNonexistent,
00081 LocalStorageUnReadable,
00082 LocalStorageUnWritable,
00083 FontFilesNoFoundOnLocal,
00084 CleanDownloadDirectoryFailed,
00085 RemoveFontLinksFailed,
00086 RemoveFontFilesFailed,
00087 UnpackFailed,
00088 MakeFontLinksFailed,
00089 UpdateApplicationFontFailed,
00090 DownloadFailed,
00091 };
00092
00093 explicit NFontUpdater(QObject *parent = 0);
00094 ~NFontUpdater( );
00095
00096 static bool needExtendedFont(const QString &font);
00097
00098 void setSource(Source source) { fontSource = source; }
00099 void setFontFrom(const QString &from) { fromFont = from; }
00100 void setFontTo(const QString &to) { toFont = to; }
00101
00102 Source source( ) const { return fontSource; }
00103 QString from( ) const { return fromFont; }
00104 QString to( ) const { return toFont; }
00105
00106
00107
00108 void setLocalStorage(const QString &path) { localStorageDir = path; }
00109
00110 QString currentPackage( ) const { return -1 == currentPackageIndex ? "" : fontPackageList[currentPackageIndex]; }
00111
00112 Error error( ) const { return updateError; }
00113 QString errorString( ) { return updateErrorString; }
00114
00115 Step currentStep( ) const { return stepSequence[currentStepIndex]; }
00116 Step nextStep( ) const { return stepSequence[nextStepIndex]; }
00117 void initUpdater( );
00118 void cleanup( );
00119 void doNextStep( );
00120
00121 bool init( );
00122 bool cleanupDownloadDirectory( );
00123 bool startDownloadCurrentPackage( );
00124 int checkNextPackageToDownload( );
00125 int checkNextPackageToUnpack( );
00126 bool checkAllRequiredPackagesAvailable( );
00127 bool removeFontSymlinks( );
00128 bool removeFontFiles( );
00129 bool unpackCurrentPackage( );
00130 bool removeCurrentPackage( );
00131 bool makeFontSymlinks( );
00132 bool updateApplicationFont( );
00133 bool finish( );
00134
00135 Q_SIGNALS:
00136 void stepFinished(bool error);
00137 void httpDone(bool error);
00138
00139 private Q_SLOTS:
00140 void onHttpResponseHeaderReceived(const QHttpResponseHeader &resp);
00141 void onHttpRequestFinished(int id, bool error);
00142
00143 private:
00144 void setError(Error error) { updateError = error; }
00145 void setErrorString(const QString &text) { updateErrorString = text; }
00146 QVector<Step> getStepSequence(Source source, const QString &from, const QString &to) const;
00147 QStringList getDefaultPackages(const QString &font) const;
00148 QString getPackageName(int index) const;
00149 QString getDefaultServerStorageDir(const QString &font) const;
00150 QString getDefaultLocalStorageDir(Source source) const;
00151 QString convertToPath(const QStringList &list) const;
00152
00153 private:
00154 Source fontSource;
00155 Error updateError;
00156 QString updateErrorString;
00157 QString fromFont;
00158 QString toFont;
00159
00160 QVector<Step> stepSequence;
00161 int currentStepIndex;
00162 int nextStepIndex;
00163
00164 int currentHttpGetId;
00165 bool currentHttpGetFinished;
00166 QFile *currentHttpGetFile;
00167 QHttp *currentHttp;
00168 QUrl currentPackageUrl;
00169 QString serverStorageDir;
00170 QString localStorageDir;
00171 QStringList fontPackageList;
00172 int currentPackageIndex;
00173 };
00174
00175 #endif // NFONTUPDATER_H
00176