update emoji autocorrect entries from po-files
[LibreOffice.git] / unotools / source / ucbhelper / localfilehelper.cxx
blobb45976984a57c3499a87e9c53395068ba5cce9a7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/sdbc/XResultSet.hpp>
21 #include <com/sun/star/ucb/XContentAccess.hpp>
22 #include <com/sun/star/ucb/CommandAbortedException.hpp>
23 #include <com/sun/star/ucb/UniversalContentBroker.hpp>
24 #include <comphelper/processfactory.hxx>
25 #include <sal/log.hxx>
26 #include <unotools/localfilehelper.hxx>
27 #include <ucbhelper/fileidentifierconverter.hxx>
28 #include <rtl/ustring.hxx>
29 #include <osl/file.hxx>
30 #include <tools/urlobj.hxx>
31 #include <ucbhelper/content.hxx>
32 #include <vector>
34 using namespace ::osl;
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::ucb;
38 namespace utl
41 bool LocalFileHelper::ConvertSystemPathToURL( const OUString& rName, const OUString& rBaseURL, OUString& rReturn )
43 rReturn.clear();
45 Reference< XUniversalContentBroker > pBroker(
46 UniversalContentBroker::create(
47 comphelper::getProcessComponentContext() ) );
48 try
50 rReturn = ::ucbhelper::getFileURLFromSystemPath( pBroker, rBaseURL, rName );
52 catch ( ::com::sun::star::uno::RuntimeException& )
54 return false;
57 return !rReturn.isEmpty();
60 bool LocalFileHelper::ConvertURLToSystemPath( const OUString& rName, OUString& rReturn )
62 rReturn.clear();
63 Reference< XUniversalContentBroker > pBroker(
64 UniversalContentBroker::create(
65 comphelper::getProcessComponentContext() ) );
66 try
68 rReturn = ::ucbhelper::getSystemPathFromFileURL( pBroker, rName );
70 catch ( ::com::sun::star::uno::RuntimeException& )
74 return !rReturn.isEmpty();
77 bool LocalFileHelper::ConvertPhysicalNameToURL(const OUString& rName, OUString& rReturn)
79 rReturn.clear();
80 Reference< XUniversalContentBroker > pBroker(
81 UniversalContentBroker::create(
82 comphelper::getProcessComponentContext() ) );
83 try
85 OUString aBase( ::ucbhelper::getLocalFileURL() );
86 rReturn = ::ucbhelper::getFileURLFromSystemPath( pBroker, aBase, rName );
88 catch (const ::com::sun::star::uno::RuntimeException&)
92 return !rReturn.isEmpty();
95 bool LocalFileHelper::ConvertURLToPhysicalName(const OUString& rName, OUString& rReturn)
97 rReturn.clear();
98 Reference< XUniversalContentBroker > pBroker(
99 UniversalContentBroker::create(
100 comphelper::getProcessComponentContext() ) );
103 INetURLObject aObj( rName );
104 INetURLObject aLocal( ::ucbhelper::getLocalFileURL() );
105 if ( aObj.GetProtocol() == aLocal.GetProtocol() )
106 rReturn = ::ucbhelper::getSystemPathFromFileURL( pBroker, rName );
108 catch (const ::com::sun::star::uno::RuntimeException&)
112 return !rReturn.isEmpty();
115 bool LocalFileHelper::IsLocalFile(const OUString& rName)
117 OUString aTmp;
118 return ConvertURLToPhysicalName(rName, aTmp);
121 bool LocalFileHelper::IsFileContent(const OUString& rName)
123 OUString aTmp;
124 return ConvertURLToSystemPath(rName, aTmp);
127 typedef ::std::vector< OUString* > StringList_Impl;
129 ::com::sun::star::uno::Sequence < OUString > LocalFileHelper::GetFolderContents( const OUString& rFolder, bool bFolder )
131 StringList_Impl* pFiles = NULL;
134 ::ucbhelper::Content aCnt(
135 rFolder, Reference< XCommandEnvironment >(),
136 comphelper::getProcessComponentContext() );
137 Reference< ::com::sun::star::sdbc::XResultSet > xResultSet;
138 ::com::sun::star::uno::Sequence< OUString > aProps(1);
139 OUString* pProps = aProps.getArray();
140 pProps[0] = "Url";
144 ::ucbhelper::ResultSetInclude eInclude = bFolder ? ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS : ::ucbhelper::INCLUDE_DOCUMENTS_ONLY;
145 xResultSet = aCnt.createCursor( aProps, eInclude );
147 catch( ::com::sun::star::ucb::CommandAbortedException& )
150 catch( Exception& )
154 if ( xResultSet.is() )
156 pFiles = new StringList_Impl;
157 Reference< XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
160 while ( xResultSet->next() )
162 OUString aId = xContentAccess->queryContentIdentifierString();
163 OUString* pFile = new OUString( aId );
164 pFiles->push_back( pFile );
167 catch( ::com::sun::star::ucb::CommandAbortedException& )
170 catch( Exception& )
175 catch( Exception& )
179 if ( pFiles )
181 size_t nCount = pFiles->size();
182 Sequence < OUString > aRet( nCount );
183 OUString* pRet = aRet.getArray();
184 for ( size_t i = 0; i < nCount; ++i )
186 OUString* pFile = (*pFiles)[ i ];
187 pRet[i] = *( pFile );
188 delete pFile;
190 delete pFiles;
191 return aRet;
193 else
194 return Sequence < OUString > ();
197 void removeTree(OUString const & url) {
198 osl::Directory dir(url);
199 osl::FileBase::RC rc = dir.open();
200 switch (rc) {
201 case osl::FileBase::E_None:
202 break;
203 case osl::FileBase::E_NOENT:
204 return; //TODO: SAL_WARN if recursive
205 default:
206 SAL_WARN("desktop.app", "cannot open directory " << dir.getURL() << ": " << +rc);
207 return;
209 for (;;) {
210 osl::DirectoryItem i;
211 rc = dir.getNextItem(i, SAL_MAX_UINT32);
212 if (rc == osl::FileBase::E_NOENT) {
213 break;
215 if (rc != osl::FileBase::E_None) {
216 SAL_WARN( "desktop.app", "cannot iterate directory " << dir.getURL() << ": " << +rc);
217 break;
219 osl::FileStatus stat(
220 osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileName |
221 osl_FileStatus_Mask_FileURL);
222 rc = i.getFileStatus(stat);
223 if (rc != osl::FileBase::E_None) {
224 SAL_WARN( "desktop.app", "cannot stat in directory " << dir.getURL() << ": " << +rc);
225 continue;
227 if (stat.getFileType() == osl::FileStatus::Directory) { //TODO: symlinks
228 removeTree(stat.getFileURL());
229 } else {
230 rc = osl::File::remove(stat.getFileURL());
231 SAL_WARN_IF(
232 rc != osl::FileBase::E_None, "desktop.app",
233 "cannot remove file " << stat.getFileURL() << ": " << +rc);
236 if (dir.isOpen()) {
237 rc = dir.close();
238 SAL_WARN_IF(
239 rc != osl::FileBase::E_None, "desktop.app",
240 "cannot close directory " << dir.getURL() << ": " << +rc);
242 rc = osl::Directory::remove(url);
243 SAL_WARN_IF(
244 rc != osl::FileBase::E_None, "desktop.app",
245 "cannot remove directory " << url << ": " << +rc);
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */