Commit bd3c925d authored by Alexander Lohnau's avatar Alexander Lohnau 💬

Run clang-format

If you want git blame to ignore formatting revisions run:
git config blame.ignoreRevsFile .git-blame-ignore-revs
parent 7e3dfb31
......@@ -18,38 +18,38 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QApplication>
#include <QLocale>
#include <QByteArray>
#include <QStandardPaths>
#include <QAbstractListModel>
#include <QVector>
#include <QIcon>
#include <QQmlApplicationEngine>
#include <QSortFilterProxyModel>
#include <QQuickImageProvider>
#include <QCommandLineParser>
#include <QFontMetrics>
#include <QPainter>
#include <QClipboard>
#include <QQuickWindow>
#include <KLocalizedString>
#include <KAboutData>
#include <KQuickAddons/QtQuickSettings>
#include <KCrash>
#include <KDBusService>
#include <KLocalizedString>
#include <KQuickAddons/QtQuickSettings>
#include <KWindowConfig>
#include <QDebug>
#include <QAbstractListModel>
#include <QApplication>
#include <QByteArray>
#include <QClipboard>
#include <QCommandLineParser>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusConnectionInterface>
#include <QDBusMessage>
#include <QDebug>
#include <QFontMetrics>
#include <QIcon>
#include <QLocale>
#include <QPainter>
#include <QQmlApplicationEngine>
#include <QQuickImageProvider>
#include <QQuickWindow>
#include <QSessionManager>
#include <QSortFilterProxyModel>
#include <QStandardPaths>
#include <QVector>
#include <QX11Info>
#include <kstartupinfo.h>
#include "emojiersettings.h"
#include "config-workspace.h"
#include "emojiersettings.h"
#undef signals
#include <ibus.h>
......@@ -67,21 +67,28 @@ class AbstractEmojiModel : public QAbstractListModel
public:
enum EmojiRole { CategoryRole = Qt::UserRole + 1, AnnotationsRole };
int rowCount(const QModelIndex & parent = {}) const override { return parent.isValid() ? 0 : m_emoji.count(); }
QVariant data(const QModelIndex & index, int role) const override {
if (!checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid | QAbstractItemModel::CheckIndexOption::ParentIsInvalid | QAbstractItemModel::CheckIndexOption::DoNotUseParent) || index.column() != 0)
int rowCount(const QModelIndex &parent = {}) const override
{
return parent.isValid() ? 0 : m_emoji.count();
}
QVariant data(const QModelIndex &index, int role) const override
{
if (!checkIndex(index,
QAbstractItemModel::CheckIndexOption::IndexIsValid | QAbstractItemModel::CheckIndexOption::ParentIsInvalid
| QAbstractItemModel::CheckIndexOption::DoNotUseParent)
|| index.column() != 0)
return {};
const auto &emoji = m_emoji[index.row()];
switch(role) {
case Qt::DisplayRole:
return emoji.content;
case Qt::ToolTipRole:
return emoji.description;
case CategoryRole:
return emoji.category;
case AnnotationsRole:
return emoji.annotations;
switch (role) {
case Qt::DisplayRole:
return emoji.content;
case Qt::ToolTipRole:
return emoji.description;
case CategoryRole:
return emoji.category;
case AnnotationsRole:
return emoji.annotations;
}
return {};
}
......@@ -97,7 +104,8 @@ class EmojiModel : public AbstractEmojiModel
public:
enum EmojiRole { CategoryRole = Qt::UserRole + 1 };
EmojiModel() {
EmojiModel()
{
QLocale locale;
QVector<QString> dicts;
const auto bcp = locale.bcp47Name();
......@@ -134,14 +142,14 @@ public:
QSet<QString> categories;
QSet<QString> processedEmoji;
for (const auto &dictPath : qAsConst(dicts)) {
GSList *list = ibus_emoji_data_load (dictPath.toUtf8().constData());
GSList *list = ibus_emoji_data_load(dictPath.toUtf8().constData());
m_emoji.reserve(g_slist_length(list));
for (GSList *l = list; l; l = l->next) {
IBusEmojiData *data = (IBusEmojiData *) l->data;
if (!IBUS_IS_EMOJI_DATA (data)) {
IBusEmojiData *data = (IBusEmojiData *)l->data;
if (!IBUS_IS_EMOJI_DATA(data)) {
qWarning() << "Your dict format is no longer supported.\n"
"Need to create the dictionaries again.";
g_slist_free (list);
"Need to create the dictionaries again.";
g_slist_free(list);
return;
}
......@@ -154,15 +162,15 @@ public:
QStringList annotations;
const auto annotations_glib = ibus_emoji_data_get_annotations(data);
for (GSList *l = annotations_glib; l; l = l->next) {
annotations << QString::fromUtf8((const gchar*) l->data);
annotations << QString::fromUtf8((const gchar *)l->data);
}
const QString category = QString::fromUtf8(ibus_emoji_data_get_category(data));
categories.insert(category);
m_emoji += { emoji, description, category, annotations };
m_emoji += {emoji, description, category, annotations};
processedEmoji << emoji;
}
g_slist_free (list);
g_slist_free(list);
}
categories.remove({});
......@@ -170,7 +178,8 @@ public:
m_categories.sort();
}
Q_SCRIPTABLE QString findFirstEmojiForCategory(const QString &category) {
Q_SCRIPTABLE QString findFirstEmojiForCategory(const QString &category)
{
for (const Emoji &emoji : m_emoji) {
if (emoji.category == category)
return emoji.content;
......@@ -192,7 +201,8 @@ public:
refresh();
}
Q_SCRIPTABLE void includeRecent(const QString &emoji, const QString &emojiDescription) {
Q_SCRIPTABLE void includeRecent(const QString &emoji, const QString &emojiDescription)
{
QStringList recent = m_settings.recent();
QStringList recentDescriptions = m_settings.recentDescriptions();
......@@ -222,7 +232,7 @@ private:
int i = 0;
m_emoji.clear();
for (const QString &c : recent) {
m_emoji += { c, recentDescriptions.at(i++), QString{}, {} };
m_emoji += {c, recentDescriptions.at(i++), QString{}, {}};
}
endResetModel();
}
......@@ -235,15 +245,20 @@ class CategoryModelFilter : public QSortFilterProxyModel
Q_OBJECT
Q_PROPERTY(QString category READ category WRITE setCategory)
public:
QString category() const { return m_category; }
void setCategory(const QString &category) {
QString category() const
{
return m_category;
}
void setCategory(const QString &category)
{
if (m_category != category) {
m_category = category;
invalidateFilter();
}
}
bool filterAcceptsRow(int source_row, const QModelIndex & source_parent) const override {
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override
{
return m_category.isEmpty() || sourceModel()->index(source_row, 0, source_parent).data(EmojiModel::CategoryRole).toString() == m_category;
}
......@@ -256,18 +271,23 @@ class SearchModelFilter : public QSortFilterProxyModel
Q_OBJECT
Q_PROPERTY(QString search READ search WRITE setSearch)
public:
QString search() const { return m_search; }
void setSearch(const QString &search) {
QString search() const
{
return m_search;
}
void setSearch(const QString &search)
{
if (m_search != search) {
m_search = search;
invalidateFilter();
}
}
bool filterAcceptsRow(int source_row, const QModelIndex & source_parent) const override {
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override
{
const auto idx = sourceModel()->index(source_row, 0, source_parent);
return idx.data(Qt::ToolTipRole).toString().contains(m_search, Qt::CaseInsensitive) ||
idx.data(AbstractEmojiModel::AnnotationsRole).toStringList().contains(m_search, Qt::CaseInsensitive);
return idx.data(Qt::ToolTipRole).toString().contains(m_search, Qt::CaseInsensitive)
|| idx.data(AbstractEmojiModel::AnnotationsRole).toStringList().contains(m_search, Qt::CaseInsensitive);
}
private:
......@@ -277,26 +297,27 @@ private:
class CopyHelperPrivate : public QObject
{
Q_OBJECT
public:
Q_INVOKABLE static void copyTextToClipboard(const QString& text)
{
QClipboard *clipboard = qGuiApp->clipboard();
clipboard->setText(text, QClipboard::Clipboard);
clipboard->setText(text, QClipboard::Selection);
}
public:
Q_INVOKABLE static void copyTextToClipboard(const QString &text)
{
QClipboard *clipboard = qGuiApp->clipboard();
clipboard->setText(text, QClipboard::Clipboard);
clipboard->setText(text, QClipboard::Selection);
}
};
class EngineWatcher : public QObject
{
public:
EngineWatcher(QQmlApplicationEngine* engine)
EngineWatcher(QQmlApplicationEngine *engine)
: QObject(engine)
{
connect(engine, &QQmlApplicationEngine::objectCreated, this, &EngineWatcher::integrateObject);
}
void integrateObject(QObject* object) {
QWindow* window = qobject_cast<QWindow*>(object);
void integrateObject(QObject *object)
{
QWindow *window = qobject_cast<QWindow *>(object);
auto conf = KSharedConfig::openConfig();
KWindowConfig::restoreWindowSize(window, conf->group("Window"));
......@@ -304,10 +325,10 @@ public:
object->installEventFilter(this);
}
bool eventFilter(QObject * object, QEvent * event) override
bool eventFilter(QObject *object, QEvent *event) override
{
if (event->type() == QEvent::Close) {
QWindow* window = qobject_cast<QWindow*>(object);
QWindow *window = qobject_cast<QWindow *>(object);
auto conf = KSharedConfig::openConfig();
auto group = conf->group("Window");
......@@ -318,7 +339,7 @@ public:
}
};
int main(int argc, char** argv)
int main(int argc, char **argv)
{
QGuiApplication::setFallbackSessionManagementEnabled(false);
QApplication app(argc, argv);
......@@ -329,11 +350,15 @@ int main(int argc, char** argv)
KLocalizedString::setApplicationDomain("org.kde.plasma.emojier");
KAboutData about(QStringLiteral("plasma.emojier"), i18n("Emoji Selector"), QStringLiteral(WORKSPACE_VERSION_STRING), i18n("Emoji Selector"),
KAboutLicense::GPL, i18n("(C) 2019 Aleix Pol i Gonzalez"));
about.addAuthor( QStringLiteral("Aleix Pol i Gonzalez"), QString(), QStringLiteral("aleixpol@kde.org") );
KAboutData about(QStringLiteral("plasma.emojier"),
i18n("Emoji Selector"),
QStringLiteral(WORKSPACE_VERSION_STRING),
i18n("Emoji Selector"),
KAboutLicense::GPL,
i18n("(C) 2019 Aleix Pol i Gonzalez"));
about.addAuthor(QStringLiteral("Aleix Pol i Gonzalez"), QString(), QStringLiteral("aleixpol@kde.org"));
about.setTranslator(i18nc("NAME OF TRANSLATORS", "Your names"), i18nc("EMAIL OF TRANSLATORS", "Your emails"));
// about.setProductName("");
// about.setProductName("");
about.setProgramLogo(app.windowIcon());
KAboutData::setApplicationData(about);
......@@ -347,8 +372,7 @@ int main(int argc, char** argv)
{
QCommandLineParser parser;
QCommandLineOption replaceOption({QStringLiteral("replace")},
i18n("Replace an existing instance"));
QCommandLineOption replaceOption({QStringLiteral("replace")}, i18n("Replace an existing instance"));
parser.addOption(replaceOption);
about.setupCommandLine(&parser);
parser.process(app);
......@@ -359,21 +383,23 @@ int main(int argc, char** argv)
}
}
KDBusService* service = new KDBusService(KDBusService::Unique | startup, &app);
KDBusService *service = new KDBusService(KDBusService::Unique | startup, &app);
qmlRegisterType<EmojiModel>("org.kde.plasma.emoji", 1, 0, "EmojiModel");
qmlRegisterType<CategoryModelFilter>("org.kde.plasma.emoji", 1, 0, "CategoryModelFilter");
qmlRegisterType<SearchModelFilter>("org.kde.plasma.emoji", 1, 0, "SearchModelFilter");
qmlRegisterType<RecentEmojiModel>("org.kde.plasma.emoji", 1, 0, "RecentEmojiModel");
qmlRegisterSingletonType<CopyHelperPrivate>("org.kde.plasma.emoji", 1, 0, "CopyHelper", [] (QQmlEngine*, QJSEngine*) -> QObject* { return new CopyHelperPrivate; });
qmlRegisterSingletonType<CopyHelperPrivate>("org.kde.plasma.emoji", 1, 0, "CopyHelper", [](QQmlEngine *, QJSEngine *) -> QObject * {
return new CopyHelperPrivate;
});
QQmlApplicationEngine engine;
new EngineWatcher(&engine);
engine.load(QUrl(QStringLiteral("qrc:/ui/emojier.qml")));
QObject::connect(service, &KDBusService::activateRequested, &engine, [&engine](const QStringList &/*arguments*/, const QString &/*workingDirectory*/) {
for (QObject* object : engine.rootObjects()) {
auto w = qobject_cast<QQuickWindow*>(object);
QObject::connect(service, &KDBusService::activateRequested, &engine, [&engine](const QStringList & /*arguments*/, const QString & /*workingDirectory*/) {
for (QObject *object : engine.rootObjects()) {
auto w = qobject_cast<QQuickWindow *>(object);
if (!w)
continue;
......
......@@ -19,13 +19,13 @@
*/
#include "app.h"
#include "gtkaccelparse_p.h"
#include "gdkkeysyms_p.h"
#include <QTimer>
#include "gtkaccelparse_p.h"
#include <QDBusConnection>
#include <QDBusServiceWatcher>
#include <QDebug>
#include <QTimer>
#include <QX11Info>
#include <QDBusServiceWatcher>
#include <QDBusConnection>
#define USED_MASK (XCB_MOD_MASK_SHIFT | XCB_MOD_MASK_CONTROL | XCB_MOD_MASK_1 | XCB_MOD_MASK_4)
......@@ -36,17 +36,17 @@ bool XcbEventFilter::nativeEventFilter(const QByteArray &eventType, void *messag
return false;
}
return qobject_cast<App*>(qApp)->nativeEvent(static_cast<xcb_generic_event_t*>(message));
return qobject_cast<App *>(qApp)->nativeEvent(static_cast<xcb_generic_event_t *>(message));
}
// callback functions from glib code
static void name_acquired_cb (GDBusConnection* connection,
const gchar* sender_name,
const gchar* object_path,
const gchar* interface_name,
const gchar* signal_name,
GVariant* parameters,
gpointer self)
static void name_acquired_cb(GDBusConnection *connection,
const gchar *sender_name,
const gchar *object_path,
const gchar *interface_name,
const gchar *signal_name,
GVariant *parameters,
gpointer self)
{
Q_UNUSED(connection);
Q_UNUSED(sender_name);
......@@ -54,17 +54,17 @@ static void name_acquired_cb (GDBusConnection* connection,
Q_UNUSED(interface_name);
Q_UNUSED(signal_name);
Q_UNUSED(parameters);
App* app = (App*) self;
App *app = (App *)self;
app->nameAcquired();
}
static void name_lost_cb (GDBusConnection* connection,
const gchar* sender_name,
const gchar* object_path,
const gchar* interface_name,
const gchar* signal_name,
GVariant* parameters,
gpointer self)
static void name_lost_cb(GDBusConnection *connection,
const gchar *sender_name,
const gchar *object_path,
const gchar *interface_name,
const gchar *signal_name,
GVariant *parameters,
gpointer self)
{
Q_UNUSED(connection);
Q_UNUSED(sender_name);
......@@ -72,29 +72,25 @@ static void name_lost_cb (GDBusConnection* connection,
Q_UNUSED(interface_name);
Q_UNUSED(signal_name);
Q_UNUSED(parameters);
App* app = (App*) self;
App *app = (App *)self;
app->nameLost();
}
static void
ibus_connected_cb (IBusBus *m_bus,
gpointer user_data)
static void ibus_connected_cb(IBusBus *m_bus, gpointer user_data)
{
Q_UNUSED(m_bus);
App* app = (App*) user_data;
App *app = (App *)user_data;
app->init();
}
static void
ibus_disconnected_cb (IBusBus *m_bus,
gpointer user_data)
static void ibus_disconnected_cb(IBusBus *m_bus, gpointer user_data)
{
Q_UNUSED(m_bus);
App* app = (App*) user_data;
App *app = (App *)user_data;
app->finalize();
}
static void initIconMap(QMap<QByteArray, QByteArray>& iconMap)
static void initIconMap(QMap<QByteArray, QByteArray> &iconMap)
{
iconMap["gtk-about"] = "help-about";
iconMap["gtk-add"] = "list-add";
......@@ -177,15 +173,16 @@ static void initIconMap(QMap<QByteArray, QByteArray>& iconMap)
iconMap["gtk-zoom-out"] = "zoom-out";
}
App::App(int &argc, char* argv[]): QGuiApplication(argc, argv)
,m_eventFilter(new XcbEventFilter)
,m_init(false)
,m_bus(ibus_bus_new ())
,m_impanel(nullptr)
,m_keyboardGrabbed(false)
,m_doGrab(false)
,m_syms(nullptr)
,m_watcher(new QDBusServiceWatcher(this))
App::App(int &argc, char *argv[])
: QGuiApplication(argc, argv)
, m_eventFilter(new XcbEventFilter)
, m_init(false)
, m_bus(ibus_bus_new())
, m_impanel(nullptr)
, m_keyboardGrabbed(false)
, m_doGrab(false)
, m_syms(nullptr)
, m_watcher(new QDBusServiceWatcher(this))
{
m_syms = xcb_key_symbols_alloc(QX11Info::connection());
installNativeEventFilter(m_eventFilter.data());
......@@ -198,16 +195,7 @@ App::App(int &argc, char* argv[]): QGuiApplication(argc, argv)
uint App::getPrimaryModifier(uint state)
{
const GdkModifierType masks[] = {
GDK_MOD5_MASK,
GDK_MOD4_MASK,
GDK_MOD3_MASK,
GDK_MOD2_MASK,
GDK_MOD1_MASK,
GDK_CONTROL_MASK,
GDK_LOCK_MASK,
GDK_LOCK_MASK
};
const GdkModifierType masks[] = {GDK_MOD5_MASK, GDK_MOD4_MASK, GDK_MOD3_MASK, GDK_MOD2_MASK, GDK_MOD1_MASK, GDK_CONTROL_MASK, GDK_LOCK_MASK, GDK_LOCK_MASK};
for (size_t i = 0; i < sizeof(masks) / sizeof(masks[0]); i++) {
GdkModifierType mask = masks[i];
if ((state & mask) == mask)
......@@ -216,16 +204,16 @@ uint App::getPrimaryModifier(uint state)
return 0;
}
bool App::nativeEvent(xcb_generic_event_t* event)
bool App::nativeEvent(xcb_generic_event_t *event)
{
if ((event->response_type & ~0x80) == XCB_KEY_PRESS) {
auto keypress = reinterpret_cast<xcb_key_press_event_t*>(event);
auto keypress = reinterpret_cast<xcb_key_press_event_t *>(event);
if (keypress->event == QX11Info::appRootWindow()) {
auto sym = xcb_key_press_lookup_keysym(m_syms, keypress, 0);
uint state = keypress->state & USED_MASK;
bool forward;
if ((forward = m_triggersList.contains(qMakePair<uint, uint>(sym, state)))
|| m_triggersList.contains(qMakePair<uint, uint>(sym, state & (~XCB_MOD_MASK_SHIFT)))) {
|| m_triggersList.contains(qMakePair<uint, uint>(sym, state & (~XCB_MOD_MASK_SHIFT)))) {
if (m_keyboardGrabbed) {
ibus_panel_impanel_navigate(m_impanel, false, forward);
} else {
......@@ -238,7 +226,7 @@ bool App::nativeEvent(xcb_generic_event_t* event)
}
}
} else if ((event->response_type & ~0x80) == XCB_KEY_RELEASE) {
auto keyrelease = reinterpret_cast<xcb_key_release_event_t*>(event);
auto keyrelease = reinterpret_cast<xcb_key_release_event_t *>(event);
if (keyrelease->event == QX11Info::appRootWindow()) {
keyRelease(keyrelease);
}
......@@ -246,7 +234,7 @@ bool App::nativeEvent(xcb_generic_event_t* event)
return false;
}
void App::keyRelease(const xcb_key_release_event_t* event)
void App::keyRelease(const xcb_key_release_event_t *event)
{
unsigned int mk = event->state & USED_MASK;
// ev.state is state before the key release, so just checking mk being 0 isn't enough
......@@ -254,9 +242,7 @@ void App::keyRelease(const xcb_key_release_event_t* event)
// modifiers are released: only one modifier is active and the currently released
// key is this modifier - if yes, release the grab
int mod_index = -1;
for (int i = XCB_MAP_INDEX_SHIFT;
i <= XCB_MAP_INDEX_5;
++i)
for (int i = XCB_MAP_INDEX_SHIFT; i <= XCB_MAP_INDEX_5; ++i)
if ((mk & (1 << i)) != 0) {
if (mod_index >= 0)
return;
......@@ -271,8 +257,7 @@ void App::keyRelease(const xcb_key_release_event_t* event)
if (reply) {
auto keycodes = xcb_get_modifier_mapping_keycodes(reply);
for (int i = 0; i < reply->keycodes_per_modifier; i++) {
if (keycodes[reply->keycodes_per_modifier * mod_index + i]
== event->detail) {
if (keycodes[reply->keycodes_per_modifier * mod_index + i] == event->detail) {
release = true;
}
}
......@@ -287,37 +272,42 @@ void App::keyRelease(const xcb_key_release_event_t* event)
}
}
void App::init()
{
// only init once
if (m_init) {
return;
}
if (!ibus_bus_is_connected (m_bus)) {
if (!ibus_bus_is_connected(m_bus)) {
return;
}
g_signal_connect (m_bus, "connected", G_CALLBACK (ibus_connected_cb), this);
g_signal_connect (m_bus, "disconnected", G_CALLBACK (ibus_disconnected_cb), this);
g_signal_connect(m_bus, "connected", G_CALLBACK(ibus_connected_cb), this);
g_signal_connect(m_bus, "disconnected", G_CALLBACK(ibus_disconnected_cb), this);
connect(m_watcher, &QDBusServiceWatcher::serviceUnregistered, this, &App::finalize);
GDBusConnection* connection = ibus_bus_get_connection (m_bus);