e009b5ea77669ed40e5c93ea8131550ad4fdb6d5
[fapman] / aaptinterface.h
1 /*
2         This file is part of Faster Application Manager.
3
4         Faster Application Manager is free software: you can redistribute it and/or modify
5         it under the terms of the GNU General Public License as published by
6         the Free Software Foundation, either version 3 of the License, or
7         (at your option) any later version.
8
9         Faster Application Manager is distributed in the hope that it will be useful,
10         but WITHOUT ANY WARRANTY; without even the implied warranty of
11         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12         GNU General Public License for more details.
13
14         You should have received a copy of the GNU General Public License
15         along with Faster Application Manager.  If not, see <http://www.gnu.org/licenses/>.
16
17         (C) Heikki Holstila 2010
18 */
19
20 #ifndef AAPTINTERFACE_H
21 #define AAPTINTERFACE_H
22
23 #include <QtGui>
24 #include <QtNetwork>
25 #include "blacklistselect.h"
26
27
28 const QString KFapmanDir = "/root/.fapman";
29 const QString KLogFile = "/root/.fapman/lastlog.txt";
30 const QString KBlacklistFile = "/root/.fapman/black.list";
31 const QString KOwnRepoFile = "/root/.fapman/repos.list";
32 const QString KOwnRepoNamesFile = "/root/.fapman/repos.names";
33
34 #ifdef Q_WS_MAEMO_5
35         const QString KHamRepoListFile = "/etc/apt/sources.list.d/hildon-application-manager.list";
36 #else
37         const QString KHamRepoListFile = "/etc/apt/sources.list";
38 #endif
39
40 const QString KLastUpdateFile = "/opt/fapman-cache/lastupdate";
41 const QString KDateCacheFile = "/opt/fapman-cache/dates.cache";
42 const QString KAptSourceList = "/opt/fapman-cache/sources.list";
43 const QString KAptListDir = "/opt/fapman-cache/lists";
44 const QString KAptArchivePartialDir = "/var/cache/apt/archives/partial";
45 const QString KAptPreferencesFile = "/etc/apt/preferences";
46
47 const QString KDpkgStatusFile = "/var/lib/dpkg/status";
48 const QString KDpkgInfoDir = "/var/lib/dpkg/info";
49
50 const int KListExpireTime = 3600;
51
52 const int KDataReadBufferSize = 100000;
53
54 class MainWindow;
55 class FileReadThread;
56 class DateFetchThread;
57 class dimmer;
58 class Package;
59 class Repository;
60 class Settings;
61
62
63 class AAptInterface : public QObject
64 {
65         Q_OBJECT
66
67 public:
68         enum interfaceMode { ModeNone, ModeReadPackages, ModeFetchDates, ModeAptGetUpdate,
69                                                  ModeAptGetSimulate, ModeAptGetInstall, ModeAptGetClean };
70         enum multiLine { MultiLineNone, MultiLineDesc, MultiLineIcon, MultiLineUpgradeDesc };
71
72         explicit AAptInterface(QObject* parent);
73         virtual ~AAptInterface();
74         void setSettings(Settings* s_) { iSettings = s_; }
75
76         void addQueuedOperation(interfaceMode mode_);
77         bool run(dimmer* uiDimmer);
78         void setProcessPackages(QStringList pkgs) { iProcessPackages=pkgs; iProcessPackagesOrig=pkgs; }
79         QStringList processPackages() const { return iProcessPackages; }
80         QStringList processPackageVersions() const { return iProcessPackageVersions; }
81
82         bool running() const;
83         bool cancel();
84
85         int numSelectedPackages() const { return iNumSelectedPackages; }
86         void setNumSelectedPackages(int p_) { iNumSelectedPackages=p_; }
87
88         const QHash<QString, Package*>* packagesAvailable() { return &iPackagesAvailable; }
89         const QHash<QString, Package*>* packagesInstalled() { return &iPackagesInstalled; }
90         QList<Repository*>* repositories() { return  &iRepositories; }
91
92         QByteArray readLogFile();
93         void readRepositoryInfo();
94         bool writeRepositories();
95         bool needRepoRefresh();
96         void removeFromBlacklist(Package* pkg, BlacklistSelect::blackList oldstate);
97         void writeBlacklist();
98
99         bool dateCacheExists();
100
101         void setNeedRefresh(int repos, int lists, int dpkg, int dates);
102         void setSkipListAndDates() { iSkipRefreshListAndDates=true; }
103         bool needListOrDateRefresh();
104
105         bool loadInstallFiles(QStringList files_);
106
107         QDateTime lastListUpdate() { return iLastListUpdate; }
108         QDateTime lastDpkgUpdate() { return iLastDpkgUpdate; }
109
110 private:
111         AAptInterface(const AAptInterface& old);
112         AAptInterface operator= (const AAptInterface& old);
113
114         void runNext();
115         void cleanAfterRunEach();
116         void cleanAfterRunAll();
117         void cleanAfterError();
118
119         bool startAptGetUpdate();
120         bool startAptGetSimulate();
121         bool startAptGetInstall();
122         bool startAptGetClean();
123
124         QString setQProcessErrorMessage(QProcess::ProcessError error);
125         QString finishProcessCommonErrorMessages(QByteArray& output);
126
127         void communicateStatusToUi(bool success, QString title="", QString msg="");
128         void logToFile( QString data, bool logtime=true );
129         void logToFile( QByteArray data, bool logtime=true );
130
131         void startPkgListRead();
132         void startFetchDates();
133
134         Package* ReadNextPackage(QFile& f, quint64& currentreaddata);
135         bool processPackageDataLine(Package*& pkg, QByteArray& line);
136
137         void readBlacklist();
138         void writeDateCache();
139         void readDateCache();
140         void readPinnedPackages();
141
142 private slots:
143         void errorAptGetUpdate(QProcess::ProcessError error);
144         void errorAptGetSimulate(QProcess::ProcessError error);
145         void errorAptGetInstall(QProcess::ProcessError error);
146         void errorAptGetClean(QProcess::ProcessError error);
147
148         void finishedAptGetUpdate(int exitCode, QProcess::ExitStatus exitStatus);
149         void finishedAptGetSimulate(int exitCode, QProcess::ExitStatus exitStatus);
150         void finishedAptGetInstall(int exitCode, QProcess::ExitStatus exitStatus);
151         void finishedAptGetClean(int exitCode, QProcess::ExitStatus exitStatus);
152
153         void uiUpdaterAptGetUpdate();
154         void uiUpdaterAptGetInstall();
155         void progressCheckTimerCallback();
156
157         void dateFetchNetworkReply(QNetworkReply* reply);
158
159 private:
160         QHash<QString, Package*> iPackagesAvailable;
161         QHash<QString, Package*> iPackagesInstalled;
162         int iNumSelectedPackages;
163
164         QStringList iProcessPackages;
165         QStringList iProcessPackagesOrig;
166         QStringList iProcessPackageVersions;
167         QList<interfaceMode> iOperationsQueue;
168         QList<Repository*> iRepositories;
169         QStringList iQueueMessages;
170
171         MainWindow* iMainWindow;
172         Settings* iSettings;
173         interfaceMode iMode;
174         QList<interfaceMode> iModeLog;
175         dimmer* iUiDimmer;
176         bool iCanCancel;
177         bool iTerminated;
178         bool iErrorDone;
179         bool iNeedRepoRefresh;
180         bool iNeedListRefresh;
181         bool iNeedDpkgRefresh;
182         bool iNeedDateRefresh;
183         bool iSkipRefreshListAndDates;
184         QDateTime iLastListUpdate;
185         QDateTime iLastDpkgUpdate;
186
187         QProcess* iProcAptGetUpdate;
188         QProcess* iProcAptGetSimulate;
189         QProcess* iProcAptGetInstall;
190         QProcess* iProcAptGetClean;
191         QByteArray iProcAptGetUpdateOutput;
192         QByteArray iProcAptGetInstallOutput;
193
194         int iAptGetDownloadCount;
195         int iAptGetInstallCount;
196         int iAptGetRemoveCount;
197         int iAptGetInstallTotal;
198         int iAptGetRemoveTotal;
199         qint64 iAptGetCurrentFileDownloadSize;
200         qint64 iAptGetCurrentFileTotalSize;
201         QString iAptGetCurrentDownloadFileName;
202         QTimer* iProgressCheckTimer;
203         int iSpeedKbps;
204         int iSpeedKbpsPrev;
205         bool iUpdateSpeed;
206
207         int iCatalogCounter;
208         int iCatalogsTotal;
209
210         QStringList iBlacklist;
211
212         multiLine iMultiLine;
213         char* iDataReadBuffer;
214
215         int iDateRequestsWaiting;
216         int iDateRequestsSent;
217         int iDateRequestsReceived;
218         int iDateRequestErrors;
219         QNetworkReply::NetworkError iNetworkError;
220 };
221
222
223 #endif // AAPTINTERFACE_H