Avoid potential negative array index access to cached text.
[LibreOffice.git] / extensions / source / abpilot / typeselectionpage.cxx
blob79cfd5d6b035af1a8fff51f7ea3f9ce0814b03eb
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 .
20 #include "typeselectionpage.hxx"
21 #include "addresssettings.hxx"
22 #include "abspilot.hxx"
23 #include <vcl/svapp.hxx>
24 #include <vcl/weld.hxx>
25 #include <com/sun/star/sdbc/XDriver.hpp>
26 #include <com/sun/star/sdbc/DriverManager.hpp>
28 namespace abp
30 using namespace ::com::sun::star::uno;
31 using namespace ::com::sun::star::sdbc;
33 // TypeSelectionPage
34 TypeSelectionPage::TypeSelectionPage(weld::Container* pPage, OAddressBookSourcePilot* pDialog)
35 : AddressBookSourcePage(pPage, pDialog, "modules/sabpilot/ui/selecttypepage.ui", "SelectTypePage")
36 , m_xEvolution(m_xBuilder->weld_radio_button("evolution"))
37 , m_xEvolutionGroupwise(m_xBuilder->weld_radio_button("groupwise"))
38 , m_xEvolutionLdap(m_xBuilder->weld_radio_button("evoldap"))
39 , m_xThunderbird(m_xBuilder->weld_radio_button("thunderbird"))
40 , m_xKab(m_xBuilder->weld_radio_button("kde"))
41 , m_xMacab(m_xBuilder->weld_radio_button("macosx"))
42 , m_xOther(m_xBuilder->weld_radio_button("other"))
44 //TODO: For now, try to keep offering the same choices like before the
45 // Mozilla cleanup, even if the status of what driver actually
46 // provides which functionality is somewhat unclear, see the discussions
47 // of fdo#57285 "Address Book Data Source Wizard lists 'macOS address
48 // book' on Linux" and fdo#57322 "Moz-free LDAP Address Book driver."
49 // In accordance with ancient OOo 3.3, this is as follows:
51 // On Linux:
52 // - EVOLUTION, EVOLUTION_GROUPWISE, EVOLUTION_LDAP (if applicable)
53 // - KAB (if applicable)
54 // - OTHER
56 // On macOS:
57 // - MACAB (if applicable)
58 // - OTHER
60 // On Windows:
61 // - THUNDERBIRD
62 // - OTHER
64 #if !defined(_WIN32)
65 bool bHaveEvolution = false;
66 bool bHaveKab = false;
67 bool bHaveMacab = false;
69 Reference< XDriverManager2 > xManager = DriverManager::create( pDialog->getORB() );
71 try
73 // check whether Evolution is available
74 Reference< XDriver > xDriver( xManager->getDriverByURL("sdbc:address:evolution:local") );
75 if ( xDriver.is() )
76 bHaveEvolution = true;
78 catch (...)
82 // check whether KDE address book is available
83 try
85 Reference< XDriver > xDriver( xManager->getDriverByURL("sdbc:address:kab") );
86 if ( xDriver.is() )
87 bHaveKab = true;
89 catch (...)
93 try
95 // check whether macOS address book is available
96 Reference< XDriver > xDriver( xManager->getDriverByURL("sdbc:address:macab") );
97 if ( xDriver.is() )
98 bHaveMacab = true;
100 catch(...)
103 #else
104 bool const bHaveEvolution = false;
105 bool const bHaveKab = false;
106 bool const bHaveMacab = false;
107 #endif
109 // Items are displayed in list order
110 m_aAllTypes.push_back( ButtonItem( m_xEvolution.get(), AST_EVOLUTION, bHaveEvolution ) );
111 m_aAllTypes.push_back( ButtonItem( m_xEvolutionGroupwise.get(), AST_EVOLUTION_GROUPWISE, bHaveEvolution ) );
112 m_aAllTypes.push_back( ButtonItem( m_xEvolutionLdap.get(), AST_EVOLUTION_LDAP, bHaveEvolution ) );
113 m_aAllTypes.push_back( ButtonItem( m_xThunderbird.get(), AST_THUNDERBIRD, true ) );
114 m_aAllTypes.push_back( ButtonItem( m_xKab.get(), AST_KAB, bHaveKab ) );
115 m_aAllTypes.push_back( ButtonItem( m_xMacab.get(), AST_MACAB, bHaveMacab ) );
116 m_aAllTypes.push_back( ButtonItem( m_xOther.get(), AST_OTHER, true ) );
118 Link<weld::Toggleable&,void> aTypeSelectionHandler = LINK(this, TypeSelectionPage, OnTypeSelected );
119 for (auto const& elem : m_aAllTypes)
121 if (!elem.m_bVisible)
122 elem.m_pItem->hide();
123 else
125 elem.m_pItem->connect_toggled( aTypeSelectionHandler );
126 elem.m_pItem->show();
131 TypeSelectionPage::~TypeSelectionPage()
133 for (auto & elem : m_aAllTypes)
135 elem.m_bVisible = false;
139 void TypeSelectionPage::Activate()
141 AddressBookSourcePage::Activate();
143 for (auto const& elem : m_aAllTypes)
145 if( elem.m_pItem->get_active() && elem.m_bVisible )
147 elem.m_pItem->grab_focus();
148 break;
152 getDialog()->enableButtons(WizardButtonFlags::PREVIOUS, false);
155 void TypeSelectionPage::Deactivate()
157 AddressBookSourcePage::Deactivate();
158 getDialog()->enableButtons(WizardButtonFlags::PREVIOUS, true);
161 void TypeSelectionPage::selectType( AddressSourceType _eType )
163 for (auto const& elem : m_aAllTypes)
165 elem.m_pItem->set_active( _eType == elem.m_eType );
169 AddressSourceType TypeSelectionPage::getSelectedType() const
171 for (auto const& elem : m_aAllTypes)
173 if ( elem.m_pItem->get_active() && elem.m_bVisible )
174 return elem.m_eType;
177 return AST_INVALID;
180 void TypeSelectionPage::initializePage()
182 AddressBookSourcePage::initializePage();
184 const AddressSettings& rSettings = getSettings();
185 selectType(rSettings.eType);
188 bool TypeSelectionPage::commitPage( ::vcl::WizardTypes::CommitPageReason _eReason )
190 if (!AddressBookSourcePage::commitPage(_eReason))
191 return false;
193 if (AST_INVALID == getSelectedType( ))
195 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xContainer.get(),
196 VclMessageType::Warning, VclButtonsType::Ok,
197 compmodule::ModuleRes(RID_STR_NEEDTYPESELECTION)));
198 xBox->run();
199 return false;
202 AddressSettings& rSettings = getSettings();
203 rSettings.eType = getSelectedType();
205 return true;
208 bool TypeSelectionPage::canAdvance() const
210 return AddressBookSourcePage::canAdvance()
211 && (AST_INVALID != getSelectedType());
214 IMPL_LINK(TypeSelectionPage, OnTypeSelected, weld::Toggleable&, rButton, void)
216 if (!rButton.get_active())
217 return;
218 getDialog()->typeSelectionChanged( getSelectedType() );
219 updateDialogTravelUI();
222 } // namespace abp
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */