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"
31 using namespace com::sun::star
;
38 typedef std::vector
< ResultListEntry
* > ResultList
;
40 DataSupplier::DataSupplier( const uno::Reference
< lang::XMultiServiceFactory
>& rxSMgr
,
41 const uno::Reference
< ::gio::Content
>& rContent
, sal_Int32 nOpenMode
)
42 : mxContent(rContent
), m_xSMgr(rxSMgr
), mnOpenMode(nOpenMode
), mbCountFinal(false)
46 bool DataSupplier::getData()
51 GFile
*pFile
= mxContent
->getGFile();
53 GFileEnumerator
* pEnumerator
= g_file_enumerate_children(pFile
, "*",
54 G_FILE_QUERY_INFO_NONE
, NULL
, NULL
);
59 GFileInfo
*pInfo
= NULL
;
60 while ((pInfo
= g_file_enumerator_next_file (pEnumerator
, NULL
, NULL
)))
64 case ucb::OpenMode::FOLDERS
:
65 if (g_file_info_get_file_type(pInfo
) != G_FILE_TYPE_DIRECTORY
)
68 case ucb::OpenMode::DOCUMENTS
:
69 if (g_file_info_get_file_type(pInfo
) != G_FILE_TYPE_REGULAR
)
72 case ucb::OpenMode::ALL
:
77 maResults
.push_back( new ResultListEntry( pInfo
) );
78 g_object_unref(pInfo
);
83 g_file_enumerator_close(pEnumerator
, NULL
, NULL
);
87 DataSupplier::~DataSupplier()
89 ResultList::const_iterator it
= maResults
.begin();
90 ResultList::const_iterator end
= maResults
.end();
99 OUString
DataSupplier::queryContentIdentifierString( sal_uInt32 nIndex
)
101 if ( nIndex
< maResults
.size() )
103 OUString aId
= maResults
[ nIndex
]->aId
;
104 if ( aId
.getLength() )
111 if ( getResult( nIndex
) )
113 GFile
*pFile
= mxContent
->getGFile();
114 char* parent
= g_file_get_uri(pFile
);
115 OUString aId
= OUString::createFromAscii( parent
);
119 g_uri_escape_string( g_file_info_get_name(maResults
[ nIndex
]->pInfo
) , NULL
, false);
121 if ( ( aId
.lastIndexOf( '/' ) + 1 ) != aId
.getLength() )
124 aId
+= OUString::createFromAscii( escaped_name
);
126 g_free( escaped_name
);
128 maResults
[ nIndex
]->aId
= aId
;
135 uno::Reference
< ucb::XContentIdentifier
> DataSupplier::queryContentIdentifier( sal_uInt32 nIndex
)
137 if ( nIndex
< maResults
.size() )
139 uno::Reference
< ucb::XContentIdentifier
> xId
= maResults
[ nIndex
]->xId
;
147 OUString aId
= queryContentIdentifierString( nIndex
);
148 if ( aId
.getLength() )
150 uno::Reference
< ucb::XContentIdentifier
> xId
= new ucbhelper::ContentIdentifier( aId
);
151 maResults
[ nIndex
]->xId
= xId
;
155 return uno::Reference
< ucb::XContentIdentifier
>();
158 uno::Reference
< ucb::XContent
> DataSupplier::queryContent( sal_uInt32 nIndex
)
160 if ( nIndex
< maResults
.size() )
162 uno::Reference
< ucb::XContent
> xContent
= maResults
[ nIndex
]->xContent
;
170 uno::Reference
< ucb::XContentIdentifier
> xId
= queryContentIdentifier( nIndex
);
175 uno::Reference
< ucb::XContent
> xContent
= mxContent
->getProvider()->queryContent( xId
);
176 maResults
[ nIndex
]->xContent
= xContent
;
179 catch ( ucb::IllegalIdentifierException
& )
183 return uno::Reference
< ucb::XContent
>();
186 bool DataSupplier::getResult( sal_uInt32 nIndex
)
188 if ( maResults
.size() > nIndex
) // Result already present.
191 if ( getData() && maResults
.size() > nIndex
)
197 sal_uInt32
DataSupplier::totalCount()
200 return maResults
.size();
203 sal_uInt32
DataSupplier::currentCount()
205 return maResults
.size();
208 bool DataSupplier::isCountFinal()
213 uno::Reference
< sdbc::XRow
> DataSupplier::queryPropertyValues( sal_uInt32 nIndex
)
215 if ( nIndex
< maResults
.size() )
217 uno::Reference
< sdbc::XRow
> xRow
= maResults
[ nIndex
]->xRow
;
225 if ( getResult( nIndex
) )
227 uno::Reference
< ucb::XContent
> xContent( queryContent( nIndex
) );
232 uno::Reference
< ucb::XCommandProcessor
> xCmdProc(
233 xContent
, uno::UNO_QUERY_THROW
);
234 sal_Int32
nCmdId( xCmdProc
->createCommandIdentifier() );
236 aCmd
.Name
= "getPropertyValues";
238 aCmd
.Argument
<<= getResultSet()->getProperties();
239 uno::Any
aResult( xCmdProc
->execute(
240 aCmd
, nCmdId
, getResultSet()->getEnvironment() ) );
241 uno::Reference
< sdbc::XRow
> xRow
;
242 if ( aResult
>>= xRow
)
244 maResults
[ nIndex
]->xRow
= xRow
;
248 catch ( uno::Exception
const & )
253 return uno::Reference
< sdbc::XRow
>();
256 void DataSupplier::releasePropertyValues( sal_uInt32 nIndex
)
258 if ( nIndex
< maResults
.size() )
259 maResults
[ nIndex
]->xRow
= uno::Reference
< sdbc::XRow
>();
262 void DataSupplier::close()
266 void DataSupplier::validate() throw( ucb::ResultSetException
)
272 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */