1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_connectivity.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include "MacabAddressBook.hxx" 32*cdf0e10cSrcweir #include "MacabRecords.hxx" 33*cdf0e10cSrcweir #include "MacabGroup.hxx" 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir #include <vector> 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir #include <premac.h> 38*cdf0e10cSrcweir #include <Carbon/Carbon.h> 39*cdf0e10cSrcweir #include <AddressBook/ABAddressBookC.h> 40*cdf0e10cSrcweir #include <postmac.h> 41*cdf0e10cSrcweir #include "connectivity/CommonTools.hxx" 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir using namespace connectivity::macab; 44*cdf0e10cSrcweir using namespace ::com::sun::star::uno; 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 47*cdf0e10cSrcweir MacabAddressBook::MacabAddressBook( ) 48*cdf0e10cSrcweir { 49*cdf0e10cSrcweir m_aAddressBook = ABGetSharedAddressBook(); 50*cdf0e10cSrcweir m_xMacabRecords = NULL; 51*cdf0e10cSrcweir m_bRetrievedGroups = sal_False; 52*cdf0e10cSrcweir } 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 55*cdf0e10cSrcweir MacabAddressBook::~MacabAddressBook() 56*cdf0e10cSrcweir { 57*cdf0e10cSrcweir if(m_xMacabRecords != NULL) 58*cdf0e10cSrcweir { 59*cdf0e10cSrcweir delete m_xMacabRecords; 60*cdf0e10cSrcweir m_xMacabRecords = NULL; 61*cdf0e10cSrcweir } 62*cdf0e10cSrcweir 63*cdf0e10cSrcweir if(!m_xMacabGroups.empty()) 64*cdf0e10cSrcweir { 65*cdf0e10cSrcweir ::std::vector<MacabGroup *>::iterator iter, end; 66*cdf0e10cSrcweir iter = m_xMacabGroups.begin(); 67*cdf0e10cSrcweir end = m_xMacabGroups.end(); 68*cdf0e10cSrcweir for( ; iter != end; ++iter) 69*cdf0e10cSrcweir delete (*iter); 70*cdf0e10cSrcweir } 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir m_bRetrievedGroups = sal_False; 73*cdf0e10cSrcweir } 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 76*cdf0e10cSrcweir /* Get the address book's default table name. This is the table name that 77*cdf0e10cSrcweir * refers to the table containing _all_ records in the address book. 78*cdf0e10cSrcweir */ 79*cdf0e10cSrcweir const ::rtl::OUString & MacabAddressBook::getDefaultTableName() 80*cdf0e10cSrcweir { 81*cdf0e10cSrcweir /* This string probably needs to be localized. */ 82*cdf0e10cSrcweir static const ::rtl::OUString aDefaultTableName 83*cdf0e10cSrcweir (::rtl::OUString::createFromAscii("Address Book")); 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir return aDefaultTableName; 86*cdf0e10cSrcweir } 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 89*cdf0e10cSrcweir MacabRecords *MacabAddressBook::getMacabRecords() 90*cdf0e10cSrcweir { 91*cdf0e10cSrcweir /* If the MacabRecords don't exist, create them. */ 92*cdf0e10cSrcweir if(m_xMacabRecords == NULL) 93*cdf0e10cSrcweir { 94*cdf0e10cSrcweir m_xMacabRecords = new MacabRecords(m_aAddressBook); 95*cdf0e10cSrcweir m_xMacabRecords->setName(getDefaultTableName()); 96*cdf0e10cSrcweir m_xMacabRecords->initialize(); 97*cdf0e10cSrcweir } 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir return m_xMacabRecords; 100*cdf0e10cSrcweir } 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 103*cdf0e10cSrcweir /* Get the MacabRecords for a given name: either a group name or the 104*cdf0e10cSrcweir * default table name. 105*cdf0e10cSrcweir */ 106*cdf0e10cSrcweir MacabRecords *MacabAddressBook::getMacabRecords(const ::rtl::OUString _tableName) 107*cdf0e10cSrcweir { 108*cdf0e10cSrcweir if(_tableName == getDefaultTableName()) 109*cdf0e10cSrcweir { 110*cdf0e10cSrcweir return getMacabRecords(); 111*cdf0e10cSrcweir } 112*cdf0e10cSrcweir else 113*cdf0e10cSrcweir { 114*cdf0e10cSrcweir return getMacabGroup(_tableName); 115*cdf0e10cSrcweir } 116*cdf0e10cSrcweir } 117*cdf0e10cSrcweir 118*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 119*cdf0e10cSrcweir MacabRecords *MacabAddressBook::getMacabRecordsMatch(const ::rtl::OUString _tableName) 120*cdf0e10cSrcweir { 121*cdf0e10cSrcweir if(match(_tableName, getDefaultTableName(), '\0')) 122*cdf0e10cSrcweir { 123*cdf0e10cSrcweir return getMacabRecords(); 124*cdf0e10cSrcweir } 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir return getMacabGroupMatch(_tableName); 127*cdf0e10cSrcweir } 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 130*cdf0e10cSrcweir ::std::vector<MacabGroup *> MacabAddressBook::getMacabGroups() 131*cdf0e10cSrcweir { 132*cdf0e10cSrcweir /* If the MacabGroups haven't been created yet, create them. */ 133*cdf0e10cSrcweir if(m_bRetrievedGroups == sal_False) 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir /* If the MacabRecords haven't been created yet, create them. */ 136*cdf0e10cSrcweir if(m_xMacabRecords == NULL) 137*cdf0e10cSrcweir { 138*cdf0e10cSrcweir m_xMacabRecords = new MacabRecords(m_aAddressBook); 139*cdf0e10cSrcweir m_xMacabRecords->setName(getDefaultTableName()); 140*cdf0e10cSrcweir m_xMacabRecords->initialize(); 141*cdf0e10cSrcweir } 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir CFArrayRef allGroups = ABCopyArrayOfAllGroups(m_aAddressBook); 144*cdf0e10cSrcweir sal_Int32 nGroups = CFArrayGetCount(allGroups); 145*cdf0e10cSrcweir m_xMacabGroups = ::std::vector<MacabGroup *>(nGroups); 146*cdf0e10cSrcweir 147*cdf0e10cSrcweir sal_Int32 i; 148*cdf0e10cSrcweir ABGroupRef xGroup; 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir /* Go through each group and create a MacabGroup out of it. */ 151*cdf0e10cSrcweir for(i = 0; i < nGroups; i++) 152*cdf0e10cSrcweir { 153*cdf0e10cSrcweir xGroup = (ABGroupRef) CFArrayGetValueAtIndex(allGroups, i); 154*cdf0e10cSrcweir m_xMacabGroups[i] = new MacabGroup(m_aAddressBook, m_xMacabRecords, xGroup); 155*cdf0e10cSrcweir } 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir CFRelease(allGroups); 158*cdf0e10cSrcweir 159*cdf0e10cSrcweir /* Manage duplicates. */ 160*cdf0e10cSrcweir manageDuplicateGroups(m_xMacabGroups); 161*cdf0e10cSrcweir m_bRetrievedGroups = sal_True; 162*cdf0e10cSrcweir } 163*cdf0e10cSrcweir 164*cdf0e10cSrcweir return m_xMacabGroups; 165*cdf0e10cSrcweir } 166*cdf0e10cSrcweir 167*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 168*cdf0e10cSrcweir MacabGroup *MacabAddressBook::getMacabGroup(::rtl::OUString _groupName) 169*cdf0e10cSrcweir { 170*cdf0e10cSrcweir // initialize groups if not already initialized 171*cdf0e10cSrcweir if(m_bRetrievedGroups == sal_False) 172*cdf0e10cSrcweir getMacabGroups(); 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir sal_Int32 nGroups = m_xMacabGroups.size(); 175*cdf0e10cSrcweir sal_Int32 i; 176*cdf0e10cSrcweir 177*cdf0e10cSrcweir for(i = 0; i < nGroups; i++) 178*cdf0e10cSrcweir { 179*cdf0e10cSrcweir if(m_xMacabGroups[i] != NULL) 180*cdf0e10cSrcweir { 181*cdf0e10cSrcweir if(m_xMacabGroups[i]->getName() == _groupName) 182*cdf0e10cSrcweir { 183*cdf0e10cSrcweir return m_xMacabGroups[i]; 184*cdf0e10cSrcweir } 185*cdf0e10cSrcweir } 186*cdf0e10cSrcweir } 187*cdf0e10cSrcweir 188*cdf0e10cSrcweir return NULL; 189*cdf0e10cSrcweir } 190*cdf0e10cSrcweir 191*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 192*cdf0e10cSrcweir MacabGroup *MacabAddressBook::getMacabGroupMatch(::rtl::OUString _groupName) 193*cdf0e10cSrcweir { 194*cdf0e10cSrcweir // initialize groups if not already initialized 195*cdf0e10cSrcweir if(m_bRetrievedGroups == sal_False) 196*cdf0e10cSrcweir getMacabGroups(); 197*cdf0e10cSrcweir 198*cdf0e10cSrcweir sal_Int32 nGroups = m_xMacabGroups.size(); 199*cdf0e10cSrcweir sal_Int32 i; 200*cdf0e10cSrcweir 201*cdf0e10cSrcweir for(i = 0; i < nGroups; i++) 202*cdf0e10cSrcweir { 203*cdf0e10cSrcweir if(m_xMacabGroups[i] != NULL) 204*cdf0e10cSrcweir { 205*cdf0e10cSrcweir if(match(m_xMacabGroups[i]->getName(), _groupName, '\0')) 206*cdf0e10cSrcweir { 207*cdf0e10cSrcweir return m_xMacabGroups[i]; 208*cdf0e10cSrcweir } 209*cdf0e10cSrcweir } 210*cdf0e10cSrcweir } 211*cdf0e10cSrcweir 212*cdf0e10cSrcweir return NULL; 213*cdf0e10cSrcweir } 214*cdf0e10cSrcweir 215*cdf0e10cSrcweir // ------------------------------------------------------------------------- 216*cdf0e10cSrcweir void MacabAddressBook::manageDuplicateGroups(::std::vector<MacabGroup *> _xGroups) const 217*cdf0e10cSrcweir { 218*cdf0e10cSrcweir /* If we have two cases of groups, say, family, this makes it: 219*cdf0e10cSrcweir * family 220*cdf0e10cSrcweir * family (2) 221*cdf0e10cSrcweir */ 222*cdf0e10cSrcweir ::std::vector<MacabGroup *>::reverse_iterator iter1, iter2; 223*cdf0e10cSrcweir sal_Int32 count; 224*cdf0e10cSrcweir 225*cdf0e10cSrcweir for(iter1 = _xGroups.rbegin(); iter1 != _xGroups.rend(); ++iter1) 226*cdf0e10cSrcweir { 227*cdf0e10cSrcweir /* If the name matches the default table name, there is already 228*cdf0e10cSrcweir * (obviously) a conflict. So, start the count of groups with this 229*cdf0e10cSrcweir * name at 2 instead of 1. 230*cdf0e10cSrcweir */ 231*cdf0e10cSrcweir if( (*iter1)->getName() == getDefaultTableName() ) 232*cdf0e10cSrcweir count = 2; 233*cdf0e10cSrcweir else 234*cdf0e10cSrcweir count = 1; 235*cdf0e10cSrcweir 236*cdf0e10cSrcweir iter2 = iter1; 237*cdf0e10cSrcweir for( ++iter2; iter2 != _xGroups.rend(); ++iter2) 238*cdf0e10cSrcweir { 239*cdf0e10cSrcweir if( (*iter1)->getName() == (*iter2)->getName() ) 240*cdf0e10cSrcweir { 241*cdf0e10cSrcweir count++; 242*cdf0e10cSrcweir } 243*cdf0e10cSrcweir } 244*cdf0e10cSrcweir 245*cdf0e10cSrcweir // duplicate! 246*cdf0e10cSrcweir if(count != 1) 247*cdf0e10cSrcweir { 248*cdf0e10cSrcweir ::rtl::OUString sName = (*iter1)->getName(); 249*cdf0e10cSrcweir sName += ::rtl::OUString::createFromAscii(" (") + 250*cdf0e10cSrcweir ::rtl::OUString::valueOf(count) + 251*cdf0e10cSrcweir ::rtl::OUString::createFromAscii(")"); 252*cdf0e10cSrcweir (*iter1)->setName(sName); 253*cdf0e10cSrcweir } 254*cdf0e10cSrcweir } 255*cdf0e10cSrcweir } 256*cdf0e10cSrcweir 257