-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappcore.cpp
More file actions
313 lines (250 loc) · 9.5 KB
/
appcore.cpp
File metadata and controls
313 lines (250 loc) · 9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "appcore.h"
#include "defines.h"
#include <QDebug>
#include <QApplication>
#include <QProcess>
AppCore::AppCore(QObject *parent)
:QObject(parent), _mainWindow(nullptr), _hasError(false), _autoQuit(false), _onlyShowInstall(false)
{
_packageSatSolver = new PackageSatSolver();
}
AppCore::~AppCore()
{
qDeleteAll(_instPacks);
delete _packageSatSolver;
if ( !!_mainWindow ) delete _mainWindow;
}
/**
* @brief Инициализируем всё необходимое
* @param mainCnfPath
* @return
*/
bool AppCore::init(QString mainCnfPath)
{
QFileInfo mainCnfInfo(mainCnfPath);
//-- Проверяем основной конфиг-файл
if ( !mainCnfInfo.exists() || !Updater::checkFileAccess(mainCnfPath) ) {
newStatus(tr("Configuration file %1 not exists or has no access. Break!").arg(mainCnfPath), -1);
return false;
}
_mainCnf = new QSettings(mainCnfPath, QSettings::IniFormat, this);
//-- Рабочей директорией будем считать расположение файла настроек и все относительные пути относительно него
QDir::setCurrent(mainCnfInfo.absoluteDir().path());
//-- Настраиваем лог
if ( !_mainCnf->value("logDir").toString().isEmpty() ) Logger::instance().setLogDir(_mainCnf->value("logDir").toString());
QString logDir = ( !_mainCnf->value("logDir").toString().isEmpty() )? _mainCnf->value("logDir").toString() : QApplication::applicationDirPath();
if ( !Updater::checkFolderAccess(logDir) ) {
newStatus(tr("No access to logs dir. Break!"), -1);
return false;
}
Logger::instance().setLogDir(logDir);
//-- Проверяем существование и возомжность записи директории временных файлов
if ( !Updater::checkFolderAccess(_mainCnf->value("tempDir").toString()) ) {
newStatus(tr("No access to tempDir. Break!"), -1);
return false;
}
newStatus(tr("Welcome to Updatus version %1 - update manager for our programs.").arg(VERSION), 1);
return true;
}
bool AppCore::upgrade(QString mainCnfPath)
{
if ( !init(mainCnfPath) ) return false;
newStatus(tr("Start upgrade from %1.").arg(mainCnfPath), 1);
_collectUpdtCnfManager = new DownloadManager(this);
_updater = new Updater(this, _mainCnf);
connect(_collectUpdtCnfManager, &DownloadManager::answerReady, this, &AppCore::onUpdtCnfDownloaded);
connect(_collectUpdtCnfManager, &DownloadManager::error, this, &AppCore::onUpdateCndDownloadError);
connect(_updater, &Updater::completed, this, &AppCore::onComplete);
connect(_updater, &Updater::error, this, &AppCore::onError);
connect(_updater, &Updater::progress, this, &AppCore::onProgress);
_mainCnf->beginReadArray("info");
QStringList infoKeys = _mainCnf->allKeys();
foreach(QString infoKey, infoKeys) {
_infoVariables[infoKey] = _mainCnf->value(infoKey);
}
_mainCnf->endArray();
int collectInstRes = collectInstalledPackadges();
if ( collectInstRes<0 ) {
newStatus(tr("Unable collect install packages. %1").arg(collectInstRes), -1);
return false;
}
int collectAvRes = collectAvaliableUpdates();
if ( collectAvRes<0 ) {
newStatus(tr("Unable collect avaliable packages. %1").arg(collectAvRes), -1);
return false;
}
return true;
}
/**
* @brief Собираем инфу о текущих установленных файлах
*/
int AppCore::collectInstalledPackadges()
{
newStatus(tr("Collect info about installed packadges..."), 1);
_mainCnf->beginReadArray("installed");
QStringList instPacksNames = _mainCnf->allKeys();
_mainCnf->endArray();
foreach(QString pName, instPacksNames) {
QString version = _mainCnf->value(QString("installed/%1").arg(pName)).toString();
Packadge * instPack = new Packadge(pName, version, *_mainCnf);
newStatus(tr("Packet installed %1").arg(instPack->fullName()), 1);
_instPacks.insert(instPack->fullName(), instPack);
}
return 1;
}
/**
* @brief Подключаемся к серверам, проверяем обновления для установленных пакетов
* @return
*/
int AppCore::collectAvaliableUpdates() //TODO: Сделать возможность указывать несколько серверов
{
QString server = _mainCnf->value("servers/main").toString();
newStatus(tr("Try collect updates cnf from server %1").arg(server), 1);
int downloadAttempts = _mainCnf->value("downloadAttempts", 3).toInt();
_collectUpdtCnfManager->request(QUrl(server+"repository.cnf"), downloadAttempts);
return 1;
}
/**
* @brief Нужен интерфейс, отображаем
*/
void AppCore::withGui()
{
_mainWindow = new MainWindow(nullptr, this);
_mainWindow->show();
}
/**
* @brief Отмечаем, нужно ли сразу выходить после завершения
* @param st
*/
void AppCore::autoQuit(bool st)
{
_autoQuit = st;
}
/**
* @brief Отмечаем, нужно ли только показать, какие будут установлены
* @param si
*/
void AppCore::onlyShowInstall(bool si)
{
_onlyShowInstall = si;
}
void AppCore::newStatus(QString msg, int mode)
{
switch (mode) {
case 1: qInfo()<<msg; break;
case -1: qWarning()<<msg; break;
default: qDebug()<<msg; break;
}
emit statusChanged(msg, mode);
}
/**
* @brief Запускаем указанную программу после проверки обновлений
* @param path
* @param arguments
* @param workingDir
*/
bool AppCore::runAfter(QString path, QStringList arguments, QString workingDir)
{
if ( path.isEmpty() ) return false;
qInfo()<<"Run after"<<path<<arguments.join(" ")<<workingDir;
bool st = QProcess::startDetached(path, arguments, workingDir);
return st;
}
/**
* @brief Выходим
* @param force - без сохранения, отправки лога, запуска прог и т.д.
*/
void AppCore::quit(bool force)
{
if ( !force && !_hasError ) {
Logger::instance().sendToServer(QUrl(_mainCnf->value("sendLogs").toString()), _infoVariables);
_mainCnf->sync();
runAfter(
_mainCnf->value("runAfter").toString(),
_mainCnf->value("runAfter-arguments").toString().split(";"),
_mainCnf->value("runAfter-workingDir").toString()
);
}
if ( (_autoQuit && !_hasError) || !_mainWindow ) QApplication::quit();
}
/**
* @brief Всё скачано и расположено по совим местам, можем завершаться
* @param newInstalled
*/
void AppCore::onComplete(bool newInstalled)
{
newStatus(tr("Complete!"), 1);
onProgress(100);
//-- Записываем дату и время последнего обновления, когда что-то было установлено
if ( newInstalled ) {
_mainCnf->setValue("lastUpdated", QDateTime::currentDateTime().toString("dd.MM.yy hh:mm"));
}
_infoVariables["status"] = "OK";
_infoVariables["hasNewInstalled"] = (newInstalled)? "yes" : "no";
quit();
}
void AppCore::onError()
{
if ( _hasError ) return; //-- Нам и одной уже достаточно
_hasError = true;
newStatus(tr("Errors occurred during the upgrade process. Execution aborted."), -1);
_infoVariables["status"] = "has errors";
quit();
}
/**
* @brief Загружен очередной файл информации о возможных обновлениях
* @param cnfFile
*/
void AppCore::onUpdtCnfDownloaded(QTemporaryFile *cnfFile)
{
newStatus(tr("Downloaded update cnf file!"), 1);
newStatus(tr("Parse cnf file"), 1);
QSettings cnfUpdates(cnfFile->fileName(), QSettings::IniFormat, this);
//-- Заносим для каждого установленного пакета возможные новые версии и их зависимости
foreach(Packadge * pack, _instPacks) {
int res = pack->parseUpdates(cnfUpdates, &_instCandidates);
if ( res<0 ) newStatus(tr("Parse cnf file problem %1").arg(pack->fullName()), -1);
}
cnfFile->close();
newStatus(tr("All cnf files downloaded and parsed"), 1);
QList<PackadgeCandidate*> toInstList;
int prepInst = _packageSatSolver->prepareInstList(_instPacks, toInstList);
if ( prepInst<0 ) {
newStatus(tr("Unable build versions three"), -1);
onError();
return;
}
if ( _onlyShowInstall ) { //-- Если нужно только показать
QDebug debug = qDebug();
debug.noquote();
debug<<"!!Will be installed:"<<endl;
if ( toInstList.count()>0 ) {
foreach(PackadgeCandidate * pc, toInstList) {
debug<<pc->name()<<pc->version()<<endl;
}
} else {
debug<<"!!Has no new versions."<<endl;
}
debug.quote();
newStatus(tr("Show only installed mode."), 1);
onProgress(100);
quit(true);
return;
}
if ( toInstList.count()==0 ) {
newStatus(tr("Has no updates..."), 1);
onComplete(false);
return;
}
newStatus(tr("Download packages..."), 1);
_updater->goInstall(toInstList);
}
void AppCore::onUpdateCndDownloadError(QString err)
{
newStatus(tr("Unable download update cnf %1.").arg(err), -1);
onError();
}
void AppCore::onProgress(int pr)
{
emit progress(pr);
}