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>
25 #include "gio_datasupplier.hxx"
26 #include "gio_content.hxx"
33 DataSupplier::DataSupplier( const rtl::Reference
< ::gio::Content
>& rContent
, sal_Int32 nOpenMode
)
34 : mxContent(rContent
), mnOpenMode(nOpenMode
), mbCountFinal(false)
38 bool DataSupplier::getData()
43 GFile
*pFile
= mxContent
->getGFile();
45 GFileEnumerator
* pEnumerator
= g_file_enumerate_children(pFile
, "*",
46 G_FILE_QUERY_INFO_NONE
, nullptr, nullptr);
51 GFileInfo
*pInfo
= nullptr;
52 while ((pInfo
= g_file_enumerator_next_file (pEnumerator
, nullptr, nullptr)))
56 case css::ucb::OpenMode::FOLDERS
:
57 if (g_file_info_get_file_type(pInfo
) != G_FILE_TYPE_DIRECTORY
)
60 case css::ucb::OpenMode::DOCUMENTS
:
61 if (g_file_info_get_file_type(pInfo
) != G_FILE_TYPE_REGULAR
)
64 case css::ucb::OpenMode::ALL
:
69 maResults
.emplace_back( new ResultListEntry( pInfo
) );
70 g_object_unref(pInfo
);
75 g_file_enumerator_close(pEnumerator
, nullptr, nullptr);
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() )
95 if ( getResult( nIndex
) )
97 GFile
*pFile
= mxContent
->getGFile();
98 char* parent
= g_file_get_uri(pFile
);
99 OUString aId
= OUString::createFromAscii( parent
);
103 g_uri_escape_string( g_file_info_get_name(maResults
[ nIndex
]->pInfo
) , nullptr, false);
105 if ( ( aId
.lastIndexOf( '/' ) + 1 ) != aId
.getLength() )
108 aId
+= OUString::createFromAscii( escaped_name
);
110 g_free( escaped_name
);
112 maResults
[ nIndex
]->aId
= aId
;
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
;
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
;
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
;
154 css::uno::Reference
< css::ucb::XContentIdentifier
> xId
= queryContentIdentifier( nIndex
);
159 css::uno::Reference
< css::ucb::XContent
> xContent
= mxContent
->getProvider()->queryContent( xId
);
160 maResults
[ nIndex
]->xContent
= 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.
175 if ( getData() && maResults
.size() > nIndex
)
181 sal_uInt32
DataSupplier::totalCount()
184 return maResults
.size();
187 sal_uInt32
DataSupplier::currentCount()
189 return maResults
.size();
192 bool DataSupplier::isCountFinal()
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
;
209 if ( getResult( nIndex
) )
211 css::uno::Reference
< css::ucb::XContent
> xContent( queryContent( nIndex
) );
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";
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
;
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: */