fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / ucb / source / ucp / gio / gio_datasupplier.cxx
blobe6824e25631b29d247c39d13a6da9166e3cb80fb
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 <vector>
22 #include <ucbhelper/contentidentifier.hxx>
23 #include <ucbhelper/providerhelper.hxx>
25 #include <com/sun/star/ucb/OpenMode.hpp>
27 #include "gio_datasupplier.hxx"
28 #include "gio_content.hxx"
29 #include "gio_provider.hxx"
31 #include <stdio.h>
33 using namespace com::sun::star;
35 using namespace gio;
37 namespace gio
40 typedef std::vector< ResultListEntry* > ResultList;
42 DataSupplier::DataSupplier( const uno::Reference< lang::XMultiServiceFactory >& rxSMgr,
43 const uno::Reference< ::gio::Content >& rContent, sal_Int32 nOpenMode )
44 : mxContent(rContent), m_xSMgr(rxSMgr), mnOpenMode(nOpenMode), mbCountFinal(false)
48 bool DataSupplier::getData()
50 if (mbCountFinal)
51 return true;
53 GFile *pFile = mxContent->getGFile();
55 GFileEnumerator* pEnumerator = g_file_enumerate_children(pFile, "*",
56 G_FILE_QUERY_INFO_NONE, NULL, NULL);
58 if (!pEnumerator)
59 return sal_False;
61 GFileInfo *pInfo = NULL;
62 while ((pInfo = g_file_enumerator_next_file (pEnumerator, NULL, NULL)))
64 switch ( mnOpenMode )
66 case ucb::OpenMode::FOLDERS:
67 if (g_file_info_get_file_type(pInfo) != G_FILE_TYPE_DIRECTORY)
68 continue;
69 break;
70 case ucb::OpenMode::DOCUMENTS:
71 if (g_file_info_get_file_type(pInfo) != G_FILE_TYPE_REGULAR)
72 continue;
73 break;
74 case ucb::OpenMode::ALL:
75 default:
76 break;
79 maResults.push_back( new ResultListEntry( pInfo ) );
80 g_object_unref(pInfo);
83 mbCountFinal = sal_True;
85 g_file_enumerator_close(pEnumerator, NULL, NULL);
86 return true;
89 DataSupplier::~DataSupplier()
91 ResultList::const_iterator it = maResults.begin();
92 ResultList::const_iterator end = maResults.end();
94 while ( it != end )
96 delete (*it);
97 ++it;
101 OUString DataSupplier::queryContentIdentifierString( sal_uInt32 nIndex )
103 if ( nIndex < maResults.size() )
105 OUString aId = maResults[ nIndex ]->aId;
106 if ( aId.getLength() )
108 // Already cached.
109 return aId;
113 if ( getResult( nIndex ) )
115 GFile *pFile = mxContent->getGFile();
116 char* parent = g_file_get_uri(pFile);
117 OUString aId = OUString::createFromAscii( parent );
118 g_free(parent);
120 char *escaped_name =
121 g_uri_escape_string( g_file_info_get_name(maResults[ nIndex ]->pInfo) , NULL, false);
123 if ( ( aId.lastIndexOf( '/' ) + 1 ) != aId.getLength() )
124 aId += OUString("/");
126 aId += OUString::createFromAscii( escaped_name );
128 g_free( escaped_name );
130 maResults[ nIndex ]->aId = aId;
131 return aId;
134 return OUString();
137 uno::Reference< ucb::XContentIdentifier > DataSupplier::queryContentIdentifier( sal_uInt32 nIndex )
139 if ( nIndex < maResults.size() )
141 uno::Reference< ucb::XContentIdentifier > xId = maResults[ nIndex ]->xId;
142 if ( xId.is() )
144 // Already cached.
145 return xId;
149 OUString aId = queryContentIdentifierString( nIndex );
150 if ( aId.getLength() )
152 uno::Reference< ucb::XContentIdentifier > xId = new ucbhelper::ContentIdentifier( aId );
153 maResults[ nIndex ]->xId = xId;
154 return xId;
157 return uno::Reference< ucb::XContentIdentifier >();
160 uno::Reference< ucb::XContent > DataSupplier::queryContent( sal_uInt32 nIndex )
162 if ( nIndex < maResults.size() )
164 uno::Reference< ucb::XContent > xContent = maResults[ nIndex ]->xContent;
165 if ( xContent.is() )
167 // Already cached.
168 return xContent;
172 uno::Reference< ucb::XContentIdentifier > xId = queryContentIdentifier( nIndex );
173 if ( xId.is() )
177 uno::Reference< ucb::XContent > xContent = mxContent->getProvider()->queryContent( xId );
178 maResults[ nIndex ]->xContent = xContent;
179 return xContent;
181 catch ( ucb::IllegalIdentifierException& )
185 return uno::Reference< ucb::XContent >();
188 sal_Bool DataSupplier::getResult( sal_uInt32 nIndex )
190 if ( maResults.size() > nIndex ) // Result already present.
191 return sal_True;
193 if ( getData() && maResults.size() > nIndex )
194 return sal_True;
196 return sal_False;
199 sal_uInt32 DataSupplier::totalCount()
201 getData();
202 return maResults.size();
205 sal_uInt32 DataSupplier::currentCount()
207 return maResults.size();
210 sal_Bool DataSupplier::isCountFinal()
212 return mbCountFinal;
215 uno::Reference< sdbc::XRow > DataSupplier::queryPropertyValues( sal_uInt32 nIndex )
217 if ( nIndex < maResults.size() )
219 uno::Reference< sdbc::XRow > xRow = maResults[ nIndex ]->xRow;
220 if ( xRow.is() )
222 // Already cached.
223 return xRow;
227 if ( getResult( nIndex ) )
229 uno::Reference< ucb::XContent > xContent( queryContent( nIndex ) );
230 if ( xContent.is() )
234 uno::Reference< ucb::XCommandProcessor > xCmdProc(
235 xContent, uno::UNO_QUERY_THROW );
236 sal_Int32 nCmdId( xCmdProc->createCommandIdentifier() );
237 ucb::Command aCmd;
238 aCmd.Name = OUString("getPropertyValues");
239 aCmd.Handle = -1;
240 aCmd.Argument <<= getResultSet()->getProperties();
241 uno::Any aResult( xCmdProc->execute(
242 aCmd, nCmdId, getResultSet()->getEnvironment() ) );
243 uno::Reference< sdbc::XRow > xRow;
244 if ( aResult >>= xRow )
246 maResults[ nIndex ]->xRow = xRow;
247 return xRow;
250 catch ( uno::Exception const & )
255 return uno::Reference< sdbc::XRow >();
258 void DataSupplier::releasePropertyValues( sal_uInt32 nIndex )
260 if ( nIndex < maResults.size() )
261 maResults[ nIndex ]->xRow = uno::Reference< sdbc::XRow >();
264 void DataSupplier::close()
268 void DataSupplier::validate() throw( ucb::ResultSetException )
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */