~people-project/people-core/deep-refactoring

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* base-contact-source.vala
 *
 * Copyright (C) 2008 Johann Prieur <johann.prieur@gmail.com>
 * Copyright (C) 2008 Ali Sabil <ali.sabil@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 */

using Core.Container;
using Core.Async;

namespace People.Backend {
	/**
	 * BaseContactSource is an abstract class implementing the
	 * {@link ContactSource} interface.
	 * <p>
	 * The user of this class MUST implement {@link BaseContactSource.validate_contact_id}
	 * and {@link BaseContactSource.create_contact_object}.
	 */
	public abstract class BaseContactSource : Object, ContactSource {

		private ContactManager contact_manager = new ContactManager ();
		private ResultManager result_manager = new ResultManager ();
		private Map<AsyncResult<bool>, string> validation_map = new HashMap<AsyncResult<bool>, string> ();

		/* ContactSource interface */
		public ContactSourceFlags flags {
			get;
			protected set;
			default = ContactSourceFlags.NONE;
		}

		public AsyncResult<Contact> request_contact (string? id) {
			if (id == null && !(ContactSourceFlags.CAN_CREATE_CONTACT in flags)) {
				var error = new Error.NOT_SUPPORTED ("Contact creation not supported by this backend");
				return new ImmediateResult<Contact>.with_error (error);
			}

			if (id == null) {
				return new ImmediateResult<Contact> (get_contact_object (null));
			} else {
				var validation = validate_contact_id (id);
				validation_map.set (validation, id);
				var result = new ResultChain<Contact> (validation);
				result.add_transform ((async_result) => {
					try {
						bool valid_id = (bool) async_result.get ();
						string contact_id = validation_map.get (async_result);
						validation_map.remove (async_result);
						if (valid_id)
							return new ImmediateResult<Contact> (get_contact_object (contact_id));
						else
							return new ImmediateResult<Contact>.with_error (new Error.INVALID_ARGUMENT ("Requested a contact with an unknown ID: " + contact_id));
					} catch (Error e) {
						return new ImmediateResult<Contact>.with_error (e);
					}
				});
				result_manager.add (result);
				return result;
			}
		}
		/* end of ContactSource interface */

		protected Contact get_contact_object (string? id) {
			Contact contact = null;

			if (id != null) {
				contact = contact_manager.request_contact (id);
			}

			if (contact == null) {
				contact = create_contact_object (id);
				register_contact (contact);
			}

			return contact;
		}

		protected void register_contact (Contact contact) {
			if (contact_manager.register_contact (contact)) {
				contact.notify["id"] += contact_state_changed;
			}
		}

		/**
		 * The user of this class MUST implement this method to allow the validation
		 * and checking for the existence of the contact ID.
		 */
		protected abstract AsyncResult<bool> validate_contact_id (string id);

		/**
		 * The user of this class MUST implement this method returning a Contact object
		 * corresponding to the contact ID. The implementation MUST NOT check for the validity
		 * of the given ID.
		 */
		protected abstract Contact create_contact_object (string? id);

		private void contact_state_changed (Contact contact, ParamSpec pspec) {
			if (contact.id == null)
				contact_removed (contact);
			else
				contact_added (contact);
		}
	}
}