1
/***************************************************************************
2
* Copyright © 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com> *
4
* This program is free software; you can redistribute it and/or *
5
* modify it under the terms of the GNU General Public License as *
6
* published by the Free Software Foundation; either version 2 of *
7
* the License or (at your option) version 3 or any later version *
8
* accepted by the membership of KDE e.V. (or its successor approved *
9
* by the membership of KDE e.V.), which shall act as a proxy *
10
* defined in Section 14 of version 3 of the license. *
12
* This program is distributed in the hope that it will be useful, *
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15
* GNU General Public License for more details. *
17
* You should have received a copy of the GNU General Public License *
18
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
19
***************************************************************************/
21
#include "OriginsBackend.h"
22
#include "BackendsSingleton.h"
23
#include "MuonInstallerMainWindow.h"
24
#include <ApplicationBackend.h>
28
#include <LibQApt/Backend>
29
#include <LibQApt/Config>
30
#include <KMessageBox>
31
#include <KLocalizedString>
33
OriginsBackend::OriginsBackend(QObject* parent)
36
QMetaObject::invokeMethod(this, "load", Qt::QueuedConnection);
39
OriginsBackend::~OriginsBackend()
41
qDeleteAll(m_sources);
44
void OriginsBackend::load()
46
qDeleteAll(m_sources);
48
//load /etc/apt/sources.list
49
load(BackendsSingleton::self()->backend()->config()->findFile("Dir::Etc::sourcelist"));
51
//load /etc/apt/sources.list.d/*.list
52
QDir d(BackendsSingleton::self()->backend()->config()->findDirectory("Dir::Etc::sourceparts"));
53
foreach(const QString& file, d.entryList(QStringList() << "*.list")) {
54
load(d.filePath(file));
58
void OriginsBackend::load(const QString& file)
60
Q_ASSERT(QFile::exists(file));
63
if(!f.open(QFile::Text|QFile::ReadOnly))
66
//skip comments and empty lines
67
QRegExp rxComment("(\\s*#.)|(\\s+$)");
68
QRegExp rxArchitecture("\\[(.+)\\] ");
70
QByteArray line = f.readLine();
71
int comment = rxComment.indexIn(line);
73
line = line.left(comment);
77
int arch = rxArchitecture.indexIn(line);
79
architecture = rxArchitecture.cap(1);
80
line.remove(arch, rxArchitecture.matchedLength());
83
QList<QByteArray> source = line.split(' ');
84
if(source.count() < 3) {
87
QByteArray uri = source[1];
88
Source* newSource = sourceForUri(source[1]);
89
Entry* entry = new Entry(newSource);
91
entry->setArch(architecture);
92
entry->setSource(source.first().endsWith("deb-src"));
93
entry->setSuite(source[2]);
96
foreach(const QByteArray& arg, source.mid(3)) {
99
newSource->addEntry(entry);
103
emit originsChanged();
106
Source* OriginsBackend::sourceForUri(const QString& uri)
108
foreach(Source* s, m_sources) {
112
Source* s = new Source(this);
118
void OriginsBackend::addRepository(const QString& repository)
120
QProcess* p = new QProcess(this);
121
p->setProcessChannelMode(QProcess::MergedChannels);
122
connect(p, SIGNAL(finished(int)), SLOT(additionDone(int)));
123
connect(p, SIGNAL(finished(int)), p, SLOT(deleteLater()));
124
p->start("kdesudo", QStringList("--") << "apt-add-repository" << "-y" << repository);
127
void OriginsBackend::removeRepository(const QString& repository)
129
QProcess* p = new QProcess(this);
130
p->setProcessChannelMode(QProcess::MergedChannels);
131
connect(p, SIGNAL(finished(int)), SLOT(removalDone(int)));
132
connect(p, SIGNAL(finished(int)), p, SLOT(deleteLater()));
133
p->start("kdesudo", QStringList("--") << "apt-add-repository" << "--remove" << "-y" << repository);
136
void OriginsBackend::additionDone(int processErrorCode)
138
if(processErrorCode==0) {
139
BackendsSingleton::self()->applicationBackend(), SLOT(reload());
142
QProcess* p = qobject_cast<QProcess*>(sender());
144
QByteArray errorMessage = p->readAllStandardOutput();
145
if(errorMessage.isEmpty())
146
KMessageBox::error(BackendsSingleton::self()->mainWindow(), errorMessage, i18n("Adding Origins..."));
150
void OriginsBackend::removalDone(int processErrorCode)
152
if(processErrorCode==0) {
153
BackendsSingleton::self()->applicationBackend(), SLOT(reload());
156
QProcess* p = qobject_cast<QProcess*>(sender());
158
QByteArray errorMessage = p->readAllStandardOutput();
159
if(errorMessage.isEmpty())
160
KMessageBox::error(BackendsSingleton::self()->mainWindow(), errorMessage, i18n("Removing Origins..."));
164
QVariantList OriginsBackend::sourcesVariant() const
167
foreach(QObject* source, m_sources) {
168
ret += qVariantFromValue<QObject*>(source);
173
QDeclarativeListProperty<Entry> Source::entries()
175
return QDeclarativeListProperty<Entry>(this, m_entries);
178
#include "moc_OriginsBackend.cpp"