Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / cui / source / options / personalization.cxx
bloba732053bab7f83498d9f2d8e02028fe4629f75a6
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 #include <vector>
27 using namespace com::sun::star;
28 using namespace ::com::sun::star::uno;
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, "cui/ui/personalization_tab.ui", "PersonalizationTabPage",
36 &rSet)
37 , m_xNoPersona(m_xBuilder->weld_radio_button("no_persona"))
38 , m_xDefaultPersona(m_xBuilder->weld_radio_button("default_persona"))
40 for (sal_uInt32 i = 0; i < MAX_DEFAULT_PERSONAS; ++i)
42 OString sDefaultId("default" + OString::number(i));
43 m_vDefaultPersonaImages[i] = m_xBuilder->weld_toggle_button(sDefaultId);
44 m_vDefaultPersonaImages[i]->connect_clicked(
45 LINK(this, SvxPersonalizationTabPage, DefaultPersona));
48 LoadDefaultImages();
51 SvxPersonalizationTabPage::~SvxPersonalizationTabPage() {}
53 std::unique_ptr<SfxTabPage> SvxPersonalizationTabPage::Create(weld::Container* pPage,
54 weld::DialogController* pController,
55 const SfxItemSet* rSet)
57 return std::make_unique<SvxPersonalizationTabPage>(pPage, pController, *rSet);
60 bool SvxPersonalizationTabPage::FillItemSet(SfxItemSet*)
62 // persona
63 OUString aPersona("default");
64 if (m_xNoPersona->get_active())
65 aPersona = "no";
67 bool bModified = false;
68 uno::Reference<uno::XComponentContext> xContext(comphelper::getProcessComponentContext());
69 if (xContext.is()
70 && (aPersona != officecfg::Office::Common::Misc::Persona::get(xContext)
71 || m_aPersonaSettings
72 != officecfg::Office::Common::Misc::PersonaSettings::get(xContext)))
74 bModified = true;
77 // write
78 std::shared_ptr<comphelper::ConfigurationChanges> batch(
79 comphelper::ConfigurationChanges::create());
80 if (aPersona == "no")
81 m_aPersonaSettings.clear();
82 officecfg::Office::Common::Misc::Persona::set(aPersona, batch);
83 officecfg::Office::Common::Misc::PersonaSettings::set(m_aPersonaSettings, batch);
84 batch->commit();
86 if (bModified)
88 // broadcast the change
89 DataChangedEvent aDataChanged(DataChangedEventType::SETTINGS, nullptr,
90 AllSettingsFlags::STYLE);
91 Application::NotifyAllWindows(aDataChanged);
94 return bModified;
97 void SvxPersonalizationTabPage::Reset(const SfxItemSet*)
99 uno::Reference<uno::XComponentContext> xContext(comphelper::getProcessComponentContext());
101 // persona
102 OUString aPersona("default");
103 if (xContext.is())
105 aPersona = officecfg::Office::Common::Misc::Persona::get(xContext);
106 m_aPersonaSettings = officecfg::Office::Common::Misc::PersonaSettings::get(xContext);
109 if (aPersona == "no")
110 m_xNoPersona->set_active(true);
111 else
112 m_xDefaultPersona->set_active(true);
115 void SvxPersonalizationTabPage::LoadDefaultImages()
117 // Load the pre saved personas
119 OUString gallery = "$BRAND_BASE_DIR/" LIBO_SHARE_FOLDER "/gallery/personas/";
120 rtl::Bootstrap::expandMacros(gallery);
121 OUString aPersonasList = gallery + "personas_list.txt";
122 SvFileStream aStream(aPersonasList, StreamMode::READ);
123 GraphicFilter aFilter;
124 Graphic aGraphic;
125 sal_Int32 nIndex = 0;
126 bool foundOne = false;
128 while (aStream.IsOpen() && !aStream.eof() && nIndex < MAX_DEFAULT_PERSONAS)
130 OString aLine;
131 OUString aPersonaSetting, aPreviewFile, aName;
132 sal_Int32 nParseIndex = 0;
134 aStream.ReadLine(aLine);
135 aPersonaSetting = OStringToOUString(aLine, RTL_TEXTENCODING_UTF8);
136 aName = aPersonaSetting.getToken(1, ';', nParseIndex);
137 aPreviewFile = aPersonaSetting.getToken(0, ';', nParseIndex);
139 if (aPreviewFile.isEmpty())
140 break;
142 m_vDefaultPersonaSettings.push_back(aPersonaSetting);
144 INetURLObject aURLObj(gallery + aPreviewFile);
145 aFilter.ImportGraphic(aGraphic, aURLObj);
147 Size aSize(aGraphic.GetSizePixel());
148 aSize.setWidth(aSize.Width() / 4);
149 aSize.setHeight(aSize.Height() / 1.5);
150 ScopedVclPtr<VirtualDevice> xVirDev
151 = m_vDefaultPersonaImages[nIndex]->create_virtual_device();
152 xVirDev->SetOutputSizePixel(aSize);
153 aGraphic.Draw(xVirDev.get(), Point(0, 0));
154 m_vDefaultPersonaImages[nIndex]->set_image(xVirDev.get());
155 xVirDev.disposeAndClear();
157 m_vDefaultPersonaImages[nIndex]->set_tooltip_text(aName);
158 m_vDefaultPersonaImages[nIndex++]->show();
159 foundOne = true;
162 m_xDefaultPersona->set_sensitive(foundOne);
165 IMPL_LINK(SvxPersonalizationTabPage, DefaultPersona, weld::Button&, rButton, void)
167 m_xDefaultPersona->set_active(true);
168 for (sal_Int32 nIndex = 0; nIndex < MAX_DEFAULT_PERSONAS; ++nIndex)
170 if (&rButton == m_vDefaultPersonaImages[nIndex].get())
171 m_aPersonaSettings = m_vDefaultPersonaSettings[nIndex];
172 else
173 m_vDefaultPersonaImages[nIndex]->set_active(false);
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */