~people-project/people-glib/trunk

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
119
120
/* contact.vala
 *
 * Copyright (C) 2009 Johann Prieur <johann.prieur@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
 *
 */


namespace People {
	[DBus (name = "net.launchpad.People.Contact")]
	private interface IContact : DBus.Object {
		//public abstract string id { get; }
		public abstract string get_identifier ();

		public abstract signal void defined_fields_retrieved (string[] fields);
		public abstract signal void fields_retrieved (HashTable<string, string> fields);

		public abstract void request_defined_fields ();
		public abstract void request_fields (string[] fields);
	}

	public class Contact : Object {
		public enum State {
			EMPTY,
			FETCHING,
			FETCHED;
		}

		public DBus.Connection connection {
			private get;
			construct;
		}

		public string bus_name {
			private get;
			construct;
		}

		public string object_path {
			private get;
			construct;
		}

		public string id {
			get {
				if (_id == null)
					_id = contact.get_identifier ();
				return _id;
			}
		}

		public State state {
			get;
			private set;
		}

		private IContact contact;
		private string _id = null;
		private HashTable<string, string> fields;

		construct {
			this.contact = connection.get_object (bus_name, object_path) as IContact;
			this.contact.defined_fields_retrieved += on_defined_fields_retrieved;
			this.contact.fields_retrieved += on_fields_retrieved;

			this.fields = new HashTable<string, string> (str_hash, str_equal);
		}

		public Contact (DBus.Connection connection, string bus_name, string object_path) {
			this.connection = connection;
			this.bus_name = bus_name;
			this.object_path = object_path;
		}

		public List<string> get_fields () {
			return this.fields.get_keys ();
		}

		public weak string? get (string field_id) {
			return this.fields.lookup (field_id);
		}

		public void fetch () requires (state == State.EMPTY) {
			this.state = State.FETCHING;
			this.contact.request_defined_fields ();
		}

		public void request_fields (string[] fields) {
			this.contact.request_fields (fields);
		}

		private void on_defined_fields_retrieved (IContact contact, string[] fields) {
			if (state == State.FETCHING) {
				this.request_fields (fields);
			}
		}

		private void on_fields_retrieved (IContact contact, HashTable<string, string> fields) {
			if (state == State.FETCHING) {
				foreach (string field_name in fields.get_keys ()) {
					this.fields.insert (field_name, fields.lookup (field_name));
				}
				state = State.FETCHED;
			}
		}
	}
}