1 // *************************************************************************
4 // * File: gsm_sorted_phonebook_base.cc
6 // * Purpose: Virtual base class for alphabetically sorted phonebook
7 // * The infrastructure in this module allows custom backends for
8 // * storing phonebook entries to be integrated into gsmlib
9 // * (eg. LDAP- or RDBMS-based phonebook stores).
11 // * Author: Peter Hofmann (software@pxh.de)
13 // * Created: 5.6.2000
14 // *************************************************************************
17 #include <gsm_config.h>
19 #include <gsmlib/gsm_sorted_phonebook_base.h>
20 #include <gsmlib/gsm_error.h>
21 #include <gsmlib/gsm_util.h>
22 #include <gsmlib/gsm_nls.h>
27 using namespace gsmlib;
29 // PhonebookEntryBase members
31 void PhonebookEntryBase::set(string telephone, string text, int index,
35 checkTextAndTelephone(text, telephone);
38 _telephone = telephone;
45 bool PhonebookEntryBase::operator==(const PhonebookEntryBase &e) const
47 assert(! ((_useIndex || e._useIndex) &&
48 (_index == -1 || e._index == -1)));
49 return _telephone == e._telephone && _text == e._text &&
50 (! (_useIndex || e._useIndex) || _index == e._index);
53 string PhonebookEntryBase::text() const throw(GsmException)
58 string PhonebookEntryBase::telephone() const throw(GsmException)
63 bool PhonebookEntryBase::empty() const throw(GsmException)
65 return (text() == "") && (telephone() == "");
68 Ref<PhonebookEntryBase> PhonebookEntryBase::clone()
70 Ref<PhonebookEntryBase> result = new PhonebookEntryBase(*this);
74 PhonebookEntryBase::PhonebookEntryBase(const PhonebookEntryBase &e)
77 set(e._telephone, e._text, e._index, e._useIndex);
80 PhonebookEntryBase &PhonebookEntryBase::operator=(const PhonebookEntryBase &e)
83 set(e._telephone, e._text, e._index, e._useIndex);
87 // CustomPhonebookRegistry members
89 map<string, CustomPhonebookFactory*>
90 *CustomPhonebookRegistry::_factoryList = NULL;
92 void CustomPhonebookRegistry::
93 registerCustomPhonebookFactory(string backendName,
94 CustomPhonebookFactory *factory)
97 if (_factoryList == NULL)
98 _factoryList = new map<string, CustomPhonebookFactory*>;
99 backendName = lowercase(backendName);
100 if (_factoryList->find(backendName) != _factoryList->end())
101 throw GsmException(stringPrintf(_("backend '%s' already registered"),
102 backendName.c_str()), ParameterError);
105 SortedPhonebookRef CustomPhonebookRegistry::
106 createPhonebook(string backendName, string source) throw(GsmException)
108 if (_factoryList == NULL)
109 _factoryList = new map<string, CustomPhonebookFactory*>;
110 backendName = lowercase(backendName);
111 if (_factoryList->find(backendName) == _factoryList->end())
112 throw GsmException(stringPrintf(_("backend '%s' not registered"),
113 backendName.c_str()), ParameterError);
114 return (*_factoryList)[backendName]->createPhonebook(source);