LibreOffice Module fpicker (master) 1
fpsmartcontent.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
20#include "fpsmartcontent.hxx"
21
22#include <com/sun/star/container/XChild.hpp>
23#include <com/sun/star/task/InteractionHandler.hpp>
24#include <com/sun/star/ucb/ContentCreationException.hpp>
25#include <com/sun/star/ucb/ContentInfo.hpp>
26#include <com/sun/star/ucb/ContentInfoAttribute.hpp>
27#include <com/sun/star/ucb/XContent.hpp>
28
32
33
34namespace svt
35{
36
37
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::task;
40 using namespace ::com::sun::star::ucb;
41 using namespace ::com::sun::star::lang;
42 using namespace ::com::sun::star::container;
43
44
45 //= SmartContent
46
47
49 :m_eState( NOT_BOUND )
50 {
51 }
52
53
54 SmartContent::SmartContent( const OUString& _rInitialURL )
55 :m_eState( NOT_BOUND )
56 {
57 bindTo( _rInitialURL );
58 }
59
60
62 {
63 /* This destructor originally contained the following blurb: "Do
64 not delete the content. Because the content will be used by
65 the cache." This is just plain silly, because it relies on
66 the provider caching created contents (which is done by
67 ucbhelper::ContentProviderImplHelper, but we do not actually
68 expect all providers to use that, right?) Otherwise we are
69 just leaking memory.
70
71 TODO: If there is real need for caching the content, it must
72 be done here.
73 */
74 }
75
76
78 {
79 Reference< XComponentContext > xContext = ::comphelper::getProcessComponentContext();
80 Reference< XInteractionHandler > xGlobalInteractionHandler(
81 InteractionHandler::createWithParent(xContext, nullptr), UNO_QUERY_THROW );
82
83 m_xOwnInteraction = new ::svt::OFilePickerInteractionHandler(xGlobalInteractionHandler);
84 m_xOwnInteraction->enableInterceptions(eInterceptions);
85
86 m_xCmdEnv = new ::ucbhelper::CommandEnvironment( m_xOwnInteraction, Reference< XProgressHandler >() );
87 }
88
89
91 {
92 m_xOwnInteraction.clear();
93
94 Reference< XComponentContext > xContext = ::comphelper::getProcessComponentContext();
95 Reference< XInteractionHandler > xGlobalInteractionHandler(
96 InteractionHandler::createWithParent(xContext, nullptr), UNO_QUERY_THROW );
97 m_xCmdEnv = new ucbhelper::CommandEnvironment( xGlobalInteractionHandler, Reference< XProgressHandler >() );
98 }
99
100
102 {
103 return m_xOwnInteraction.get();
104 }
105
106
108 {
109 if (m_xOwnInteraction.is())
110 return IHT_OWN;
111
112 if (!m_xCmdEnv.is())
113 return IHT_NONE;
114
115 return IHT_DEFAULT;
116 }
117
118
120 {
121 m_xOwnInteraction.clear();
122 m_xCmdEnv.clear();
123 }
124
125
126 void SmartContent::bindTo( const OUString& _rURL )
127 {
128 if ( getURL() == _rURL )
129 // nothing to do, regardless of the state
130 return;
131
132 m_oContent.reset();
133 m_eState = INVALID; // default to INVALID
134 m_sURL = _rURL;
135
136 if ( !m_sURL.isEmpty() )
137 {
138 try
139 {
142 // from now on, the state is unknown -> we cannot know for sure if the content
143 // is really valid (some UCP's only tell this when asking for properties, not upon
144 // creation)
145 }
146 catch( const ContentCreationException& )
147 {
148 }
149 catch( const Exception& )
150 {
151 TOOLS_WARN_EXCEPTION( "fpicker", "SmartContent::bindTo: unexpected exception caught!" );
152 }
153 }
154 else
155 {
157 }
158
159
160 // don't forget to reset the may internal used interaction handler ...
161 // But do it only for our own specialized interaction helper!
163 if (pHandler)
164 {
165 pHandler->resetUseState();
166 pHandler->forgetRequest();
167 }
168 }
169
170
171 bool SmartContent::implIs( const OUString& _rURL, Type _eType )
172 {
173 // bind to this content
174 bindTo( _rURL );
175
176 // did we survive this?
177 if ( isInvalid() || !isBound() )
178 return false;
179
180 assert( m_oContent && "SmartContent::implIs: inconsistence!" );
181 // if, after a bindTo, we don't have a content, then we should be INVALID, or at least
182 // NOT_BOUND (the latter happens, for example, if somebody tries to ask for an empty URL)
183
184 bool bIs = false;
185 try
186 {
187 if ( Folder == _eType )
188 bIs = m_oContent->isFolder();
189 else
190 bIs = m_oContent->isDocument();
191
192 // from here on, we definitely know that the content is valid
193 m_eState = VALID;
194 }
195 catch( const Exception& )
196 {
197 // now we're definitely invalid
199 }
200 return bIs;
201 }
202
203
204 void SmartContent::getTitle( OUString& /* [out] */ _rTitle )
205 {
206 if ( !isBound() || isInvalid() )
207 return;
208
209 try
210 {
211 OUString sTitle;
212 m_oContent->getPropertyValue("Title") >>= sTitle;
213 _rTitle = sTitle;
214
215 // from here on, we definitely know that the content is valid
216 m_eState = VALID;
217 }
218 catch( const css::uno::Exception& )
219 {
220 // now we're definitely invalid
222 }
223 }
224
225
227 {
228 if ( !isBound() || isInvalid() )
229 return false;
230
231 bool bRet = false;
232 try
233 {
234 Reference< XChild > xChild( m_oContent->get(), UNO_QUERY );
235 if ( xChild.is() )
236 {
237 Reference< XContent > xParent( xChild->getParent(), UNO_QUERY );
238 if ( xParent.is() )
239 {
240 const OUString aParentURL( xParent->getIdentifier()->getContentIdentifier() );
241 bRet = ( !aParentURL.isEmpty() && aParentURL != m_oContent->getURL() );
242
243 // now we're definitely valid
244 m_eState = VALID;
245 }
246 }
247 }
248 catch( const Exception& )
249 {
250 // now we're definitely invalid
252 }
253 return bRet;
254 }
255
256
258 {
259 if ( !isBound() || isInvalid() )
260 return false;
261
262 bool bRet = false;
263 try
264 {
265 const css::uno::Sequence<css::ucb::ContentInfo> aContentsInfo = m_oContent->queryCreatableContentsInfo();
266 for ( auto const& rInfo : aContentsInfo )
267 {
268 // Simply look for the first KIND_FOLDER...
269 if ( rInfo.Attributes & ContentInfoAttribute::KIND_FOLDER )
270 {
271 bRet = true;
272 break;
273 }
274 }
275
276 // now we're definitely valid
277 m_eState = VALID;
278 }
279 catch( const Exception& )
280 {
281 // now we're definitely invalid
283 }
284 return bRet;
285 }
286
287 OUString SmartContent::createFolder( const OUString& _rTitle )
288 {
289 OUString aCreatedUrl;
290 try
291 {
292 OUString sFolderType;
293
294 const css::uno::Sequence<css::ucb::ContentInfo> aContentsInfo = m_oContent->queryCreatableContentsInfo();
295 for ( auto const& rInfo : aContentsInfo )
296 {
297 // Simply look for the first KIND_FOLDER...
298 if ( rInfo.Attributes & ContentInfoAttribute::KIND_FOLDER )
299 {
300 sFolderType = rInfo.Type;
301 break;
302 }
303 }
304
305 if ( !sFolderType.isEmpty() )
306 {
307 ucbhelper::Content aCreated;
308 Sequence< OUString > aNames { "Title" };
309 Sequence< Any > aValues { Any(_rTitle) };
310 m_oContent->insertNewContent( sFolderType, aNames, aValues, aCreated );
311
312 aCreatedUrl = aCreated.getURL();
313 }
314 }
315 catch( const Exception& )
316 {
317 }
318 return aCreatedUrl;
319 }
320
321
322} // namespace svt
323
324
325/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
an InteractionHandler implementation which extends another handler with some customizability
EInterceptedInteractions
flags, which indicates special handled interactions These values will be used combined as flags - so ...
InteractionHandlerType
describes different types of interaction handlers
std::optional<::ucbhelper::Content > m_oContent
::svt::OFilePickerInteractionHandler * getOwnInteractionHandler() const
return the internal used interaction handler object ... Because this pointer will be valid only,...
bool isBound() const
checks if the content is bound
void getTitle(OUString &_rTitle)
retrieves the title of the content @precond the content is bound and not invalid
void enableDefaultInteractionHandler()
disable the specialized interaction handler and use the global UI interaction handler only.
void disableInteractionHandler()
disable internal used interaction handler object ...
bool hasParentFolder()
checks if the content has a parent folder @precond the content is bound and not invalid
void bindTo(const OUString &_rURL)
(re)creates the content for the given URL
void enableOwnInteractionHandler(::svt::OFilePickerInteractionHandler::EInterceptedInteractions eInterceptions)
create and set a specialized interaction handler at the internal used command environment.
css::uno::Reference< css::ucb::XCommandEnvironment > m_xCmdEnv
bool canCreateFolder()
checks if sub folders below the content can be created @precond the content is bound and not invalid
OUString const & getURL() const
returns the URL of the content
OUString createFolder(const OUString &_rTitle)
creates a new folder with the given title and return the corresponding URL.
InteractionHandlerType queryCurrentInteractionHandler() const
return the type of the internal used interaction handler object ...
bool isInvalid() const
checks if the content is valid
bool implIs(const OUString &_rURL, Type _eType)
checks if the currently bound content is a folder or document
rtl::Reference<::svt::OFilePickerInteractionHandler > m_xOwnInteraction
const OUString & getURL() const
#define TOOLS_WARN_EXCEPTION(area, stream)
@ Exception
Reference< XComponentContext > getProcessComponentContext()