code cleanup
[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 const QString KHamRepoListFile = "/etc/apt/sources.list.d/hildon-application-manager.list";
35
36 const QString KLastUpdateFile = "/opt/fapman-cache/lastupdate";
37 const QString KDateCacheFile = "/opt/fapman-cache/dates.cache";
38 const QString KAptSourceList = "/opt/fapman-cache/sources.list";
39 const QString KAptListDir = "/opt/fapman-cache/lists";
40 const QString KAptArchivePartialDir = "/var/cache/apt/archives/partial";
41 const QString KAptPreferencesFile = "/etc/apt/preferences";
42
43 const QString KDpkgStatusFile = "/var/lib/dpkg/status";
44 const QString KDpkgInfoDir = "/var/lib/dpkg/info";
45
46 const int KListExpireTime = 3600;
47
48 const int KDataReadBufferSize = 100000;
49
50 class MainWindow;
51 class FileReadThread;
52 class DateFetchThread;
53 class dimmer;
54 class Package;
55 class Repository;
56 class Settings;
57
58
59 class AAptInterface : public QObject
60 {
61         Q_OBJECT
62
63 public:
64         enum interfaceMode { ModeNone, ModeReadPackages, ModeFetchDates, ModeAptGetUpdate,
65                                                  ModeAptGetSimulate, ModeAptGetInstall, ModeAptGetClean };
66         enum multiLine { MultiLineNone, MultiLineDesc, MultiLineIcon, MultiLineUpgradeDesc };
67
68         explicit AAptInterface(QObject* parent);
69         virtual ~AAptInterface();
70         void setSettings(Settings* s_) { iSettings = s_; }
71
72         void addQueuedOperation(interfaceMode mode_);
73         bool run(dimmer* uiDimmer);
74         void setProcessPackages(QStringList pkgs) { iProcessPackages=pkgs; iProcessPackagesOrig=pkgs; }
75         QStringList processPackages() const { return iProcessPackages; }
76         QStringList processPackageVersions() const { return iProcessPackageVersions; }
77
78         bool running() const;
79         bool cancel();
80
81         int numSelectedPackages() const { return iNumSelectedPackages; }
82         void setNumSelectedPackages(int p_) { iNumSelectedPackages=p_; }
83
84         QHash<QString, Package*>* packagesAvailable() { return &iPackagesAvailable; }
85         QHash<QString, Package*>* packagesInstalled() { return &iPackagesInstalled; }
86         QList<Repository*>* repositories() { return  &iRepositories; }
87
88         QByteArray readLogFile();
89         void readRepositoryInfo();
90         bool writeRepositories();
91         bool needRepoRefresh();
92         void removeFromBlacklist(Package* pkg, BlacklistSelect::blackList oldstate);
93         void writeBlacklist();
94
95         bool dateCacheExists();
96
97         void setNeedRefresh(int repos, int lists, int dpkg, int dates);
98         void setSkipListAndDates() { iSkipRefreshListAndDates=true; }
99         bool needListOrDateRefresh();
100
101         bool loadInstallFiles(QStringList files_);
102
103         QDateTime lastListUpdate() { return iLastListUpdate; }
104         QDateTime lastDpkgUpdate() { return iLastDpkgUpdate; }
105
106 private:
107         AAptInterface(const AAptInterface& old);
108         AAptInterface operator= (const AAptInterface& old);
109
110         void runNext();
111         void cleanAfterRunEach();
112         void cleanAfterRunAll();
113         void cleanAfterError();
114
115         bool startAptGetUpdate();
116         bool startAptGetSimulate();
117         bool startAptGetInstall();
118         bool startAptGetClean();
119
120         QString setQProcessErrorMessage(QProcess::ProcessError error);
121         QString finishProcessCommonErrorMessages(QByteArray& output);
122
123         void communicateStatusToUi(bool success, QString title="", QString msg="");
124         void logToFile( QString data, bool logtime=true );
125         void logToFile( QByteArray data, bool logtime=true );
126
127         void startPkgListRead();
128         void startFetchDates();
129
130         Package* ReadNextPackage(QFile& f, quint64& currentreaddata);
131         bool processPackageDataLine(Package*& pkg, QByteArray& line);
132
133         void readBlacklist();
134         void writeDateCache();
135         void readDateCache();
136         void readPinnedPackages();
137
138 private slots:
139         void errorAptGetUpdate(QProcess::ProcessError error);
140         void errorAptGetSimulate(QProcess::ProcessError error);
141         void errorAptGetInstall(QProcess::ProcessError error);
142         void errorAptGetClean(QProcess::ProcessError error);
143
144         void finishedAptGetUpdate(int exitCode, QProcess::ExitStatus exitStatus);
145         void finishedAptGetSimulate(int exitCode, QProcess::ExitStatus exitStatus);
146         void finishedAptGetInstall(int exitCode, QProcess::ExitStatus exitStatus);
147         void finishedAptGetClean(int exitCode, QProcess::ExitStatus exitStatus);
148
149         void uiUpdaterAptGetUpdate();
150         void uiUpdaterAptGetInstall();
151         void progressCheckTimerCallback();
152
153         void dateFetchNetworkReply(QNetworkReply* reply);
154
155 private:
156         QHash<QString, Package*> iPackagesAvailable;
157         QHash<QString, Package*> iPackagesInstalled;
158         int iNumSelectedPackages;
159
160         QStringList iProcessPackages;
161         QStringList iProcessPackagesOrig;
162         QStringList iProcessPackageVersions;
163         QList<interfaceMode> iOperationsQueue;
164         QList<Repository*> iRepositories;
165         QStringList iQueueMessages;
166
167         MainWindow* iMainWindow;
168         Settings* iSettings;
169         interfaceMode iMode;
170         QList<interfaceMode> iModeLog;
171         dimmer* iUiDimmer;
172         bool iCanCancel;
173         bool iTerminated;
174         bool iErrorDone;
175         bool iNeedRepoRefresh;
176         bool iNeedListRefresh;
177         bool iNeedDpkgRefresh;
178         bool iNeedDateRefresh;
179         bool iSkipRefreshListAndDates;
180         QDateTime iLastListUpdate;
181         QDateTime iLastDpkgUpdate;
182
183         QProcess* iProcAptGetUpdate;
184         QProcess* iProcAptGetSimulate;
185         QProcess* iProcAptGetInstall;
186         QProcess* iProcAptGetClean;
187         QByteArray iProcAptGetUpdateOutput;
188         QByteArray iProcAptGetInstallOutput;
189
190         int iAptGetDownloadCount;
191         int iAptGetInstallCount;
192         int iAptGetRemoveCount;
193         int iAptGetInstallTotal;
194         int iAptGetRemoveTotal;
195         qint64 iAptGetCurrentFileDownloadSize;
196         qint64 iAptGetCurrentFileTotalSize;
197         QString iAptGetCurrentDownloadFileName;
198         QTimer* iProgressCheckTimer;
199         int iSpeedKbps;
200         int iSpeedKbpsPrev;
201         bool iUpdateSpeed;
202
203         int iCatalogCounter;
204         int iCatalogsTotal;
205
206         QStringList iBlacklist;
207
208         multiLine iMultiLine;
209         char* iDataReadBuffer;
210
211         int iDateRequestsWaiting;
212         int iDateRequestsSent;
213         int iDateRequestsReceived;
214         int iDateRequestErrors;
215         QNetworkReply::NetworkError iNetworkError;
216 };
217
218
219 #endif // AAPTINTERFACE_H