Use correct object
[LibreOffice.git] / cui / source / options / personalization.cxx
blob51af4ec8e39ccfe91b5bebf294d3a628bce4c7cf
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/.
8 */
10 #include <config_folders.h>
12 #include "personalization.hxx"
14 #include <dialmgr.hxx>
16 #include <comphelper/processfactory.hxx>
17 #include <officecfg/Office/Common.hxx>
18 #include <rtl/bootstrap.hxx>
19 #include <tools/urlobj.hxx>
20 #include <tools/stream.hxx>
21 #include <vcl/event.hxx>
22 #include <vcl/svapp.hxx>
23 #include <vcl/settings.hxx>
24 #include <vcl/graphicfilter.hxx>
25 #include <vcl/virdev.hxx>
26 #include <personas.hrc>
28 using namespace com::sun::star;
29 using namespace ::com::sun::star::beans;
31 // persona
32 SvxPersonalizationTabPage::SvxPersonalizationTabPage(weld::Container* pPage,
33 weld::DialogController* pController,
34 const SfxItemSet& rSet)
35 : SfxTabPage(pPage, pController, u"cui/ui/personalization_tab.ui"_ustr,
36 u"PersonalizationTabPage"_ustr, &rSet)
37 , m_xNoPersona(m_xBuilder->weld_radio_button(u"no_persona"_ustr))
38 , m_xPersonaImg(m_xBuilder->weld_widget(u"lockpersona"_ustr))
39 , m_xDefaultPersona(m_xBuilder->weld_radio_button(u"default_persona"_ustr))
40 , m_xContentGrid(m_xBuilder->weld_container(u"gridpersonasetting"_ustr))
42 for (sal_uInt32 i = 0; i < MAX_DEFAULT_PERSONAS; ++i)
44 OUString sDefaultId("default" + OUString::number(i));
45 m_vDefaultPersonaImages[i] = m_xBuilder->weld_toggle_button(sDefaultId);
46 m_vDefaultPersonaImages[i]->connect_clicked(
47 LINK(this, SvxPersonalizationTabPage, DefaultPersona));
50 LoadDefaultImages();
53 SvxPersonalizationTabPage::~SvxPersonalizationTabPage() {}
55 std::unique_ptr<SfxTabPage> SvxPersonalizationTabPage::Create(weld::Container* pPage,
56 weld::DialogController* pController,
57 const SfxItemSet* rSet)
59 return std::make_unique<SvxPersonalizationTabPage>(pPage, pController, *rSet);
62 OUString SvxPersonalizationTabPage::GetAllStrings()
64 OUString sAllStrings;
65 OUString radioButton[] = { u"no_persona"_ustr, u"default_persona"_ustr };
67 for (const auto& radio : radioButton)
69 if (const auto pString = m_xBuilder->weld_radio_button(radio))
70 sAllStrings += pString->get_label() + " ";
73 if (const auto pString = m_xBuilder->weld_label(u"personas_label"_ustr))
74 sAllStrings += pString->get_label() + " ";
76 return sAllStrings.replaceAll("_", "");
79 bool SvxPersonalizationTabPage::FillItemSet(SfxItemSet*)
81 // persona
82 OUString aPersona(u"default"_ustr);
83 if (m_xNoPersona->get_active())
84 aPersona = "no";
86 bool bModified = false;
87 if (aPersona != officecfg::Office::Common::Misc::Persona::get()
88 || m_aPersonaSettings != officecfg::Office::Common::Misc::PersonaSettings::get())
90 bModified = true;
93 // write
94 std::shared_ptr<comphelper::ConfigurationChanges> batch(
95 comphelper::ConfigurationChanges::create());
96 if (aPersona == "no")
97 m_aPersonaSettings.clear();
98 officecfg::Office::Common::Misc::Persona::set(aPersona, batch);
99 officecfg::Office::Common::Misc::PersonaSettings::set(m_aPersonaSettings, batch);
100 batch->commit();
102 if (bModified)
104 // broadcast the change
105 DataChangedEvent aDataChanged(DataChangedEventType::SETTINGS, nullptr,
106 AllSettingsFlags::STYLE);
107 Application::NotifyAllWindows(aDataChanged);
110 return bModified;
113 void SvxPersonalizationTabPage::Reset(const SfxItemSet*)
115 // persona
116 OUString aPersona = officecfg::Office::Common::Misc::Persona::get();
117 m_aPersonaSettings = officecfg::Office::Common::Misc::PersonaSettings::get();
119 if (aPersona == "no")
120 m_xNoPersona->set_active(true);
121 else
122 m_xDefaultPersona->set_active(true);
124 if (officecfg::Office::Common::Misc::Persona::isReadOnly())
126 m_xNoPersona->set_sensitive(false);
127 m_xDefaultPersona->set_sensitive(false);
128 m_xPersonaImg->set_visible(true);
131 if (officecfg::Office::Common::Misc::PersonaSettings::isReadOnly())
132 m_xContentGrid->set_sensitive(false);
135 void SvxPersonalizationTabPage::LoadDefaultImages()
137 // Load the pre saved personas
139 OUString gallery = u"$BRAND_BASE_DIR/" LIBO_SHARE_FOLDER "/gallery/personas/"_ustr;
140 rtl::Bootstrap::expandMacros(gallery);
141 OUString aPersonasList = gallery + "personas_list.txt";
142 SvFileStream aStream(aPersonasList, StreamMode::READ);
143 GraphicFilter aFilter;
144 Graphic aGraphic;
145 sal_Int32 nIndex = 0;
146 bool foundOne = false;
148 OStringBuffer aLine;
149 int nLineNumberFilePersona = 0;
150 while (aStream.IsOpen() && !aStream.eof() && nIndex < MAX_DEFAULT_PERSONAS)
152 OUString aPersonaSetting, aPreviewFile, aName;
153 sal_Int32 nParseIndex = 0;
155 aStream.ReadLine(aLine);
156 aPersonaSetting = OStringToOUString(aLine, RTL_TEXTENCODING_UTF8);
157 aName = CuiResId(RID_PERSONAS_COLOR[nLineNumberFilePersona].first);
158 aPreviewFile = aPersonaSetting.getToken(2, ';', nParseIndex);
160 if (aPreviewFile.isEmpty())
161 break;
163 m_vDefaultPersonaSettings.push_back(aPersonaSetting);
165 INetURLObject aURLObj(rtl::Concat2View(gallery + aPreviewFile));
166 aFilter.ImportGraphic(aGraphic, aURLObj);
168 Size aSize(aGraphic.GetSizePixel());
169 aSize.setWidth(aSize.Width() / 4);
170 aSize.setHeight(aSize.Height() / 1.5);
171 ScopedVclPtr<VirtualDevice> xVirDev
172 = m_vDefaultPersonaImages[nIndex]->create_virtual_device();
173 xVirDev->SetOutputSizePixel(aSize);
174 aGraphic.Draw(*xVirDev, Point(0, 0));
175 m_vDefaultPersonaImages[nIndex]->set_image(xVirDev.get());
176 xVirDev.disposeAndClear();
178 m_vDefaultPersonaImages[nIndex]->set_tooltip_text(aName);
179 m_vDefaultPersonaImages[nIndex++]->show();
180 foundOne = true;
181 ++nLineNumberFilePersona;
184 m_xDefaultPersona->set_sensitive(foundOne);
187 IMPL_LINK(SvxPersonalizationTabPage, DefaultPersona, weld::Button&, rButton, void)
189 m_xDefaultPersona->set_active(true);
190 for (sal_Int32 nIndex = 0; nIndex < MAX_DEFAULT_PERSONAS; ++nIndex)
192 if (&rButton == m_vDefaultPersonaImages[nIndex].get())
193 m_aPersonaSettings = m_vDefaultPersonaSettings[nIndex];
194 else
195 m_vDefaultPersonaImages[nIndex]->set_active(false);
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */