Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / cui / source / options / personalization.cxx
blobc1ee526574e637327e319376829db3dc5c60e1d4
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 <comphelper/processfactory.hxx>
15 #include <officecfg/Office/Common.hxx>
16 #include <rtl/bootstrap.hxx>
17 #include <tools/urlobj.hxx>
18 #include <tools/stream.hxx>
19 #include <vcl/event.hxx>
20 #include <vcl/svapp.hxx>
21 #include <vcl/settings.hxx>
22 #include <vcl/graphicfilter.hxx>
23 #include <vcl/virdev.hxx>
25 using namespace com::sun::star;
26 using namespace ::com::sun::star::uno;
27 using namespace ::com::sun::star::beans;
29 // persona
30 SvxPersonalizationTabPage::SvxPersonalizationTabPage(weld::Container* pPage,
31 weld::DialogController* pController,
32 const SfxItemSet& rSet)
33 : SfxTabPage(pPage, pController, "cui/ui/personalization_tab.ui", "PersonalizationTabPage",
34 &rSet)
35 , m_xNoPersona(m_xBuilder->weld_radio_button("no_persona"))
36 , m_xDefaultPersona(m_xBuilder->weld_radio_button("default_persona"))
38 for (sal_uInt32 i = 0; i < MAX_DEFAULT_PERSONAS; ++i)
40 OUString sDefaultId("default" + OUString::number(i));
41 m_vDefaultPersonaImages[i] = m_xBuilder->weld_toggle_button(sDefaultId);
42 m_vDefaultPersonaImages[i]->connect_clicked(
43 LINK(this, SvxPersonalizationTabPage, DefaultPersona));
46 LoadDefaultImages();
49 SvxPersonalizationTabPage::~SvxPersonalizationTabPage() {}
51 std::unique_ptr<SfxTabPage> SvxPersonalizationTabPage::Create(weld::Container* pPage,
52 weld::DialogController* pController,
53 const SfxItemSet* rSet)
55 return std::make_unique<SvxPersonalizationTabPage>(pPage, pController, *rSet);
58 bool SvxPersonalizationTabPage::FillItemSet(SfxItemSet*)
60 // persona
61 OUString aPersona("default");
62 if (m_xNoPersona->get_active())
63 aPersona = "no";
65 bool bModified = false;
66 if (aPersona != officecfg::Office::Common::Misc::Persona::get()
67 || m_aPersonaSettings != officecfg::Office::Common::Misc::PersonaSettings::get())
69 bModified = true;
72 // write
73 std::shared_ptr<comphelper::ConfigurationChanges> batch(
74 comphelper::ConfigurationChanges::create());
75 if (aPersona == "no")
76 m_aPersonaSettings.clear();
77 officecfg::Office::Common::Misc::Persona::set(aPersona, batch);
78 officecfg::Office::Common::Misc::PersonaSettings::set(m_aPersonaSettings, batch);
79 batch->commit();
81 if (bModified)
83 // broadcast the change
84 DataChangedEvent aDataChanged(DataChangedEventType::SETTINGS, nullptr,
85 AllSettingsFlags::STYLE);
86 Application::NotifyAllWindows(aDataChanged);
89 return bModified;
92 void SvxPersonalizationTabPage::Reset(const SfxItemSet*)
94 // persona
95 OUString aPersona = officecfg::Office::Common::Misc::Persona::get();
96 m_aPersonaSettings = officecfg::Office::Common::Misc::PersonaSettings::get();
98 if (aPersona == "no")
99 m_xNoPersona->set_active(true);
100 else
101 m_xDefaultPersona->set_active(true);
104 void SvxPersonalizationTabPage::LoadDefaultImages()
106 // Load the pre saved personas
108 OUString gallery = "$BRAND_BASE_DIR/" LIBO_SHARE_FOLDER "/gallery/personas/";
109 rtl::Bootstrap::expandMacros(gallery);
110 OUString aPersonasList = gallery + "personas_list.txt";
111 SvFileStream aStream(aPersonasList, StreamMode::READ);
112 GraphicFilter aFilter;
113 Graphic aGraphic;
114 sal_Int32 nIndex = 0;
115 bool foundOne = false;
117 OStringBuffer aLine;
118 while (aStream.IsOpen() && !aStream.eof() && nIndex < MAX_DEFAULT_PERSONAS)
120 OUString aPersonaSetting, aPreviewFile, aName;
121 sal_Int32 nParseIndex = 0;
123 aStream.ReadLine(aLine);
124 aPersonaSetting = OStringToOUString(aLine, RTL_TEXTENCODING_UTF8);
125 aName = aPersonaSetting.getToken(1, ';', nParseIndex);
126 aPreviewFile = aPersonaSetting.getToken(0, ';', nParseIndex);
128 if (aPreviewFile.isEmpty())
129 break;
131 m_vDefaultPersonaSettings.push_back(aPersonaSetting);
133 INetURLObject aURLObj(rtl::Concat2View(gallery + aPreviewFile));
134 aFilter.ImportGraphic(aGraphic, aURLObj);
136 Size aSize(aGraphic.GetSizePixel());
137 aSize.setWidth(aSize.Width() / 4);
138 aSize.setHeight(aSize.Height() / 1.5);
139 ScopedVclPtr<VirtualDevice> xVirDev
140 = m_vDefaultPersonaImages[nIndex]->create_virtual_device();
141 xVirDev->SetOutputSizePixel(aSize);
142 aGraphic.Draw(*xVirDev, Point(0, 0));
143 m_vDefaultPersonaImages[nIndex]->set_image(xVirDev.get());
144 xVirDev.disposeAndClear();
146 m_vDefaultPersonaImages[nIndex]->set_tooltip_text(aName);
147 m_vDefaultPersonaImages[nIndex++]->show();
148 foundOne = true;
151 m_xDefaultPersona->set_sensitive(foundOne);
154 IMPL_LINK(SvxPersonalizationTabPage, DefaultPersona, weld::Button&, rButton, void)
156 m_xDefaultPersona->set_active(true);
157 for (sal_Int32 nIndex = 0; nIndex < MAX_DEFAULT_PERSONAS; ++nIndex)
159 if (&rButton == m_vDefaultPersonaImages[nIndex].get())
160 m_aPersonaSettings = m_vDefaultPersonaSettings[nIndex];
161 else
162 m_vDefaultPersonaImages[nIndex]->set_active(false);
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */