Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / ucb / source / ucp / gio / gio_datasupplier.cxx
blobfd4335dc570c5a04f5a32127c70bfff448704006
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>
25 #include "gio_datasupplier.hxx"
26 #include "gio_content.hxx"
28 using namespace gio;
30 namespace gio
33 DataSupplier::DataSupplier( const rtl::Reference< ::gio::Content >& rContent, sal_Int32 nOpenMode )
34 : mxContent(rContent), mnOpenMode(nOpenMode), mbCountFinal(false)
38 bool DataSupplier::getData()
40 if (mbCountFinal)
41 return true;
43 GFile *pFile = mxContent->getGFile();
45 GFileEnumerator* pEnumerator = g_file_enumerate_children(pFile, "*",
46 G_FILE_QUERY_INFO_NONE, nullptr, nullptr);
48 if (!pEnumerator)
49 return false;
51 GFileInfo *pInfo = nullptr;
52 while ((pInfo = g_file_enumerator_next_file (pEnumerator, nullptr, nullptr)))
54 switch ( mnOpenMode )
56 case css::ucb::OpenMode::FOLDERS:
57 if (g_file_info_get_file_type(pInfo) != G_FILE_TYPE_DIRECTORY)
58 continue;
59 break;
60 case css::ucb::OpenMode::DOCUMENTS:
61 if (g_file_info_get_file_type(pInfo) != G_FILE_TYPE_REGULAR)
62 continue;
63 break;
64 case css::ucb::OpenMode::ALL:
65 default:
66 break;
69 maResults.emplace_back( new ResultListEntry( pInfo ) );
70 g_object_unref(pInfo);
73 mbCountFinal = true;
75 g_file_enumerator_close(pEnumerator, nullptr, nullptr);
76 return true;
79 DataSupplier::~DataSupplier()
83 OUString DataSupplier::queryContentIdentifierString( sal_uInt32 nIndex )
85 if ( nIndex < maResults.size() )
87 OUString aId = maResults[ nIndex ]->aId;
88 if ( aId.getLength() )
90 // Already cached.
91 return aId;
95 if ( getResult( nIndex ) )
97 GFile *pFile = mxContent->getGFile();
98 char* parent = g_file_get_uri(pFile);
99 OUString aId = OUString::createFromAscii( parent );
100 g_free(parent);
102 char *escaped_name =
103 g_uri_escape_string( g_file_info_get_name(maResults[ nIndex ]->pInfo) , nullptr, false);
105 if ( ( aId.lastIndexOf( '/' ) + 1 ) != aId.getLength() )
106 aId += "/";
108 aId += OUString::createFromAscii( escaped_name );
110 g_free( escaped_name );
112 maResults[ nIndex ]->aId = aId;
113 return aId;
116 return OUString();
119 css::uno::Reference< css::ucb::XContentIdentifier > DataSupplier::queryContentIdentifier( sal_uInt32 nIndex )
121 if ( nIndex < maResults.size() )
123 css::uno::Reference< css::ucb::XContentIdentifier > xId = maResults[ nIndex ]->xId;
124 if ( xId.is() )
126 // Already cached.
127 return xId;
131 OUString aId = queryContentIdentifierString( nIndex );
132 if ( aId.getLength() )
134 css::uno::Reference< css::ucb::XContentIdentifier > xId = new ucbhelper::ContentIdentifier( aId );
135 maResults[ nIndex ]->xId = xId;
136 return xId;
139 return css::uno::Reference< css::ucb::XContentIdentifier >();
142 css::uno::Reference< css::ucb::XContent > DataSupplier::queryContent( sal_uInt32 nIndex )
144 if ( nIndex < maResults.size() )
146 css::uno::Reference< css::ucb::XContent > xContent = maResults[ nIndex ]->xContent;
147 if ( xContent.is() )
149 // Already cached.
150 return xContent;
154 css::uno::Reference< css::ucb::XContentIdentifier > xId = queryContentIdentifier( nIndex );
155 if ( xId.is() )
159 css::uno::Reference< css::ucb::XContent > xContent = mxContent->getProvider()->queryContent( xId );
160 maResults[ nIndex ]->xContent = xContent;
161 return xContent;
163 catch ( css::ucb::IllegalIdentifierException& )
167 return css::uno::Reference< css::ucb::XContent >();
170 bool DataSupplier::getResult( sal_uInt32 nIndex )
172 if ( maResults.size() > nIndex ) // Result already present.
173 return true;
175 if ( getData() && maResults.size() > nIndex )
176 return true;
178 return false;
181 sal_uInt32 DataSupplier::totalCount()
183 getData();
184 return maResults.size();
187 sal_uInt32 DataSupplier::currentCount()
189 return maResults.size();
192 bool DataSupplier::isCountFinal()
194 return mbCountFinal;
197 css::uno::Reference< css::sdbc::XRow > DataSupplier::queryPropertyValues( sal_uInt32 nIndex )
199 if ( nIndex < maResults.size() )
201 css::uno::Reference< css::sdbc::XRow > xRow = maResults[ nIndex ]->xRow;
202 if ( xRow.is() )
204 // Already cached.
205 return xRow;
209 if ( getResult( nIndex ) )
211 css::uno::Reference< css::ucb::XContent > xContent( queryContent( nIndex ) );
212 if ( xContent.is() )
216 css::uno::Reference< css::ucb::XCommandProcessor > xCmdProc(
217 xContent, css::uno::UNO_QUERY_THROW );
218 sal_Int32 nCmdId( xCmdProc->createCommandIdentifier() );
219 css::ucb::Command aCmd;
220 aCmd.Name = "getPropertyValues";
221 aCmd.Handle = -1;
222 aCmd.Argument <<= getResultSet()->getProperties();
223 css::uno::Any aResult( xCmdProc->execute(
224 aCmd, nCmdId, getResultSet()->getEnvironment() ) );
225 css::uno::Reference< css::sdbc::XRow > xRow;
226 if ( aResult >>= xRow )
228 maResults[ nIndex ]->xRow = xRow;
229 return xRow;
232 catch ( css::uno::Exception const & )
237 return css::uno::Reference< css::sdbc::XRow >();
240 void DataSupplier::releasePropertyValues( sal_uInt32 nIndex )
242 if ( nIndex < maResults.size() )
243 maResults[ nIndex ]->xRow.clear();
246 void DataSupplier::close()
250 void DataSupplier::validate()
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */