tdf#152515 vcl: Implement Win32 vert CJK printing for all fonts
[LibreOffice.git] / unotools / source / misc / ZipPackageHelper.cxx
blob601238317a72b5810b7802f0123c9d586dc46166
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/io/XActiveDataSink.hpp>
21 #include <com/sun/star/beans/NamedValue.hpp>
22 #include <com/sun/star/container/XNamed.hpp>
23 #include <com/sun/star/container/XChild.hpp>
24 #include <com/sun/star/container/XNameContainer.hpp>
25 #include <com/sun/star/util/XChangesBatch.hpp>
26 #include <com/sun/star/uno/XComponentContext.hpp>
27 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
28 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
30 #include <unotools/ZipPackageHelper.hxx>
31 #include <comphelper/storagehelper.hxx>
32 #include <osl/file.hxx>
33 #include <unotools/streamwrap.hxx>
34 #include <tools/stream.hxx>
35 #include <tools/urlobj.hxx>
37 #include <rtl/uri.hxx>
39 namespace com::sun::star::io { class XInputStream; }
41 using namespace utl;
42 using namespace osl;
43 using namespace comphelper;
44 using namespace com::sun::star;
45 using namespace com::sun::star::lang;
46 using namespace com::sun::star::uno;
47 using namespace com::sun::star::util;
48 using namespace com::sun::star::container;
49 using namespace com::sun::star::beans;
50 using namespace com::sun::star::io;
52 using ::rtl::Uri;
54 ZipPackageHelper::ZipPackageHelper(
55 const Reference< XComponentContext >& rxContext,
56 const OUString& sPackageURL)
57 : mxContext( rxContext )
59 // create the package zip file
60 Sequence< Any > aArguments{
61 Any(sPackageURL),
62 // let ZipPackage be used
63 Any(beans::NamedValue(u"StorageFormat"_ustr, Any(ZIP_STORAGE_FORMAT_STRING)))
66 mxHNameAccess.set(
67 mxContext->getServiceManager()->createInstanceWithArgumentsAndContext(
68 u"com.sun.star.packages.comp.ZipPackage"_ustr,
69 aArguments, mxContext ), UNO_QUERY);
71 if( !mxHNameAccess.is() )
72 return;
74 mxFactory.set(mxHNameAccess, UNO_QUERY);
76 // get root zip folder
77 mxHNameAccess->getByHierarchicalName( u"/"_ustr ) >>= mxRootFolder;
80 static OUString encodeZipUri( const OUString& rURI )
82 return Uri::encode( rURI, rtl_UriCharClassUric, rtl_UriEncodeCheckEscapes, RTL_TEXTENCODING_UTF8 );
85 Reference< XInterface >& ZipPackageHelper::getRootFolder()
87 return mxRootFolder;
90 Reference< XInterface > ZipPackageHelper::addFolder( Reference< XInterface > const & xRootFolder,
91 const OUString& rName )
93 if ( rName == ".." || rName == "." )
94 throw lang::IllegalArgumentException();
96 Reference< XInterface > xFolder( mxFactory->createInstanceWithArguments({ Any(true) } ));
97 Reference< XNamed > xNamed( xFolder, UNO_QUERY );
98 Reference< XChild > xChild( xFolder, UNO_QUERY );
100 if( xNamed.is() && xChild.is() )
102 OUString aName( encodeZipUri( rName ) );
103 xNamed->setName( aName );
104 xChild->setParent( xRootFolder );
107 return xFolder;
110 void ZipPackageHelper::addFolderWithContent( Reference< XInterface > const & xRootFolder, const OUString& rDirURL )
112 if (rDirURL.isEmpty())
113 return;
115 osl::Directory aDirectory(rDirURL);
117 if (aDirectory.open() != osl::FileBase::E_None)
118 return;
120 osl::DirectoryItem aDirectoryItem;
122 while (osl::FileBase::E_None == aDirectory.getNextItem(aDirectoryItem))
124 osl::FileStatus aFileStatus(osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileURL | osl_FileStatus_Mask_FileName);
126 if (osl::FileBase::E_None == aDirectoryItem.getFileStatus(aFileStatus))
128 if (aFileStatus.isDirectory())
130 const OUString aFileName(aFileStatus.getFileName());
132 if (!aFileName.isEmpty())
134 Reference<XInterface> folder(addFolder(xRootFolder, aFileName));
135 addFolderWithContent(folder, aFileStatus.getFileURL());
138 else if (aFileStatus.isRegular())
140 addFile(xRootFolder, aFileStatus.getFileURL());
146 void ZipPackageHelper::addFile( css::uno::Reference< css::uno::XInterface > const & xRootFolder,
147 const OUString& rSourceFileURL )
149 INetURLObject aURL( rSourceFileURL );
150 OUString aName( aURL.getName() );
152 SvFileStream* pStream = new SvFileStream(rSourceFileURL, StreamMode::READ );
153 Reference< XInputStream > xInput( new utl::OSeekableInputStreamWrapper( pStream, true ) );
154 Reference< XActiveDataSink > xSink( mxFactory->createInstance(), UNO_QUERY );
155 assert(xSink); // this should never fail
156 if( !xSink.is() )
157 return;
159 Reference< XNameContainer > xNameContainer(xRootFolder, UNO_QUERY );
160 xNameContainer->insertByName(encodeZipUri( aName ), Any(xSink));
161 xSink->setInputStream( xInput );
164 void ZipPackageHelper::savePackage()
166 Reference< XChangesBatch > xBatch( mxHNameAccess, UNO_QUERY );
167 if( xBatch.is() )
168 xBatch->commitChanges();
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */