ssud.cpp 4.77 KB
Newer Older
1 2 3 4 5 6 7 8
/**
 * @file ssud.cpp
 * @copyright 2013 Jolla Ltd.
 * @author Bernd Wachter <bwachter@lart.info>
 * @date 2013
 */

#include "ssud.h"
9
#include "ssu_adaptor.h"
10 11

#include "libssu/ssudeviceinfo.h"
12
#include "libssu/ssurepomanager.h"
13 14 15 16 17 18

#include <QDBusConnection>

const char *Ssud::SERVICE_NAME = "org.nemo.ssu";
const char *Ssud::OBJECT_PATH = "/org/nemo/ssu";

Pekka Vuorela's avatar
Pekka Vuorela committed
19 20
Ssud::Ssud(QObject *parent)
    : QObject(parent)
21
{
22 23
    qDBusRegisterMetaType<SsuRepo>();
    qDBusRegisterMetaType<QList<SsuRepo>>();
24 25 26 27
    QDBusConnection connection = QDBusConnection::systemBus();
    if (!connection.registerObject(OBJECT_PATH, this)) {
        qFatal("Cannot register object at %s", OBJECT_PATH);
    }
28

29 30 31
    if (!connection.registerService(SERVICE_NAME)) {
        qFatal("Cannot register D-Bus service at %s", SERVICE_NAME);
    }
32

33 34
    // prepare for controlled suicide on boredom
    const int AUTOCLOSE_TIMEOUT_MS = 180 * 1000;
35

36 37
    autoclose.setSingleShot(true);
    autoclose.setInterval(AUTOCLOSE_TIMEOUT_MS);
38

39 40
    connect(&autoclose, SIGNAL(timeout()),
            this, SLOT(quit()));
41

42
    new SsuAdaptor(this);
43

44 45 46 47 48 49
    connect(&ssu, SIGNAL(done()),
            this, SIGNAL(done()));
    connect(&ssu, SIGNAL(credentialsChanged()),
            this, SIGNAL(credentialsChanged()));
    connect(&ssu, SIGNAL(registrationStatusChanged()),
            this, SIGNAL(registrationStatusChanged()));
50

51 52
    // a cry for help everytime we do something to prevent suicide
    autoclose.start();
53 54
}

55 56
Ssud::~Ssud()
{
57 58
}

59 60 61
QString Ssud::deviceModel()
{
    SsuDeviceInfo deviceInfo;
62

63 64
    autoclose.start();
    return deviceInfo.deviceModel();
65 66
}

67 68 69
QString Ssud::deviceFamily()
{
    SsuDeviceInfo deviceInfo;
70

71 72
    autoclose.start();
    return deviceInfo.deviceFamily();
73
}
74

75 76 77
QString Ssud::deviceUid()
{
    SsuDeviceInfo deviceInfo;
78

79 80
    autoclose.start();
    return deviceInfo.deviceUid();
81 82
}

83 84 85
QString Ssud::deviceVariant()
{
    SsuDeviceInfo deviceInfo;
86

87 88
    autoclose.start();
    return deviceInfo.deviceVariant();
89
}
90

91 92 93
QString Ssud::displayName(int type)
{
    SsuDeviceInfo deviceInfo;
94

95 96
    autoclose.start();
    return deviceInfo.displayName(type);
97 98
}

99 100 101 102
bool Ssud::error()
{
    autoclose.start();
    return ssu.error();
103 104
}

105 106 107 108
QString Ssud::lastError()
{
    autoclose.start();
    return ssu.lastError();
109 110
}

111 112 113
void Ssud::quit()
{
    QCoreApplication::quit();
114 115
}

116 117 118 119
bool Ssud::isRegistered()
{
    autoclose.start();
    return ssu.isRegistered();
120 121
}

122 123 124 125 126
void Ssud::registerDevice(const QString &username, const QString &password)
{
    autoclose.stop();
    ssu.sendRegistration(username, password);
    autoclose.start();
127 128
}

129 130 131 132
void Ssud::unregisterDevice()
{
    autoclose.start();
    ssu.unregister();
133
};
134

135 136
QString Ssud::domain()
{
137 138 139
    autoclose.start();
    return ssu.domain();
}
140

141 142
// called by DBus Adaptor, return integer instead of enum Ssu::DeviceModeFlags
int Ssud::deviceMode()
143 144 145
{
    autoclose.start();
    return ssu.deviceMode();
146 147
}

148 149 150
void Ssud::setDeviceMode(int mode)
{
    setDeviceMode(mode, Ssu::Replace);
151 152
}

153 154 155 156 157
void Ssud::setDeviceMode(int mode, int editMode)
{
    ssu.setDeviceMode(
        Ssu::DeviceModeFlags(mode),
        Ssu::EditMode(editMode)
158
    );
159

160 161 162
    SsuRepoManager repoManager;
    repoManager.update();
    autoclose.start();
163 164
}

165 166 167 168
QString Ssud::flavour()
{
    autoclose.start();
    return ssu.flavour();
169 170
}

171 172 173
void Ssud::setFlavour(const QString &flavour)
{
    ssu.setFlavour(flavour);
174

175 176 177
    SsuRepoManager repoManager;
    repoManager.update();
    autoclose.start();
178 179 180
}


181 182 183 184
QString Ssud::release(bool rnd)
{
    autoclose.start();
    return ssu.release(rnd);
185 186
}

187 188 189
void Ssud::setRelease(const QString &release, bool rnd)
{
    ssu.setRelease(release, rnd);
190

191 192 193
    SsuRepoManager repoManager;
    repoManager.update();
    autoclose.start();
194 195
}

196 197 198
void Ssud::modifyRepo(int action, const QString &repo)
{
    SsuRepoManager repoManager;
199

200
    autoclose.stop();
201

202
    switch (action) {
203
    case Add:
204 205
        repoManager.add(repo);
        break;
206
    case Remove:
207 208
        repoManager.remove(repo);
        break;
209
    case Disable:
210 211
        repoManager.disable(repo);
        break;
212
    case Enable:
213 214 215
        repoManager.enable(repo);
        break;
    }
216

217 218
    repoManager.update();
    autoclose.start();
219 220
}

221 222 223 224 225 226
void Ssud::addRepo(const QString &repo, const QString &url)
{
    SsuRepoManager repoManager;
    repoManager.add(repo, url);
    repoManager.update();
    autoclose.start();
227 228
}

229 230 231 232 233 234
void Ssud::updateRepos()
{
    SsuRepoManager repoManager;
    autoclose.stop();
    repoManager.update();
    autoclose.start();
235
}
236

237
QList<SsuRepo> Ssud::listRepos(bool rnd)
238
{
239
    QList<SsuRepo> reposList;
240 241 242 243
    SsuRepoManager repoManager;

    for (const QString &repo : repoManager.repos(rnd)) {
        const QString repoUrl = ssu.repoUrl(repo, rnd);
244 245 246 247 248 249

        SsuRepo ssuRepo;
        ssuRepo.name = repo;
        ssuRepo.url = repoUrl;

        reposList.append(ssuRepo);
250 251 252 253
    }
    autoclose.start();
    return reposList;
}