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 .
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"
33 using namespace com::sun::star
;
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()
53 GFile
*pFile
= mxContent
->getGFile();
55 GFileEnumerator
* pEnumerator
= g_file_enumerate_children(pFile
, "*",
56 G_FILE_QUERY_INFO_NONE
, NULL
, NULL
);
61 GFileInfo
*pInfo
= NULL
;
62 while ((pInfo
= g_file_enumerator_next_file (pEnumerator
, NULL
, NULL
)))
66 case ucb::OpenMode::FOLDERS
:
67 if (g_file_info_get_file_type(pInfo
) != G_FILE_TYPE_DIRECTORY
)
70 case ucb::OpenMode::DOCUMENTS
:
71 if (g_file_info_get_file_type(pInfo
) != G_FILE_TYPE_REGULAR
)
74 case ucb::OpenMode::ALL
:
79 maResults
.push_back( new ResultListEntry( pInfo
) );
80 g_object_unref(pInfo
);
83 mbCountFinal
= sal_True
;
85 g_file_enumerator_close(pEnumerator
, NULL
, NULL
);
89 DataSupplier::~DataSupplier()
91 ResultList::const_iterator it
= maResults
.begin();
92 ResultList::const_iterator end
= maResults
.end();
101 OUString
DataSupplier::queryContentIdentifierString( sal_uInt32 nIndex
)
103 if ( nIndex
< maResults
.size() )
105 OUString aId
= maResults
[ nIndex
]->aId
;
106 if ( aId
.getLength() )
113 if ( getResult( nIndex
) )
115 GFile
*pFile
= mxContent
->getGFile();
116 char* parent
= g_file_get_uri(pFile
);
117 OUString aId
= OUString::createFromAscii( parent
);
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
;
137 uno::Reference
< ucb::XContentIdentifier
> DataSupplier::queryContentIdentifier( sal_uInt32 nIndex
)
139 if ( nIndex
< maResults
.size() )
141 uno::Reference
< ucb::XContentIdentifier
> xId
= maResults
[ nIndex
]->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
;
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
;
172 uno::Reference
< ucb::XContentIdentifier
> xId
= queryContentIdentifier( nIndex
);
177 uno::Reference
< ucb::XContent
> xContent
= mxContent
->getProvider()->queryContent( xId
);
178 maResults
[ nIndex
]->xContent
= 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.
193 if ( getData() && maResults
.size() > nIndex
)
199 sal_uInt32
DataSupplier::totalCount()
202 return maResults
.size();
205 sal_uInt32
DataSupplier::currentCount()
207 return maResults
.size();
210 sal_Bool
DataSupplier::isCountFinal()
215 uno::Reference
< sdbc::XRow
> DataSupplier::queryPropertyValues( sal_uInt32 nIndex
)
217 if ( nIndex
< maResults
.size() )
219 uno::Reference
< sdbc::XRow
> xRow
= maResults
[ nIndex
]->xRow
;
227 if ( getResult( nIndex
) )
229 uno::Reference
< ucb::XContent
> xContent( queryContent( nIndex
) );
234 uno::Reference
< ucb::XCommandProcessor
> xCmdProc(
235 xContent
, uno::UNO_QUERY_THROW
);
236 sal_Int32
nCmdId( xCmdProc
->createCommandIdentifier() );
238 aCmd
.Name
= OUString("getPropertyValues");
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
;
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: */