Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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>
33 :
34 : using namespace ::osl;
35 : using namespace ::com::sun::star::uno;
36 : using namespace ::com::sun::star::ucb;
37 :
38 : namespace utl
39 : {
40 :
41 20 : bool LocalFileHelper::ConvertSystemPathToURL( const OUString& rName, const OUString& rBaseURL, OUString& rReturn )
42 : {
43 20 : rReturn.clear();
44 :
45 : Reference< XUniversalContentBroker > pBroker(
46 : UniversalContentBroker::create(
47 20 : comphelper::getProcessComponentContext() ) );
48 : try
49 : {
50 20 : rReturn = ::ucbhelper::getFileURLFromSystemPath( pBroker, rBaseURL, rName );
51 : }
52 0 : catch ( ::com::sun::star::uno::RuntimeException& )
53 : {
54 0 : return false;
55 : }
56 :
57 20 : return !rReturn.isEmpty();
58 : }
59 :
60 0 : bool LocalFileHelper::ConvertURLToSystemPath( const OUString& rName, OUString& rReturn )
61 : {
62 0 : rReturn.clear();
63 : Reference< XUniversalContentBroker > pBroker(
64 : UniversalContentBroker::create(
65 0 : comphelper::getProcessComponentContext() ) );
66 : try
67 : {
68 0 : rReturn = ::ucbhelper::getSystemPathFromFileURL( pBroker, rName );
69 : }
70 0 : catch ( ::com::sun::star::uno::RuntimeException& )
71 : {
72 : }
73 :
74 0 : return !rReturn.isEmpty();
75 : }
76 :
77 38405 : bool LocalFileHelper::ConvertPhysicalNameToURL(const OUString& rName, OUString& rReturn)
78 : {
79 38405 : rReturn.clear();
80 : Reference< XUniversalContentBroker > pBroker(
81 : UniversalContentBroker::create(
82 38405 : comphelper::getProcessComponentContext() ) );
83 : try
84 : {
85 38405 : OUString aBase( ::ucbhelper::getLocalFileURL() );
86 38405 : rReturn = ::ucbhelper::getFileURLFromSystemPath( pBroker, aBase, rName );
87 : }
88 0 : catch (const ::com::sun::star::uno::RuntimeException&)
89 : {
90 : }
91 :
92 38405 : return !rReturn.isEmpty();
93 : }
94 :
95 78082 : bool LocalFileHelper::ConvertURLToPhysicalName(const OUString& rName, OUString& rReturn)
96 : {
97 78082 : rReturn.clear();
98 : Reference< XUniversalContentBroker > pBroker(
99 : UniversalContentBroker::create(
100 78082 : comphelper::getProcessComponentContext() ) );
101 : try
102 : {
103 78082 : INetURLObject aObj( rName );
104 156164 : INetURLObject aLocal( ::ucbhelper::getLocalFileURL() );
105 78082 : if ( aObj.GetProtocol() == aLocal.GetProtocol() )
106 148942 : rReturn = ::ucbhelper::getSystemPathFromFileURL( pBroker, rName );
107 : }
108 0 : catch (const ::com::sun::star::uno::RuntimeException&)
109 : {
110 : }
111 :
112 78082 : return !rReturn.isEmpty();
113 : }
114 :
115 71555 : bool LocalFileHelper::IsLocalFile(const OUString& rName)
116 : {
117 71555 : OUString aTmp;
118 71555 : return ConvertURLToPhysicalName(rName, aTmp);
119 : }
120 :
121 0 : bool LocalFileHelper::IsFileContent(const OUString& rName)
122 : {
123 0 : OUString aTmp;
124 0 : return ConvertURLToSystemPath(rName, aTmp);
125 : }
126 :
127 : typedef ::std::vector< OUString* > StringList_Impl;
128 :
129 60 : ::com::sun::star::uno::Sequence < OUString > LocalFileHelper::GetFolderContents( const OUString& rFolder, bool bFolder )
130 : {
131 60 : StringList_Impl* pFiles = NULL;
132 : try
133 : {
134 : ::ucbhelper::Content aCnt(
135 : rFolder, Reference< XCommandEnvironment >(),
136 60 : comphelper::getProcessComponentContext() );
137 120 : Reference< ::com::sun::star::sdbc::XResultSet > xResultSet;
138 120 : ::com::sun::star::uno::Sequence< OUString > aProps(1);
139 60 : OUString* pProps = aProps.getArray();
140 60 : pProps[0] = "Url";
141 :
142 : try
143 : {
144 60 : ::ucbhelper::ResultSetInclude eInclude = bFolder ? ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS : ::ucbhelper::INCLUDE_DOCUMENTS_ONLY;
145 60 : xResultSet = aCnt.createCursor( aProps, eInclude );
146 : }
147 0 : catch( ::com::sun::star::ucb::CommandAbortedException& )
148 : {
149 : }
150 23 : catch( Exception& )
151 : {
152 : }
153 :
154 60 : if ( xResultSet.is() )
155 : {
156 37 : pFiles = new StringList_Impl;
157 37 : Reference< XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
158 : try
159 : {
160 225 : while ( xResultSet->next() )
161 : {
162 151 : OUString aId = xContentAccess->queryContentIdentifierString();
163 151 : OUString* pFile = new OUString( aId );
164 151 : pFiles->push_back( pFile );
165 151 : }
166 : }
167 0 : catch( ::com::sun::star::ucb::CommandAbortedException& )
168 : {
169 : }
170 0 : catch( Exception& )
171 : {
172 37 : }
173 60 : }
174 : }
175 0 : catch( Exception& )
176 : {
177 : }
178 :
179 60 : if ( pFiles )
180 : {
181 37 : size_t nCount = pFiles->size();
182 37 : Sequence < OUString > aRet( nCount );
183 37 : OUString* pRet = aRet.getArray();
184 188 : for ( size_t i = 0; i < nCount; ++i )
185 : {
186 151 : OUString* pFile = (*pFiles)[ i ];
187 151 : pRet[i] = *( pFile );
188 151 : delete pFile;
189 : }
190 37 : delete pFiles;
191 37 : return aRet;
192 : }
193 : else
194 23 : return Sequence < OUString > ();
195 : }
196 :
197 64 : void removeTree(OUString const & url) {
198 64 : osl::Directory dir(url);
199 64 : osl::FileBase::RC rc = dir.open();
200 64 : switch (rc) {
201 : case osl::FileBase::E_None:
202 11 : break;
203 : case osl::FileBase::E_NOENT:
204 53 : return; //TODO: SAL_WARN if recursive
205 : default:
206 : SAL_WARN("desktop.app", "cannot open directory " << dir.getURL() << ": " << +rc);
207 0 : return;
208 : }
209 : for (;;) {
210 42 : osl::DirectoryItem i;
211 42 : rc = dir.getNextItem(i, SAL_MAX_UINT32);
212 42 : if (rc == osl::FileBase::E_NOENT) {
213 11 : break;
214 : }
215 31 : if (rc != osl::FileBase::E_None) {
216 : SAL_WARN( "desktop.app", "cannot iterate directory " << dir.getURL() << ": " << +rc);
217 0 : break;
218 : }
219 : osl::FileStatus stat(
220 : osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileName |
221 62 : osl_FileStatus_Mask_FileURL);
222 31 : rc = i.getFileStatus(stat);
223 31 : if (rc != osl::FileBase::E_None) {
224 : SAL_WARN( "desktop.app", "cannot stat in directory " << dir.getURL() << ": " << +rc);
225 0 : continue;
226 : }
227 31 : if (stat.getFileType() == osl::FileStatus::Directory) { //TODO: symlinks
228 0 : removeTree(stat.getFileURL());
229 : } else {
230 31 : 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);
234 : }
235 62 : }
236 11 : if (dir.isOpen()) {
237 11 : rc = dir.close();
238 : SAL_WARN_IF(
239 : rc != osl::FileBase::E_None, "desktop.app",
240 : "cannot close directory " << dir.getURL() << ": " << +rc);
241 : }
242 11 : rc = osl::Directory::remove(url);
243 : SAL_WARN_IF(
244 : rc != osl::FileBase::E_None, "desktop.app",
245 11 : "cannot remove directory " << url << ": " << +rc);
246 : }
247 :
248 : }
249 :
250 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|