1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_extensions.hxx"
26 
27 #include "logpacker.hxx"
28 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
29 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
30 #include <com/sun/star/embed/XStorage.hpp>
31 #include <com/sun/star/embed/XTransactedObject.hpp>
32 #include <com/sun/star/embed/ElementModes.hpp>
33 #include <rtl/ustrbuf.hxx>
34 
35 
36 using namespace com::sun::star::embed;
37 using namespace com::sun::star::io;
38 using namespace com::sun::star::lang;
39 using namespace com::sun::star::uno;
40 using ::com::sun::star::ucb::XSimpleFileAccess;
41 using ::rtl::OUString;
42 using ::rtl::OUStringBuffer;
43 
44 
45 namespace
46 {
getZipfileurl(const OUString & csvfileurl)47     static const OUString getZipfileurl(const OUString& csvfileurl)
48     {
49         OUStringBuffer buf(csvfileurl);
50         buf.setLength(csvfileurl.getLength()-3);
51         buf.appendAscii("zip");
52         return buf.makeStringAndClear();
53     };
54 
countLines(const Sequence<sal_Int8> & data)55     static sal_Int32 countLines(const Sequence<sal_Int8>& data)
56     {
57         sal_Int32 result = 0;
58         for(sal_Int32 idx = data.getLength()-1; idx>=0; --idx)
59             if(data[idx]==0x0a) result++;
60         return result;
61     };
62 }
63 
64 namespace oooimprovement
65 {
LogPacker(const Reference<XMultiServiceFactory> & sf)66     LogPacker::LogPacker(const Reference<XMultiServiceFactory>& sf)
67         : m_ServiceFactory(sf)
68     {}
69 
pack(const OUString & fileurl)70     sal_Int32 LogPacker::pack(const OUString& fileurl)
71     {
72         Reference<XSimpleFileAccess> file_access(
73             m_ServiceFactory->createInstance(OUString::createFromAscii("com.sun.star.ucb.SimpleFileAccess")),
74             UNO_QUERY_THROW);
75 
76         Reference<XStorage> storage;
77         {
78             Reference<XSingleServiceFactory> storage_factory(
79                 m_ServiceFactory->createInstance(OUString::createFromAscii("com.sun.star.embed.StorageFactory")),
80                 UNO_QUERY_THROW);
81             Sequence<Any> storage_init_args(2);
82             storage_init_args[0] = Any(getZipfileurl(fileurl));
83             storage_init_args[1] = Any(ElementModes::WRITE);
84             storage = Reference<XStorage>(
85                 storage_factory->createInstanceWithArguments(storage_init_args),
86                 UNO_QUERY_THROW);
87         }
88 
89         Reference<XOutputStream> zipped_stream = storage->openStreamElement(
90             OUString::createFromAscii("logdata.csv"),
91             ElementModes::WRITE)->getOutputStream();
92         Reference<XInputStream> unzipped_stream = file_access->openFileRead(fileurl);
93         const sal_Int32 bufsize = 1024;
94         sal_Int32 read_bytes;
95         sal_Int32 logged_events = -1; // ignore header row
96         Sequence<sal_Int8> buf(bufsize);
97         do
98         {
99             read_bytes = unzipped_stream->readBytes(buf, bufsize);
100             buf.realloc(read_bytes);
101             logged_events += countLines(buf);
102             zipped_stream->writeBytes(buf);
103         } while(read_bytes == bufsize);
104         unzipped_stream->closeInput();
105         zipped_stream->flush();
106         zipped_stream->closeOutput();
107         Reference<XTransactedObject>(storage, UNO_QUERY_THROW)->commit();
108         file_access->kill(fileurl);
109         return logged_events;
110     }
111 }
112