LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / basctl / source / dlged / managelang.cxx
blobc7cd423cccee2a3b332e80e0140f41013554aa87
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 <basidesh.hxx>
21 #include <basobj.hxx>
22 #include <iderdll.hxx>
23 #include <iderid.hxx>
24 #include <localizationmgr.hxx>
25 #include <managelang.hxx>
27 #include <strings.hrc>
29 #include <comphelper/sequence.hxx>
30 #include <sfx2/bindings.hxx>
31 #include <sfx2/sfxsids.hrc>
32 #include <svtools/langtab.hxx>
33 #include <svx/langbox.hxx>
34 #include <vcl/svapp.hxx>
35 #include <vcl/weld.hxx>
36 #include <vcl/settings.hxx>
37 #include <tools/debug.hxx>
39 namespace basctl
42 using namespace ::com::sun::star::i18n;
43 using namespace ::com::sun::star::lang;
44 using namespace ::com::sun::star::resource;
45 using namespace ::com::sun::star::uno;
47 bool localesAreEqual( const Locale& rLocaleLeft, const Locale& rLocaleRight )
49 bool bRet = ( rLocaleLeft.Language == rLocaleRight.Language &&
50 rLocaleLeft.Country == rLocaleRight.Country &&
51 rLocaleLeft.Variant == rLocaleRight.Variant );
52 return bRet;
55 ManageLanguageDialog::ManageLanguageDialog(weld::Window* pParent, std::shared_ptr<LocalizationMgr> const & xLMgr)
56 : GenericDialogController(pParent, "modules/BasicIDE/ui/managelanguages.ui", "ManageLanguagesDialog")
57 , m_xLocalizationMgr(xLMgr)
58 , m_sDefLangStr(IDEResId(RID_STR_DEF_LANG))
59 , m_sCreateLangStr(IDEResId(RID_STR_CREATE_LANG))
60 , m_xLanguageLB(m_xBuilder->weld_tree_view("treeview"))
61 , m_xAddPB(m_xBuilder->weld_button("add"))
62 , m_xDeletePB(m_xBuilder->weld_button("delete"))
63 , m_xMakeDefPB(m_xBuilder->weld_button("default"))
65 m_xLanguageLB->set_size_request(m_xLanguageLB->get_approximate_digit_width() * 42,
66 m_xLanguageLB->get_height_rows(10));
68 Init();
69 FillLanguageBox();
70 SelectHdl( *m_xLanguageLB );
73 ManageLanguageDialog::~ManageLanguageDialog()
75 ClearLanguageBox();
78 void ManageLanguageDialog::Init()
80 // get current IDE
81 Shell* pShell = GetShell();
82 const OUString& sLibName = pShell->GetCurLibName();
83 // set dialog title with library name
84 OUString sText = m_xDialog->get_title();
85 sText = sText.replaceAll("$1", sLibName);
86 m_xDialog->set_title(sText);
87 // set handler
88 m_xAddPB->connect_clicked( LINK( this, ManageLanguageDialog, AddHdl ) );
89 m_xDeletePB->connect_clicked( LINK( this, ManageLanguageDialog, DeleteHdl ) );
90 m_xMakeDefPB->connect_clicked( LINK( this, ManageLanguageDialog, MakeDefHdl ) );
91 m_xLanguageLB->connect_changed( LINK( this, ManageLanguageDialog, SelectHdl ) );
93 m_xLanguageLB->set_selection_mode(SelectionMode::Multiple);
96 void ManageLanguageDialog::FillLanguageBox()
98 DBG_ASSERT( m_xLocalizationMgr, "ManageLanguageDialog::FillLanguageBox(): no localization manager" );
100 if ( m_xLocalizationMgr->isLibraryLocalized() )
102 Locale aDefaultLocale = m_xLocalizationMgr->getStringResourceManager()->getDefaultLocale();
103 Sequence< Locale > aLocaleSeq = m_xLocalizationMgr->getStringResourceManager()->getLocales();
104 const Locale* pLocale = aLocaleSeq.getConstArray();
105 sal_Int32 i, nCount = aLocaleSeq.getLength();
106 for ( i = 0; i < nCount; ++i )
108 bool bIsDefault = localesAreEqual( aDefaultLocale, pLocale[i] );
109 LanguageType eLangType = LanguageTag::convertToLanguageType( pLocale[i] );
110 OUString sLanguage = SvtLanguageTable::GetLanguageString( eLangType );
111 if ( bIsDefault )
113 sLanguage += " " + m_sDefLangStr;
115 LanguageEntry* pEntry = new LanguageEntry(pLocale[i], bIsDefault);
116 m_xLanguageLB->append(OUString::number(reinterpret_cast<sal_Int64>(pEntry)), sLanguage);
119 else
120 m_xLanguageLB->append_text(m_sCreateLangStr);
123 void ManageLanguageDialog::ClearLanguageBox()
125 const sal_Int32 nCount = m_xLanguageLB->n_children();
126 for (sal_Int32 i = 0; i < nCount; ++i)
128 LanguageEntry* pEntry = reinterpret_cast<LanguageEntry*>(m_xLanguageLB->get_id(i).toInt64());
129 delete pEntry;
131 m_xLanguageLB->clear();
134 IMPL_LINK_NOARG(ManageLanguageDialog, AddHdl, weld::Button&, void)
136 auto xDlg = std::make_shared<SetDefaultLanguageDialog>(m_xDialog.get(), m_xLocalizationMgr);
137 weld::DialogController::runAsync(xDlg, [xDlg,this](sal_Int32 nResult)
139 if (!nResult )
140 return;
141 // add new locales
142 Sequence< Locale > aLocaleSeq = xDlg->GetLocales();
143 m_xLocalizationMgr->handleAddLocales( aLocaleSeq );
144 // update listbox
145 ClearLanguageBox();
146 FillLanguageBox();
148 if (SfxBindings* pBindings = GetBindingsPtr())
149 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
153 IMPL_LINK_NOARG(ManageLanguageDialog, DeleteHdl, weld::Button&, void)
155 std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(m_xDialog.get(), "modules/BasicIDE/ui/deletelangdialog.ui"));
156 std::unique_ptr<weld::MessageDialog> xQBox(xBuilder->weld_message_dialog("DeleteLangDialog"));
157 if (xQBox->run() != RET_OK)
158 return;
160 std::vector<int> aSelection = m_xLanguageLB->get_selected_rows();
161 int nCount = aSelection.size();
162 int nPos = m_xLanguageLB->get_selected_index();
163 // remove locales
164 Sequence< Locale > aLocaleSeq( nCount );
165 auto aLocaleSeqRange = asNonConstRange(aLocaleSeq);
166 for (int i = 0; i < nCount; ++i)
168 const sal_Int32 nSelPos = aSelection[i];
169 LanguageEntry* pEntry = reinterpret_cast<LanguageEntry*>(m_xLanguageLB->get_id(nSelPos).toInt64());
170 if ( pEntry )
171 aLocaleSeqRange[i] = pEntry->m_aLocale;
173 m_xLocalizationMgr->handleRemoveLocales( aLocaleSeq );
174 // update listbox
175 ClearLanguageBox();
176 FillLanguageBox();
177 // reset selection
178 nCount = m_xLanguageLB->n_children();
179 if (nCount <= nPos)
180 nPos = nCount - 1;
181 m_xLanguageLB->select(nPos);
182 SelectHdl( *m_xLanguageLB );
185 IMPL_LINK_NOARG(ManageLanguageDialog, MakeDefHdl, weld::Button&, void)
187 const sal_Int32 nPos = m_xLanguageLB->get_selected_index();
188 LanguageEntry* pSelectEntry = reinterpret_cast<LanguageEntry*>(m_xLanguageLB->get_id(nPos).toInt64());
189 if (pSelectEntry && !pSelectEntry->m_bIsDefault)
191 // set new default entry
192 m_xLocalizationMgr->handleSetDefaultLocale( pSelectEntry->m_aLocale );
193 // update Listbox
194 ClearLanguageBox();
195 FillLanguageBox();
196 // reset selection
197 m_xLanguageLB->select(nPos);
198 SelectHdl( *m_xLanguageLB );
202 IMPL_LINK_NOARG(ManageLanguageDialog, SelectHdl, weld::TreeView&, void)
204 const sal_Int32 nCount = m_xLanguageLB->n_children();
205 bool bEmpty = ( !nCount ||
206 m_xLanguageLB->find_text(m_sCreateLangStr) != -1 );
207 bool bSelect = ( m_xLanguageLB->get_selected_index() != -1 );
208 bool bEnable = !bEmpty && bSelect;
210 m_xDeletePB->set_sensitive(bEnable);
211 m_xMakeDefPB->set_sensitive(bEnable && nCount > 1 && m_xLanguageLB->count_selected_rows() == 1);
214 // class SetDefaultLanguageDialog -----------------------------------------------
216 SetDefaultLanguageDialog::SetDefaultLanguageDialog(weld::Window* pParent, std::shared_ptr<LocalizationMgr> const & xLMgr)
217 : GenericDialogController(pParent, "modules/BasicIDE/ui/defaultlanguage.ui", "DefaultLanguageDialog")
218 , m_xLocalizationMgr(xLMgr)
219 , m_xLanguageFT(m_xBuilder->weld_label("defaultlabel"))
220 , m_xLanguageLB(m_xBuilder->weld_tree_view("entries"))
221 , m_xCheckLangFT(m_xBuilder->weld_label("checkedlabel"))
222 , m_xCheckLangLB(m_xBuilder->weld_tree_view("checkedentries"))
223 , m_xDefinedFT(m_xBuilder->weld_label("defined"))
224 , m_xAddedFT(m_xBuilder->weld_label("added"))
225 , m_xAltTitle(m_xBuilder->weld_label("alttitle"))
226 , m_xLanguageCB(new SvxLanguageBox(m_xBuilder->weld_combo_box("hidden")))
228 m_xLanguageLB->set_size_request(-1, m_xLanguageLB->get_height_rows(10));
229 m_xCheckLangLB->set_size_request(-1, m_xCheckLangLB->get_height_rows(10));
230 m_xCheckLangLB->enable_toggle_buttons(weld::ColumnToggleType::Check);
232 if (m_xLocalizationMgr->isLibraryLocalized())
234 // change to "Add Interface Language" mode
235 m_xLanguageLB->hide();
236 m_xCheckLangLB->show();
237 m_xDialog->set_title(m_xAltTitle->get_label());
238 m_xLanguageFT->hide();
239 m_xCheckLangFT->show();
240 m_xDefinedFT->hide();
241 m_xAddedFT->show();
244 FillLanguageBox();
247 SetDefaultLanguageDialog::~SetDefaultLanguageDialog()
251 void SetDefaultLanguageDialog::FillLanguageBox()
253 // fill list with all languages
254 m_xLanguageCB->SetLanguageList(SvxLanguageListFlags::ALL, false);
256 if (m_xLocalizationMgr->isLibraryLocalized())
258 // remove the already localized languages
259 Sequence< Locale > aLocaleSeq = m_xLocalizationMgr->getStringResourceManager()->getLocales();
260 const Locale* pLocale = aLocaleSeq.getConstArray();
261 const sal_Int32 nCountLoc = aLocaleSeq.getLength();
262 for ( sal_Int32 i = 0; i < nCountLoc; ++i )
263 m_xLanguageCB->remove_id(LanguageTag::convertToLanguageType(pLocale[i]));
265 // fill checklistbox if not in default mode
266 const sal_Int32 nCountLang = m_xLanguageCB->get_count();
267 for (sal_Int32 j = 0; j < nCountLang; ++j)
269 LanguageType eLang = m_xLanguageCB->get_id(j);
270 m_xCheckLangLB->append();
271 const int nRow = m_xCheckLangLB->n_children() - 1;
272 m_xCheckLangLB->set_toggle(nRow, TRISTATE_FALSE);
273 m_xCheckLangLB->set_text(nRow, m_xLanguageCB->get_text(j), 0);
274 m_xCheckLangLB->set_id(nRow, OUString::number(eLang.get()));
276 m_xLanguageCB.reset();
277 m_xLanguageLB.reset();
279 else
281 const sal_Int32 nCountLang = m_xLanguageCB->get_count();
282 for (sal_Int32 j = 0; j < nCountLang; ++j)
284 LanguageType eLang = m_xLanguageCB->get_id(j);
285 m_xLanguageLB->append(OUString::number(eLang.get()), m_xLanguageCB->get_text(j));
287 m_xLanguageCB.reset();
289 // preselect current UI language
290 m_xLanguageLB->select_id(OUString::number(Application::GetSettings().GetUILanguageTag().getLanguageType().get()));
294 Sequence< Locale > SetDefaultLanguageDialog::GetLocales() const
296 bool bNotLocalized = !m_xLocalizationMgr->isLibraryLocalized();
297 if (bNotLocalized)
299 LanguageType eType(m_xLanguageLB->get_selected_id().toUInt32());
300 return {LanguageTag(eType).getLocale()};
302 std::vector<Locale> aLocaleSeq;
303 const sal_Int32 nCount = m_xCheckLangLB->n_children();
304 for (sal_Int32 i = 0; i < nCount; ++i)
306 if (m_xCheckLangLB->get_toggle(i) == TRISTATE_TRUE)
308 LanguageType eType(m_xCheckLangLB->get_id(i).toUInt32());
309 aLocaleSeq.push_back(LanguageTag::convertToLocale(eType));
312 return comphelper::containerToSequence(aLocaleSeq);
315 } // namespace basctl
317 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */