summaryrefslogtreecommitdiffstats
path: root/cui/source/options/personalization.cxx
blob: 657344d60c754d71c999a50190203c5a087008f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <config_folders.h>

#include "personalization.hxx"

#include <dialmgr.hxx>

#include <comphelper/processfactory.hxx>
#include <officecfg/Office/Common.hxx>
#include <rtl/bootstrap.hxx>
#include <tools/urlobj.hxx>
#include <tools/stream.hxx>
#include <vcl/event.hxx>
#include <vcl/svapp.hxx>
#include <vcl/settings.hxx>
#include <vcl/graphicfilter.hxx>
#include <vcl/virdev.hxx>
#include <personas.hrc>

using namespace com::sun::star;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::beans;

// persona
SvxPersonalizationTabPage::SvxPersonalizationTabPage(weld::Container* pPage,
                                                     weld::DialogController* pController,
                                                     const SfxItemSet& rSet)
    : SfxTabPage(pPage, pController, "cui/ui/personalization_tab.ui", "PersonalizationTabPage",
                 &rSet)
    , m_xNoPersona(m_xBuilder->weld_radio_button("no_persona"))
    , m_xPersonaImg(m_xBuilder->weld_widget("lockpersona"))
    , m_xDefaultPersona(m_xBuilder->weld_radio_button("default_persona"))
    , m_xContentGrid(m_xBuilder->weld_container("gridpersonasetting"))
{
    for (sal_uInt32 i = 0; i < MAX_DEFAULT_PERSONAS; ++i)
    {
        OUString sDefaultId("default" + OUString::number(i));
        m_vDefaultPersonaImages[i] = m_xBuilder->weld_toggle_button(sDefaultId);
        m_vDefaultPersonaImages[i]->connect_clicked(
            LINK(this, SvxPersonalizationTabPage, DefaultPersona));
    }

    LoadDefaultImages();
}

SvxPersonalizationTabPage::~SvxPersonalizationTabPage() {}

std::unique_ptr<SfxTabPage> SvxPersonalizationTabPage::Create(weld::Container* pPage,
                                                              weld::DialogController* pController,
                                                              const SfxItemSet* rSet)
{
    return std::make_unique<SvxPersonalizationTabPage>(pPage, pController, *rSet);
}

OUString SvxPersonalizationTabPage::GetAllStrings()
{
    OUString sAllStrings;
    OUString radioButton[] = { "no_persona", "default_persona" };

    for (const auto& radio : radioButton)
    {
        if (const auto& pString = m_xBuilder->weld_radio_button(radio))
            sAllStrings += pString->get_label() + " ";
    }

    if (const auto& pString = m_xBuilder->weld_label("personas_label"))
        sAllStrings += pString->get_label() + " ";

    return sAllStrings.replaceAll("_", "");
}

bool SvxPersonalizationTabPage::FillItemSet(SfxItemSet*)
{
    // persona
    OUString aPersona("default");
    if (m_xNoPersona->get_active())
        aPersona = "no";

    bool bModified = false;
    if (aPersona != officecfg::Office::Common::Misc::Persona::get()
        || m_aPersonaSettings != officecfg::Office::Common::Misc::PersonaSettings::get())
    {
        bModified = true;
    }

    // write
    std::shared_ptr<comphelper::ConfigurationChanges> batch(
        comphelper::ConfigurationChanges::create());
    if (aPersona == "no")
        m_aPersonaSettings.clear();
    officecfg::Office::Common::Misc::Persona::set(aPersona, batch);
    officecfg::Office::Common::Misc::PersonaSettings::set(m_aPersonaSettings, batch);
    batch->commit();

    if (bModified)
    {
        // broadcast the change
        DataChangedEvent aDataChanged(DataChangedEventType::SETTINGS, nullptr,
                                      AllSettingsFlags::STYLE);
        Application::NotifyAllWindows(aDataChanged);
    }

    return bModified;
}

void SvxPersonalizationTabPage::Reset(const SfxItemSet*)
{
    // persona
    OUString aPersona = officecfg::Office::Common::Misc::Persona::get();
    m_aPersonaSettings = officecfg::Office::Common::Misc::PersonaSettings::get();

    if (aPersona == "no")
        m_xNoPersona->set_active(true);
    else
        m_xDefaultPersona->set_active(true);

    if (officecfg::Office::Common::Misc::Persona::isReadOnly())
    {
        m_xNoPersona->set_sensitive(false);
        m_xDefaultPersona->set_sensitive(false);
        m_xPersonaImg->set_visible(true);
    }

    if (officecfg::Office::Common::Misc::PersonaSettings::isReadOnly())
        m_xContentGrid->set_sensitive(false);
}

void SvxPersonalizationTabPage::LoadDefaultImages()
{
    // Load the pre saved personas

    OUString gallery = "$BRAND_BASE_DIR/" LIBO_SHARE_FOLDER "/gallery/personas/";
    rtl::Bootstrap::expandMacros(gallery);
    OUString aPersonasList = gallery + "personas_list.txt";
    SvFileStream aStream(aPersonasList, StreamMode::READ);
    GraphicFilter aFilter;
    Graphic aGraphic;
    sal_Int32 nIndex = 0;
    bool foundOne = false;

    OStringBuffer aLine;
    int nLineNumberFilePersona = 0;
    while (aStream.IsOpen() && !aStream.eof() && nIndex < MAX_DEFAULT_PERSONAS)
    {
        OUString aPersonaSetting, aPreviewFile, aName;
        sal_Int32 nParseIndex = 0;

        aStream.ReadLine(aLine);
        aPersonaSetting = OStringToOUString(aLine, RTL_TEXTENCODING_UTF8);
        aName = CuiResId(RID_PERSONAS_COLOR[nLineNumberFilePersona].first);
        aPreviewFile = aPersonaSetting.getToken(2, ';', nParseIndex);

        if (aPreviewFile.isEmpty())
            break;

        m_vDefaultPersonaSettings.push_back(aPersonaSetting);

        INetURLObject aURLObj(rtl::Concat2View(gallery + aPreviewFile));
        aFilter.ImportGraphic(aGraphic, aURLObj);

        Size aSize(aGraphic.GetSizePixel());
        aSize.setWidth(aSize.Width() / 4);
        aSize.setHeight(aSize.Height() / 1.5);
        ScopedVclPtr<VirtualDevice> xVirDev
            = m_vDefaultPersonaImages[nIndex]->create_virtual_device();
        xVirDev->SetOutputSizePixel(aSize);
        aGraphic.Draw(*xVirDev, Point(0, 0));
        m_vDefaultPersonaImages[nIndex]->set_image(xVirDev.get());
        xVirDev.disposeAndClear();

        m_vDefaultPersonaImages[nIndex]->set_tooltip_text(aName);
        m_vDefaultPersonaImages[nIndex++]->show();
        foundOne = true;
        ++nLineNumberFilePersona;
    }

    m_xDefaultPersona->set_sensitive(foundOne);
}

IMPL_LINK(SvxPersonalizationTabPage, DefaultPersona, weld::Button&, rButton, void)
{
    m_xDefaultPersona->set_active(true);
    for (sal_Int32 nIndex = 0; nIndex < MAX_DEFAULT_PERSONAS; ++nIndex)
    {
        if (&rButton == m_vDefaultPersonaImages[nIndex].get())
            m_aPersonaSettings = m_vDefaultPersonaSettings[nIndex];
        else
            m_vDefaultPersonaImages[nIndex]->set_active(false);
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */