warn about system upgrade, minor fixes, 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         AAptInterface(QObject* parent);
69         ~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() { return iProcessPackages; }
76         QStringList processPackageVersions() { return iProcessPackageVersions; }
77
78         bool running();
79         bool cancel();
80
81         int numSelectedPackages() { 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         void runNext();
108         void cleanAfterRunEach();
109         void cleanAfterRunAll();
110         void cleanAfterError();
111
112         bool startAptGetUpdate();
113         bool startAptGetSimulate();
114         bool startAptGetInstall();
115         bool startAptGetClean();
116
117         QString setQProcessErrorMessage(QProcess::ProcessError error);
118         QString finishProcessCommonErrorMessages(QByteArray& output);
119
120         void communicateStatusToUi(bool success, QString title="", QString msg="");
121         void logToFile( QString data, bool logtime=true );
122         void logToFile( QByteArray data, bool logtime=true );
123
124         void startPkgListRead();
125         void startFetchDates();
126
127         Package* ReadNextPackage(QFile& f, quint64& currentreaddata);
128         bool processPackageDataLine(Package*& pkg, QByteArray& line);
129
130         void readBlacklist();
131         void writeDateCache();
132         void readDateCache();
133
134 private slots:
135         void errorAptGetUpdate(QProcess::ProcessError error);
136         void errorAptGetSimulate(QProcess::ProcessError error);
137         void errorAptGetInstall(QProcess::ProcessError error);
138         void errorAptGetClean(QProcess::ProcessError error);
139
140         void finishedAptGetUpdate(int exitCode, QProcess::ExitStatus exitStatus);
141         void finishedAptGetSimulate(int exitCode, QProcess::ExitStatus exitStatus);
142         void finishedAptGetInstall(int exitCode, QProcess::ExitStatus exitStatus);
143         void finishedAptGetClean(int exitCode, QProcess::ExitStatus exitStatus);
144
145         void uiUpdaterAptGetUpdate();
146         void uiUpdaterAptGetInstall();
147         void progressCheckTimerCallback();
148
149         void dateFetchNetworkReply(QNetworkReply* reply);
150
151 private:
152         QHash<QString, Package*> iPackagesAvailable;
153         QHash<QString, Package*> iPackagesInstalled;
154         int iNumSelectedPackages;
155
156         QStringList iProcessPackages;
157         QStringList iProcessPackagesOrig;
158         QStringList iProcessPackageVersions;
159         QList<interfaceMode> iOperationsQueue;
160         QList<Repository*> iRepositories;
161         QStringList iQueueMessages;
162
163         MainWindow* iMainWindow;
164         Settings* iSettings;
165         interfaceMode iMode;
166         QList<interfaceMode> iModeLog;
167         dimmer* iUiDimmer;
168         bool iCanCancel;
169         bool iTerminated;
170         bool iErrorDone;
171         bool iNeedRepoRefresh;
172         bool iNeedListRefresh;
173         bool iNeedDpkgRefresh;
174         bool iNeedDateRefresh;
175         bool iSkipRefreshListAndDates;
176         QDateTime iLastListUpdate;
177         QDateTime iLastDpkgUpdate;
178
179         QProcess* iProcAptGetUpdate;
180         QProcess* iProcAptGetSimulate;
181         QProcess* iProcAptGetInstall;
182         QProcess* iProcAptGetClean;
183         QByteArray iProcAptGetUpdateOutput;
184         QByteArray iProcAptGetInstallOutput;
185
186         int iAptGetDownloadCount;
187         int iAptGetInstallCount;
188         int iAptGetRemoveCount;
189         int iAptGetInstallTotal;
190         int iAptGetRemoveTotal;
191         qint64 iAptGetCurrentFileDownloadSize;
192         qint64 iAptGetCurrentFileTotalSize;
193         QString iAptGetCurrentDownloadFileName;
194         QTimer* iProgressCheckTimer;
195         int iSpeedKbps;
196         int iSpeedKbpsPrev;
197         bool iUpdateSpeed;
198
199         int iCatalogCounter;
200         int iCatalogsTotal;
201
202         QStringList iBlacklist;
203
204         multiLine iMultiLine;
205         char* iDataReadBuffer;
206
207         int iDateRequestsWaiting;
208         int iDateRequestsSent;
209         int iDateRequestsReceived;
210         int iDateRequestErrors;
211         QNetworkReply::NetworkError iNetworkError;
212 };
213
214
215 #endif // AAPTINTERFACE_H