blob: c4ec32df095a861fc2c54741981e311bdb971d22 [file] [log] [blame]
Edric Milaret3e6aefe2015-06-05 16:07:26 -04001/***************************************************************************
2 * Copyright (C) 2015 by Savoir-Faire Linux *
3 * Author : Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com>*
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 **************************************************************************/
18#include "accountserializationadapter.h"
19
20#include <QtWidgets/QWidget>
21#include <QtWidgets/QLayout>
22#include <QtWidgets/QLineEdit>
23#include <QtWidgets/QSpinBox>
24#include <QtWidgets/QAbstractButton>
25
26#include <account.h>
27#include <accountmodel.h>
28
29constexpr static const char LRC_CFG[] = "lrcfg_";
30constexpr static const int LRC_CFG_LEN = 6 ;
31
32struct ConnHolder {
Edric Milarete0855a92015-06-12 11:38:24 -040033 QMetaObject::Connection c;
34 ~ConnHolder();
Edric Milaret3e6aefe2015-06-05 16:07:26 -040035};
36Q_DECLARE_METATYPE(ConnHolder*);
37
38ConnHolder::~ConnHolder()
39{
Edric Milarete0855a92015-06-12 11:38:24 -040040 QObject::disconnect(c);
Edric Milaret3e6aefe2015-06-05 16:07:26 -040041}
42
43static void avoidDuplicate(QWidget* w)
44{
Edric Milarete0855a92015-06-12 11:38:24 -040045 if (qvariant_cast<ConnHolder*>(w->property("lrcfgConn")))
46 delete qvariant_cast<ConnHolder*>(w->property("lrcfgConn"));
Edric Milaret3e6aefe2015-06-05 16:07:26 -040047}
48
49/**
50 * This check for some supported widgets and bind the widgets and property
51 */
Edric Milarete0855a92015-06-12 11:38:24 -040052static void setupWidget(QWidget* w,
53 Account* a,
54 const QHash<QByteArray, int>& roles)
Edric Milaret3e6aefe2015-06-05 16:07:26 -040055{
Edric Milarete0855a92015-06-12 11:38:24 -040056 if (w->objectName().left(LRC_CFG_LEN) == LRC_CFG) {
57 const QByteArray prop = w->objectName().mid(LRC_CFG_LEN, 999).toLatin1();
58 if (roles.contains(prop)) {
59 const int role = roles[prop];
60 auto roleState = a->roleState(static_cast<Account::Role>(role));
61 if (roleState == Account::RoleState::READ_WRITE
62 || roleState == Account::RoleState::READ_ONLY) {
63 if (qobject_cast<QLineEdit*>(w)) {
64 QLineEdit* le = qobject_cast<QLineEdit*>(w);
65 avoidDuplicate(le);
66 le->setText(a->roleData(role).toString());
67 ConnHolder* c = new ConnHolder {
68 QObject::connect(le, &QLineEdit::textChanged,
69 [a,role](const QString& text) {
70 if (a->roleData(role) != text)
71 a->setRoleData(role, text);
72 })
73 };
74 le->setProperty("lrcfgConn",QVariant::fromValue(c));
75 }
76 else if (qobject_cast<QSpinBox*>(w)) {
77 QSpinBox* sb = qobject_cast<QSpinBox*>(w);
78 avoidDuplicate(sb);
79 sb->setValue(a->roleData(role).toInt());
80 ConnHolder* c = new ConnHolder {
81 QObject::connect(sb, static_cast<void (QSpinBox::*)
82 (int)>(&QSpinBox::valueChanged),
83 [a,role](int value) {
84 if (a->roleData(role).toInt() != value)
85 a->setRoleData(role, value);
86 })
87 };
88 sb->setProperty("lrcfgConn",QVariant::fromValue(c));
89 }
90 else if (qobject_cast<QAbstractButton*>(w)) {
91 //QCheckBox, QRadioButton, QToolButton, QPushButton
92 QAbstractButton* b = qobject_cast<QAbstractButton*>(w);
93 avoidDuplicate(b);
94 b->setChecked(a->roleData(role).toBool());
95 ConnHolder* c = new ConnHolder {
96 QObject::connect(b, &QAbstractButton::toggled,
97 [a,role](bool c) {
98 if (a->roleData(role).toBool() != c)
99 a->setRoleData(role, c);
100 })
101 };
102 b->setProperty("lrcfgConn",QVariant::fromValue(c));
103 }
104 w->setVisible(true);
105 w->setEnabled(roleState == Account::RoleState::READ_WRITE);
106 } else {
107 w->setVisible(false);
108 }
109 }
110 else {
111 qWarning() << "Unknown config properties" << w->objectName();
112 }
113 }
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400114}
115
116static void clearConnections(QWidget* w)
117{
Edric Milarete0855a92015-06-12 11:38:24 -0400118 if (w->objectName().left(LRC_CFG_LEN) == LRC_CFG) {
119 avoidDuplicate(w);
120 }
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400121}
122
Edric Milarete0855a92015-06-12 11:38:24 -0400123static void drill(QWidget* w, Account* a,
124 const QHash<QByteArray, int>& roles,
125 bool clear = false)
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400126{
Edric Milarete0855a92015-06-12 11:38:24 -0400127 for (QObject *object : w->children()) {
128 if (!object->isWidgetType())
129 continue;
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400130
Edric Milarete0855a92015-06-12 11:38:24 -0400131 QWidget* w2 = static_cast<QWidget*>(object);
132 if (clear)
133 clearConnections(w2);
134 else
135 setupWidget(w2, a, roles);
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400136
Edric Milarete0855a92015-06-12 11:38:24 -0400137 drill(w2, a, roles);
138 }
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400139}
140
Edric Milarete0855a92015-06-12 11:38:24 -0400141AccountSerializationAdapter::AccountSerializationAdapter(Account* a,
142 QWidget* w)
143 : QObject(w)
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400144{
Edric Milarete0855a92015-06-12 11:38:24 -0400145 static QHash<QByteArray, int> reverse;
146 if (reverse.isEmpty()) {
147 const QHash<int, QByteArray> a = AccountModel::instance()->roleNames();
148 for (QHash<int, QByteArray>::const_iterator i = a.begin(); i != a.end(); ++i) {
149 reverse[i.value()] = i.key();
150 }
151 }
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400152
Edric Milarete0855a92015-06-12 11:38:24 -0400153 drill(w, a, reverse);
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400154}
155
156AccountSerializationAdapter::~AccountSerializationAdapter()
Edric Milarete0855a92015-06-12 11:38:24 -0400157{}