1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
26 #include "gio_datasupplier.hxx"
27 #include "gio_content.hxx"
34 DataSupplier::DataSupplier( rtl::Reference
< ::gio::Content
> xContent
, sal_Int32 nOpenMode
)
35 : mxContent(std::move(xContent
)), mnOpenMode(nOpenMode
), mbCountFinal(false)
39 bool DataSupplier::getData()
44 GFile
*pFile
= mxContent
->getGFile();
46 GFileEnumerator
* pEnumerator
= g_file_enumerate_children(pFile
, "*",
47 G_FILE_QUERY_INFO_NONE
, nullptr, nullptr);
52 GFileInfo
*pInfo
= nullptr;
53 while ((pInfo
= g_file_enumerator_next_file (pEnumerator
, nullptr, nullptr)))
57 case css::ucb::OpenMode::FOLDERS
:
58 if (g_file_info_get_file_type(pInfo
) != G_FILE_TYPE_DIRECTORY
)
61 case css::ucb::OpenMode::DOCUMENTS
:
62 if (g_file_info_get_file_type(pInfo
) != G_FILE_TYPE_REGULAR
)
65 case css::ucb::OpenMode::ALL
:
70 maResults
.emplace_back( new ResultListEntry( pInfo
) );
71 g_object_unref(pInfo
);
76 g_file_enumerator_close(pEnumerator
, nullptr, nullptr);
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() )
96 if ( getResult( rResultSetGuard
, nIndex
) )
98 GFile
*pFile
= mxContent
->getGFile();
99 char* parent
= g_file_get_uri(pFile
);
100 OUString aId
= OUString::createFromAscii( parent
);
104 g_uri_escape_string( g_file_info_get_name(maResults
[ nIndex
]->pInfo
) , nullptr, false);
106 if ( ( aId
.lastIndexOf( '/' ) + 1 ) != aId
.getLength() )
109 aId
+= OUString::createFromAscii( escaped_name
);
111 g_free( escaped_name
);
113 maResults
[ nIndex
]->aId
= aId
;
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
;
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
;
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
;
155 css::uno::Reference
< css::ucb::XContentIdentifier
> xId
= queryContentIdentifier( rResultSetGuard
, nIndex
);
160 css::uno::Reference
< css::ucb::XContent
> xContent
= mxContent
->getProvider()->queryContent( xId
);
161 maResults
[ nIndex
]->xContent
= 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.
176 if ( getData() && maResults
.size() > nIndex
)
182 sal_uInt32
DataSupplier::totalCount(std::unique_lock
<std::mutex
>& /*rResultSetGuard*/)
185 return maResults
.size();
188 sal_uInt32
DataSupplier::currentCount()
190 return maResults
.size();
193 bool DataSupplier::isCountFinal()
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
;
210 if ( !getResult( rResultSetGuard
, nIndex
) )
213 css::uno::Reference
< css::ucb::XContent
> xContent( queryContent( rResultSetGuard
, nIndex
) );
219 css::uno::Reference
< css::ucb::XCommandProcessor
> xCmdProc( xContent
, css::uno::UNO_QUERY
);
222 sal_Int32
nCmdId( xCmdProc
->createCommandIdentifier() );
223 css::ucb::Command aCmd
;
224 aCmd
.Name
= "getPropertyValues";
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
;
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: */