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