1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3*cdf0e10cSrcweir  *
4*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
7*cdf0e10cSrcweir  *
8*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
9*cdf0e10cSrcweir  *
10*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
11*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
12*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
13*cdf0e10cSrcweir  *
14*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
15*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
18*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
19*cdf0e10cSrcweir  *
20*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
21*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
22*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
23*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
24*cdf0e10cSrcweir  *
25*cdf0e10cSrcweir  ************************************************************************/
26*cdf0e10cSrcweir 
27*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
28*cdf0e10cSrcweir #include "precompiled_extensions.hxx"
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include "logpacker.hxx"
31*cdf0e10cSrcweir #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/embed/XStorage.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/embed/XTransactedObject.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/embed/ElementModes.hpp>
36*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir using namespace com::sun::star::embed;
40*cdf0e10cSrcweir using namespace com::sun::star::io;
41*cdf0e10cSrcweir using namespace com::sun::star::lang;
42*cdf0e10cSrcweir using namespace com::sun::star::uno;
43*cdf0e10cSrcweir using ::com::sun::star::ucb::XSimpleFileAccess;
44*cdf0e10cSrcweir using ::rtl::OUString;
45*cdf0e10cSrcweir using ::rtl::OUStringBuffer;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir namespace
49*cdf0e10cSrcweir {
50*cdf0e10cSrcweir     static const OUString getZipfileurl(const OUString& csvfileurl)
51*cdf0e10cSrcweir     {
52*cdf0e10cSrcweir         OUStringBuffer buf(csvfileurl);
53*cdf0e10cSrcweir         buf.setLength(csvfileurl.getLength()-3);
54*cdf0e10cSrcweir         buf.appendAscii("zip");
55*cdf0e10cSrcweir         return buf.makeStringAndClear();
56*cdf0e10cSrcweir     };
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir     static sal_Int32 countLines(const Sequence<sal_Int8>& data)
59*cdf0e10cSrcweir     {
60*cdf0e10cSrcweir         sal_Int32 result = 0;
61*cdf0e10cSrcweir         for(sal_Int32 idx = data.getLength()-1; idx>=0; --idx)
62*cdf0e10cSrcweir             if(data[idx]==0x0a) result++;
63*cdf0e10cSrcweir         return result;
64*cdf0e10cSrcweir     };
65*cdf0e10cSrcweir }
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir namespace oooimprovement
68*cdf0e10cSrcweir {
69*cdf0e10cSrcweir     LogPacker::LogPacker(const Reference<XMultiServiceFactory>& sf)
70*cdf0e10cSrcweir         : m_ServiceFactory(sf)
71*cdf0e10cSrcweir     {}
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir     sal_Int32 LogPacker::pack(const OUString& fileurl)
74*cdf0e10cSrcweir     {
75*cdf0e10cSrcweir         Reference<XSimpleFileAccess> file_access(
76*cdf0e10cSrcweir             m_ServiceFactory->createInstance(OUString::createFromAscii("com.sun.star.ucb.SimpleFileAccess")),
77*cdf0e10cSrcweir             UNO_QUERY_THROW);
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir         Reference<XStorage> storage;
80*cdf0e10cSrcweir         {
81*cdf0e10cSrcweir             Reference<XSingleServiceFactory> storage_factory(
82*cdf0e10cSrcweir                 m_ServiceFactory->createInstance(OUString::createFromAscii("com.sun.star.embed.StorageFactory")),
83*cdf0e10cSrcweir                 UNO_QUERY_THROW);
84*cdf0e10cSrcweir             Sequence<Any> storage_init_args(2);
85*cdf0e10cSrcweir             storage_init_args[0] = Any(getZipfileurl(fileurl));
86*cdf0e10cSrcweir             storage_init_args[1] = Any(ElementModes::WRITE);
87*cdf0e10cSrcweir             storage = Reference<XStorage>(
88*cdf0e10cSrcweir                 storage_factory->createInstanceWithArguments(storage_init_args),
89*cdf0e10cSrcweir                 UNO_QUERY_THROW);
90*cdf0e10cSrcweir         }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir         Reference<XOutputStream> zipped_stream = storage->openStreamElement(
93*cdf0e10cSrcweir             OUString::createFromAscii("logdata.csv"),
94*cdf0e10cSrcweir             ElementModes::WRITE)->getOutputStream();
95*cdf0e10cSrcweir         Reference<XInputStream> unzipped_stream = file_access->openFileRead(fileurl);
96*cdf0e10cSrcweir         const sal_Int32 bufsize = 1024;
97*cdf0e10cSrcweir         sal_Int32 read_bytes;
98*cdf0e10cSrcweir         sal_Int32 logged_events = -1; // ignore header row
99*cdf0e10cSrcweir         Sequence<sal_Int8> buf(bufsize);
100*cdf0e10cSrcweir         do
101*cdf0e10cSrcweir         {
102*cdf0e10cSrcweir             read_bytes = unzipped_stream->readBytes(buf, bufsize);
103*cdf0e10cSrcweir             buf.realloc(read_bytes);
104*cdf0e10cSrcweir             logged_events += countLines(buf);
105*cdf0e10cSrcweir             zipped_stream->writeBytes(buf);
106*cdf0e10cSrcweir         } while(read_bytes == bufsize);
107*cdf0e10cSrcweir         unzipped_stream->closeInput();
108*cdf0e10cSrcweir         zipped_stream->flush();
109*cdf0e10cSrcweir         zipped_stream->closeOutput();
110*cdf0e10cSrcweir         Reference<XTransactedObject>(storage, UNO_QUERY_THROW)->commit();
111*cdf0e10cSrcweir         file_access->kill(fileurl);
112*cdf0e10cSrcweir         return logged_events;
113*cdf0e10cSrcweir     }
114*cdf0e10cSrcweir }
115