~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
/* address-book-manager.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;

using People.Utils;


namespace People {

	public class AddressBookManager : Object {

		public Config config {
			get;
			set construct;
		}

		private Map<string, Backend.ContactSourceFactory> backends;
		private Map<string, AddressBook> address_books;

		construct {
			backends = new HashMap<string, Backend.ContactSourceFactory>(
					str_hash, str_equal);
			backends.set("dummy", new Backend.Dummy.ContactSourceFactory());
			backends.set("keyfile", new Backend.Keyfile.ContactSourceFactory());
			backends.set("sqlite", new Backend.Sqlite.ContactSourceFactory());
			backends.set("lastfm", new Backend.Lastfm.ContactSourceFactory());
			backends.set("friendfeed", new Backend.Friendfeed.ContactSourceFactory());
			backends.set("google", new Backend.Google.ContactSourceFactory());

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

			foreach (string address_book_id in config.list_address_books()) {
				var source_ids = config.get_address_book_sources(address_book_id);
				Map<string, Backend.ContactSource> sources =
					new HashMap<string, Backend.ContactSource>(str_hash, str_equal);

				foreach (string source_id in source_ids) {
					var source_class = config.get_contact_source_class(source_id);
					var source_parameters = config.get_contact_source_parameters(source_id);
					var factory = backends.get(source_class);
					if (factory == null) {
						warning ("No factory available for contact source '%s'", source_class);
					} else {
						try {
							var source = factory.request_contact_source(source_parameters).get_result ();
							sources.set(source_id, new ProxyContactSource (source));
						} catch (Backend.Error e) {
							warning("Failed to request contact source '%s' (%s)",
								source_id, e.message);
						}
					}
				}

				var address_book = new AddressBook(
						new BiMap<string, Backend.ContactSource>(sources),
						config.get_address_book_meta_contacts (address_book_id));
				address_books.set(address_book_id, address_book);
			}
		}

		public AddressBookManager(Config config) {
			this.config = config;
		}

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

		public AddressBook get_address_book(string address_book_id) {
			return address_books.get(address_book_id);
		}
	}
}