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