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