1 /*
2  * ContactList
3  *
4  * Copyright (C) 2001 Barnaby Gray <barnaby@beedesign.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
19  *
20  */
21 
22 #include "ContactList.h"
23 #include "events.h"
24 
25 using std::string;
26 
27 namespace ICQ2000 {
28 
ContactList()29   ContactList::ContactList() { }
30 
ContactList(const ContactList & cl)31   ContactList::ContactList(const ContactList& cl)
32     : m_cmap(cl.m_cmap)
33   { }
34 
ContactList(ContactRef ct)35   ContactList::ContactList(ContactRef ct)
36   {
37     add(ct);
38   }
39 
operator [](unsigned int uin)40   ContactRef ContactList::operator[](unsigned int uin)
41   {
42     return lookup_uin(uin);
43   }
44 
lookup_uin(unsigned int uin)45   ContactRef ContactList::lookup_uin(unsigned int uin)
46   {
47     if (m_cmap.count(uin) != 0) return (*(m_cmap.find(uin))).second;
48     return NULL;
49   }
50 
lookup_mobile(const string & m)51   ContactRef ContactList::lookup_mobile(const string& m)
52   {
53     iterator curr = begin();
54     while (curr != end()) {
55       if ((*curr)->getNormalisedMobileNo() == m) return (*curr);
56       ++curr;
57     }
58 
59     return NULL;
60   }
61 
lookup_email(const string & em)62   ContactRef ContactList::lookup_email(const string& em)
63   {
64     iterator curr = begin();
65     while (curr != end()) {
66       if ((*curr)->getEmail() == em) return (*curr);
67       ++curr;
68     }
69 
70     return NULL;
71   }
72 
add(ContactRef ct)73   ContactRef ContactList::add(ContactRef ct) {
74     m_cmap.insert( std::make_pair(ct->getUIN(), ct) );
75     return ct;
76   }
77 
remove(unsigned int uin)78   void ContactList::remove(unsigned int uin) {
79     if (m_cmap.count(uin) != 0) {
80       m_cmap.erase(uin);
81     }
82   }
83 
empty() const84   bool ContactList::empty() const {
85     return m_cmap.empty();
86   }
87 
size() const88   unsigned int ContactList::size() const {
89     return m_cmap.size();
90   }
91 
exists(unsigned int uin)92   bool ContactList::exists(unsigned int uin) {
93     return (m_cmap.count(uin) != 0);
94   }
95 
mobile_exists(const string & m)96   bool ContactList::mobile_exists(const string& m) {
97     iterator curr = begin();
98     while (curr != end()) {
99       if ((*curr)->getNormalisedMobileNo() == m) return true;
100       ++curr;
101     }
102     return false;
103   }
104 
email_exists(const string & em)105   bool ContactList::email_exists(const string& em) {
106     iterator curr = begin();
107     while (curr != end()) {
108       if ((*curr)->getEmail() == em) return true;
109       ++curr;
110     }
111     return false;
112   }
113 
begin()114   ContactList::iterator ContactList::begin() {
115     return iterator(m_cmap.begin());
116   }
117 
end()118   ContactList::iterator ContactList::end() {
119     return iterator(m_cmap.end());
120   }
121 
begin() const122   ContactList::const_iterator ContactList::begin() const {
123     return const_iterator(m_cmap.begin());
124   }
125 
end() const126   ContactList::const_iterator ContactList::end() const {
127     return const_iterator(m_cmap.end());
128   }
129 
130 }
131