1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
28 #include <Carbon/Carbon.h>
29 #include <AddressBook/ABAddressBookC.h>
31 #include "connectivity/CommonTools.hxx"
33 using namespace connectivity::macab
;
34 using namespace ::com::sun::star::uno
;
37 MacabAddressBook::MacabAddressBook( )
39 m_aAddressBook
= ABGetSharedAddressBook();
40 m_xMacabRecords
= NULL
;
41 m_bRetrievedGroups
= sal_False
;
45 MacabAddressBook::~MacabAddressBook()
47 if(m_xMacabRecords
!= NULL
)
49 delete m_xMacabRecords
;
50 m_xMacabRecords
= NULL
;
53 if(!m_xMacabGroups
.empty())
55 ::std::vector
<MacabGroup
*>::iterator iter
, end
;
56 iter
= m_xMacabGroups
.begin();
57 end
= m_xMacabGroups
.end();
58 for( ; iter
!= end
; ++iter
)
62 m_bRetrievedGroups
= sal_False
;
66 /* Get the address book's default table name. This is the table name that
67 * refers to the table containing _all_ records in the address book.
69 const OUString
& MacabAddressBook::getDefaultTableName()
71 /* This string probably needs to be localized. */
72 static const OUString aDefaultTableName
73 (OUString("Address Book"));
75 return aDefaultTableName
;
79 MacabRecords
*MacabAddressBook::getMacabRecords()
81 /* If the MacabRecords don't exist, create them. */
82 if(m_xMacabRecords
== NULL
)
84 m_xMacabRecords
= new MacabRecords(m_aAddressBook
);
85 m_xMacabRecords
->setName(getDefaultTableName());
86 m_xMacabRecords
->initialize();
89 return m_xMacabRecords
;
93 /* Get the MacabRecords for a given name: either a group name or the
96 MacabRecords
*MacabAddressBook::getMacabRecords(const OUString
& _tableName
)
98 if(_tableName
== getDefaultTableName())
100 return getMacabRecords();
104 return getMacabGroup(_tableName
);
109 MacabRecords
*MacabAddressBook::getMacabRecordsMatch(const OUString
& _tableName
)
111 if(match(_tableName
, getDefaultTableName(), '\0'))
113 return getMacabRecords();
116 return getMacabGroupMatch(_tableName
);
120 ::std::vector
<MacabGroup
*> MacabAddressBook::getMacabGroups()
122 /* If the MacabGroups haven't been created yet, create them. */
123 if(m_bRetrievedGroups
== sal_False
)
125 /* If the MacabRecords haven't been created yet, create them. */
126 if(m_xMacabRecords
== NULL
)
128 m_xMacabRecords
= new MacabRecords(m_aAddressBook
);
129 m_xMacabRecords
->setName(getDefaultTableName());
130 m_xMacabRecords
->initialize();
133 CFArrayRef allGroups
= ABCopyArrayOfAllGroups(m_aAddressBook
);
134 sal_Int32 nGroups
= CFArrayGetCount(allGroups
);
135 m_xMacabGroups
= ::std::vector
<MacabGroup
*>(nGroups
);
140 /* Go through each group and create a MacabGroup out of it. */
141 for(i
= 0; i
< nGroups
; i
++)
143 xGroup
= (ABGroupRef
) CFArrayGetValueAtIndex(allGroups
, i
);
144 m_xMacabGroups
[i
] = new MacabGroup(m_aAddressBook
, m_xMacabRecords
, xGroup
);
147 CFRelease(allGroups
);
149 /* Manage duplicates. */
150 manageDuplicateGroups(m_xMacabGroups
);
151 m_bRetrievedGroups
= sal_True
;
154 return m_xMacabGroups
;
158 MacabGroup
*MacabAddressBook::getMacabGroup(OUString
const & _groupName
)
160 // initialize groups if not already initialized
161 if(m_bRetrievedGroups
== sal_False
)
164 sal_Int32 nGroups
= m_xMacabGroups
.size();
167 for(i
= 0; i
< nGroups
; i
++)
169 if(m_xMacabGroups
[i
] != NULL
)
171 if(m_xMacabGroups
[i
]->getName() == _groupName
)
173 return m_xMacabGroups
[i
];
182 MacabGroup
*MacabAddressBook::getMacabGroupMatch(OUString
const & _groupName
)
184 // initialize groups if not already initialized
185 if(m_bRetrievedGroups
== sal_False
)
188 sal_Int32 nGroups
= m_xMacabGroups
.size();
191 for(i
= 0; i
< nGroups
; i
++)
193 if(m_xMacabGroups
[i
] != NULL
)
195 if(match(m_xMacabGroups
[i
]->getName(), _groupName
, '\0'))
197 return m_xMacabGroups
[i
];
206 void MacabAddressBook::manageDuplicateGroups(::std::vector
<MacabGroup
*> _xGroups
) const
208 /* If we have two cases of groups, say, family, this makes it:
212 ::std::vector
<MacabGroup
*>::reverse_iterator iter1
, iter2
;
215 for(iter1
= _xGroups
.rbegin(); iter1
!= _xGroups
.rend(); ++iter1
)
217 /* If the name matches the default table name, there is already
218 * (obviously) a conflict. So, start the count of groups with this
219 * name at 2 instead of 1.
221 if( (*iter1
)->getName() == getDefaultTableName() )
227 for( ++iter2
; iter2
!= _xGroups
.rend(); ++iter2
)
229 if( (*iter1
)->getName() == (*iter2
)->getName() )
238 OUString sName
= (*iter1
)->getName() + " (" +
239 OUString::number(count
) +
241 (*iter1
)->setName(sName
);
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */