Commit f06a3e76 authored by Jonah Brüchert's avatar Jonah Brüchert 🌳

Implement adding instant messenger addresses

parent 904d305f
qt5_add_resources(RESOURCES resources.qrc)
add_executable(plasma-phonebook main.cpp phonesmodel.cpp declarativeaddressee.cpp contactimporter.cpp ${RESOURCES})
add_executable(plasma-phonebook
main.cpp
phonesmodel.cpp
declarativeaddressee.cpp
contactimporter.cpp
imppmodel.cpp
${RESOURCES})
target_link_libraries(plasma-phonebook
Qt5::Core Qt5::Qml Qt5::Quick Qt5::Svg
KF5::Contacts KF5::People KF5::I18n
......
......@@ -124,7 +124,7 @@ Kirigami.ScrollablePage {
Controls.Button {
icon.name: "list-remove"
onClicked: {
addressee.phoneNumbers.removePhoneNumber(phoneField.text)
addressee.phoneNumbers.removePhoneNumber(display)
}
}
}
......@@ -180,7 +180,7 @@ Kirigami.ScrollablePage {
}
Controls.Button {
icon.name: "list-remove"
onClicked: addressee.removeEmail(textField.text)
onClicked: addressee.removeEmail(modelData)
}
}
}
......@@ -210,5 +210,64 @@ Kirigami.ScrollablePage {
}
}
}
Kirigami.Separator {
Layout.fillWidth: true
}
ColumnLayout {
id: impp
Kirigami.FormData.label: i18n("Instant Messenger:")
Repeater {
model: addressee.impps
delegate: RowLayout {
Controls.TextField {
id: imppField
text: display
onAccepted: {
display = text
}
Connections {
target: root;
onSave: imppField.accepted()
}
}
Controls.Button {
icon.name: "list-remove"
onClicked: {
addressee.impps.removeImpp(display)
}
}
}
}
RowLayout {
Controls.TextField {
id: toAddImpp
placeholderText: i18n("xmpp:person@example.com")
}
// add last text field on save()
Connections {
target: root;
onSave: {
if (toAddImpp.text !== "")
addressee.impps.addImpp(toAddImpp.text)
}
}
// button to add additional text field
Controls.Button {
icon.name: "list-add"
enabled: toAddImpp.text.length > 0
onClicked: {
addressee.impps.addImpp(toAddImpp.text)
toAddImpp.text = ""
}
}
}
}
}
}
......@@ -24,6 +24,7 @@
#include <QCryptographicHash>
#include <QFile>
#include "phonesmodel.h"
#include "imppmodel.h"
void Addressee::setRaw(const QByteArray& raw)
{
......@@ -47,9 +48,15 @@ PhonesModel* Addressee::phoneNumbers() const
return m_phonesModel;
}
ImppModel* Addressee::impps() const
{
return m_imppModel;
}
Addressee::Addressee(QObject* parent)
: QObject(parent)
, m_phonesModel(new PhonesModel(this))
, m_imppModel(new ImppModel(this))
{
connect(this, &Addressee::nameChanged, this, &Addressee::anyNameChanged);
}
......@@ -30,6 +30,7 @@
Q_SIGNAL void name##Changed(const type& name);
class PhonesModel;
class ImppModel;
class Addressee : public QObject
{
......@@ -50,6 +51,9 @@ public:
Q_PROPERTY(PhonesModel* phoneNumbers READ phoneNumbers CONSTANT)
PhonesModel* phoneNumbers() const;
Q_PROPERTY(ImppModel* impps READ impps CONSTANT)
ImppModel* impps() const;
Q_PROPERTY(QStringList emails READ emails WRITE setEmails NOTIFY emailsChanged)
PROPERTY(QStringList, emails, setEmails)
......@@ -73,8 +77,10 @@ Q_SIGNALS:
private:
friend class PhonesModel;
friend class ImppModel;
KContacts::Addressee m_addressee;
PhonesModel* m_phonesModel = nullptr;
ImppModel* m_imppModel = nullptr;
};
#endif
/*
* Copyright 2019 Aleix Pol Gonzalez <aleixpol@kde.org>
* Copyright 2019 Jonah Brüchert <jbb@kaidan.im>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "imppmodel.h"
#include "declarativeaddressee.h"
#include <QDebug>
ImppModel::ImppModel(Addressee* a)
: QAbstractListModel(a)
, m_addressee(a)
{
connect(a, &Addressee::urlChanged, this, [this] {
beginResetModel();
endResetModel();
});
}
QVariant ImppModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid() || index.row() < 0 || index.row() >= m_addressee->m_addressee.imppList().count()) {
return {};
}
switch(role) {
case Qt::DisplayRole:
return m_addressee->m_addressee.imppList()[index.row()].address();
}
return {};
}
bool ImppModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
if (!index.isValid() || index.row() < 0 || index.row() >= m_addressee->m_addressee.imppList().count()) {
return false;
}
switch(role) {
case Qt::DisplayRole: {
auto impps = m_addressee->m_addressee.imppList();
impps[index.row()].setAddress(value.toString());
m_addressee->m_addressee.setImppList(impps);
dataChanged(index, index, {Qt::DisplayRole});
} return true;
}
return false;
}
int ImppModel::rowCount(const QModelIndex& parent) const
{
return parent.isValid() ? 0 : m_addressee->m_addressee.imppList().count();
}
void ImppModel::addImpp(const QString& address)
{
const int c = rowCount();
// Construct IMPP
auto impp = KContacts::Impp();
impp.setAddress(address);
beginInsertRows({}, c, c);
m_addressee->m_addressee.insertImpp(impp);
endInsertRows();
}
void ImppModel::removeImpp(const QString &address)
{
for (int i = 0; 0 < m_addressee->m_addressee.imppList().count(); i++) {
if (m_addressee->m_addressee.imppList()[i].address() == address) {
beginRemoveRows({}, i, i);
m_addressee->m_addressee.imppList().remove(i);
break;
}
}
endRemoveRows();
}
/*
* Copyright 2019 Aleix Pol Gonzalez <aleixpol@kde.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef IMPPMODEL_H
#define IMPPMODEL_H
#include <QAbstractListModel>
class Addressee;
class ImppModel : public QAbstractListModel
{
Q_OBJECT
public:
ImppModel(Addressee* a);
bool setData(const QModelIndex & index, const QVariant & value, int role) override;
QVariant data(const QModelIndex & index, int role) const override;
int rowCount(const QModelIndex & parent = {}) const override;
Q_SCRIPTABLE void addImpp(const QString &number);
Q_SCRIPTABLE void removeImpp(const QString &number);
private:
Addressee* m_addressee;
};
#endif
......@@ -27,6 +27,7 @@
#include "declarativeaddressee.h"
#include "phonesmodel.h"
#include "imppmodel.h"
#include "contactimporter.h"
......@@ -44,6 +45,7 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
qmlRegisterType<Addressee>("org.kde.kcontacts", 1, 0, "Addressee");
qmlRegisterUncreatableType<PhonesModel>("org.kde.kcontacts", 1, 0, "PhonesModel", "Get it from the Addressee");
qmlRegisterUncreatableType<ImppModel>("org.kde.kcontacts", 1, 0, "ImppModel", "Get it from the Addressee");
qmlRegisterType<ContactImporter>("org.kde.phonebook", 1, 0, "ContactImporter");
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment