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