1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include <vector>
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <ucbhelper/contentidentifier.hxx>
31*cdf0e10cSrcweir #include <ucbhelper/providerhelper.hxx>
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <com/sun/star/ucb/OpenMode.hpp>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include "gio_datasupplier.hxx"
36*cdf0e10cSrcweir #include "gio_content.hxx"
37*cdf0e10cSrcweir #include "gio_provider.hxx"
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include <stdio.h>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir using namespace com::sun::star;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir using namespace gio;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir namespace gio
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir typedef std::vector< ResultListEntry* > ResultList;
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir DataSupplier::DataSupplier( const uno::Reference< lang::XMultiServiceFactory >& rxSMgr,
51*cdf0e10cSrcweir     const uno::Reference< ::gio::Content >& rContent, sal_Int32 nOpenMode )
52*cdf0e10cSrcweir     : mxContent(rContent), m_xSMgr(rxSMgr), mnOpenMode(nOpenMode), mbCountFinal(false)
53*cdf0e10cSrcweir {
54*cdf0e10cSrcweir }
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir bool DataSupplier::getData()
57*cdf0e10cSrcweir {
58*cdf0e10cSrcweir     if (mbCountFinal)
59*cdf0e10cSrcweir         return true;
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir     GFile *pFile = mxContent->getGFile();
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir     GFileEnumerator* pEnumerator = g_file_enumerate_children(pFile, "*",
64*cdf0e10cSrcweir         G_FILE_QUERY_INFO_NONE, NULL, NULL);
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir     if (!pEnumerator)
67*cdf0e10cSrcweir         return sal_False;
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir     GFileInfo *pInfo = NULL;
70*cdf0e10cSrcweir     while ((pInfo = g_file_enumerator_next_file (pEnumerator, NULL, NULL)))
71*cdf0e10cSrcweir     {
72*cdf0e10cSrcweir         switch ( mnOpenMode )
73*cdf0e10cSrcweir         {
74*cdf0e10cSrcweir             case ucb::OpenMode::FOLDERS:
75*cdf0e10cSrcweir                 if (g_file_info_get_file_type(pInfo) != G_FILE_TYPE_DIRECTORY)
76*cdf0e10cSrcweir                     continue;
77*cdf0e10cSrcweir                 break;
78*cdf0e10cSrcweir             case ucb::OpenMode::DOCUMENTS:
79*cdf0e10cSrcweir                 if (g_file_info_get_file_type(pInfo) != G_FILE_TYPE_REGULAR)
80*cdf0e10cSrcweir                     continue;
81*cdf0e10cSrcweir                 break;
82*cdf0e10cSrcweir             case ucb::OpenMode::ALL:
83*cdf0e10cSrcweir             default:
84*cdf0e10cSrcweir                 break;
85*cdf0e10cSrcweir         }
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir         maResults.push_back( new ResultListEntry( pInfo ) );
88*cdf0e10cSrcweir         g_object_unref(pInfo);
89*cdf0e10cSrcweir     }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir     mbCountFinal = sal_True;
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir     g_file_enumerator_close(pEnumerator, NULL, NULL);
94*cdf0e10cSrcweir     return true;
95*cdf0e10cSrcweir }
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir DataSupplier::~DataSupplier()
98*cdf0e10cSrcweir {
99*cdf0e10cSrcweir     ResultList::const_iterator it  = maResults.begin();
100*cdf0e10cSrcweir     ResultList::const_iterator end = maResults.end();
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir     while ( it != end )
103*cdf0e10cSrcweir     {
104*cdf0e10cSrcweir         delete (*it);
105*cdf0e10cSrcweir         it++;
106*cdf0e10cSrcweir     }
107*cdf0e10cSrcweir }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir ::rtl::OUString DataSupplier::queryContentIdentifierString( sal_uInt32 nIndex )
110*cdf0e10cSrcweir {
111*cdf0e10cSrcweir     if ( nIndex < maResults.size() )
112*cdf0e10cSrcweir     {
113*cdf0e10cSrcweir         ::rtl::OUString aId = maResults[ nIndex ]->aId;
114*cdf0e10cSrcweir         if ( aId.getLength() )
115*cdf0e10cSrcweir         {
116*cdf0e10cSrcweir             // Already cached.
117*cdf0e10cSrcweir             return aId;
118*cdf0e10cSrcweir         }
119*cdf0e10cSrcweir     }
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir     if ( getResult( nIndex ) )
122*cdf0e10cSrcweir     {
123*cdf0e10cSrcweir         GFile *pFile = mxContent->getGFile();
124*cdf0e10cSrcweir         char* parent = g_file_get_uri(pFile);
125*cdf0e10cSrcweir         rtl::OUString aId = rtl::OUString::createFromAscii( parent );
126*cdf0e10cSrcweir         g_free(parent);
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir         char *escaped_name =
129*cdf0e10cSrcweir             g_uri_escape_string( g_file_info_get_name(maResults[ nIndex ]->pInfo) , NULL, false);
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir         if ( ( aId.lastIndexOf( '/' ) + 1 ) != aId.getLength() )
132*cdf0e10cSrcweir                 aId += rtl::OUString::createFromAscii( "/" );
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir         aId += rtl::OUString::createFromAscii( escaped_name );
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir         g_free( escaped_name );
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir         maResults[ nIndex ]->aId = aId;
139*cdf0e10cSrcweir         return aId;
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir         return aId;
142*cdf0e10cSrcweir     }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir     return ::rtl::OUString();
145*cdf0e10cSrcweir }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir uno::Reference< ucb::XContentIdentifier > DataSupplier::queryContentIdentifier( sal_uInt32 nIndex )
148*cdf0e10cSrcweir {
149*cdf0e10cSrcweir     if ( nIndex < maResults.size() )
150*cdf0e10cSrcweir     {
151*cdf0e10cSrcweir         uno::Reference< ucb::XContentIdentifier > xId = maResults[ nIndex ]->xId;
152*cdf0e10cSrcweir         if ( xId.is() )
153*cdf0e10cSrcweir         {
154*cdf0e10cSrcweir             // Already cached.
155*cdf0e10cSrcweir             return xId;
156*cdf0e10cSrcweir         }
157*cdf0e10cSrcweir     }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir     ::rtl::OUString aId = queryContentIdentifierString( nIndex );
160*cdf0e10cSrcweir     if ( aId.getLength() )
161*cdf0e10cSrcweir     {
162*cdf0e10cSrcweir         uno::Reference< ucb::XContentIdentifier > xId = new ucbhelper::ContentIdentifier( aId );
163*cdf0e10cSrcweir         maResults[ nIndex ]->xId = xId;
164*cdf0e10cSrcweir         return xId;
165*cdf0e10cSrcweir     }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir     return uno::Reference< ucb::XContentIdentifier >();
168*cdf0e10cSrcweir }
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir uno::Reference< ucb::XContent > DataSupplier::queryContent( sal_uInt32 nIndex )
171*cdf0e10cSrcweir {
172*cdf0e10cSrcweir     if ( nIndex < maResults.size() )
173*cdf0e10cSrcweir     {
174*cdf0e10cSrcweir         uno::Reference< ucb::XContent > xContent = maResults[ nIndex ]->xContent;
175*cdf0e10cSrcweir         if ( xContent.is() )
176*cdf0e10cSrcweir         {
177*cdf0e10cSrcweir             // Already cached.
178*cdf0e10cSrcweir             return xContent;
179*cdf0e10cSrcweir         }
180*cdf0e10cSrcweir     }
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir     uno::Reference< ucb::XContentIdentifier > xId = queryContentIdentifier( nIndex );
183*cdf0e10cSrcweir     if ( xId.is() )
184*cdf0e10cSrcweir     {
185*cdf0e10cSrcweir         try
186*cdf0e10cSrcweir         {
187*cdf0e10cSrcweir             uno::Reference< ucb::XContent > xContent = mxContent->getProvider()->queryContent( xId );
188*cdf0e10cSrcweir             maResults[ nIndex ]->xContent = xContent;
189*cdf0e10cSrcweir             return xContent;
190*cdf0e10cSrcweir         }
191*cdf0e10cSrcweir         catch ( ucb::IllegalIdentifierException& )
192*cdf0e10cSrcweir         {
193*cdf0e10cSrcweir         }
194*cdf0e10cSrcweir     }
195*cdf0e10cSrcweir     return uno::Reference< ucb::XContent >();
196*cdf0e10cSrcweir }
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir sal_Bool DataSupplier::getResult( sal_uInt32 nIndex )
199*cdf0e10cSrcweir {
200*cdf0e10cSrcweir     if ( maResults.size() > nIndex ) // Result already present.
201*cdf0e10cSrcweir         return sal_True;
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir     if ( getData() && maResults.size() > nIndex )
204*cdf0e10cSrcweir         return sal_True;
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir     return sal_False;
207*cdf0e10cSrcweir }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir sal_uInt32 DataSupplier::totalCount()
210*cdf0e10cSrcweir {
211*cdf0e10cSrcweir     getData();
212*cdf0e10cSrcweir     return maResults.size();
213*cdf0e10cSrcweir }
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir sal_uInt32 DataSupplier::currentCount()
216*cdf0e10cSrcweir {
217*cdf0e10cSrcweir     return maResults.size();
218*cdf0e10cSrcweir }
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir sal_Bool DataSupplier::isCountFinal()
221*cdf0e10cSrcweir {
222*cdf0e10cSrcweir     return mbCountFinal;
223*cdf0e10cSrcweir }
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir uno::Reference< sdbc::XRow > DataSupplier::queryPropertyValues( sal_uInt32 nIndex  )
226*cdf0e10cSrcweir {
227*cdf0e10cSrcweir     if ( nIndex < maResults.size() )
228*cdf0e10cSrcweir     {
229*cdf0e10cSrcweir         uno::Reference< sdbc::XRow > xRow = maResults[ nIndex ]->xRow;
230*cdf0e10cSrcweir         if ( xRow.is() )
231*cdf0e10cSrcweir         {
232*cdf0e10cSrcweir             // Already cached.
233*cdf0e10cSrcweir             return xRow;
234*cdf0e10cSrcweir         }
235*cdf0e10cSrcweir     }
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir     if ( getResult( nIndex ) )
238*cdf0e10cSrcweir     {
239*cdf0e10cSrcweir         uno::Reference< ucb::XContent > xContent( queryContent( nIndex ) );
240*cdf0e10cSrcweir         if ( xContent.is() )
241*cdf0e10cSrcweir         {
242*cdf0e10cSrcweir             try
243*cdf0e10cSrcweir             {
244*cdf0e10cSrcweir                 uno::Reference< ucb::XCommandProcessor > xCmdProc(
245*cdf0e10cSrcweir                     xContent, uno::UNO_QUERY_THROW );
246*cdf0e10cSrcweir                 sal_Int32 nCmdId( xCmdProc->createCommandIdentifier() );
247*cdf0e10cSrcweir                 ucb::Command aCmd;
248*cdf0e10cSrcweir                 aCmd.Name = rtl::OUString::createFromAscii( "getPropertyValues" );
249*cdf0e10cSrcweir                 aCmd.Handle = -1;
250*cdf0e10cSrcweir                 aCmd.Argument <<= getResultSet()->getProperties();
251*cdf0e10cSrcweir                 uno::Any aResult( xCmdProc->execute(
252*cdf0e10cSrcweir                     aCmd, nCmdId, getResultSet()->getEnvironment() ) );
253*cdf0e10cSrcweir                 uno::Reference< sdbc::XRow > xRow;
254*cdf0e10cSrcweir                 if ( aResult >>= xRow )
255*cdf0e10cSrcweir                 {
256*cdf0e10cSrcweir                     maResults[ nIndex ]->xRow = xRow;
257*cdf0e10cSrcweir                     return xRow;
258*cdf0e10cSrcweir                 }
259*cdf0e10cSrcweir             }
260*cdf0e10cSrcweir             catch ( uno::Exception const & )
261*cdf0e10cSrcweir             {
262*cdf0e10cSrcweir             }
263*cdf0e10cSrcweir         }
264*cdf0e10cSrcweir     }
265*cdf0e10cSrcweir     return uno::Reference< sdbc::XRow >();
266*cdf0e10cSrcweir }
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir void DataSupplier::releasePropertyValues( sal_uInt32 nIndex )
269*cdf0e10cSrcweir {
270*cdf0e10cSrcweir     if ( nIndex < maResults.size() )
271*cdf0e10cSrcweir         maResults[ nIndex ]->xRow = uno::Reference< sdbc::XRow >();
272*cdf0e10cSrcweir }
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir void DataSupplier::close()
275*cdf0e10cSrcweir {
276*cdf0e10cSrcweir }
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir void DataSupplier::validate() throw( ucb::ResultSetException )
279*cdf0e10cSrcweir {
280*cdf0e10cSrcweir }
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir }
283