Update git submodules
[LibreOffice.git] / ucb / source / ucp / gio / gio_datasupplier.cxx
blob5586935fcfe63d2e3eb6b2c351f68e40032e02fd
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 <ucbhelper/contentidentifier.hxx>
21 #include <ucbhelper/providerhelper.hxx>
22 #include <com/sun/star/ucb/IllegalIdentifierException.hpp>
23 #include <com/sun/star/ucb/OpenMode.hpp>
24 #include <utility>
26 #include "gio_datasupplier.hxx"
27 #include "gio_content.hxx"
29 using namespace gio;
31 namespace gio
34 DataSupplier::DataSupplier( rtl::Reference< ::gio::Content > xContent, sal_Int32 nOpenMode )
35 : mxContent(std::move(xContent)), mnOpenMode(nOpenMode), mbCountFinal(false)
39 bool DataSupplier::getData()
41 if (mbCountFinal)
42 return true;
44 GFile *pFile = mxContent->getGFile();
46 GFileEnumerator* pEnumerator = g_file_enumerate_children(pFile, "*",
47 G_FILE_QUERY_INFO_NONE, nullptr, nullptr);
49 if (!pEnumerator)
50 return false;
52 GFileInfo *pInfo = nullptr;
53 while ((pInfo = g_file_enumerator_next_file (pEnumerator, nullptr, nullptr)))
55 switch ( mnOpenMode )
57 case css::ucb::OpenMode::FOLDERS:
58 if (g_file_info_get_file_type(pInfo) != G_FILE_TYPE_DIRECTORY)
59 continue;
60 break;
61 case css::ucb::OpenMode::DOCUMENTS:
62 if (g_file_info_get_file_type(pInfo) != G_FILE_TYPE_REGULAR)
63 continue;
64 break;
65 case css::ucb::OpenMode::ALL:
66 default:
67 break;
70 maResults.emplace_back( new ResultListEntry( pInfo ) );
71 g_object_unref(pInfo);
74 mbCountFinal = true;
76 g_file_enumerator_close(pEnumerator, nullptr, nullptr);
77 return true;
80 DataSupplier::~DataSupplier()
84 OUString DataSupplier::queryContentIdentifierString( std::unique_lock<std::mutex>& rResultSetGuard, sal_uInt32 nIndex )
86 if ( nIndex < maResults.size() )
88 OUString aId = maResults[ nIndex ]->aId;
89 if ( aId.getLength() )
91 // Already cached.
92 return aId;
96 if ( getResult( rResultSetGuard, nIndex ) )
98 GFile *pFile = mxContent->getGFile();
99 char* parent = g_file_get_uri(pFile);
100 OUString aId = OUString::createFromAscii( parent );
101 g_free(parent);
103 char *escaped_name =
104 g_uri_escape_string( g_file_info_get_name(maResults[ nIndex ]->pInfo) , nullptr, false);
106 if ( ( aId.lastIndexOf( '/' ) + 1 ) != aId.getLength() )
107 aId += "/";
109 aId += OUString::createFromAscii( escaped_name );
111 g_free( escaped_name );
113 maResults[ nIndex ]->aId = aId;
114 return aId;
117 return OUString();
120 css::uno::Reference< css::ucb::XContentIdentifier > DataSupplier::queryContentIdentifier( std::unique_lock<std::mutex>& rResultSetGuard, sal_uInt32 nIndex )
122 if ( nIndex < maResults.size() )
124 css::uno::Reference< css::ucb::XContentIdentifier > xId = maResults[ nIndex ]->xId;
125 if ( xId.is() )
127 // Already cached.
128 return xId;
132 OUString aId = queryContentIdentifierString( rResultSetGuard, nIndex );
133 if ( aId.getLength() )
135 css::uno::Reference< css::ucb::XContentIdentifier > xId = new ucbhelper::ContentIdentifier( aId );
136 maResults[ nIndex ]->xId = xId;
137 return xId;
140 return css::uno::Reference< css::ucb::XContentIdentifier >();
143 css::uno::Reference< css::ucb::XContent > DataSupplier::queryContent( std::unique_lock<std::mutex>& rResultSetGuard, sal_uInt32 nIndex )
145 if ( nIndex < maResults.size() )
147 css::uno::Reference< css::ucb::XContent > xContent = maResults[ nIndex ]->xContent;
148 if ( xContent.is() )
150 // Already cached.
151 return xContent;
155 css::uno::Reference< css::ucb::XContentIdentifier > xId = queryContentIdentifier( rResultSetGuard, nIndex );
156 if ( xId.is() )
160 css::uno::Reference< css::ucb::XContent > xContent = mxContent->getProvider()->queryContent( xId );
161 maResults[ nIndex ]->xContent = xContent;
162 return xContent;
164 catch ( css::ucb::IllegalIdentifierException& )
168 return css::uno::Reference< css::ucb::XContent >();
171 bool DataSupplier::getResult( std::unique_lock<std::mutex>& /*rResultSetGuard*/, sal_uInt32 nIndex )
173 if ( maResults.size() > nIndex ) // Result already present.
174 return true;
176 if ( getData() && maResults.size() > nIndex )
177 return true;
179 return false;
182 sal_uInt32 DataSupplier::totalCount(std::unique_lock<std::mutex>& /*rResultSetGuard*/)
184 getData();
185 return maResults.size();
188 sal_uInt32 DataSupplier::currentCount()
190 return maResults.size();
193 bool DataSupplier::isCountFinal()
195 return mbCountFinal;
198 css::uno::Reference< css::sdbc::XRow > DataSupplier::queryPropertyValues( std::unique_lock<std::mutex>& rResultSetGuard, sal_uInt32 nIndex )
200 if ( nIndex < maResults.size() )
202 css::uno::Reference< css::sdbc::XRow > xRow = maResults[ nIndex ]->xRow;
203 if ( xRow.is() )
205 // Already cached.
206 return xRow;
210 if ( !getResult( rResultSetGuard, nIndex ) )
211 return {};
213 css::uno::Reference< css::ucb::XContent > xContent( queryContent( rResultSetGuard, nIndex ) );
214 if ( !xContent )
215 return {};
219 css::uno::Reference< css::ucb::XCommandProcessor > xCmdProc( xContent, css::uno::UNO_QUERY );
220 if ( !xCmdProc )
221 return {};
222 sal_Int32 nCmdId( xCmdProc->createCommandIdentifier() );
223 css::ucb::Command aCmd;
224 aCmd.Name = "getPropertyValues";
225 aCmd.Handle = -1;
226 aCmd.Argument <<= getResultSet()->getProperties();
227 css::uno::Any aResult( xCmdProc->execute(
228 aCmd, nCmdId, getResultSet()->getEnvironment() ) );
229 css::uno::Reference< css::sdbc::XRow > xRow;
230 if ( aResult >>= xRow )
232 maResults[ nIndex ]->xRow = xRow;
233 return xRow;
236 catch ( css::uno::Exception const & )
239 return css::uno::Reference< css::sdbc::XRow >();
242 void DataSupplier::releasePropertyValues( sal_uInt32 nIndex )
244 if ( nIndex < maResults.size() )
245 maResults[ nIndex ]->xRow.clear();
248 void DataSupplier::close()
252 void DataSupplier::validate()
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */