1 /*************************************************************************
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * Copyright 2008 by Sun Microsystems, Inc.
6 * OpenOffice.org - a multi-platform office productivity suite
8 * $RCSfile: logstorage.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
28 ************************************************************************/
30 // MARKER(update_precomp.py): autogen include statement, do not remove
31 #include "precompiled_extensions.hxx"
33 #include "logstorage.hxx"
35 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
36 #include <com/sun/star/util/XStringSubstitution.hpp>
39 using namespace com::sun::star::io
;
40 using namespace com::sun::star::lang
;
41 using namespace com::sun::star::uno
;
42 using namespace com::sun::star::util
;
43 using ::com::sun::star::ucb::XSimpleFileAccess
;
44 using ::rtl::OUString
;
50 using namespace oooimprovement
;
52 static const OUString CSSU_PATHSUB
= OUString::createFromAscii("com.sun.star.util.PathSubstitution");
54 static OUString
getLogPathFromCfg(const Reference
<XMultiServiceFactory
>& sf
)
57 OUString result
=config
.getLogPath();
58 Reference
<XStringSubstitution
> path_sub(
59 sf
->createInstance(CSSU_PATHSUB
),
62 result
= path_sub
->substituteVariables(result
, sal_False
);
66 static bool isZipfile(const OUString
& fileurl
)
68 static const OUString file_extension
= OUString::createFromAscii(".zip");
69 return fileurl
.match(file_extension
, fileurl
.getLength()-file_extension
.getLength());
72 static bool isLogfile(const OUString
& fileurl
)
74 static const OUString file_extension
= OUString::createFromAscii(".csv");
75 static const OUString current
= OUString::createFromAscii("Current.csv");
77 fileurl
.match(file_extension
, fileurl
.getLength()-file_extension
.getLength())
78 && !fileurl
.match(current
, fileurl
.getLength()-current
.getLength());
81 static bool isZipOrLogFile(const OUString
& fileurl
)
83 return isZipfile(fileurl
) || isLogfile(fileurl
);
86 static Sequence
<OUString
> getAllLogStoragefiles(const Reference
<XMultiServiceFactory
>& sf
)
88 Reference
<XSimpleFileAccess
> file_access(
89 sf
->createInstance(OUString::createFromAscii("com.sun.star.ucb.SimpleFileAccess")),
91 return file_access
->getFolderContents(
92 getLogPathFromCfg(sf
),
96 static vector
<OUString
> getLogStoragefiles(
97 const Reference
<XMultiServiceFactory
>& sf
,
98 bool (*condition
)(const OUString
& path
))
100 Sequence
<OUString
> candidates
= getAllLogStoragefiles(sf
);
101 vector
<OUString
> result
;
102 result
.reserve(candidates
.getLength());
103 for(sal_Int32 idx
=0; idx
<candidates
.getLength(); ++idx
)
104 if(condition(candidates
[idx
]))
105 result
.push_back(candidates
[idx
]);
109 static void assureLogStorageExists(const Reference
<XMultiServiceFactory
>& sf
)
111 Reference
<XSimpleFileAccess
> file_access(
112 sf
->createInstance(OUString::createFromAscii("com.sun.star.ucb.SimpleFileAccess")),
114 OUString
log_path(getLogPathFromCfg(sf
));
115 if(!file_access
->isFolder(log_path
))
116 file_access
->createFolder(log_path
);
120 namespace oooimprovement
123 LogStorage::LogStorage(const Reference
<XMultiServiceFactory
>& sf
)
124 : m_ServiceFactory(sf
)
127 void LogStorage::assureExists()
129 assureLogStorageExists(m_ServiceFactory
);
132 void LogStorage::clear()
134 Reference
<XSimpleFileAccess
> file_access(
135 m_ServiceFactory
->createInstance(OUString::createFromAscii("com.sun.star.ucb.SimpleFileAccess")),
137 vector
<OUString
> files_to_kill
= getLogStoragefiles(m_ServiceFactory
, &isZipOrLogFile
);
138 for(vector
<OUString
>::iterator item
= files_to_kill
.begin();
139 item
!= files_to_kill
.end();
141 file_access
->kill(*item
);
144 const vector
<OUString
> LogStorage::getUnzippedLogFiles() const
145 { return getLogStoragefiles(m_ServiceFactory
, &isLogfile
); }
147 const vector
<OUString
> LogStorage::getZippedLogFiles() const
148 { return getLogStoragefiles(m_ServiceFactory
, &isZipfile
); }