summaryrefslogtreecommitdiff
path: root/cui/source/options/certpath.cxx
blob: 9ea41a5161c76aeb871cde3646bc57e66ef5bbde (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* -*- 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 <officecfg/Office/Common.hxx>
#include <osl/file.hxx>
#include <osl/security.hxx>
#include <osl/thread.h>
#include "svtools/treelistentry.hxx"
#include <unotools/securityoptions.hxx>
#include <cuires.hrc>
#include "certpath.hxx"
#include "dialmgr.hxx"

#include <com/sun/star/mozilla/MozillaBootstrap.hpp>
#include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
#include <com/sun/star/ui/dialogs/FolderPicker.hpp>
#include <comphelper/processfactory.hxx>

using namespace ::com::sun::star;

CertPathDialog::CertPathDialog(vcl::Window* pParent)
    : ModalDialog(pParent, "CertDialog", "cui/ui/certdialog.ui")
{
    get(m_pOKBtn, "ok");
    get(m_pAddBtn, "add");
    get(m_pCertPathListContainer, "paths");
    Size aSize(LogicToPixel(Size(210, 60), MapUnit::MapAppFont));
    m_pCertPathListContainer->set_width_request(aSize.Width());
    m_pCertPathListContainer->set_height_request(aSize.Height());
    m_pCertPathList =
        VclPtr<svx::SvxRadioButtonListBox>::Create(*m_pCertPathListContainer, 0);
    m_sAddDialogText = get<FixedText>("certdir")->GetText();
    m_sManual = get<FixedText>("manual")->GetText();

    static long aStaticTabs[]=
    {
        3, 0, 15, 75
    };

    m_pCertPathList->SvSimpleTable::SetTabs( aStaticTabs );

    OUString sProfile(get<FixedText>("profile")->GetText());
    OUString sDirectory(get<FixedText>("dir")->GetText());

    OUStringBuffer sHeader;
    sHeader.append('\t').append(sProfile).append('\t').append(sDirectory);
    m_pCertPathList->InsertHeaderEntry( sHeader.makeStringAndClear(), HEADERBAR_APPEND, HeaderBarItemBits::LEFT );
    m_pCertPathList->SetCheckButtonHdl( LINK( this, CertPathDialog, CheckHdl_Impl ) );

    m_pAddBtn->SetClickHdl( LINK( this, CertPathDialog, AddHdl_Impl ) );
    m_pOKBtn->SetClickHdl( LINK( this, CertPathDialog, OKHdl_Impl ) );

    try
    {
        mozilla::MozillaProductType productTypes[3] = {
            mozilla::MozillaProductType_Thunderbird,
            mozilla::MozillaProductType_Firefox,
            mozilla::MozillaProductType_Mozilla };
        const char* productNames[3] = {
            "thunderbird",
            "firefox",
            "mozilla" };

        uno::Reference<mozilla::XMozillaBootstrap> xMozillaBootstrap = mozilla::MozillaBootstrap::create( comphelper::getProcessComponentContext() );

        for (sal_Int32 i = 0; i < sal_Int32(SAL_N_ELEMENTS(productTypes)); ++i)
        {
            OUString profile = xMozillaBootstrap->getDefaultProfile(productTypes[i]);

            if (!profile.isEmpty())
            {
                OUString sProfilePath = xMozillaBootstrap->getProfilePath( productTypes[i], profile );
                OUStringBuffer sEntry;
                sEntry.append('\t').appendAscii(productNames[i]).append(':').append(profile).append('\t').append(sProfilePath);
                SvTreeListEntry *pEntry = m_pCertPathList->InsertEntry(sEntry.makeStringAndClear());
                OUString* pCertPath = new OUString(sProfilePath);
                pEntry->SetUserData(pCertPath);
            }
        }
    }
    catch (const uno::Exception&)
    {
    }

    SvTreeListEntry *pEntry = m_pCertPathList->GetEntry(0);
    if (pEntry)
    {
        m_pCertPathList->SetCheckButtonState(pEntry, SvButtonState::Checked);
        HandleCheckEntry(pEntry);
    }

    try
    {
        OUString sUserSetCertPath =
            officecfg::Office::Common::Security::Scripting::CertDir::get().get_value_or(OUString());

        if (!sUserSetCertPath.isEmpty())
            AddCertPath(m_sManual, sUserSetCertPath);
    }
    catch (const uno::Exception &e)
    {
        SAL_WARN("cui.options", "CertPathDialog::CertPathDialog(): caught exception" << e.Message);
    }

    const char* pEnv = getenv("MOZILLA_CERTIFICATE_FOLDER");
    if (pEnv)
        AddCertPath("$MOZILLA_CERTIFICATE_FOLDER", OUString(pEnv, strlen(pEnv), osl_getThreadTextEncoding()));
}

IMPL_LINK_NOARG(CertPathDialog, OKHdl_Impl, Button*, void)
{
    try
    {
        std::shared_ptr< comphelper::ConfigurationChanges > batch(
            comphelper::ConfigurationChanges::create());
        officecfg::Office::Common::Security::Scripting::CertDir::set(
            getDirectory(), batch);
        batch->commit();
    }
    catch (const uno::Exception &e)
    {
        SAL_WARN("cui.options", "CertPathDialog::OKHdl_Impl(): caught exception" << e.Message);
    }

    EndDialog(RET_OK);
}

OUString CertPathDialog::getDirectory() const
{
    SvTreeListEntry* pEntry = m_pCertPathList->FirstSelected();
    void* pCertPath = pEntry ? pEntry->GetUserData() : nullptr;
    return pCertPath ? *static_cast<OUString*>(pCertPath) : OUString();
}

CertPathDialog::~CertPathDialog()
{
    disposeOnce();
}

void CertPathDialog::dispose()
{
    SvTreeListEntry* pEntry = m_pCertPathList->First();
    while (pEntry)
    {
        OUString* pCertPath = static_cast<OUString*>(pEntry->GetUserData());
        delete pCertPath;
        pEntry = m_pCertPathList->Next( pEntry );
    }
    m_pCertPathList.disposeAndClear();
    m_pCertPathListContainer.clear();
    m_pAddBtn.clear();
    m_pOKBtn.clear();
    ModalDialog::dispose();
}

IMPL_LINK( CertPathDialog, CheckHdl_Impl, SvTreeListBox*, pList, void )
{
    SvTreeListEntry* pEntry = pList ? m_pCertPathList->GetEntry(m_pCertPathList->GetCurMousePoint())
                                : m_pCertPathList->FirstSelected();
    if (pEntry)
        m_pCertPathList->HandleEntryChecked(pEntry);
}

void CertPathDialog::HandleCheckEntry( SvTreeListEntry* _pEntry )
{
    m_pCertPathList->Select( _pEntry );
    SvButtonState eState = m_pCertPathList->GetCheckButtonState( _pEntry );

    if (SvButtonState::Checked == eState)
    {
        // uncheck the other entries
        SvTreeListEntry* pEntry = m_pCertPathList->First();
        while (pEntry)
        {
            if (pEntry != _pEntry)
                m_pCertPathList->SetCheckButtonState(pEntry, SvButtonState::Unchecked);
            pEntry = m_pCertPathList->Next(pEntry);
        }
    }
    else
        m_pCertPathList->SetCheckButtonState(_pEntry, SvButtonState::Checked);
}

void CertPathDialog::AddCertPath(const OUString &rProfile, const OUString &rPath)
{
    SvTreeListEntry* pEntry = m_pCertPathList->First();
    while (pEntry)
    {
        OUString* pCertPath = static_cast<OUString*>(pEntry->GetUserData());
        //already exists, just select the original one
        if (*pCertPath == rPath)
        {
            m_pCertPathList->SetCheckButtonState(pEntry, SvButtonState::Checked);
            HandleCheckEntry(pEntry);
            return;
        }
        pEntry = m_pCertPathList->Next(pEntry);
    }

    OUString sEntry( "\t" + rProfile + "\t" + rPath );
    pEntry = m_pCertPathList->InsertEntry(sEntry);
    OUString* pCertPath = new OUString(rPath);
    pEntry->SetUserData(pCertPath);
    m_pCertPathList->SetCheckButtonState(pEntry, SvButtonState::Checked);
    HandleCheckEntry(pEntry);
}

IMPL_LINK_NOARG(CertPathDialog, AddHdl_Impl, Button*, void)
{
    try
    {
        uno::Reference<ui::dialogs::XFolderPicker2> xFolderPicker = ui::dialogs::FolderPicker::create(comphelper::getProcessComponentContext());

        OUString sURL;
        osl::Security().getHomeDir(sURL);
        xFolderPicker->setDisplayDirectory(sURL);
        xFolderPicker->setDescription(m_sAddDialogText);

        if (xFolderPicker->execute() == ui::dialogs::ExecutableDialogResults::OK)
        {
            sURL = xFolderPicker->getDirectory();
            OUString aPath;
            if (osl::FileBase::E_None == osl::FileBase::getSystemPathFromFileURL(sURL, aPath))
                AddCertPath(m_sManual, aPath);
        }
    }
    catch (uno::Exception & e)
    {
        SAL_WARN("cui.options", "caught UNO exception: " << e.Message);
    }
}

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