Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge pull request #49 from nemomobile/mer1367
[ssu] Introduce SsuFeatureModel. Fixes MER#1367
- Loading branch information
Showing
24 changed files
with
448 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
TEMPLATE = lib | ||
TARGET = declarativessu | ||
TARGET = $$qtLibraryTarget($$TARGET) | ||
|
||
include(../libssu/libssu.pri) | ||
|
||
MODULENAME = Nemo/Ssu | ||
TARGETPATH = $$[QT_INSTALL_QML]/$$MODULENAME | ||
|
||
QT += qml | ||
QT -= gui | ||
|
||
CONFIG += plugin | ||
|
||
target.path = $$TARGETPATH | ||
|
||
HEADERS = declarativessufeaturemodel.h | ||
SOURCES += plugin.cpp \ | ||
declarativessufeaturemodel.cpp | ||
|
||
qmldir.files = qmldir *.qml *.js | ||
qmldir.path = $$target.path | ||
|
||
INSTALLS += target qmldir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* @file declarativessufeaturemodel.cpp | ||
* @copyright 2015 Jolla Ltd. | ||
* @author Joona Petrell <joona.petrell@jolla.com> | ||
* @date 2015 | ||
*/ | ||
|
||
#include "declarativessufeaturemodel.h" | ||
#include <QHash> | ||
#include <qqml.h> | ||
#include <QQmlEngine> | ||
#include <qqmlinfo.h> | ||
|
||
QJSValue DeclarativeSsuFeatureModel::get(int index) const | ||
{ | ||
if (index < 0 || index >= count()) { | ||
qmlInfo(this) << "Index" << index << "out of bounds"; | ||
return QJSValue(); | ||
} | ||
const QHash<QByteArray, QString> feature = featureAt(index); | ||
|
||
QJSEngine *const engine = qmlEngine(this); | ||
QJSValue value = engine->newObject(); | ||
|
||
QHash<QByteArray, QString>::const_iterator i = feature.constBegin(); | ||
while (i != feature.constEnd()) { | ||
value.setProperty(QString::fromLatin1(i.key()), i.value()); | ||
++i; | ||
} | ||
|
||
return value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* @file declarativessufeaturemodel.h | ||
* @copyright 2015 Jolla Ltd. | ||
* @author Joona Petrell <joona.petrell@jolla.com> | ||
* @date 2015 | ||
*/ | ||
|
||
#ifndef _DECLARATIVESSUFEATUREMODEL_H | ||
#define _DECLARATIVESSUFEATUREMODEL_H | ||
|
||
#include "../libssu/ssufeaturemodel.h" | ||
#include <QJSValue> | ||
|
||
class DeclarativeSsuFeatureModel: public SsuFeatureModel | ||
{ | ||
Q_OBJECT | ||
public: | ||
Q_INVOKABLE QJSValue get(int index) const; | ||
}; | ||
|
||
#endif | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* @file plugin.cpp | ||
* @copyright 2015 Jolla Ltd. | ||
* @author Joona Petrell <joona.petrell@jollamobile.com> | ||
* @date 2015 | ||
*/ | ||
|
||
#include <QQmlExtensionPlugin> | ||
#include <QQmlEngine> | ||
|
||
#include <qqml.h> | ||
#include "declarativessufeaturemodel.h" | ||
|
||
class NemoSsuPlugin : public QQmlExtensionPlugin | ||
{ | ||
Q_OBJECT | ||
Q_PLUGIN_METADATA(IID "org.nemomobile.ssu") | ||
|
||
public: | ||
virtual void registerTypes(const char *) | ||
{ | ||
qmlRegisterType<DeclarativeSsuFeatureModel>("Nemo.Ssu", 1, 0, "FeatureModel"); | ||
} | ||
}; | ||
|
||
#include "plugin.moc" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module Nemo.Ssu | ||
plugin declarativessu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/** | ||
* @file ssufeaturemodel.cpp | ||
* @copyright 2015 Jolla Ltd. | ||
* @author Joona Petrell <joona.petrell@jolla.com> | ||
* @date 2015 | ||
*/ | ||
|
||
#include "ssufeaturemodel.h" | ||
#include <QList> | ||
#include <QSettings> | ||
#include <QHash> | ||
#include <QDebug> | ||
#include <QDirIterator> | ||
#include "../constants.h" | ||
|
||
bool featureLessThan(const QHash<QByteArray, QString> &feature1, const QHash<QByteArray, QString> &feature2) | ||
{ | ||
return feature1.value("name") < feature2.value("name"); | ||
} | ||
|
||
class SsuFeatureModelPrivate { | ||
public: | ||
SsuFeatureModelPrivate(const QString &p) : path(p) | ||
{ | ||
load(); | ||
} | ||
|
||
~SsuFeatureModelPrivate() | ||
{} | ||
|
||
void load() { | ||
QDirIterator it(path, QDir::AllEntries|QDir::NoDot|QDir::NoDotDot, QDirIterator::FollowSymlinks); | ||
QStringList settingsFiles; | ||
|
||
while (it.hasNext()) { | ||
it.next(); | ||
settingsFiles.append(it.filePath()); | ||
} | ||
foreach (const QString &settingsFile, settingsFiles) { | ||
QSettings settings(settingsFile, QSettings::IniFormat); | ||
|
||
foreach (const QString &childGroup, settings.childGroups()) { | ||
settings.beginGroup(childGroup); | ||
QStringList keys = settings.childKeys(); | ||
|
||
if (keys.contains("name")) { | ||
QHash<QByteArray, QString> feature; | ||
feature.insert("name", settings.value("name").toString()); | ||
if (keys.contains("version")) { | ||
feature.insert("version", settings.value("version").toString()); | ||
} else { | ||
feature.insert("version", QString("")); | ||
} | ||
features.append(feature); | ||
} | ||
settings.endGroup(); | ||
} | ||
} | ||
qSort(features.begin(), features.end(), featureLessThan); | ||
} | ||
|
||
QString path; | ||
QList<QHash<QByteArray, QString> > features; | ||
}; | ||
|
||
SsuFeatureModel::SsuFeatureModel(QObject *parent, const QString &path) | ||
: QAbstractListModel(parent), | ||
d(new SsuFeatureModelPrivate(path)) | ||
{ | ||
} | ||
|
||
SsuFeatureModel::SsuFeatureModel(QObject *parent) | ||
: QAbstractListModel(parent), | ||
d(new SsuFeatureModelPrivate(SSU_FEATURE_CONFIGURATION_DIR)) | ||
{ | ||
} | ||
|
||
SsuFeatureModel::~SsuFeatureModel() | ||
{ | ||
delete d; | ||
} | ||
|
||
int SsuFeatureModel::count() const | ||
{ | ||
return rowCount(); | ||
} | ||
|
||
const QHash<QByteArray, QString> &SsuFeatureModel::featureAt(int index) const | ||
{ | ||
return d->features.at(index); | ||
} | ||
|
||
int SsuFeatureModel::rowCount(const QModelIndex &parent) const | ||
{ | ||
return parent.isValid() ? 0 : d->features.count(); | ||
} | ||
|
||
void SsuFeatureModel::reload() | ||
{ | ||
beginResetModel(); | ||
d->features.clear(); | ||
d->load(); | ||
endResetModel(); | ||
} | ||
|
||
QVariant SsuFeatureModel::data(const QModelIndex &index, int role) const | ||
{ | ||
int row = index.row(); | ||
if (!index.isValid() || row < 0 || row >= d->features.count()) | ||
return QVariant(); | ||
|
||
switch (role) { | ||
case Name: | ||
return d->features.at(row).value("name"); | ||
case Version: | ||
return d->features.at(row).value("version"); | ||
} | ||
return QVariant(); | ||
} | ||
|
||
QHash<int, QByteArray> SsuFeatureModel::roleNames() const | ||
{ | ||
QHash<int,QByteArray> roles; | ||
roles.insert(Name, "name"); | ||
roles.insert(Version, "version"); | ||
return roles; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* @file ssufeaturemodel.h | ||
* @copyright 2015 Jolla Ltd. | ||
* @author Joona Petrell <joona.petrell@jolla.com> | ||
* @date 2015 | ||
*/ | ||
|
||
#ifndef _SSUFEATUREMODEL_H | ||
#define _SSUFEATUREMODEL_H | ||
|
||
#include <QAbstractListModel> | ||
|
||
class SsuFeatureModelPrivate; | ||
class FeatureModelTest; | ||
|
||
class SsuFeatureModel: public QAbstractListModel | ||
{ | ||
Q_OBJECT | ||
Q_PROPERTY(int count READ count NOTIFY countChanged) | ||
public: | ||
enum Roles { | ||
Name = Qt::UserRole, | ||
Version | ||
}; | ||
|
||
SsuFeatureModel(QObject *parent = 0); | ||
SsuFeatureModel(QObject *parent, const QString &path); | ||
~SsuFeatureModel(); | ||
|
||
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; | ||
virtual QVariant data(const QModelIndex &index, int role) const; | ||
|
||
public slots: | ||
void reload(); | ||
int count() const; | ||
|
||
protected: | ||
QHash<int, QByteArray> roleNames() const; | ||
const QHash<QByteArray, QString> &featureAt(int index) const; | ||
|
||
signals: | ||
void countChanged(); | ||
|
||
private: | ||
SsuFeatureModelPrivate *d; | ||
friend class FeatureModelTest; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.