diff options
Diffstat (limited to 'src/localesettings')
-rw-r--r-- | src/localesettings/locale.xml | 12 | ||||
-rw-r--r-- | src/localesettings/localefiltermodel.cpp | 96 | ||||
-rw-r--r-- | src/localesettings/localefiltermodel.h | 59 | ||||
-rw-r--r-- | src/localesettings/localemodel.cpp | 166 | ||||
-rw-r--r-- | src/localesettings/localemodel.h | 95 | ||||
-rw-r--r-- | src/localesettings/localesettings.pro | 22 | ||||
-rw-r--r-- | src/localesettings/systemlocale.cpp | 57 | ||||
-rw-r--r-- | src/localesettings/systemlocale.h | 62 | ||||
-rw-r--r-- | src/localesettings/systemlocale_p.h | 87 |
9 files changed, 656 insertions, 0 deletions
diff --git a/src/localesettings/locale.xml b/src/localesettings/locale.xml new file mode 100644 index 0000000..b722456 --- /dev/null +++ b/src/localesettings/locale.xml @@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="utf-8"?> +<node name="/Service" xmlns:tp="http://telepathy.freedesktop.org/wiki/DbusSpec#extensions-v0"> +<node> + <interface name="org.freedesktop.locale1"> + <property name="Locale" type="as" access="read"/> + <method name="SetLocale"> + <arg name="locale" type="as" direction="in"/> + <arg name="user_interaction" type="b" direction="in"/> + </method> + </interface> +</node> + diff --git a/src/localesettings/localefiltermodel.cpp b/src/localesettings/localefiltermodel.cpp new file mode 100644 index 0000000..027caba --- /dev/null +++ b/src/localesettings/localefiltermodel.cpp @@ -0,0 +1,96 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "localemodel.h" +#include "localefiltermodel.h" + +LocaleFilterModel::LocaleFilterModel(QObject* parent) + :QSortFilterProxyModel(parent) +{ + connect(this, &LocaleFilterModel::filterChanged, this, &LocaleFilterModel::invalidate); +} + +LocaleFilterModel::~LocaleFilterModel() +{ + +} + +QString LocaleFilterModel::filter() const +{ + return m_filter; +} + +void LocaleFilterModel::setFilter(const QString& aFilter) +{ + m_filter = aFilter; + emit filterChanged(); +} + +bool LocaleFilterModel::filterAcceptsRow( int source_row, const QModelIndex& source_parent ) const +{ + bool ret = false; + if (this->sourceModel()) + { + QModelIndex index = this->sourceModel()->index( source_row, 0, source_parent ); + if (index.isValid()) + { + QVariant nameRole = index.data(LocaleModel::Country); + if (nameRole.isValid()) + { + QString name = nameRole.toString(); + if (name.contains(m_filter, Qt::CaseInsensitive)) { + ret = true; + } + } + } + } + return ret; +} + +QVariant LocaleFilterModel::itemFromRow(const int row) const +{ + QModelIndex idx = index(row, 0); + QModelIndex mapped = mapToSource(idx); + if (mapped.isValid()) + { + QVariant nameRole = mapped.data(LocaleModel::Code); + if (nameRole.isValid()) + { + return nameRole; + } + } + return QVariant::fromValue(QString("")); +} + diff --git a/src/localesettings/localefiltermodel.h b/src/localesettings/localefiltermodel.h new file mode 100644 index 0000000..d63adb9 --- /dev/null +++ b/src/localesettings/localefiltermodel.h @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef LOCALEFILTERMODEL_H +#define LOCALEFILTERMODEL_H + +#include <QSortFilterProxyModel> + +class Q_DECL_EXPORT LocaleFilterModel : public QSortFilterProxyModel +{ + Q_OBJECT + Q_PROPERTY(QString filter READ filter WRITE setFilter NOTIFY filterChanged) +public: + explicit LocaleFilterModel(QObject* parent); + virtual ~LocaleFilterModel(); + bool filterAcceptsRow( int source_row, const QModelIndex& source_parent ) const override; + QString filter() const; + void setFilter(const QString& aFilter); + Q_INVOKABLE QVariant itemFromRow(const int row) const; +signals: + void filterChanged(); +private: + QString m_filter; + +}; + +#endif // LOCALEFILTERMODEL_H diff --git a/src/localesettings/localemodel.cpp b/src/localesettings/localemodel.cpp new file mode 100644 index 0000000..40d5fc8 --- /dev/null +++ b/src/localesettings/localemodel.cpp @@ -0,0 +1,166 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QLocale> +#include <QFuture> +#include <QFutureWatcher> +#include <QThread> +#include <QtConcurrent/QtConcurrentRun> +#include "localemodel.h" + + +LocaleItem::LocaleItem(const QLocale& locale, QObject *parent) + :QObject(parent) +{ + m_code = locale.name(); + m_country = locale.nativeCountryName(); + if (m_country.isEmpty()) { + m_country = locale.countryToString(locale.country()); + } + + m_language = locale.nativeLanguageName(); + if (m_language.isEmpty()) { + m_language = locale.languageToString(locale.language()); + } +} + +QString LocaleItem::language() const +{ + return m_language; +} + +QString LocaleItem::country() const +{ + return m_country; +} + +QString LocaleItem::code() const +{ + return m_code; +} + +LocaleModel::LocaleModel(QObject *parent) + : QAbstractListModel(parent) +{ + m_roleNames.insert(Qt::UserRole, "modelData"); + m_roleNames.insert(Country, "country"); + m_roleNames.insert(Language, "language"); + m_roleNames.insert(Code, "code"); + + QFutureWatcher<void> *watcher = new QFutureWatcher<void>(this); + QFuture<void> future = QtConcurrent::run(LocaleModel::generateModel, this); + watcher->setFuture(future); + connect(watcher, SIGNAL(finished()), this, SLOT(modelReady())); +} + +void LocaleModel::modelReady() +{ + beginResetModel(); + sort(0); + endResetModel(); +} + +void LocaleModel::generateModel(LocaleModel* model) +{ + QList<QLocale> allLocales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry); + + for (const QLocale &locale : allLocales) { + if (locale.name() != "C") { + LocaleItem *l = new LocaleItem(locale); + l->moveToThread(model->thread()); + QMetaObject::invokeMethod(model, "addNewItem", Q_ARG( QObject*, qobject_cast<QObject*>(l))); + } + } + +} + +void LocaleModel::addNewItem(QObject *item) +{ + beginInsertRows(QModelIndex(), m_items.count(), m_items.count()); + LocaleItem* newItem = qobject_cast<LocaleItem*>(item); + if (newItem) + m_items.append(newItem); + endInsertRows(); +} + +LocaleModel::~LocaleModel() +{ + qDeleteAll(m_items); +} + +QHash<int, QByteArray> LocaleModel::roleNames() const +{ + return m_roleNames; +} + +int LocaleModel::rowCount(const QModelIndex & parent) const +{ + Q_UNUSED(parent); + return m_items.count(); +} + +QVariant LocaleModel::data(const QModelIndex & index, int role) const +{ + if (!index.isValid()) return QVariant(); + + LocaleItem *item = m_items[index.row()]; + + switch (role) { + case Country: + return item->country(); + break; + case Language: + return item->language(); + break; + case Code: + return item->code(); + break; + default: + return ""; + } +} + +bool LocaleModel::variantLessThan(const LocaleItem* v1, const LocaleItem* v2) +{ + return v1->language() < v2->language(); +} + +void LocaleModel::sort(int column, Qt::SortOrder order) +{ + Q_UNUSED(column); + Q_UNUSED(order); + qSort(m_items.begin(), m_items.end(), LocaleModel::variantLessThan); +} diff --git a/src/localesettings/localemodel.h b/src/localesettings/localemodel.h new file mode 100644 index 0000000..6ed5844 --- /dev/null +++ b/src/localesettings/localemodel.h @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef LOCALEMODEL_H +#define LOCALEMODEL_H + +#include <QObject> +#include <QAbstractListModel> +#include <QLocale> +#include <QMutex> + +class LocaleItem : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString code READ code CONSTANT) + Q_PROPERTY(QString country READ country CONSTANT) + Q_PROPERTY(QString language READ language CONSTANT) +public: + explicit LocaleItem(const QLocale &locale, QObject *parent=0); + QString country() const; + QString language() const; + QString code() const; + +private: + QString m_country; + QString m_language; + QString m_code; +}; + +class Q_DECL_EXPORT LocaleModel : public QAbstractListModel +{ + Q_OBJECT + +public: + explicit LocaleModel(QObject *parent=0); + virtual ~LocaleModel(); + // from QAbstractItemModel + int rowCount(const QModelIndex & parent = QModelIndex()) const; + QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const; + QHash<int, QByteArray> roleNames() const; + void sort(int column, Qt::SortOrder order=Qt::AscendingOrder); + static bool variantLessThan(const LocaleItem* v1, const LocaleItem* v2); + + enum Roles { + Language = Qt::DisplayPropertyRole, + Country = Qt::UserRole, + Code + }; + + static void generateModel(LocaleModel* model); + +signals: + void addItem(LocaleItem* item); + +private slots: + void addNewItem(QObject* item); + void modelReady(); + +private: + QList<LocaleItem*> m_items; + QHash<int, QByteArray> m_roleNames; +}; +#endif // LOCALEMODEL_H diff --git a/src/localesettings/localesettings.pro b/src/localesettings/localesettings.pro new file mode 100644 index 0000000..808477d --- /dev/null +++ b/src/localesettings/localesettings.pro @@ -0,0 +1,22 @@ +load(qt_build_config) + +TARGET = QtLocaleSettings +VERSION = 1.0 +CONFIG += dll warn_on + +QT = core dbus + +MODULE = localesettings +load(qt_module) + +DBUS_INTERFACES = locale.xml + +HEADERS = \ + systemlocale.h \ + systemlocale_p.h \ + localemodel.h \ + localefiltermodel.h +SOURCES += systemlocale.cpp \ + localemodel.cpp \ + localefiltermodel.cpp + diff --git a/src/localesettings/systemlocale.cpp b/src/localesettings/systemlocale.cpp new file mode 100644 index 0000000..21a1105 --- /dev/null +++ b/src/localesettings/systemlocale.cpp @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QLocale> +#include "systemlocale.h" +#include "systemlocale_p.h" + +SystemLocale::SystemLocale(QObject *parent) + :QObject(parent) + ,d_ptr(new SystemLocalePrivate(this)) +{ +} + +QString SystemLocale::locale() const +{ + Q_D(const SystemLocale); + return d->locale(); +} + +void SystemLocale::setLocale(const QString& aLocale) +{ + Q_D(SystemLocale); + d->setLocale(aLocale); + emit localeChanged(); +} diff --git a/src/localesettings/systemlocale.h b/src/localesettings/systemlocale.h new file mode 100644 index 0000000..9472e9c --- /dev/null +++ b/src/localesettings/systemlocale.h @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef LOCALE_H +#define LOCALE_H + +#include <QObject> + +class SystemLocalePrivate; + +class Q_DECL_EXPORT SystemLocale : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString locale READ locale WRITE setLocale NOTIFY localeChanged) +public: + explicit SystemLocale(QObject *parent = 0); + QString locale() const; + void setLocale(const QString& aLocale); +signals: + void localeChanged(); +public slots: + +protected: + SystemLocalePrivate *d_ptr; + + Q_DISABLE_COPY(SystemLocale) + Q_DECLARE_PRIVATE(SystemLocale) +}; + +#endif // LOCALE_H diff --git a/src/localesettings/systemlocale_p.h b/src/localesettings/systemlocale_p.h new file mode 100644 index 0000000..417b958 --- /dev/null +++ b/src/localesettings/systemlocale_p.h @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef LOCALE_P_H +#define LOCALE_P_H + +#include "systemlocale.h" +#include "locale_interface.h" + +class SystemLocalePrivate +{ + Q_DECLARE_PUBLIC(SystemLocale) +public: + SystemLocalePrivate(SystemLocale *qq) + :q_ptr(qq) + { + m_localeInterface = new OrgFreedesktopLocale1Interface(QStringLiteral("org.freedesktop.locale1"), + QStringLiteral("/org/freedesktop/locale1"), + QDBusConnection::systemBus(), qq); + + QStringList locale = m_localeInterface->locale(); + foreach (QString l, locale) { + QStringList nameValue = l.split('='); + if (nameValue.length() == 2) + m_localeCache[nameValue.at(0)] = nameValue.at(1); + } + } + + QString locale() const { + return m_localeCache["LANG"]; + } + + void setLocale(const QString& aLocale) { + m_localeCache["LANG"] = aLocale; + updateLocale(); + } + + void updateLocale() { + QStringList newLocale; + QMap<QString, QString>::iterator i; + for (i = m_localeCache.begin(); i != m_localeCache.end(); ++i) { + QString val = i.key() + "=" + i.value(); + newLocale.append(val); + } + m_localeInterface->SetLocale(newLocale, true); + } + + +private: + QMap<QString, QString> m_localeCache; + OrgFreedesktopLocale1Interface *m_localeInterface; + SystemLocale *q_ptr; +}; + +#endif // LOCALE_P_H |