Commit 1dfc0650 authored by Jonah Brüchert's avatar Jonah Brüchert 🌳

Implement importing contacts

parent 0900ea14
qt5_add_resources(RESOURCES resources.qrc)
add_executable(plasma-phonebook main.cpp phonesmodel.cpp declarativeaddressee.cpp ${RESOURCES})
add_executable(plasma-phonebook main.cpp phonesmodel.cpp declarativeaddressee.cpp contactimporter.cpp ${RESOURCES})
target_link_libraries(plasma-phonebook
Qt5::Core Qt5::Qml Qt5::Quick Qt5::Svg
KF5::Contacts
KF5::Contacts KF5::People
)
install(TARGETS plasma-phonebook ${KF5_INSTALL_TARGETS_DEFAULT_ARGS})
......
#include "contactimporter.h"
#include <QDebug>
#include <QFile>
#include <KContacts/VCardConverter>
#include <KPeople/PersonPluginManager>
ContactImporter::ContactImporter(QObject *parent) : QObject(parent)
{
}
void ContactImporter::importVCards(const QUrl &path)
{
QFile inputFile(path.toLocalFile());
if (!inputFile.open(QIODevice::ReadOnly)) {
qWarning() << "Couldn't read vCard to import: Couldn't open file for reading";
return;
}
auto const importedVCards = m_converter.parseVCards(inputFile.readAll());
for (const auto &adr : importedVCards) {
QVariantMap properties;
properties[QStringLiteral("vcard")] = m_converter.exportVCard(adr, KContacts::VCardConverter::v3_0);
KPeople::PersonPluginManager::addContact(properties);
}
}
#ifndef CONTACTIMPORTER_H
#define CONTACTIMPORTER_H
#include <QObject>
#include <KContacts/VCardConverter>
class ContactImporter : public QObject
{
Q_OBJECT
public:
explicit ContactImporter(QObject *parent = nullptr);
Q_INVOKABLE void importVCards(const QUrl &path);
private:
KContacts::VCardConverter m_converter;
};
#endif // CONTACTIMPORTER_H
......@@ -23,14 +23,29 @@
import QtQuick 2.7
import QtQuick.Controls 2.0 as Controls
import QtQuick.Layouts 1.7
import QtQuick.Dialogs 1.0
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.kirigami 2.9 as Kirigami
import org.kde.people 1.0 as KPeople
import org.kde.phonebook 1.0
Kirigami.ScrollablePage {
title: i18n("Address book")
FileDialog {
id: importFileDialog
selectMultiple: false
selectExisting: true
onAccepted: importer.importVCards(fileUrl)
}
ContactImporter {
id: importer
}
// Brighter background color
Kirigami.Theme.colorSet: Kirigami.Theme.View
background: Rectangle {
......@@ -45,6 +60,16 @@ Kirigami.ScrollablePage {
pageStack.push(Qt.resolvedUrl("AddContactPage.qml"), {state: "create"})
}
}
contextualActions: [
Kirigami.Action {
icon.name: "document-import"
text: i18n("Import contacts")
onTriggered: {
importFileDialog.open()
}
}
]
}
Controls.Label {
......
......@@ -22,9 +22,11 @@
#include <QQmlApplicationEngine>
#include <QtQml>
#include <QUrl>
#include "declarativeaddressee.h"
#include "declarativeaddressee.h"
#include "phonesmodel.h"
#include "contactimporter.h"
Q_DECL_EXPORT int main(int argc, char *argv[])
{
......@@ -39,6 +41,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");
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