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