~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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* config.vala
 *
 * 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 Gee;

namespace People {

	public class ConfigXml : Object, Config {

		private class ContactSource : Object {
			public string id;
			public string @class;
			public Gee.Map<string, string> parameters =
					new HashMap<string, string> (str_hash, str_equal, str_equal);
		}

		private class MetaContact : Object {
			public Gee.List<string> ids = new Gee.ArrayList<string> (str_equal);
		}

		private class AddressBook : Object {
			public string id;
			public Gee.List<ContactSource> contact_sources =
					new Gee.ArrayList<ContactSource> ();
			public Gee.Set<MetaContact> meta_contacts =
					new Gee.HashSet<MetaContact> ();
		}

		private Gee.Map<string, ContactSource> contact_sources;
		private ContactSource? current_contact_source;
		private string? current_parameter;

		private Gee.Map<string, AddressBook> address_books;
		private AddressBook? current_address_book;
		private MetaContact? current_meta_contact;

		private Gee.List<string> elements_stack = new Gee.ArrayList<string>(str_equal);

		public string config_path {
			get;
			construct;
		}

		public string data_dir {
			get;
			set construct;
		}

		construct {
			string content;
			ulong length;

			try {
				FileUtils.get_contents(config_path, out content, out length);

				contact_sources = new HashMap<string, ContactSource> (str_hash, str_equal);
				address_books = new HashMap<string, ContactSource> (str_hash, str_equal);

				MarkupParser parser = {start_element, end_element, text, null, null};
				MarkupParseContext context = new MarkupParseContext (parser,
						MarkupParseFlags.TREAT_CDATA_AS_TEXT, this, null);

				try {
					context.parse(content, (long) length);
				} catch (MarkupError e) {
					warning("Failed to parse config file: %s (%s)", config_path, e.message);
					contact_sources = new HashMap<string, ContactSource> (str_hash, str_equal);
					address_books = new HashMap<string, ContactSource> (str_hash, str_equal);
				}
			} catch (FileError e) {
				warning("Failed to load config file: %s (%s)", config_path, e.message);
			}
		}

		public ConfigXml(string config_path, string data_dir) {
			this.config_path = config_path;
			this.data_dir = data_dir;
		}

		public Gee.Set<string> list_contact_sources() {
			Gee.Set<string> result = new Gee.HashSet<string>(str_hash, str_equal);
			foreach (string source_id in contact_sources.get_keys())
				result.add (source_id);
			return result;
		}

		public string? get_contact_source_class(string source_id) {
			if (!contact_sources.contains(source_id))
				return null;
			return contact_sources.get(source_id).@class;
		}

		public Gee.Map<string, string>? get_contact_source_parameters(string source_id) {
			if (!contact_sources.contains(source_id))
				return null;
			return new Gee.ReadOnlyMap<string, string> (contact_sources.get(source_id).parameters);
		}

		public Gee.Set<string> list_address_books() {
			return address_books.get_keys();
		}

		public Gee.List<string>? get_address_book_sources(string address_book_id) {
			if (!address_books.contains(address_book_id))
				return null;

			Gee.List<string> result = new Gee.ArrayList<string> ();
			AddressBook book = address_books.get (address_book_id);
			foreach (ContactSource source in book.contact_sources) {
				result.add (source.id);
			}
			return result;
		}

		public Gee.Set<Gee.List<string>>? get_address_book_meta_contacts (string address_book_id) {
			if (!address_books.contains (address_book_id))
				return null;

			Gee.Set<Gee.List<string>> result = new Gee.HashSet<Gee.List<string>> ();
			AddressBook book = address_books.get (address_book_id);
			foreach (MetaContact contact in book.meta_contacts) {
				result.add (contact.ids);
			}
			return result;
		}

		private void start_element(MarkupParseContext context,
				string element_name,
				string[] attribute_names, string[] attribute_values) throws MarkupError {
			this.elements_stack.insert(0, element_name);

			string parent = null;
			if (this.elements_stack.size > 1)
				parent = this.elements_stack.get(1);

			if (element_name == "contact-source") {
				if (parent == "contact-sources") {
					uint i = 0;
					ContactSource contact_source = new ContactSource();

					foreach (string attribute in attribute_names) {
						if (attribute == "id")
							contact_source.id = attribute_values[i];
						else if (attribute == "class")
							contact_source.@class = attribute_values[i];
						i++;
					}
					if (contact_source.id != null && contact_source.@class != null) {
						this.current_contact_source = contact_source;
						this.contact_sources.set(contact_source.id, contact_source);
					}
				} else if (parent == "address-book") {
					uint i = 0;
					string id = null;

					foreach (string attribute in attribute_names) {
						if (attribute == "id")
							id = attribute_values[i];
						i++;
					}

					if (id != null && this.contact_sources.contains(id)) {
						var contact_source = this.contact_sources.get(id);
						this.current_address_book.contact_sources.add(contact_source);
					}
				}
			} else if (element_name == "meta-contact") {
				if (parent == "address-book") {
					MetaContact meta_contact = new MetaContact ();
					this.current_meta_contact = meta_contact;
					this.current_address_book.meta_contacts.add (meta_contact);
				}
			} else if (element_name == "contact") {
				if (parent == "meta-contact") {
					uint i = 0;
					string source = null;
					string id = null;

					foreach (string attribute in attribute_names) {
						if (attribute == "source") {
							source = attribute_values[i];
						} else if (attribute == "id") {
							id = attribute_values[i];
						}
						i++;
					}

					if (source != null && id != null && this.contact_sources.contains (source)) {
						this.current_meta_contact.ids.add ("%s:%s".printf (source, id));
					}
				}
			} else if (element_name == "param") {
				if (parent == "contact-source") {
					uint i = 0;
					string name = null;

					foreach (string attribute in attribute_names) {
						if (attribute == "name")
							name = attribute_values[i];
						i++;
					}

					this.current_parameter = name;
				}
			} else if (element_name == "address-book") {
				if (parent == "address-books") {
					uint i = 0;
					AddressBook address_book = new AddressBook();

					foreach (string attribute in attribute_names) {
						if (attribute == "id")
							address_book.id = attribute_values[i];
						i++;
					}

					if (address_book.id != null) {
						this.address_books.set(address_book.id, address_book);
						this.current_address_book = address_book;
					}
				}
			}
		}

		private void end_element(MarkupParseContext context, string element_name) throws MarkupError {
			this.elements_stack.remove_at(0);

			if (element_name == "contact-source") {
				this.current_contact_source = null;
			} else if (element_name == "param") {
				this.current_parameter = null;
			} else if (element_name == "address-book") {
				this.current_address_book = null;
			} else if (element_name == "meta-contact") {
				this.current_meta_contact = null;
			}
		}

		private void text(MarkupParseContext context,
				string text, ulong text_len) throws MarkupError {
			if (this.current_parameter != null) {
				if (this.current_parameter.has_suffix ("-path") && !text.has_prefix ("/")) {
					string path = Path.build_filename (this.data_dir, text);
					this.current_contact_source.parameters.set(this.current_parameter, path);
				} else {
					this.current_contact_source.parameters.set(this.current_parameter, text);
				}
			}
		}
	}
}