LibreOffice Module ucb (master) 1
gio_datasupplier.cxx
Go to the documentation of this file.
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
22#include <com/sun/star/ucb/IllegalIdentifierException.hpp>
23#include <com/sun/star/ucb/OpenMode.hpp>
24#include <utility>
25
26#include "gio_datasupplier.hxx"
27#include "gio_content.hxx"
28
29using namespace gio;
30
31namespace gio
32{
33
35 : mxContent(std::move(xContent)), mnOpenMode(nOpenMode), mbCountFinal(false)
36{
37}
38
39bool DataSupplier::getData()
40{
41 if (mbCountFinal)
42 return true;
43
44 GFile *pFile = mxContent->getGFile();
45
46 GFileEnumerator* pEnumerator = g_file_enumerate_children(pFile, "*",
47 G_FILE_QUERY_INFO_NONE, nullptr, nullptr);
48
49 if (!pEnumerator)
50 return false;
51
52 GFileInfo *pInfo = nullptr;
53 while ((pInfo = g_file_enumerator_next_file (pEnumerator, nullptr, nullptr)))
54 {
55 switch ( mnOpenMode )
56 {
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;
68 }
69
70 maResults.emplace_back( new ResultListEntry( pInfo ) );
71 g_object_unref(pInfo);
72 }
73
74 mbCountFinal = true;
75
76 g_file_enumerator_close(pEnumerator, nullptr, nullptr);
77 return true;
78}
79
81{
82}
83
84OUString DataSupplier::queryContentIdentifierString( sal_uInt32 nIndex )
85{
86 if ( nIndex < maResults.size() )
87 {
88 OUString aId = maResults[ nIndex ]->aId;
89 if ( aId.getLength() )
90 {
91 // Already cached.
92 return aId;
93 }
94 }
95
96 if ( getResult( nIndex ) )
97 {
98 GFile *pFile = mxContent->getGFile();
99 char* parent = g_file_get_uri(pFile);
100 OUString aId = OUString::createFromAscii( parent );
101 g_free(parent);
102
103 char *escaped_name =
104 g_uri_escape_string( g_file_info_get_name(maResults[ nIndex ]->pInfo) , nullptr, false);
105
106 if ( ( aId.lastIndexOf( '/' ) + 1 ) != aId.getLength() )
107 aId += "/";
108
109 aId += OUString::createFromAscii( escaped_name );
110
111 g_free( escaped_name );
112
113 maResults[ nIndex ]->aId = aId;
114 return aId;
115 }
116
117 return OUString();
118}
119
120css::uno::Reference< css::ucb::XContentIdentifier > DataSupplier::queryContentIdentifier( sal_uInt32 nIndex )
121{
122 if ( nIndex < maResults.size() )
123 {
124 css::uno::Reference< css::ucb::XContentIdentifier > xId = maResults[ nIndex ]->xId;
125 if ( xId.is() )
126 {
127 // Already cached.
128 return xId;
129 }
130 }
131
132 OUString aId = queryContentIdentifierString( nIndex );
133 if ( aId.getLength() )
134 {
135 css::uno::Reference< css::ucb::XContentIdentifier > xId = new ucbhelper::ContentIdentifier( aId );
136 maResults[ nIndex ]->xId = xId;
137 return xId;
138 }
139
140 return css::uno::Reference< css::ucb::XContentIdentifier >();
141}
142
143css::uno::Reference< css::ucb::XContent > DataSupplier::queryContent( sal_uInt32 nIndex )
144{
145 if ( nIndex < maResults.size() )
146 {
147 css::uno::Reference< css::ucb::XContent > xContent = maResults[ nIndex ]->xContent;
148 if ( xContent.is() )
149 {
150 // Already cached.
151 return xContent;
152 }
153 }
154
155 css::uno::Reference< css::ucb::XContentIdentifier > xId = queryContentIdentifier( nIndex );
156 if ( xId.is() )
157 {
158 try
159 {
160 css::uno::Reference< css::ucb::XContent > xContent = mxContent->getProvider()->queryContent( xId );
161 maResults[ nIndex ]->xContent = xContent;
162 return xContent;
163 }
164 catch ( css::ucb::IllegalIdentifierException& )
165 {
166 }
167 }
168 return css::uno::Reference< css::ucb::XContent >();
169}
170
171bool DataSupplier::getResult( sal_uInt32 nIndex )
172{
173 if ( maResults.size() > nIndex ) // Result already present.
174 return true;
175
176 if ( getData() && maResults.size() > nIndex )
177 return true;
178
179 return false;
180}
181
183{
184 getData();
185 return maResults.size();
186}
187
189{
190 return maResults.size();
191}
192
194{
195 return mbCountFinal;
196}
197
198css::uno::Reference< css::sdbc::XRow > DataSupplier::queryPropertyValues( sal_uInt32 nIndex )
199{
200 if ( nIndex < maResults.size() )
201 {
202 css::uno::Reference< css::sdbc::XRow > xRow = maResults[ nIndex ]->xRow;
203 if ( xRow.is() )
204 {
205 // Already cached.
206 return xRow;
207 }
208 }
209
210 if ( getResult( nIndex ) )
211 {
212 css::uno::Reference< css::ucb::XContent > xContent( queryContent( nIndex ) );
213 if ( xContent.is() )
214 {
215 try
216 {
217 css::uno::Reference< css::ucb::XCommandProcessor > xCmdProc(
218 xContent, css::uno::UNO_QUERY_THROW );
219 sal_Int32 nCmdId( xCmdProc->createCommandIdentifier() );
220 css::ucb::Command aCmd;
221 aCmd.Name = "getPropertyValues";
222 aCmd.Handle = -1;
223 aCmd.Argument <<= getResultSet()->getProperties();
224 css::uno::Any aResult( xCmdProc->execute(
225 aCmd, nCmdId, getResultSet()->getEnvironment() ) );
226 css::uno::Reference< css::sdbc::XRow > xRow;
227 if ( aResult >>= xRow )
228 {
229 maResults[ nIndex ]->xRow = xRow;
230 return xRow;
231 }
232 }
233 catch ( css::uno::Exception const & )
234 {
235 }
236 }
237 }
238 return css::uno::Reference< css::sdbc::XRow >();
239}
240
241void DataSupplier::releasePropertyValues( sal_uInt32 nIndex )
242{
243 if ( nIndex < maResults.size() )
244 maResults[ nIndex ]->xRow.clear();
245}
246
248{
249}
250
252{
253}
254
255}
256
257/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual sal_uInt32 totalCount() override
virtual void releasePropertyValues(sal_uInt32 nIndex) override
virtual css::uno::Reference< css::sdbc::XRow > queryPropertyValues(sal_uInt32 nIndex) override
virtual OUString queryContentIdentifierString(sal_uInt32 nIndex) override
virtual bool isCountFinal() override
DataSupplier(rtl::Reference< Content > xContent, sal_Int32 nOpenMode)
virtual css::uno::Reference< css::ucb::XContentIdentifier > queryContentIdentifier(sal_uInt32 nIndex) override
virtual ~DataSupplier() override
virtual bool getResult(sal_uInt32 nIndex) override
virtual sal_uInt32 currentCount() override
rtl::Reference< ::gio::Content > mxContent
virtual css::uno::Reference< css::ucb::XContent > queryContent(sal_uInt32 nIndex) override
virtual void validate() override
virtual void close() override
rtl::Reference< ResultSet > getResultSet() const
sal_Int32 nIndex
css::uno::Environment getEnvironment(OUString const &name, std::u16string_view implementation)