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