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