Verified Commit 7fbae42c authored by Jonah Brüchert's avatar Jonah Brüchert 🌳

Add feature to update existing contacts

parent 85a960d9
......@@ -34,28 +34,6 @@ Kirigami.SwipeListItem {
property var icon
property string personUri
actions: [
Kirigami.Action {
icon.name: "mail-message"
onTriggered: {
personActions.triggerAction(KPeople.TextChatAction)
}
},
Kirigami.Action {
icon.name: "call-start"
onTriggered: {
personActions.triggerAction(KPeople.AudioCallAction)
}
},
Kirigami.Action {
icon.name: "list-remove"
onTriggered: {
phonebook.deleteContact(personUri)
}
}
]
RowLayout {
KPeople.PersonData {
id: personData
......
......@@ -46,6 +46,7 @@ Kirigami.ScrollablePage {
main: Kirigami.Action {
icon.name: "contact-new-symbolic"
onTriggered: {
form.state = "create"
form.open()
}
}
......@@ -100,7 +101,37 @@ Kirigami.ScrollablePage {
icon: model.decoration
personUri: model.personUri
actions: [
Kirigami.Action {
icon.name: "mail-message"
onTriggered: {
personActions.triggerAction(KPeople.TextChatAction)
}
},
Kirigami.Action {
icon.name: "call-start"
onTriggered: {
personActions.triggerAction(KPeople.AudioCallAction)
}
},
Kirigami.Action {
icon.name: "list-remove"
onTriggered: {
phonebook.deleteContact(personUri)
}
},
Kirigami.Action {
icon.name: "document-edit"
onTriggered: {
form.state = "update"
form.personUri = personUri
form.open()
}
}
]
onClicked: {
console.log(phonebook.getContact(personUri))
pageStack.push(detailPage, {
personUri: model.personUri
})
......
......@@ -87,6 +87,7 @@ Flickable {
}
ListView {
id: actionsListView
anchors.top: header.bottom
width: parent.width
model: personActions
......
......@@ -24,9 +24,19 @@ import QtGraphicalEffects 1.0
ColumnLayout {
id: root
property var model;
spacing: 2 * Kirigami.Units.largeSpacing
property string personUri
states: [
State {
name: "create"
},
State {
name: "update"
}
]
/*HIG.Header {
id: header
content.anchors.leftMargin: Kirigami.Units.largeSpacing
......@@ -86,12 +96,24 @@ ColumnLayout {
Kirigami.FormData.isSection: true
}
}
Button {
text: "Save"
Layout.alignment: Qt.AlignRight
anchors.rightMargin: Kirigami.Units.largeSpacing
onClicked: {
phonebook.addContact(firstname.text + " " + lastname.text, phoneNumber.text, email.text)
if (root.state === "create") {
phonebook.addContact(firstname.text + " " + lastname.text, phoneNumber.text, email.text)
}
else if (root.state === "update") {
phonebook.updateContact(root.personUri, firstname.text + " " + lastname.text, phoneNumber.text, email.text)
}
firstname.text = ""
lastname.text = ""
phoneNumber.text = ""
email.text = ""
formSheet.close()
}
}
......
......@@ -27,11 +27,15 @@ Kirigami.OverlaySheet {
id: formSheet
Layout.fillWidth: true
property alias state: form.state
property alias personUri: form.personUri
background: Rectangle {
color: Kirigami.Theme.backgroundColor
}
Form {
id: form
}
/*actions {
......
......@@ -38,13 +38,19 @@ void PhoneBook::addContact(QString name, QString tel, QString email)
// addresses
Addressee adr;
adr.setName(name);
adr.setEmails(QStringList(email));
PhoneNumber::List phoneNums;
PhoneNumber phoneNum;
phoneNum.setNumber(tel);
phoneNum.setType(PhoneNumber::Cell);
phoneNums.append(phoneNum);
adr.setPhoneNumbers(phoneNums);
if (!email.isEmpty()) {
adr.setEmails(QStringList(email));
}
if (!tel.isEmpty()) {
PhoneNumber::List phoneNums;
PhoneNumber phoneNum;
phoneNum.setNumber(tel);
phoneNum.setType(PhoneNumber::Cell);
phoneNums.append(phoneNum);
adr.setPhoneNumbers(phoneNums);
}
// create vcard
VCardConverter converter;
......@@ -67,5 +73,54 @@ void PhoneBook::addContact(QString name, QString tel, QString email)
void PhoneBook::deleteContact(QString personUri)
{
QFile::remove(personUri.replace("vcard:/", ""));
if (!(QUrl(personUri).scheme() == "vcard")) {
qWarning() << "uri of contact to remove is not a vcard, cannot remove.";
return;
}
QFile::remove(personUri.remove("vcard:/"));
}
void PhoneBook::updateContact(QString personUri, QString name, QString tel, QString email)
{
if (!(QUrl(personUri).scheme() == "vcard")) {
qWarning() << "uri of contact to update is not a vcard, cannot update.";
return;
}
QFile file(personUri.remove("vcard:/"));
if (!(file.exists())) {
qWarning() << "Can't read vcard, file doesn't exist";
return;
}
if (!file.open(QIODevice::ReadWrite | QIODevice::Truncate)) {
qWarning() << "Couldn't update vCard: Couldn't open file for reading / writing.";
return;
}
VCardConverter converter;
Addressee adr = converter.parseVCard(file.readAll());
if (!name.isEmpty()) {
adr.setName(name);
}
if (!tel.isEmpty()) {
PhoneNumber::List phoneNums;
PhoneNumber phoneNum;
phoneNum.setNumber(tel);
phoneNum.setType(PhoneNumber::Cell);
phoneNums.append(phoneNum);
adr.setPhoneNumbers(phoneNums);
}
if (!email.isEmpty()) {
adr.setEmails(QStringList(email));
}
QByteArray vcard = converter.createVCard(adr);
qDebug() << vcard;
file.write(vcard);
file.close();
}
......@@ -34,6 +34,7 @@ signals:
public slots:
void addContact(QString name, QString tel, QString email);
void deleteContact(QString personUri);
void updateContact(QString personUri, QString name, QString tel, QString email);
};
#endif // PHONEBOOK_H
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