merge the formfield patch from ooo-build
[ooovba.git] / connectivity / source / drivers / macab / MacabAddressBook.cxx
blob3305675c7a12d320822cd7ecaec632901ace50b3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: MacabAddressBook.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
34 #include "MacabAddressBook.hxx"
35 #include "MacabRecords.hxx"
36 #include "MacabGroup.hxx"
38 #include <vector>
40 #include <premac.h>
41 #include <Carbon/Carbon.h>
42 #include <AddressBook/ABAddressBookC.h>
43 #include <postmac.h>
44 #include "connectivity/CommonTools.hxx"
46 using namespace connectivity::macab;
47 using namespace ::com::sun::star::uno;
49 // -----------------------------------------------------------------------------
50 MacabAddressBook::MacabAddressBook( )
52 m_aAddressBook = ABGetSharedAddressBook();
53 m_xMacabRecords = NULL;
54 m_bRetrievedGroups = sal_False;
57 // -----------------------------------------------------------------------------
58 MacabAddressBook::~MacabAddressBook()
60 if(m_xMacabRecords != NULL)
62 delete m_xMacabRecords;
63 m_xMacabRecords = NULL;
66 if(!m_xMacabGroups.empty())
68 ::std::vector<MacabGroup *>::iterator iter, end;
69 iter = m_xMacabGroups.begin();
70 end = m_xMacabGroups.end();
71 for( ; iter != end; ++iter)
72 delete (*iter);
75 m_bRetrievedGroups = sal_False;
78 // -----------------------------------------------------------------------------
79 /* Get the address book's default table name. This is the table name that
80 * refers to the table containing _all_ records in the address book.
82 const ::rtl::OUString & MacabAddressBook::getDefaultTableName()
84 /* This string probably needs to be localized. */
85 static const ::rtl::OUString aDefaultTableName
86 (::rtl::OUString::createFromAscii("Address Book"));
88 return aDefaultTableName;
91 // -----------------------------------------------------------------------------
92 MacabRecords *MacabAddressBook::getMacabRecords()
94 /* If the MacabRecords don't exist, create them. */
95 if(m_xMacabRecords == NULL)
97 m_xMacabRecords = new MacabRecords(m_aAddressBook);
98 m_xMacabRecords->setName(getDefaultTableName());
99 m_xMacabRecords->initialize();
102 return m_xMacabRecords;
105 // -----------------------------------------------------------------------------
106 /* Get the MacabRecords for a given name: either a group name or the
107 * default table name.
109 MacabRecords *MacabAddressBook::getMacabRecords(const ::rtl::OUString _tableName)
111 if(_tableName == getDefaultTableName())
113 return getMacabRecords();
115 else
117 return getMacabGroup(_tableName);
121 // -----------------------------------------------------------------------------
122 MacabRecords *MacabAddressBook::getMacabRecordsMatch(const ::rtl::OUString _tableName)
124 if(match(_tableName, getDefaultTableName(), '\0'))
126 return getMacabRecords();
129 return getMacabGroupMatch(_tableName);
132 // -----------------------------------------------------------------------------
133 ::std::vector<MacabGroup *> MacabAddressBook::getMacabGroups()
135 /* If the MacabGroups haven't been created yet, create them. */
136 if(m_bRetrievedGroups == sal_False)
138 /* If the MacabRecords haven't been created yet, create them. */
139 if(m_xMacabRecords == NULL)
141 m_xMacabRecords = new MacabRecords(m_aAddressBook);
142 m_xMacabRecords->setName(getDefaultTableName());
143 m_xMacabRecords->initialize();
146 CFArrayRef allGroups = ABCopyArrayOfAllGroups(m_aAddressBook);
147 sal_Int32 nGroups = CFArrayGetCount(allGroups);
148 m_xMacabGroups = ::std::vector<MacabGroup *>(nGroups);
150 sal_Int32 i;
151 ABGroupRef xGroup;
153 /* Go through each group and create a MacabGroup out of it. */
154 for(i = 0; i < nGroups; i++)
156 xGroup = (ABGroupRef) CFArrayGetValueAtIndex(allGroups, i);
157 m_xMacabGroups[i] = new MacabGroup(m_aAddressBook, m_xMacabRecords, xGroup);
160 CFRelease(allGroups);
162 /* Manage duplicates. */
163 manageDuplicateGroups(m_xMacabGroups);
164 m_bRetrievedGroups = sal_True;
167 return m_xMacabGroups;
170 // -----------------------------------------------------------------------------
171 MacabGroup *MacabAddressBook::getMacabGroup(::rtl::OUString _groupName)
173 // initialize groups if not already initialized
174 if(m_bRetrievedGroups == sal_False)
175 getMacabGroups();
177 sal_Int32 nGroups = m_xMacabGroups.size();
178 sal_Int32 i;
180 for(i = 0; i < nGroups; i++)
182 if(m_xMacabGroups[i] != NULL)
184 if(m_xMacabGroups[i]->getName() == _groupName)
186 return m_xMacabGroups[i];
191 return NULL;
194 // -----------------------------------------------------------------------------
195 MacabGroup *MacabAddressBook::getMacabGroupMatch(::rtl::OUString _groupName)
197 // initialize groups if not already initialized
198 if(m_bRetrievedGroups == sal_False)
199 getMacabGroups();
201 sal_Int32 nGroups = m_xMacabGroups.size();
202 sal_Int32 i;
204 for(i = 0; i < nGroups; i++)
206 if(m_xMacabGroups[i] != NULL)
208 if(match(m_xMacabGroups[i]->getName(), _groupName, '\0'))
210 return m_xMacabGroups[i];
215 return NULL;
218 // -------------------------------------------------------------------------
219 void MacabAddressBook::manageDuplicateGroups(::std::vector<MacabGroup *> _xGroups) const
221 /* If we have two cases of groups, say, family, this makes it:
222 * family
223 * family (2)
225 ::std::vector<MacabGroup *>::reverse_iterator iter1, iter2;
226 sal_Int32 count;
228 for(iter1 = _xGroups.rbegin(); iter1 != _xGroups.rend(); ++iter1)
230 /* If the name matches the default table name, there is already
231 * (obviously) a conflict. So, start the count of groups with this
232 * name at 2 instead of 1.
234 if( (*iter1)->getName() == getDefaultTableName() )
235 count = 2;
236 else
237 count = 1;
239 iter2 = iter1;
240 for( ++iter2; iter2 != _xGroups.rend(); ++iter2)
242 if( (*iter1)->getName() == (*iter2)->getName() )
244 count++;
248 // duplicate!
249 if(count != 1)
251 ::rtl::OUString sName = (*iter1)->getName();
252 sName += ::rtl::OUString::createFromAscii(" (") +
253 ::rtl::OUString::valueOf(count) +
254 ::rtl::OUString::createFromAscii(")");
255 (*iter1)->setName(sName);