~ubuntu-branches/ubuntu/wily/kscreen/wily

« back to all changes in this revision

Viewing changes to kded/serializer.cpp

  • Committer: Package Import Robot
  • Author(s): Scarlett Clark, Scarlett Clark, Jonathan Riddell, Harald Sitter
  • Date: 2014-08-20 08:35:15 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20140820083515-i9lk9nyt0adwd2q5
Tags: 2.0.0~git20141114-0ubuntu1
[ Scarlett Clark ]
* Update packaging for frameworks branch
* Git snapshot of the frameworks branch

[ Jonathan Riddell ]
* Remove kscreen-console.1 manpage which is out of date

[ Harald Sitter ]
* switch to new pkg-kde-tools

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include <QtCore/QVariant>
25
25
#include <QtCore/QVariantList>
26
26
#include <QtCore/QVariantMap>
27
 
 
28
 
#include <qjson/serializer.h>
29
 
#include <qjson/parser.h>
30
 
 
31
 
#include <kdebug.h>
 
27
#include <QtCore/QStandardPaths>
 
28
#include <QJsonDocument>
 
29
#include <QDir>
32
30
 
33
31
#include <kscreen/config.h>
34
32
#include <kscreen/output.h>
35
33
#include <kscreen/edid.h>
36
 
#include <KStandardDirs>
37
34
 
38
35
QString Serializer::currentId()
39
36
{
45
42
            continue;
46
43
        }
47
44
 
48
 
        kDebug() << "Part of the Id: " << Serializer::outputId(output);
 
45
        qDebug() << "Part of the Id: " << Serializer::outputId(output);
49
46
        hashList.insert(0, Serializer::outputId(output));
50
47
    }
51
48
 
52
49
    qSort(hashList.begin(), hashList.end());
53
50
 
54
51
    QCryptographicHash hash(QCryptographicHash::Md5);
55
 
    hash.addData(hashList.join(QString()).toAscii());
 
52
    hash.addData(hashList.join(QString()).toLatin1());
56
53
    return hash.result().toHex();
57
54
}
58
55
 
63
60
 
64
61
bool Serializer::configExists(const QString& id)
65
62
{
66
 
    QString path = KStandardDirs::locateLocal("data", "kscreen/"+id);
 
63
    QString path(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/kscreen/") + id);
67
64
    return QFile::exists(path);
68
65
}
69
66
 
70
67
KScreen::Config* Serializer::config(const QString& id)
71
68
{
72
 
    QJson::Parser parser;
73
69
    KScreen::Config* config = KScreen::Config::current();
74
70
    if (!config) {
75
71
        return 0;
76
72
    }
77
73
 
 
74
    QFile file(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/kscreen/") + id);
 
75
    if (!file.open(QIODevice::ReadOnly))
 
76
        return 0;
 
77
 
78
78
    KScreen::OutputList outputList = config->outputs();
79
 
    QFile file(KStandardDirs::locateLocal("data", "kscreen/"+id));
80
 
    file.open(QIODevice::ReadOnly);
81
 
 
82
 
    QVariantList outputs = parser.parse(file.readAll()).toList();
 
79
    QJsonDocument parser;
 
80
    QVariantList outputs = parser.fromJson(file.readAll()).toVariant().toList();
83
81
    Q_FOREACH(KScreen::Output* output, outputList) {
84
82
        if (!output->isConnected() && output->isEnabled()) {
85
83
            output->setEnabled(false);
125
123
        if (output->isEnabled()) {
126
124
            KScreen::Mode *mode = output->currentMode();
127
125
            if (!mode) {
128
 
                kWarning() << "CurrentMode is null" << output->name();
 
126
                qWarning() << "CurrentMode is null" << output->name();
129
127
                return false;
130
128
            }
131
129
 
145
143
        outputList.append(info);
146
144
    }
147
145
 
148
 
    QJson::Serializer serializer;
149
 
    QByteArray json = serializer.serialize(outputList);
150
 
 
151
 
    QString path = KStandardDirs::locateLocal("data", "kscreen/"+ Serializer::currentId());
152
 
    QFile file(path);
153
 
    file.open(QIODevice::WriteOnly);
154
 
    file.write(json);
155
 
    file.close();
156
 
 
157
 
    kDebug() << "Config saved on: " << path;
 
146
    QString directory = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/kscreen/");
 
147
    bool b = QDir().mkpath(directory);
 
148
    Q_ASSERT(b);
 
149
    QString filePath = directory + Serializer::currentId();
 
150
    QFile file(filePath);
 
151
    b = file.open(QIODevice::WriteOnly);
 
152
    Q_ASSERT(b);
 
153
    file.write(QJsonDocument::fromVariant(outputList).toJson());
 
154
    qDebug() << "Config saved on: " << filePath;
 
155
 
158
156
    return true;
159
157
}
160
158
 
185
183
        QVariantMap modeSize = modeInfo["size"].toMap();
186
184
        QSize size(modeSize["width"].toInt(), modeSize["height"].toInt());
187
185
 
188
 
        kDebug() << "Finding a mode with: ";
189
 
        kDebug() << size;
190
 
        kDebug() << modeInfo["refresh"].toString();
 
186
        qDebug() << "Finding a mode with: ";
 
187
        qDebug() << size;
 
188
        qDebug() << modeInfo["refresh"].toString();
191
189
 
192
190
        KScreen::ModeList modes = output->modes();
193
191
        Q_FOREACH(KScreen::Mode* mode, modes) {
198
196
                continue;
199
197
            }
200
198
 
201
 
            kDebug() << "Found: " << mode->id() << " " << mode->name();
 
199
            qDebug() << "Found: " << mode->id() << " " << mode->name();
202
200
            output->setCurrentModeId(mode->id());
203
201
            break;
204
202
        }
220
218
QVariantMap Serializer::metadata(const KScreen::Output* output)
221
219
{
222
220
    QVariantMap metadata;
223
 
    metadata["name"] = output->name();
 
221
    metadata[QStringLiteral("name")] = output->name();
224
222
    if (!output->edid() || !output->edid()->isValid()) {
225
223
        return metadata;
226
224
    }
227
225
 
228
 
    metadata["fullname"] = output->edid()->deviceId();
 
226
    metadata[QStringLiteral("fullname")] = output->edid()->deviceId();
229
227
    return metadata;
230
228
}