summaryrefslogtreecommitdiff
path: root/src/core/contacts-nickname-chunk.vala
diff options
context:
space:
mode:
authorNiels De Graef <nielsdegraef@gmail.com>2022-08-16 12:36:08 +0200
committerNiels De Graef <nielsdegraef@gmail.com>2022-09-03 08:50:19 +0200
commit1d44c11483e3d00611c0beed9afc8f2c9facc3a8 (patch)
tree6a38fecd4acca16fb8c6d1b4322b2e10fbe0d5ad /src/core/contacts-nickname-chunk.vala
parent4d0710b8a6c7a3cd2ee0311b62f5e2707c34c305 (diff)
downloadgnome-contacts-1d44c11483e3d00611c0beed9afc8f2c9facc3a8.tar.gz
Introduce the concept of Contacts.Chunk
This commit introduces a new class `Contacts.Chunk`. Just like libfolks, we see a contact as a collection of data, or to word it differently: a collection built up from "chunks" of information. The net result of adding this concept adds quite a bit of lines of code, but it does have some major benefits: * Rather than stuffing new properties into yet another if-else spread out over multiple places in contacts-utils (and quite a bit of other files), we can create a new subclass of `Contacts.Chunk` * This also goes for property-specific logic, which we can consolidate within their appropriate classes/files. * All of our logic is now unit-testable In the future, this would allow for more cleanups/features: * We can put the serialization code for each property inside the `Contacts.Chunk` * We can extend ContactSheet to show a vCard's information, before actually importing it into a Folks.Individual. * We can write unit tests on the set of chunks, rather than regularly having to deal with yet another regression in e.g. the birthday editor.
Diffstat (limited to 'src/core/contacts-nickname-chunk.vala')
-rw-r--r--src/core/contacts-nickname-chunk.vala60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/core/contacts-nickname-chunk.vala b/src/core/contacts-nickname-chunk.vala
new file mode 100644
index 0000000..ba505f0
--- /dev/null
+++ b/src/core/contacts-nickname-chunk.vala
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2022 Niels De Graef <nielsdegraef@gmail.com>
+ *
+ * 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) any later version.
+ *
+ * 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/>.
+ */
+
+using Folks;
+
+/**
+ * A {@link Chunk} that represents the nickname of a contact.
+ */
+public class Contacts.NicknameChunk : Chunk {
+
+ public string nickname {
+ get { return this._nickname; }
+ set {
+ if (this._nickname == value)
+ return;
+
+ bool was_empty = this.is_empty;
+ this._nickname = value;
+ notify_property ("nickname");
+ if (this.is_empty != was_empty)
+ notify_property ("is-empty");
+ }
+ }
+ private string _nickname = "";
+
+ public override string property_name { get { return "nickname"; } }
+
+ public override bool is_empty { get { return this._nickname.strip () == ""; } }
+
+ construct {
+ if (persona != null) {
+ return_if_fail (persona is NameDetails);
+ persona.bind_property ("nickname", this, "nickname", BindingFlags.SYNC_CREATE);
+ }
+ }
+
+ public override Value? to_value () {
+ return this.nickname;
+ }
+
+ public override async void save_to_persona () throws GLib.Error
+ requires (this.persona is NameDetails) {
+
+ yield ((NameDetails) this.persona).change_nickname (this.nickname);
+ }
+}