Update ooo320-m1
[ooovba.git] / ucb / source / ucp / gio / gio_datasupplier.cxx
blob384c5278ac49b3f1a48276841489e6b4679f64e6
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: gio_datasupplier.cxx,v $
10 * $Revision: 1.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <vector>
33 #include <ucbhelper/contentidentifier.hxx>
34 #include <ucbhelper/providerhelper.hxx>
36 #include <com/sun/star/ucb/OpenMode.hpp>
38 #include "gio_datasupplier.hxx"
39 #include "gio_content.hxx"
40 #include "gio_provider.hxx"
42 #include <stdio.h>
44 using namespace com::sun::star;
46 using namespace gio;
48 namespace gio
51 typedef std::vector< ResultListEntry* > ResultList;
53 DataSupplier::DataSupplier( const uno::Reference< lang::XMultiServiceFactory >& rxSMgr,
54 const uno::Reference< ::gio::Content >& rContent, sal_Int32 nOpenMode )
55 : mxContent(rContent), m_xSMgr(rxSMgr), mnOpenMode(nOpenMode), mbCountFinal(false)
59 bool DataSupplier::getData()
61 if (mbCountFinal)
62 return true;
64 GFile *pFile = mxContent->getGFile();
66 GFileEnumerator* pEnumerator = g_file_enumerate_children(pFile, "*",
67 G_FILE_QUERY_INFO_NONE, NULL, NULL);
69 if (!pEnumerator)
70 return sal_False;
72 GFileInfo *pInfo = NULL;
73 while ((pInfo = g_file_enumerator_next_file (pEnumerator, NULL, NULL)))
75 switch ( mnOpenMode )
77 case ucb::OpenMode::FOLDERS:
78 if (g_file_info_get_file_type(pInfo) != G_FILE_TYPE_DIRECTORY)
79 continue;
80 break;
81 case ucb::OpenMode::DOCUMENTS:
82 if (g_file_info_get_file_type(pInfo) != G_FILE_TYPE_REGULAR)
83 continue;
84 break;
85 case ucb::OpenMode::ALL:
86 default:
87 break;
90 maResults.push_back( new ResultListEntry( pInfo ) );
91 g_object_unref(pInfo);
94 mbCountFinal = sal_True;
96 g_file_enumerator_close(pEnumerator, NULL, NULL);
97 return true;
100 DataSupplier::~DataSupplier()
102 ResultList::const_iterator it = maResults.begin();
103 ResultList::const_iterator end = maResults.end();
105 while ( it != end )
107 delete (*it);
108 it++;
112 ::rtl::OUString DataSupplier::queryContentIdentifierString( sal_uInt32 nIndex )
114 if ( nIndex < maResults.size() )
116 ::rtl::OUString aId = maResults[ nIndex ]->aId;
117 if ( aId.getLength() )
119 // Already cached.
120 return aId;
124 if ( getResult( nIndex ) )
126 GFile *pFile = mxContent->getGFile();
127 char* parent = g_file_get_uri(pFile);
128 rtl::OUString aId = rtl::OUString::createFromAscii( parent );
129 g_free(parent);
131 char *escaped_name =
132 g_uri_escape_string( g_file_info_get_name(maResults[ nIndex ]->pInfo) , NULL, false);
134 if ( ( aId.lastIndexOf( '/' ) + 1 ) != aId.getLength() )
135 aId += rtl::OUString::createFromAscii( "/" );
137 aId += rtl::OUString::createFromAscii( escaped_name );
139 g_free( escaped_name );
141 maResults[ nIndex ]->aId = aId;
142 return aId;
144 return aId;
147 return ::rtl::OUString();
150 uno::Reference< ucb::XContentIdentifier > DataSupplier::queryContentIdentifier( sal_uInt32 nIndex )
152 if ( nIndex < maResults.size() )
154 uno::Reference< ucb::XContentIdentifier > xId = maResults[ nIndex ]->xId;
155 if ( xId.is() )
157 // Already cached.
158 return xId;
162 ::rtl::OUString aId = queryContentIdentifierString( nIndex );
163 if ( aId.getLength() )
165 uno::Reference< ucb::XContentIdentifier > xId = new ucbhelper::ContentIdentifier( aId );
166 maResults[ nIndex ]->xId = xId;
167 return xId;
170 return uno::Reference< ucb::XContentIdentifier >();
173 uno::Reference< ucb::XContent > DataSupplier::queryContent( sal_uInt32 nIndex )
175 if ( nIndex < maResults.size() )
177 uno::Reference< ucb::XContent > xContent = maResults[ nIndex ]->xContent;
178 if ( xContent.is() )
180 // Already cached.
181 return xContent;
185 uno::Reference< ucb::XContentIdentifier > xId = queryContentIdentifier( nIndex );
186 if ( xId.is() )
190 uno::Reference< ucb::XContent > xContent = mxContent->getProvider()->queryContent( xId );
191 maResults[ nIndex ]->xContent = xContent;
192 return xContent;
194 catch ( ucb::IllegalIdentifierException& )
198 return uno::Reference< ucb::XContent >();
201 sal_Bool DataSupplier::getResult( sal_uInt32 nIndex )
203 if ( maResults.size() > nIndex ) // Result already present.
204 return sal_True;
206 if ( getData() && maResults.size() > nIndex )
207 return sal_True;
209 return sal_False;
212 sal_uInt32 DataSupplier::totalCount()
214 getData();
215 return maResults.size();
218 sal_uInt32 DataSupplier::currentCount()
220 return maResults.size();
223 sal_Bool DataSupplier::isCountFinal()
225 return mbCountFinal;
228 uno::Reference< sdbc::XRow > DataSupplier::queryPropertyValues( sal_uInt32 nIndex )
230 if ( nIndex < maResults.size() )
232 uno::Reference< sdbc::XRow > xRow = maResults[ nIndex ]->xRow;
233 if ( xRow.is() )
235 // Already cached.
236 return xRow;
240 if ( getResult( nIndex ) )
242 uno::Reference< sdbc::XRow > xRow = Content::getPropertyValuesFromGFileInfo(
243 maResults[ nIndex ]->pInfo, m_xSMgr, getResultSet()->getProperties());
245 maResults[ nIndex ]->xRow = xRow;
246 return xRow;
248 return uno::Reference< sdbc::XRow >();
251 void DataSupplier::releasePropertyValues( sal_uInt32 nIndex )
253 if ( nIndex < maResults.size() )
254 maResults[ nIndex ]->xRow = uno::Reference< sdbc::XRow >();
257 void DataSupplier::close()
261 void DataSupplier::validate() throw( ucb::ResultSetException )