| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 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/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 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 . |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <ucbhelper/contentidentifier.hxx> |
| 21 |
#include <ucbhelper/providerhelper.hxx> |
| 22 |
#include <com/sun/star/ucb/IllegalIdentifierException.hpp> |
| 23 |
#include <com/sun/star/ucb/OpenMode.hpp> |
| 24 |
#include <utility> |
| 25 |
|
| 26 |
#include "gio_datasupplier.hxx" |
| 27 |
#include "gio_content.hxx" |
| 28 |
|
| 29 |
using namespace gio; |
| 30 |
|
| 31 |
namespace gio |
| 32 |
{ |
| 33 |
|
| 34 |
DataSupplier::DataSupplier( rtl::Reference< ::gio::Content > xContent, sal_Int32 nOpenMode ) |
| 35 |
: mxContent(std::move(xContent)), mnOpenMode(nOpenMode), mbCountFinal(false) |
| 36 |
{ |
| 37 |
} |
| 38 |
|
| 39 |
bool DataSupplier::getData() |
| 40 |
{ |
| 41 |
if (mbCountFinal) |
| 42 |
return true; |
| 43 |
|
| 44 |
GFile *pFile = mxContent->getGFile(); |
| 45 |
|
| 46 |
GFileEnumerator* pEnumerator = g_file_enumerate_children(pFile, "*", |
| 47 |
G_FILE_QUERY_INFO_NONE, nullptr, nullptr); |
| 48 |
|
| 49 |
if (!pEnumerator) |
| 50 |
return false; |
| 51 |
|
| 52 |
GFileInfo *pInfo = nullptr; |
| 53 |
while ((pInfo = g_file_enumerator_next_file (pEnumerator, nullptr, nullptr))) |
| 54 |
{ |
| 55 |
switch ( mnOpenMode ) |
| 56 |
{ |
| 57 |
case css::ucb::OpenMode::FOLDERS: |
| 58 |
if (g_file_info_get_file_type(pInfo) != G_FILE_TYPE_DIRECTORY) |
| 59 |
continue; |
| 60 |
break; |
| 61 |
case css::ucb::OpenMode::DOCUMENTS: |
| 62 |
if (g_file_info_get_file_type(pInfo) != G_FILE_TYPE_REGULAR) |
| 63 |
continue; |
| 64 |
break; |
| 65 |
case css::ucb::OpenMode::ALL: |
| 66 |
default: |
| 67 |
break; |
| 68 |
} |
| 69 |
|
| 70 |
maResults.emplace_back( new ResultListEntry( pInfo ) ); |
| 71 |
g_object_unref(pInfo); |
| 72 |
} |
| 73 |
|
| 74 |
mbCountFinal = true; |
| 75 |
|
| 76 |
g_file_enumerator_close(pEnumerator, nullptr, nullptr); |
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
DataSupplier::~DataSupplier() |
| 81 |
{ |
| 82 |
} |
| 83 |
|
| 84 |
OUString DataSupplier::queryContentIdentifierString( std::unique_lock<std::mutex>& rResultSetGuard, sal_uInt32 nIndex ) |
| 85 |
{ |
| 86 |
if ( nIndex < maResults.size() ) |
| 87 |
{ |
| 88 |
OUString aId = maResults[ nIndex ]->aId; |
| 89 |
if ( aId.getLength() ) |
| 90 |
{ |
| 91 |
// Already cached. |
| 92 |
return aId; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
if ( getResult( rResultSetGuard, nIndex ) ) |
| 97 |
{ |
| 98 |
GFile *pFile = mxContent->getGFile(); |
| 99 |
char* parent = g_file_get_uri(pFile); |
| 100 |
OUString aId = OUString::createFromAscii( parent ); |
| 101 |
g_free(parent); |
| 102 |
|
| 103 |
char *escaped_name = |
| 104 |
g_uri_escape_string( g_file_info_get_name(maResults[ nIndex ]->pInfo) , nullptr, false); |
| 105 |
|
| 106 |
if ( ( aId.lastIndexOf( '/' ) + 1 ) != aId.getLength() ) |
| 107 |
aId += "/"; |
| 108 |
|
| 109 |
aId += OUString::createFromAscii( escaped_name ); |
| 110 |
|
| 111 |
g_free( escaped_name ); |
| 112 |
|
| 113 |
maResults[ nIndex ]->aId = aId; |
| 114 |
return aId; |
| 115 |
} |
| 116 |
|
| 117 |
return OUString(); |
| 118 |
} |
| 119 |
|
| 120 |
css::uno::Reference< css::ucb::XContentIdentifier > DataSupplier::queryContentIdentifier( std::unique_lock<std::mutex>& rResultSetGuard, sal_uInt32 nIndex ) |
| 121 |
{ |
| 122 |
if ( nIndex < maResults.size() ) |
| 123 |
{ |
| 124 |
css::uno::Reference< css::ucb::XContentIdentifier > xId = maResults[ nIndex ]->xId; |
| 125 |
if ( xId.is() ) |
| 126 |
{ |
| 127 |
// Already cached. |
| 128 |
return xId; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
OUString aId = queryContentIdentifierString( rResultSetGuard, nIndex ); |
| 133 |
if ( aId.getLength() ) |
| 134 |
{ |
| 135 |
css::uno::Reference< css::ucb::XContentIdentifier > xId = new ucbhelper::ContentIdentifier( aId ); |
| 136 |
maResults[ nIndex ]->xId = xId; |
| 137 |
return xId; |
| 138 |
} |
| 139 |
|
| 140 |
return css::uno::Reference< css::ucb::XContentIdentifier >(); |
| 141 |
} |
| 142 |
|
| 143 |
css::uno::Reference< css::ucb::XContent > DataSupplier::queryContent( std::unique_lock<std::mutex>& rResultSetGuard, sal_uInt32 nIndex ) |
| 144 |
{ |
| 145 |
if ( nIndex < maResults.size() ) |
| 146 |
{ |
| 147 |
css::uno::Reference< css::ucb::XContent > xContent = maResults[ nIndex ]->xContent; |
| 148 |
if ( xContent.is() ) |
| 149 |
{ |
| 150 |
// Already cached. |
| 151 |
return xContent; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
css::uno::Reference< css::ucb::XContentIdentifier > xId = queryContentIdentifier( rResultSetGuard, nIndex ); |
| 156 |
if ( xId.is() ) |
| 157 |
{ |
| 158 |
try |
| 159 |
{ |
| 160 |
css::uno::Reference< css::ucb::XContent > xContent = mxContent->getProvider()->queryContent( xId ); |
| 161 |
maResults[ nIndex ]->xContent = xContent; |
| 162 |
return xContent; |
| 163 |
} |
| 164 |
catch ( css::ucb::IllegalIdentifierException& ) |
| 165 |
{ |
| 166 |
} |
| 167 |
} |
| 168 |
return css::uno::Reference< css::ucb::XContent >(); |
| 169 |
} |
| 170 |
|
| 171 |
bool DataSupplier::getResult( std::unique_lock<std::mutex>& /*rResultSetGuard*/, sal_uInt32 nIndex ) |
| 172 |
{ |
| 173 |
if ( maResults.size() > nIndex ) // Result already present. |
| 174 |
return true; |
| 175 |
|
| 176 |
if ( getData() && maResults.size() > nIndex ) |
| 177 |
return true; |
| 178 |
|
| 179 |
return false; |
| 180 |
} |
| 181 |
|
| 182 |
sal_uInt32 DataSupplier::totalCount(std::unique_lock<std::mutex>& /*rResultSetGuard*/) |
| 183 |
{ |
| 184 |
getData(); |
| 185 |
return maResults.size(); |
| 186 |
} |
| 187 |
|
| 188 |
sal_uInt32 DataSupplier::currentCount() |
| 189 |
{ |
| 190 |
return maResults.size(); |
| 191 |
} |
| 192 |
|
| 193 |
bool DataSupplier::isCountFinal() |
| 194 |
{ |
| 195 |
return mbCountFinal; |
| 196 |
} |
| 197 |
|
| 198 |
css::uno::Reference< css::sdbc::XRow > DataSupplier::queryPropertyValues( std::unique_lock<std::mutex>& rResultSetGuard, sal_uInt32 nIndex ) |
| 199 |
{ |
| 200 |
if ( nIndex < maResults.size() ) |
| 201 |
{ |
| 202 |
css::uno::Reference< css::sdbc::XRow > xRow = maResults[ nIndex ]->xRow; |
| 203 |
if ( xRow.is() ) |
| 204 |
{ |
| 205 |
// Already cached. |
| 206 |
return xRow; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
if ( !getResult( rResultSetGuard, nIndex ) ) |
| 211 |
return {}; |
| 212 |
|
| 213 |
css::uno::Reference< css::ucb::XContent > xContent( queryContent( rResultSetGuard, nIndex ) ); |
| 214 |
if ( !xContent ) |
| 215 |
return {}; |
| 216 |
|
| 217 |
try |
| 218 |
{ |
| 219 |
css::uno::Reference< css::ucb::XCommandProcessor > xCmdProc( xContent, css::uno::UNO_QUERY ); |
| 220 |
if ( !xCmdProc ) |
| 221 |
return {}; |
| 222 |
sal_Int32 nCmdId( xCmdProc->createCommandIdentifier() ); |
| 223 |
css::ucb::Command aCmd; |
| 224 |
aCmd.Name = "getPropertyValues"; |
| 225 |
aCmd.Handle = -1; |
| 226 |
aCmd.Argument <<= getResultSet()->getProperties(); |
| 227 |
css::uno::Any aResult( xCmdProc->execute( |
| 228 |
aCmd, nCmdId, getResultSet()->getEnvironment() ) ); |
| 229 |
css::uno::Reference< css::sdbc::XRow > xRow; |
| 230 |
if ( aResult >>= xRow ) |
| 231 |
{ |
| 232 |
maResults[ nIndex ]->xRow = xRow; |
| 233 |
return xRow; |
| 234 |
} |
| 235 |
} |
| 236 |
catch ( css::uno::Exception const & ) |
| 237 |
{ |
| 238 |
} |
| 239 |
return css::uno::Reference< css::sdbc::XRow >(); |
| 240 |
} |
| 241 |
|
| 242 |
void DataSupplier::releasePropertyValues( sal_uInt32 nIndex ) |
| 243 |
{ |
| 244 |
if ( nIndex < maResults.size() ) |
| 245 |
maResults[ nIndex ]->xRow.clear(); |
| 246 |
} |
| 247 |
|
| 248 |
void DataSupplier::close() |
| 249 |
{ |
| 250 |
} |
| 251 |
|
| 252 |
void DataSupplier::validate() |
| 253 |
{ |
| 254 |
} |
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |