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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* -*- 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 "SafeModeDialog.hxx"

#include <osl/file.hxx>
#include <sfx2/safemode.hxx>
#include <tools/diagnose_ex.h>
#include <vcl/svapp.hxx>
#include <vcl/weld.hxx>
#include <comphelper/processfactory.hxx>
#include <unotools/ZipPackageHelper.hxx>
#include <unotools/configmgr.hxx>
#include <svx/dialmgr.hxx>
#include <svx/strings.hrc>

#include <com/sun/star/task/OfficeRestartManager.hpp>
#include <com/sun/star/task/XInteractionHandler.hpp>
#include <com/sun/star/system/XSystemShellExecute.hpp>
#include <com/sun/star/system/SystemShellExecuteFlags.hpp>
#include <com/sun/star/system/SystemShellExecute.hpp>

using namespace css;

SafeModeDialog::SafeModeDialog(weld::Window* pParent)
    : GenericDialogController(pParent, "svx/ui/safemodedialog.ui", "SafeModeDialog")
    , mxBtnContinue(m_xBuilder->weld_button("btn_continue"))
    , mxBtnRestart(m_xBuilder->weld_button("btn_restart"))
    , mxBtnApply(m_xBuilder->weld_button("btn_apply"))
    , mxBoxRestore(m_xBuilder->weld_container("group_restore"))
    , mxBoxConfigure(m_xBuilder->weld_container("group_configure"))
    , mxBoxDeinstall(m_xBuilder->weld_container("group_deinstall"))
    , mxBoxReset(m_xBuilder->weld_container("group_reset"))
    , mxRadioRestore(m_xBuilder->weld_radio_button("radio_restore"))
    , mxRadioConfigure(m_xBuilder->weld_radio_button("radio_configure"))
    , mxRadioExtensions(m_xBuilder->weld_radio_button("radio_extensions"))
    , mxRadioReset(m_xBuilder->weld_radio_button("radio_reset"))
    , mxCBCheckProfilesafeConfig(m_xBuilder->weld_check_button("check_profilesafe_config"))
    , mxCBCheckProfilesafeExtensions(m_xBuilder->weld_check_button("check_profilesafe_extensions"))
    , mxCBDisableAllExtensions(m_xBuilder->weld_check_button("check_disable_all_extensions"))
    , mxCBDeinstallUserExtensions(m_xBuilder->weld_check_button("check_deinstall_user_extensions"))
    , mxCBResetSharedExtensions(m_xBuilder->weld_check_button("check_reset_shared_extensions"))
    , mxCBResetBundledExtensions(m_xBuilder->weld_check_button("check_reset_bundled_extensions"))
    , mxCBDisableHWAcceleration(m_xBuilder->weld_check_button("check_disable_hw_acceleration"))
    , mxCBResetCustomizations(m_xBuilder->weld_check_button("check_reset_customizations"))
    , mxCBResetWholeUserProfile(m_xBuilder->weld_check_button("check_reset_whole_userprofile"))
    , mxBugLink(m_xBuilder->weld_link_button("linkbutton_bugs"))
    , mxUserProfileLink(m_xBuilder->weld_link_button("linkbutton_profile"))
    , mxBtnCreateZip(m_xBuilder->weld_button("btn_create_zip"))
    , mxExpander(m_xBuilder->weld_expander("expander"))
    , maBackupFileHelper()
{
    m_xDialog->set_centered_on_parent(false);
    mxRadioRestore->connect_clicked(LINK(this, SafeModeDialog, RadioBtnHdl));
    mxRadioConfigure->connect_clicked(LINK(this, SafeModeDialog, RadioBtnHdl));
    mxRadioExtensions->connect_clicked(LINK(this, SafeModeDialog, RadioBtnHdl));
    mxRadioReset->connect_clicked(LINK(this, SafeModeDialog, RadioBtnHdl));

    mxBtnContinue->connect_clicked(LINK(this, SafeModeDialog, DialogBtnHdl));
    mxBtnRestart->connect_clicked(LINK(this, SafeModeDialog, DialogBtnHdl));
    mxBtnApply->connect_clicked(LINK(this, SafeModeDialog, DialogBtnHdl));

    mxCBCheckProfilesafeConfig->connect_toggled(LINK(this, SafeModeDialog, CheckBoxHdl));
    mxCBCheckProfilesafeExtensions->connect_toggled(LINK(this, SafeModeDialog, CheckBoxHdl));
    mxCBDisableAllExtensions->connect_toggled(LINK(this, SafeModeDialog, CheckBoxHdl));
    mxCBDeinstallUserExtensions->connect_toggled(LINK(this, SafeModeDialog, CheckBoxHdl));
    mxCBResetSharedExtensions->connect_toggled(LINK(this, SafeModeDialog, CheckBoxHdl));
    mxCBResetBundledExtensions->connect_toggled(LINK(this, SafeModeDialog, CheckBoxHdl));
    mxCBDisableHWAcceleration->connect_toggled(LINK(this, SafeModeDialog, CheckBoxHdl));
    mxCBResetCustomizations->connect_toggled(LINK(this, SafeModeDialog, CheckBoxHdl));
    mxCBResetWholeUserProfile->connect_toggled(LINK(this, SafeModeDialog, CheckBoxHdl));

    mxBtnCreateZip->connect_clicked(LINK(this, SafeModeDialog, CreateZipBtnHdl));

    // Disable restart btn until some checkbox is active
    mxBtnApply->set_sensitive(false);

    // Check the first radio button and call its handler,
    // it'll disable the relevant parts
    mxRadioRestore->set_active(true);
    RadioBtnHdl(*mxRadioRestore);

    // Set URL for help button (module=safemode)
    OUString sURL("http://hub.libreoffice.org/send-feedback/?LOversion=" + utl::ConfigManager::getAboutBoxProductVersion() +
        "&LOlocale=" + utl::ConfigManager::getUILocale() + "&LOmodule=safemode");
    mxBugLink->set_uri(sURL);

    mxUserProfileLink->set_uri(comphelper::BackupFileHelper::getUserProfileURL());
}

SafeModeDialog::~SafeModeDialog()
{
}

void SafeModeDialog::enableDisableWidgets()
{
    mxCBCheckProfilesafeConfig->set_sensitive(maBackupFileHelper.isPopPossible());
    mxCBCheckProfilesafeExtensions->set_sensitive(maBackupFileHelper.isPopPossibleExtensionInfo());
    mxCBDisableAllExtensions->set_sensitive(comphelper::BackupFileHelper::isTryDisableAllExtensionsPossible());
    mxCBDeinstallUserExtensions->set_sensitive(comphelper::BackupFileHelper::isTryDeinstallUserExtensionsPossible());
    mxCBResetSharedExtensions->set_sensitive(comphelper::BackupFileHelper::isTryResetSharedExtensionsPossible());
    mxCBResetBundledExtensions->set_sensitive(comphelper::BackupFileHelper::isTryResetBundledExtensionsPossible());
    mxCBResetCustomizations->set_sensitive(comphelper::BackupFileHelper::isTryResetCustomizationsPossible());

    // no disable of mxCBResetWholeUserProfile, always possible (as last choice)
}

short SafeModeDialog::run()
{
    short nRet = weld::GenericDialogController::run();
    // Remove the safe mode flag before exiting this dialog
    sfx2::SafeMode::removeFlag();
    return nRet;
}

void SafeModeDialog::applyChanges()
{
    // Restore
    if (mxRadioRestore->get_active())
    {
        if (mxCBCheckProfilesafeConfig->get_active())
        {
            // reset UserConfiguration to last known working state
            // ProfileSafeMode/BackupFileHelper
            maBackupFileHelper.tryPop();
        }

        if (mxCBCheckProfilesafeExtensions->get_active())
        {
            // reset State of installed Extensions to last known working state
            // ProfileSafeMode/BackupFileHelper
            maBackupFileHelper.tryPopExtensionInfo();
        }
    }

    // Configure
    if (mxRadioConfigure->get_active())
    {
        if (mxCBDisableAllExtensions->get_active())
        {
            // Disable all extensions
            comphelper::BackupFileHelper::tryDisableAllExtensions();
        }

        if (mxCBDisableHWAcceleration->get_active())
        {
            comphelper::BackupFileHelper::tryDisableHWAcceleration();
        }
    }

    // Deinstall
    if (mxRadioExtensions->get_active())
    {
        if (mxCBDeinstallUserExtensions->get_active())
        {
            // Deinstall all User Extensions (installed for User only)
            comphelper::BackupFileHelper::tryDeinstallUserExtensions();
        }

        if (mxCBResetSharedExtensions->get_active())
        {
            // Reset shared Extensions
            comphelper::BackupFileHelper::tryResetSharedExtensions();
        }
        if (mxCBResetBundledExtensions->get_active())
        {
            // Reset bundled Extensions
            comphelper::BackupFileHelper::tryResetBundledExtensions();
        }
    }

    // Reset
    if (mxRadioReset->get_active())
    {
        if (mxCBResetCustomizations->get_active())
        {
            // Reset customizations (Settings and UserInterface modifications)
            comphelper::BackupFileHelper::tryResetCustomizations();
        }

        if (mxCBResetWholeUserProfile->get_active())
        {
            // Reset the whole UserProfile
            comphelper::BackupFileHelper::tryResetUserProfile();
        }
    }

    // finally, restart
    css::task::OfficeRestartManager::get(comphelper::getProcessComponentContext())->requestRestart(
        css::uno::Reference< css::task::XInteractionHandler >());
}

IMPL_LINK(SafeModeDialog, RadioBtnHdl, weld::Button&, rBtn, void)
{
    if (&rBtn == mxRadioRestore.get())
    {
        // Enable the currently selected box
        mxBoxRestore->set_sensitive(true);
        // Make sure only possible choices are active
        enableDisableWidgets();
        // Disable the unselected boxes
        mxBoxReset->set_sensitive(false);
        mxBoxConfigure->set_sensitive(false);
        mxBoxDeinstall->set_sensitive(false);
    }
    else if (&rBtn == mxRadioConfigure.get())
    {
        // Enable the currently selected box
        mxBoxConfigure->set_sensitive(true);
        // Make sure only possible choices are active
        enableDisableWidgets();
        // Disable the unselected boxes
        mxBoxRestore->set_sensitive(false);
        mxBoxReset->set_sensitive(false);
        mxBoxDeinstall->set_sensitive(false);

    }
    else if (&rBtn == mxRadioExtensions.get())
    {
        // Enable the currently selected box
        mxBoxDeinstall->set_sensitive(true);
        // Make sure only possible choices are active
        enableDisableWidgets();
        // Disable the unselected boxes
        mxBoxRestore->set_sensitive(false);
        mxBoxConfigure->set_sensitive(false);
        mxBoxReset->set_sensitive(false);
    }
    else if (&rBtn == mxRadioReset.get())
    {
        // Enable the currently selected box
        mxBoxReset->set_sensitive(true);
        // Make sure only possible choices are active
        enableDisableWidgets();
        // Disable the unselected boxes
        mxBoxConfigure->set_sensitive(false);
        mxBoxRestore->set_sensitive(false);
        mxBoxDeinstall->set_sensitive(false);
    }
}

IMPL_LINK(SafeModeDialog, DialogBtnHdl, weld::Button&, rBtn, void)
{
    if (&rBtn == mxBtnContinue.get())
    {
        m_xDialog->response(RET_CLOSE);
    }
    else if (&rBtn == mxBtnRestart.get())
    {
        sfx2::SafeMode::putRestartFlag();
        m_xDialog->response(RET_CLOSE);
        uno::Reference< uno::XComponentContext > xContext = comphelper::getProcessComponentContext();
        css::task::OfficeRestartManager::get(xContext)->requestRestart(
            css::uno::Reference< css::task::XInteractionHandler >());
    }
    else if (&rBtn == mxBtnApply.get())
    {
        sfx2::SafeMode::putRestartFlag();
        m_xDialog->response(RET_CLOSE);
        applyChanges();
    }
}

namespace {
    class ProfileExportedDialog : public weld::GenericDialogController
    {
    private:
        std::unique_ptr<weld::Button> m_xButton;

        DECL_LINK(OpenHdl, weld::Button&, void);
    public:
        explicit ProfileExportedDialog(weld::Window* pParent);
    };

    ProfileExportedDialog::ProfileExportedDialog(weld::Window* pParent)
        : GenericDialogController(pParent, "svx/ui/profileexporteddialog.ui", "ProfileExportedDialog")
        , m_xButton(m_xBuilder->weld_button("ok"))
    {
        m_xButton->connect_clicked(LINK(this, ProfileExportedDialog, OpenHdl));
    }

    IMPL_LINK_NOARG(ProfileExportedDialog, OpenHdl, weld::Button&, void)<--- syntax error
    {
        const OUString uri(comphelper::BackupFileHelper::getUserProfileURL());
        css::uno::Reference< css::system::XSystemShellExecute > exec(
        css::system::SystemShellExecute::create(comphelper::getProcessComponentContext()));
        try {
            exec->execute(uri, OUString(), css::system::SystemShellExecuteFlags::URIS_ONLY);
        } catch (const css::uno::Exception &) {
            TOOLS_WARN_EXCEPTION("svx.dialog", "opening <" << uri << "> failed:");
        }
        m_xDialog->response(RET_OK);
    }
}

IMPL_LINK(SafeModeDialog, CreateZipBtnHdl, weld::Button&, /*rBtn*/, void)
{
    const OUString zipFileURL(comphelper::BackupFileHelper::getUserProfileURL() + "/libreoffice-profile.zip");
    osl::File::remove(zipFileURL); // Remove previous exports
    try
    {
        utl::ZipPackageHelper aZipHelper(comphelper::getProcessComponentContext(), zipFileURL);
        aZipHelper.addFolderWithContent(aZipHelper.getRootFolder(), comphelper::BackupFileHelper::getUserProfileWorkURL());
        aZipHelper.savePackage();
    }
    catch (const uno::Exception &)
    {
        std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
                                                                 VclMessageType::Warning, VclButtonsType::Ok,
                                                                 SvxResId(RID_SVXSTR_SAFEMODE_ZIP_FAILURE)));
        xBox->run();
        return;
    }

    ProfileExportedDialog aDialog(m_xDialog.get());
    aDialog.run();
}

IMPL_LINK(SafeModeDialog, CheckBoxHdl, weld::ToggleButton&, /*pCheckBox*/, void)
{
    const bool bEnable(
        mxCBCheckProfilesafeConfig->get_active() ||
        mxCBCheckProfilesafeExtensions->get_active() ||
        mxCBDisableAllExtensions->get_active() ||
        mxCBDeinstallUserExtensions->get_active() ||
        mxCBResetSharedExtensions->get_active() ||
        mxCBResetBundledExtensions->get_active() ||
        mxCBDisableHWAcceleration->get_active() ||
        mxCBResetCustomizations->get_active() ||
        mxCBResetWholeUserProfile->get_active());

    mxBtnApply->set_sensitive(bEnable);
}

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