bump product version to 7.2.5.1
[LibreOffice.git] / connectivity / source / drivers / macab / MacabAddressBook.cxx
bloba14b7abee9b93d341f7b342d6a36b6ebf38c9c46
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "MacabAddressBook.hxx"
22 #include "MacabRecords.hxx"
23 #include "MacabGroup.hxx"
25 #include <vector>
27 #include <premac.h>
28 #include <Carbon/Carbon.h>
29 #include <AddressBook/ABAddressBookC.h>
30 #include <postmac.h>
31 #include <connectivity/CommonTools.hxx>
33 using namespace connectivity::macab;
34 using namespace ::com::sun::star::uno;
36 namespace {
38 void manageDuplicateGroups(std::vector<MacabGroup *> _xGroups)
40 /* If we have two cases of groups, say, family, this makes it:
41 * family
42 * family (2)
44 std::vector<MacabGroup *>::reverse_iterator iter1, iter2;
45 sal_Int32 count;
47 for(iter1 = _xGroups.rbegin(); iter1 != _xGroups.rend(); ++iter1)
49 /* If the name matches the default table name, there is already
50 * (obviously) a conflict. So, start the count of groups with this
51 * name at 2 instead of 1.
53 if( (*iter1)->getName() == MacabAddressBook::getDefaultTableName() )
54 count = 2;
55 else
56 count = 1;
58 iter2 = iter1;
59 for( ++iter2; iter2 != _xGroups.rend(); ++iter2)
61 if( (*iter1)->getName() == (*iter2)->getName() )
63 count++;
67 // duplicate!
68 if(count != 1)
70 OUString sName = (*iter1)->getName() + " (" +
71 OUString::number(count) +
72 ")";
73 (*iter1)->setName(sName);
80 MacabAddressBook::MacabAddressBook( )
81 : m_aAddressBook(ABGetSharedAddressBook()),
82 m_xMacabRecords(nullptr),
83 m_bRetrievedGroups(false)
88 MacabAddressBook::~MacabAddressBook()
90 if(m_xMacabRecords != nullptr)
92 delete m_xMacabRecords;
93 m_xMacabRecords = nullptr;
96 for(MacabGroup* pMacabGroup : m_xMacabGroups)
97 delete pMacabGroup;
99 m_bRetrievedGroups = false;
103 /* Get the address book's default table name. This is the table name that
104 * refers to the table containing _all_ records in the address book.
106 const OUString & MacabAddressBook::getDefaultTableName()
108 /* This string probably needs to be localized. */
109 static const OUString aDefaultTableName
110 (OUString("Address Book"));
112 return aDefaultTableName;
116 MacabRecords *MacabAddressBook::getMacabRecords()
118 /* If the MacabRecords don't exist, create them. */
119 if(m_xMacabRecords == nullptr)
121 m_xMacabRecords = new MacabRecords(m_aAddressBook);
122 m_xMacabRecords->setName(getDefaultTableName());
123 m_xMacabRecords->initialize();
126 return m_xMacabRecords;
130 /* Get the MacabRecords for a given name: either a group name or the
131 * default table name.
133 MacabRecords *MacabAddressBook::getMacabRecords(std::u16string_view _tableName)
135 if(_tableName == getDefaultTableName())
137 return getMacabRecords();
139 else
141 return getMacabGroup(_tableName);
146 MacabRecords *MacabAddressBook::getMacabRecordsMatch(const OUString& _tableName)
148 if(match(_tableName, getDefaultTableName(), '\0'))
150 return getMacabRecords();
153 return getMacabGroupMatch(_tableName);
157 std::vector<MacabGroup *> MacabAddressBook::getMacabGroups()
159 /* If the MacabGroups haven't been created yet, create them. */
160 if(!m_bRetrievedGroups)
162 /* If the MacabRecords haven't been created yet, create them. */
163 if(m_xMacabRecords == nullptr)
165 m_xMacabRecords = new MacabRecords(m_aAddressBook);
166 m_xMacabRecords->setName(getDefaultTableName());
167 m_xMacabRecords->initialize();
170 CFArrayRef allGroups = ABCopyArrayOfAllGroups(m_aAddressBook);
171 sal_Int32 nGroups = CFArrayGetCount(allGroups);
172 m_xMacabGroups = std::vector<MacabGroup *>(nGroups);
174 sal_Int32 i;
175 ABGroupRef xGroup;
177 /* Go through each group and create a MacabGroup out of it. */
178 for(i = 0; i < nGroups; i++)
180 xGroup = static_cast<ABGroupRef>(const_cast<void *>(CFArrayGetValueAtIndex(allGroups, i)));
181 m_xMacabGroups[i] = new MacabGroup(m_aAddressBook, m_xMacabRecords, xGroup);
184 CFRelease(allGroups);
186 /* Manage duplicates. */
187 manageDuplicateGroups(m_xMacabGroups);
188 m_bRetrievedGroups = true;
191 return m_xMacabGroups;
195 MacabGroup *MacabAddressBook::getMacabGroup(std::u16string_view _groupName)
197 // initialize groups if not already initialized
198 if(!m_bRetrievedGroups)
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] != nullptr)
208 if(m_xMacabGroups[i]->getName() == _groupName)
210 return m_xMacabGroups[i];
215 return nullptr;
219 MacabGroup *MacabAddressBook::getMacabGroupMatch(OUString const & _groupName)
221 // initialize groups if not already initialized
222 if(!m_bRetrievedGroups)
223 getMacabGroups();
225 sal_Int32 nGroups = m_xMacabGroups.size();
226 sal_Int32 i;
228 for(i = 0; i < nGroups; i++)
230 if(m_xMacabGroups[i] != nullptr)
232 if(match(m_xMacabGroups[i]->getName(), _groupName, '\0'))
234 return m_xMacabGroups[i];
239 return nullptr;
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */