~ubuntu-branches/ubuntu/vivid/muon/vivid-proposed

« back to all changes in this revision

Viewing changes to discover/OriginsBackend.cpp

Tags: upstream-1.3.65
Import upstream version 1.3.65

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright © 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com>       *
 
3
 *                                                                         *
 
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.                    *
 
11
 *                                                                         *
 
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.                          *
 
16
 *                                                                         *
 
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
 ***************************************************************************/
 
20
 
 
21
#include "OriginsBackend.h"
 
22
#include "BackendsSingleton.h"
 
23
#include "MuonInstallerMainWindow.h"
 
24
#include <ApplicationBackend.h>
 
25
#include <QProcess>
 
26
#include <QDebug>
 
27
#include <QDir>
 
28
#include <LibQApt/Backend>
 
29
#include <LibQApt/Config>
 
30
#include <KMessageBox>
 
31
#include <KLocalizedString>
 
32
 
 
33
OriginsBackend::OriginsBackend(QObject* parent)
 
34
    : QObject(parent)
 
35
{
 
36
    QMetaObject::invokeMethod(this, "load", Qt::QueuedConnection);
 
37
}
 
38
 
 
39
OriginsBackend::~OriginsBackend()
 
40
{
 
41
    qDeleteAll(m_sources);
 
42
}
 
43
 
 
44
void OriginsBackend::load()
 
45
{
 
46
    qDeleteAll(m_sources);
 
47
    m_sources.clear();
 
48
    //load /etc/apt/sources.list
 
49
    load(BackendsSingleton::self()->backend()->config()->findFile("Dir::Etc::sourcelist"));
 
50
    
 
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));
 
55
    }
 
56
}
 
57
 
 
58
void OriginsBackend::load(const QString& file)
 
59
{
 
60
    Q_ASSERT(QFile::exists(file));
 
61
    QFile f(file);
 
62
    
 
63
    if(!f.open(QFile::Text|QFile::ReadOnly))
 
64
        return;
 
65
    
 
66
    //skip comments and empty lines
 
67
    QRegExp rxComment("(\\s*#.)|(\\s+$)");
 
68
    QRegExp rxArchitecture("\\[(.+)\\] ");
 
69
    while(!f.atEnd()) {
 
70
        QByteArray line = f.readLine();
 
71
        int comment = rxComment.indexIn(line);
 
72
        if(comment>=0)
 
73
            line = line.left(comment);
 
74
        
 
75
        if(!line.isEmpty()) {
 
76
            QString architecture;
 
77
            int arch = rxArchitecture.indexIn(line);
 
78
            if(arch>=0) {
 
79
                architecture = rxArchitecture.cap(1);
 
80
                line.remove(arch, rxArchitecture.matchedLength());
 
81
            }
 
82
            
 
83
            QList<QByteArray> source = line.split(' ');
 
84
            if(source.count() < 3) {
 
85
                return;
 
86
            }
 
87
            QByteArray uri = source[1];
 
88
            Source* newSource = sourceForUri(source[1]);
 
89
            Entry* entry = new Entry(newSource);
 
90
            
 
91
            entry->setArch(architecture);
 
92
            entry->setSource(source.first().endsWith("deb-src"));
 
93
            entry->setSuite(source[2]);
 
94
            
 
95
            QStringList args;
 
96
            foreach(const QByteArray& arg, source.mid(3)) {
 
97
                args += arg;
 
98
            }
 
99
            newSource->addEntry(entry);
 
100
        }
 
101
    }
 
102
    
 
103
    emit originsChanged();
 
104
}
 
105
 
 
106
Source* OriginsBackend::sourceForUri(const QString& uri)
 
107
{
 
108
    foreach(Source* s, m_sources) {
 
109
        if(s->uri()==uri)
 
110
            return s;
 
111
    }
 
112
    Source* s = new Source(this);
 
113
    s->setUri(uri);
 
114
    m_sources += s;
 
115
    return s;
 
116
}
 
117
 
 
118
void OriginsBackend::addRepository(const QString& repository)
 
119
{
 
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);
 
125
}
 
126
 
 
127
void OriginsBackend::removeRepository(const QString& repository)
 
128
{
 
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);
 
134
}
 
135
 
 
136
void OriginsBackend::additionDone(int processErrorCode)
 
137
{
 
138
    if(processErrorCode==0) {
 
139
        BackendsSingleton::self()->applicationBackend(), SLOT(reload());
 
140
        load();
 
141
    } else {
 
142
        QProcess* p = qobject_cast<QProcess*>(sender());
 
143
        Q_ASSERT(p);
 
144
        QByteArray errorMessage = p->readAllStandardOutput();
 
145
        if(errorMessage.isEmpty())
 
146
            KMessageBox::error(BackendsSingleton::self()->mainWindow(), errorMessage, i18n("Adding Origins..."));
 
147
    }
 
148
}
 
149
 
 
150
void OriginsBackend::removalDone(int processErrorCode)
 
151
{
 
152
    if(processErrorCode==0) {
 
153
        BackendsSingleton::self()->applicationBackend(), SLOT(reload());
 
154
        load();
 
155
    } else {
 
156
        QProcess* p = qobject_cast<QProcess*>(sender());
 
157
        Q_ASSERT(p);
 
158
        QByteArray errorMessage = p->readAllStandardOutput();
 
159
        if(errorMessage.isEmpty())
 
160
            KMessageBox::error(BackendsSingleton::self()->mainWindow(), errorMessage, i18n("Removing Origins..."));
 
161
    }
 
162
}
 
163
 
 
164
QVariantList OriginsBackend::sourcesVariant() const
 
165
{
 
166
    QVariantList ret;
 
167
    foreach(QObject* source, m_sources) {
 
168
        ret += qVariantFromValue<QObject*>(source);
 
169
    }
 
170
    return ret;
 
171
}
 
172
 
 
173
QDeclarativeListProperty<Entry> Source::entries()
 
174
{
 
175
    return QDeclarativeListProperty<Entry>(this, m_entries);
 
176
}
 
177
 
 
178
#include "moc_OriginsBackend.cpp"