diff options
author | Samuel Mehrbrodt <Samuel.Mehrbrodt@cib.de> | 2016-11-02 10:26:23 +0100 |
---|---|---|
committer | Katarina Behrens <Katarina.Behrens@cib.de> | 2017-01-19 18:12:52 +0100 |
commit | 2b89fc89d949d295b8c7d863d6fdb4b967445680 (patch) | |
tree | 162a743a8ffd0aa705faafc0d109e93865d9f3db | |
parent | SafeModeDialog: Close dialog before restarting (diff) | |
download | core-2b89fc89d949d295b8c7d863d6fdb4b967445680.tar.gz core-2b89fc89d949d295b8c7d863d6fdb4b967445680.zip |
Add helper for creating zip files
Change-Id: I8b49b74622bf4d671d640e4cd06dc2da348ee222
Reviewed-on: https://gerrit.libreoffice.org/30478
Reviewed-by: Samuel Mehrbrodt <Samuel.Mehrbrodt@cib.de>
Tested-by: Samuel Mehrbrodt <Samuel.Mehrbrodt@cib.de>
-rw-r--r-- | include/unotools/ZipPackageHelper.hxx | 65 | ||||
-rw-r--r-- | svx/source/dialog/SafeModeDialog.cxx | 72 | ||||
-rw-r--r-- | unotools/Library_utl.mk | 1 | ||||
-rw-r--r-- | unotools/source/misc/ZipPackageHelper.cxx | 199 |
4 files changed, 333 insertions, 4 deletions
diff --git a/include/unotools/ZipPackageHelper.hxx b/include/unotools/ZipPackageHelper.hxx new file mode 100644 index 000000000000..77c8b9178a8a --- /dev/null +++ b/include/unotools/ZipPackageHelper.hxx @@ -0,0 +1,65 @@ +/* -*- 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +#ifndef INCLUDED_UNOTOOLS_ZIPPACKAGEHELPER_HXX +#define INCLUDED_UNOTOOLS_ZIPPACKAGEHELPER_HXX + +#include <unotools/unotoolsdllapi.h> + +#include <com/sun/star/container/XHierarchicalNameAccess.hpp> +#include <com/sun/star/lang/XSingleServiceFactory.hpp> +#include <com/sun/star/lang/XMultiServiceFactory.hpp> +#include <com/sun/star/uno/XComponentContext.hpp> +#include <com/sun/star/uno/XInterface.hpp> +#include <com/sun/star/io/XInputStream.hpp> + +namespace utl { + +class UNOTOOLS_DLLPUBLIC ZipPackageHelper +{ +public: + ZipPackageHelper( const css::uno::Reference< css::uno::XComponentContext >& rxContext, + const OUString& sPackageURL); + + void savePackage(); + + void addFile( css::uno::Reference< css::uno::XInterface >& xRootFolder, + const OUString& rSourceFile ) + throw( css::uno::Exception, std::exception ); + + css::uno::Reference< css::uno::XInterface > addFolder( css::uno::Reference< css::uno::XInterface >& xRootFolder, + const OUString& rName ) + throw( css::uno::Exception, std::exception ); + + void addFolderWithContent( css::uno::Reference< css::uno::XInterface >& xRootFolder, + const OUString& rDirURL ); + + css::uno::Reference< css::uno::XInterface >& getRootFolder(); + +private: + css::uno::Reference< css::uno::XComponentContext > mxContext; + css::uno::Reference< css::container::XHierarchicalNameAccess > mxHNameAccess; + css::uno::Reference< css::lang::XSingleServiceFactory > mxFactory; + css::uno::Reference< css::uno::XInterface > mxRootFolder; +}; + +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/svx/source/dialog/SafeModeDialog.cxx b/svx/source/dialog/SafeModeDialog.cxx index e4c8b383f844..4640bfbfefa9 100644 --- a/svx/source/dialog/SafeModeDialog.cxx +++ b/svx/source/dialog/SafeModeDialog.cxx @@ -17,8 +17,11 @@ #include <vcl/layout.hxx> #include <comphelper/anytostring.hxx> #include <comphelper/processfactory.hxx> +#include <unotools/ZipPackageHelper.hxx> #include <cppuhelper/exc_hlp.hxx> #include <unotools/configmgr.hxx> +#include <svx/dialmgr.hxx> +#include <svx/dialogs.hrc> #include <com/sun/star/frame/Desktop.hpp> #include <com/sun/star/frame/XDesktop2.hpp> @@ -79,14 +82,15 @@ SafeModeDialog::SafeModeDialog(vcl::Window* pParent) get(mpBugLink, "linkbutton_bugs"); get(mpUserProfileLink, "linkbutton_profile"); + get(mpBtnCreateZip, "btn_create_zip"); mpRadioRestore->SetClickHdl(LINK(this, SafeModeDialog, RadioBtnHdl)); mpRadioConfigure->SetClickHdl(LINK(this, SafeModeDialog, RadioBtnHdl)); mpRadioReset->SetClickHdl(LINK(this, SafeModeDialog, RadioBtnHdl)); - mpBtnContinue->SetClickHdl(LINK(this, SafeModeDialog, BtnHdl)); - mpBtnQuit->SetClickHdl(LINK(this, SafeModeDialog, BtnHdl)); - mpBtnRestart->SetClickHdl(LINK(this, SafeModeDialog, BtnHdl)); + mpBtnContinue->SetClickHdl(LINK(this, SafeModeDialog, DialogBtnHdl)); + mpBtnQuit->SetClickHdl(LINK(this, SafeModeDialog, DialogBtnHdl)); + mpBtnRestart->SetClickHdl(LINK(this, SafeModeDialog, DialogBtnHdl)); mpCBCheckProfilesafeConfig->SetToggleHdl(LINK(this, SafeModeDialog, CheckBoxHdl)); mpCBCheckProfilesafeExtensions->SetToggleHdl(LINK(this, SafeModeDialog, CheckBoxHdl)); @@ -98,6 +102,7 @@ SafeModeDialog::SafeModeDialog(vcl::Window* pParent) mpCBResetWholeUserProfile->SetToggleHdl(LINK(this, SafeModeDialog, CheckBoxHdl)); mpBugLink->SetClickHdl(LINK(this, SafeModeDialog, HandleHyperlink)); + mpBtnCreateZip->SetClickHdl(LINK(this, SafeModeDialog, CreateZipBtnHdl)); // Disable restart btn until some checkbox is active mpBtnRestart->Disable(); @@ -176,6 +181,7 @@ void SafeModeDialog::dispose() mpBugLink.clear(); mpUserProfileLink.clear(); + mpBtnCreateZip.clear(); Dialog::dispose(); } @@ -276,6 +282,8 @@ IMPL_LINK(SafeModeDialog, RadioBtnHdl, Button*, pBtn) mpBoxReset->Disable(); mpBoxConfigure->Disable(); } + + return 0; } void SafeModeDialog::openWebBrowser(const OUString & sURL, const OUString &sTitle) @@ -302,7 +310,7 @@ void SafeModeDialog::openWebBrowser(const OUString & sURL, const OUString &sTitl } -IMPL_LINK(SafeModeDialog, BtnHdl, Button*, pBtn) +IMPL_LINK(SafeModeDialog, DialogBtnHdl, Button*, pBtn) { if (pBtn == mpBtnContinue.get()) { @@ -318,6 +326,62 @@ IMPL_LINK(SafeModeDialog, BtnHdl, Button*, pBtn) Close(); applyChanges(); } + + return 0; +} + +namespace { + class ProfileExportedDialog : public ModalDialog + { + private: + DECL_LINK(OpenHdl, void *); + public: + explicit ProfileExportedDialog(); + }; + + ProfileExportedDialog::ProfileExportedDialog() + : ModalDialog(nullptr, "SafeModeQueryDialog", "svx/ui/profileexporteddialog.ui") + { + get<Button>("openfolder")->SetClickHdl(LINK(this, ProfileExportedDialog, OpenHdl)); + } + + IMPL_LINK_NOARG(ProfileExportedDialog, OpenHdl) + { + 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 (css::uno::Exception) { + } + EndDialog(RET_OK); + + return 0; + } +} + +IMPL_LINK(SafeModeDialog, CreateZipBtnHdl, Button*, /*pBtn*/ ) +{ + const OUString zipFileName("libreoffice-profile.zip"); + const OUString zipFileURL(comphelper::BackupFileHelper::getUserProfileURL() + "/" + zipFileName); + osl::File::remove(zipFileURL); // Remove previous exports + try + { + utl::ZipPackageHelper aZipHelper(comphelper::getProcessComponentContext(), zipFileURL); + aZipHelper.addFolderWithContent(aZipHelper.getRootFolder(), comphelper::BackupFileHelper::getUserProfileWorkURL()); + aZipHelper.savePackage(); + } + catch (uno::Exception) + { + ScopedVclPtrInstance< MessageDialog > aErrorBox(this, SVX_RESSTR(RID_SVXSTR_SAFEMODE_ZIP_FAILURE)); + aErrorBox->Execute(); + return 1; + } + + ScopedVclPtrInstance< ProfileExportedDialog > aDialog; + aDialog->Execute(); + + return 0; } IMPL_LINK(SafeModeDialog, CheckBoxHdl, CheckBox*, /*pCheckBox*/ ) diff --git a/unotools/Library_utl.mk b/unotools/Library_utl.mk index 52f2eff48653..dba0c1695820 100644 --- a/unotools/Library_utl.mk +++ b/unotools/Library_utl.mk @@ -104,6 +104,7 @@ $(eval $(call gb_Library_add_exception_objects,utl,\ unotools/source/misc/mediadescriptor \ unotools/source/misc/sharedunocomponent \ unotools/source/misc/syslocale \ + unotools/source/misc/ZipPackageHelper \ unotools/source/streaming/streamhelper \ unotools/source/streaming/streamwrap \ unotools/source/ucbhelper/localfilehelper \ diff --git a/unotools/source/misc/ZipPackageHelper.cxx b/unotools/source/misc/ZipPackageHelper.cxx new file mode 100644 index 000000000000..0656c45f29d8 --- /dev/null +++ b/unotools/source/misc/ZipPackageHelper.cxx @@ -0,0 +1,199 @@ +/* -*- 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <com/sun/star/io/XActiveDataControl.hpp> +#include <com/sun/star/io/XActiveDataSource.hpp> +#include <com/sun/star/frame/XConfigManager.hpp> +#include <com/sun/star/io/XInputStream.hpp> +#include <com/sun/star/io/XActiveDataSink.hpp> +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/beans/NamedValue.hpp> +#include <com/sun/star/container/XNamed.hpp> +#include <com/sun/star/container/XChild.hpp> +#include <com/sun/star/container/XNameContainer.hpp> +#include <com/sun/star/util/XChangesBatch.hpp> +#include <com/sun/star/lang/XUnoTunnel.hpp> + +#include <unotools/ZipPackageHelper.hxx> +#include <comphelper/processfactory.hxx> +#include <comphelper/oslfile2streamwrap.hxx> +#include <comphelper/storagehelper.hxx> +#include <osl/file.hxx> +#include <unotools/pathoptions.hxx> +#include <unotools/streamwrap.hxx> +#include <unotools/tempfile.hxx> +#include <svl/urihelper.hxx> +#include <tools/stream.hxx> +#include <tools/urlobj.hxx> + +#include <rtl/uri.hxx> + +using namespace utl; +using namespace osl; +using namespace comphelper; +using namespace com::sun::star; +using namespace com::sun::star::lang; +using namespace com::sun::star::frame; +using namespace com::sun::star::uno; +using namespace com::sun::star::util; +using namespace com::sun::star::container; +using namespace com::sun::star::beans; +using namespace com::sun::star::io; + +using ::rtl::Uri; + +ZipPackageHelper::ZipPackageHelper( + const Reference< XComponentContext >& rxContext, + const OUString& sPackageURL) +: mxContext( rxContext ), + mxHNameAccess(), + mxFactory(), + mxRootFolder() +{ + // create the package zip file + Sequence< Any > aArguments( 2 ); + aArguments[ 0 ] <<= sPackageURL; + + // let ZipPackage be used + beans::NamedValue aArg; + aArg.Name = "StorageFormat"; + aArg.Value <<= OUString(ZIP_STORAGE_FORMAT_STRING); + aArguments[ 1 ] <<= aArg; + + Reference< XHierarchicalNameAccess > xHNameAccess( + mxContext->getServiceManager()->createInstanceWithArgumentsAndContext( + "com.sun.star.packages.comp.ZipPackage", + aArguments, mxContext ), UNO_QUERY); + mxHNameAccess = xHNameAccess; + + if( !mxHNameAccess.is() ) + return; + + Reference<XSingleServiceFactory> xFactory(mxHNameAccess, UNO_QUERY); + mxFactory = xFactory; + + // get root zip folder + OUString szRootFolder("/"); + mxHNameAccess->getByHierarchicalName( szRootFolder ) >>= mxRootFolder; +} + +static OUString encodeZipUri( const OUString& rURI ) +{ + return Uri::encode( rURI, rtl_UriCharClassUric, rtl_UriEncodeCheckEscapes, RTL_TEXTENCODING_UTF8 ); +} + +Reference< XInterface >& ZipPackageHelper::getRootFolder() +{ + return mxRootFolder; +} + +Reference< XInterface > ZipPackageHelper::addFolder( Reference< XInterface >& xRootFolder, + const OUString& rName ) + throw( css::uno::Exception, std::exception ) +{ + if ( rName == ".." || rName == "." ) + throw lang::IllegalArgumentException(); + + Sequence< Any > aArgs(1); + aArgs[0] <<= true; + + Reference< XInterface > xFolder( mxFactory->createInstanceWithArguments(aArgs) ); + Reference< XNamed > xNamed( xFolder, UNO_QUERY ); + Reference< XChild > xChild( xFolder, UNO_QUERY ); + + if( xNamed.is() && xChild.is() ) + { + OUString aName( encodeZipUri( rName ) ); + xNamed->setName( aName ); + xChild->setParent( xRootFolder ); + } + + return xFolder; +} + +void ZipPackageHelper::addFolderWithContent( Reference< XInterface >& xRootFolder, const OUString& rDirURL ) +{ + if (rDirURL.isEmpty()) + return; + + osl::Directory aDirectory(rDirURL); + + if (aDirectory.open() != osl::FileBase::E_None) + return; + + osl::DirectoryItem aDirectoryItem; + + while (osl::FileBase::E_None == aDirectory.getNextItem(aDirectoryItem)) + { + osl::FileStatus aFileStatus(osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileURL | osl_FileStatus_Mask_FileName); + + if (osl::FileBase::E_None == aDirectoryItem.getFileStatus(aFileStatus)) + { + if (aFileStatus.isDirectory()) + { + const OUString aFileName(aFileStatus.getFileName()); + + if (!aFileName.isEmpty()) + { + Reference<XInterface> folder(addFolder(xRootFolder, aFileName)); + addFolderWithContent(folder, aFileStatus.getFileURL()); + } + } + else if (aFileStatus.isRegular()) + { + addFile(xRootFolder, aFileStatus.getFileURL()); + } + } + } +} + +void ZipPackageHelper::addFile( css::uno::Reference< css::uno::XInterface >& xRootFolder, + const OUString& rSourceFile ) + throw( Exception, std::exception ) +{ + OUString aFileURL( rSourceFile ); + + /*if( !aFileURL.matchIgnoreAsciiCase("file://") ) + { + aFileURL = URIHelper::SmartRel2Abs( aFileURL, Link<OUString *, bool>(), false ); + }*/ + + INetURLObject aURL( aFileURL ); + OUString aName( aURL.getName() ); + + SvFileStream* pStream = new SvFileStream(aFileURL, StreamMode::READ ); + Reference< XInputStream > xInput( new utl::OSeekableInputStreamWrapper( pStream, true ) ); + Reference< XActiveDataSink > xSink( mxFactory->createInstance(), UNO_QUERY ); + Reference< XUnoTunnel > xTunnel( xSink, UNO_QUERY ); + if( !xSink.is() || !xTunnel.is()) + return; + + Reference< XNameContainer > xNameContainer(xRootFolder, UNO_QUERY ); + xNameContainer->insertByName(encodeZipUri( aName ), makeAny(xTunnel)); + xSink->setInputStream( xInput ); +} + +void ZipPackageHelper::savePackage() +{ + Reference< XChangesBatch > xBatch( mxHNameAccess, UNO_QUERY ); + if( xBatch.is() ) + xBatch->commitChanges(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |