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 "logstorage.hxx"
28cdf0e10cSrcweir #include "config.hxx"
29cdf0e10cSrcweir #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
30cdf0e10cSrcweir #include <com/sun/star/util/XStringSubstitution.hpp>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir 
33cdf0e10cSrcweir using namespace com::sun::star::io;
34cdf0e10cSrcweir using namespace com::sun::star::lang;
35cdf0e10cSrcweir using namespace com::sun::star::uno;
36cdf0e10cSrcweir using namespace com::sun::star::util;
37cdf0e10cSrcweir using ::com::sun::star::ucb::XSimpleFileAccess;
38cdf0e10cSrcweir using ::rtl::OUString;
39cdf0e10cSrcweir using ::std::vector;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir 
42cdf0e10cSrcweir namespace
43cdf0e10cSrcweir {
44cdf0e10cSrcweir     using namespace oooimprovement;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir     static const OUString CSSU_PATHSUB = OUString::createFromAscii("com.sun.star.util.PathSubstitution");
47cdf0e10cSrcweir 
getLogPathFromCfg(const Reference<XMultiServiceFactory> & sf)48cdf0e10cSrcweir     static OUString getLogPathFromCfg(const Reference<XMultiServiceFactory>& sf)
49cdf0e10cSrcweir     {
50cdf0e10cSrcweir         Config config(sf);
51cdf0e10cSrcweir         OUString result=config.getLogPath();
52cdf0e10cSrcweir         Reference<XStringSubstitution> path_sub(
53cdf0e10cSrcweir             sf->createInstance(CSSU_PATHSUB),
54cdf0e10cSrcweir             UNO_QUERY);
55cdf0e10cSrcweir         if(path_sub.is())
56cdf0e10cSrcweir             result = path_sub->substituteVariables(result, sal_False);
57cdf0e10cSrcweir         return result;
58cdf0e10cSrcweir     }
59cdf0e10cSrcweir 
isZipfile(const OUString & fileurl)60cdf0e10cSrcweir     static bool isZipfile(const OUString& fileurl)
61cdf0e10cSrcweir     {
62cdf0e10cSrcweir         static const OUString file_extension = OUString::createFromAscii(".zip");
63cdf0e10cSrcweir         return fileurl.match(file_extension, fileurl.getLength()-file_extension.getLength());
64cdf0e10cSrcweir     };
65cdf0e10cSrcweir 
isLogfile(const OUString & fileurl)66cdf0e10cSrcweir     static bool isLogfile(const OUString& fileurl)
67cdf0e10cSrcweir     {
68cdf0e10cSrcweir         static const OUString file_extension = OUString::createFromAscii(".csv");
69cdf0e10cSrcweir         static const OUString current = OUString::createFromAscii("Current.csv");
70cdf0e10cSrcweir         return
71cdf0e10cSrcweir             fileurl.match(file_extension, fileurl.getLength()-file_extension.getLength())
72cdf0e10cSrcweir             && !fileurl.match(current, fileurl.getLength()-current.getLength());
73cdf0e10cSrcweir     };
74cdf0e10cSrcweir 
isZipOrLogFile(const OUString & fileurl)75cdf0e10cSrcweir     static bool isZipOrLogFile(const OUString& fileurl)
76cdf0e10cSrcweir     {
77cdf0e10cSrcweir         return isZipfile(fileurl) || isLogfile(fileurl);
78cdf0e10cSrcweir     }
79cdf0e10cSrcweir 
getAllLogStoragefiles(const Reference<XMultiServiceFactory> & sf)80cdf0e10cSrcweir     static Sequence<OUString> getAllLogStoragefiles(const Reference<XMultiServiceFactory>& sf)
81cdf0e10cSrcweir     {
82cdf0e10cSrcweir         Reference<XSimpleFileAccess> file_access(
83cdf0e10cSrcweir             sf->createInstance(OUString::createFromAscii("com.sun.star.ucb.SimpleFileAccess")),
84cdf0e10cSrcweir             UNO_QUERY_THROW);
85cdf0e10cSrcweir         return file_access->getFolderContents(
86cdf0e10cSrcweir             getLogPathFromCfg(sf),
87cdf0e10cSrcweir             false);
88cdf0e10cSrcweir     };
89cdf0e10cSrcweir 
getLogStoragefiles(const Reference<XMultiServiceFactory> & sf,bool (* condition)(const OUString & path))90cdf0e10cSrcweir     static vector<OUString> getLogStoragefiles(
91cdf0e10cSrcweir         const Reference<XMultiServiceFactory>& sf,
92cdf0e10cSrcweir         bool (*condition)(const OUString& path))
93cdf0e10cSrcweir     {
94cdf0e10cSrcweir         Sequence<OUString> candidates = getAllLogStoragefiles(sf);
95cdf0e10cSrcweir         vector<OUString> result;
96cdf0e10cSrcweir         result.reserve(candidates.getLength());
97cdf0e10cSrcweir         for(sal_Int32 idx=0; idx<candidates.getLength(); ++idx)
98cdf0e10cSrcweir             if(condition(candidates[idx]))
99cdf0e10cSrcweir                 result.push_back(candidates[idx]);
100cdf0e10cSrcweir         return result;
101cdf0e10cSrcweir     };
102cdf0e10cSrcweir 
assureLogStorageExists(const Reference<XMultiServiceFactory> & sf)103cdf0e10cSrcweir     static void assureLogStorageExists(const Reference<XMultiServiceFactory>& sf)
104cdf0e10cSrcweir     {
105cdf0e10cSrcweir         Reference<XSimpleFileAccess> file_access(
106cdf0e10cSrcweir             sf->createInstance(OUString::createFromAscii("com.sun.star.ucb.SimpleFileAccess")),
107cdf0e10cSrcweir             UNO_QUERY_THROW);
108cdf0e10cSrcweir         OUString log_path(getLogPathFromCfg(sf));
109cdf0e10cSrcweir         if(!file_access->isFolder(log_path))
110cdf0e10cSrcweir             file_access->createFolder(log_path);
111cdf0e10cSrcweir     };
112cdf0e10cSrcweir }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir namespace oooimprovement
115cdf0e10cSrcweir {
116cdf0e10cSrcweir 
LogStorage(const Reference<XMultiServiceFactory> & sf)117cdf0e10cSrcweir     LogStorage::LogStorage(const Reference<XMultiServiceFactory>& sf)
118cdf0e10cSrcweir         : m_ServiceFactory(sf)
119cdf0e10cSrcweir     {}
120cdf0e10cSrcweir 
assureExists()121cdf0e10cSrcweir     void LogStorage::assureExists()
122cdf0e10cSrcweir     {
123cdf0e10cSrcweir         assureLogStorageExists(m_ServiceFactory);
124cdf0e10cSrcweir     }
125cdf0e10cSrcweir 
clear()126cdf0e10cSrcweir     void LogStorage::clear()
127cdf0e10cSrcweir     {
128cdf0e10cSrcweir         Reference<XSimpleFileAccess> file_access(
129cdf0e10cSrcweir             m_ServiceFactory->createInstance(OUString::createFromAscii("com.sun.star.ucb.SimpleFileAccess")),
130cdf0e10cSrcweir             UNO_QUERY_THROW);
131cdf0e10cSrcweir         vector<OUString> files_to_kill = getLogStoragefiles(m_ServiceFactory, &isZipOrLogFile);
132cdf0e10cSrcweir         for(vector<OUString>::iterator item = files_to_kill.begin();
133cdf0e10cSrcweir             item != files_to_kill.end();
134cdf0e10cSrcweir             item++)
135cdf0e10cSrcweir             file_access->kill(*item);
136cdf0e10cSrcweir     }
137cdf0e10cSrcweir 
getUnzippedLogFiles() const138cdf0e10cSrcweir     const vector<OUString> LogStorage::getUnzippedLogFiles() const
139cdf0e10cSrcweir     { return getLogStoragefiles(m_ServiceFactory, &isLogfile); }
140cdf0e10cSrcweir 
getZippedLogFiles() const141cdf0e10cSrcweir     const vector<OUString> LogStorage::getZippedLogFiles() const
142cdf0e10cSrcweir     { return getLogStoragefiles(m_ServiceFactory, &isZipfile); }
143cdf0e10cSrcweir }
144