1b557fc96SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3b557fc96SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4b557fc96SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5b557fc96SAndrew Rist  * distributed with this work for additional information
6b557fc96SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7b557fc96SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8b557fc96SAndrew Rist  * "License"); you may not use this file except in compliance
9b557fc96SAndrew Rist  * with the License.  You may obtain a copy of the License at
10b557fc96SAndrew Rist  *
11b557fc96SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12b557fc96SAndrew Rist  *
13b557fc96SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14b557fc96SAndrew Rist  * software distributed under the License is distributed on an
15b557fc96SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16b557fc96SAndrew Rist  * KIND, either express or implied.  See the License for the
17b557fc96SAndrew Rist  * specific language governing permissions and limitations
18b557fc96SAndrew Rist  * under the License.
19b557fc96SAndrew Rist  *
20b557fc96SAndrew Rist  *************************************************************/
21b557fc96SAndrew Rist 
22b557fc96SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_fpicker.hxx"
26cdf0e10cSrcweir #include "fpsmartcontent.hxx"
27cdf0e10cSrcweir 
28cdf0e10cSrcweir /** === begin UNO includes === **/
29cdf0e10cSrcweir #include <com/sun/star/container/XChild.hpp>
30cdf0e10cSrcweir #include <com/sun/star/ucb/ContentInfo.hpp>
31cdf0e10cSrcweir #include <com/sun/star/ucb/ContentInfoAttribute.hpp>
32cdf0e10cSrcweir #include <com/sun/star/ucb/XContent.hpp>
33cdf0e10cSrcweir /** === end UNO includes === **/
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
36cdf0e10cSrcweir #include <ucbhelper/commandenvironment.hxx>
37cdf0e10cSrcweir #include <tools/solar.h>
38cdf0e10cSrcweir #include <tools/debug.hxx>
39cdf0e10cSrcweir #include <tools/string.hxx>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir //........................................................................
42cdf0e10cSrcweir namespace svt
43cdf0e10cSrcweir {
44cdf0e10cSrcweir //........................................................................
45cdf0e10cSrcweir 
46cdf0e10cSrcweir     using namespace ::com::sun::star::uno;
47cdf0e10cSrcweir     using namespace ::com::sun::star::task;
48cdf0e10cSrcweir     using namespace ::com::sun::star::ucb;
49cdf0e10cSrcweir     using namespace ::com::sun::star::lang;
50cdf0e10cSrcweir     using namespace ::com::sun::star::container;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir     //====================================================================
53cdf0e10cSrcweir     //= SmartContent
54cdf0e10cSrcweir     //====================================================================
55cdf0e10cSrcweir     //--------------------------------------------------------------------
SmartContent()56cdf0e10cSrcweir     SmartContent::SmartContent()
57cdf0e10cSrcweir         :m_pContent( NULL )
58cdf0e10cSrcweir         ,m_eState( NOT_BOUND )
59cdf0e10cSrcweir         ,m_pOwnInteraction( NULL )
60cdf0e10cSrcweir     {
61cdf0e10cSrcweir     }
62cdf0e10cSrcweir 
63cdf0e10cSrcweir     //--------------------------------------------------------------------
SmartContent(const::rtl::OUString & _rInitialURL)64cdf0e10cSrcweir     SmartContent::SmartContent( const ::rtl::OUString& _rInitialURL )
65cdf0e10cSrcweir         :m_pContent( NULL )
66cdf0e10cSrcweir         ,m_eState( NOT_BOUND )
67cdf0e10cSrcweir     {
68cdf0e10cSrcweir         bindTo( _rInitialURL );
69cdf0e10cSrcweir     }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     //--------------------------------------------------------------------
~SmartContent()72cdf0e10cSrcweir     SmartContent::~SmartContent()
73cdf0e10cSrcweir     {
74cdf0e10cSrcweir         //Do not delete the content. Because the content will be used by the cache.
75cdf0e10cSrcweir         //DELETEZ( m_pContent );
76cdf0e10cSrcweir     }
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     //--------------------------------------------------------------------
enableOwnInteractionHandler(::svt::OFilePickerInteractionHandler::EInterceptedInteractions eInterceptions)79cdf0e10cSrcweir     void SmartContent::enableOwnInteractionHandler(::svt::OFilePickerInteractionHandler::EInterceptedInteractions eInterceptions)
80cdf0e10cSrcweir     {
81cdf0e10cSrcweir         Reference< XMultiServiceFactory > xFactory = ::comphelper::getProcessServiceFactory();
82cdf0e10cSrcweir         Reference< XInteractionHandler >  xGlobalInteractionHandler = Reference< XInteractionHandler >(
83cdf0e10cSrcweir             xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.task.InteractionHandler") ) ), UNO_QUERY );
84cdf0e10cSrcweir 
85cdf0e10cSrcweir         m_pOwnInteraction = new ::svt::OFilePickerInteractionHandler(xGlobalInteractionHandler);
86cdf0e10cSrcweir         m_pOwnInteraction->enableInterceptions(eInterceptions);
87cdf0e10cSrcweir         m_xOwnInteraction = m_pOwnInteraction;
88cdf0e10cSrcweir 
89cdf0e10cSrcweir         m_xCmdEnv = new ::ucbhelper::CommandEnvironment( m_xOwnInteraction, Reference< XProgressHandler >() );
90cdf0e10cSrcweir     }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     //--------------------------------------------------------------------
enableDefaultInteractionHandler()93cdf0e10cSrcweir     void SmartContent::enableDefaultInteractionHandler()
94cdf0e10cSrcweir     {
95cdf0e10cSrcweir         // Don't free the memory here! It will be done by the next
96*07a3d7f1SPedro Giffuni         // call automatically - releasing of the uno reference ...
97cdf0e10cSrcweir         m_pOwnInteraction = NULL;
98cdf0e10cSrcweir         m_xOwnInteraction = Reference< XInteractionHandler >();
99cdf0e10cSrcweir 
100cdf0e10cSrcweir         Reference< XMultiServiceFactory > xFactory = ::comphelper::getProcessServiceFactory();
101cdf0e10cSrcweir         Reference< XInteractionHandler >  xGlobalInteractionHandler = Reference< XInteractionHandler >(
102cdf0e10cSrcweir             xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.task.InteractionHandler") ) ), UNO_QUERY );
103cdf0e10cSrcweir         m_xCmdEnv = new ucbhelper::CommandEnvironment( xGlobalInteractionHandler, Reference< XProgressHandler >() );
104cdf0e10cSrcweir     }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir     //--------------------------------------------------------------------
getOwnInteractionHandler() const107cdf0e10cSrcweir     ::svt::OFilePickerInteractionHandler* SmartContent::getOwnInteractionHandler() const
108cdf0e10cSrcweir     {
109cdf0e10cSrcweir         if (!m_xOwnInteraction.is())
110cdf0e10cSrcweir             return NULL;
111cdf0e10cSrcweir         return m_pOwnInteraction;
112cdf0e10cSrcweir     }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir     //--------------------------------------------------------------------
queryCurrentInteractionHandler() const115cdf0e10cSrcweir     SmartContent::InteractionHandlerType SmartContent::queryCurrentInteractionHandler() const
116cdf0e10cSrcweir     {
117cdf0e10cSrcweir         if (m_xOwnInteraction.is())
118cdf0e10cSrcweir             return IHT_OWN;
119cdf0e10cSrcweir 
120cdf0e10cSrcweir         if (!m_xCmdEnv.is())
121cdf0e10cSrcweir             return IHT_NONE;
122cdf0e10cSrcweir 
123cdf0e10cSrcweir         return IHT_DEFAULT;
124cdf0e10cSrcweir     }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir     //--------------------------------------------------------------------
disableInteractionHandler()127cdf0e10cSrcweir     void SmartContent::disableInteractionHandler()
128cdf0e10cSrcweir     {
129cdf0e10cSrcweir         // Don't free the memory here! It will be done by the next
130*07a3d7f1SPedro Giffuni         // call automatically - releasing of the uno reference ...
131cdf0e10cSrcweir         m_pOwnInteraction = NULL;
132cdf0e10cSrcweir         m_xOwnInteraction.clear();
133cdf0e10cSrcweir 
134cdf0e10cSrcweir         m_xCmdEnv.clear();
135cdf0e10cSrcweir     }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     //--------------------------------------------------------------------
bindTo(const::rtl::OUString & _rURL)138cdf0e10cSrcweir     void SmartContent::bindTo( const ::rtl::OUString& _rURL )
139cdf0e10cSrcweir     {
140cdf0e10cSrcweir         if ( getURL() == _rURL )
141cdf0e10cSrcweir             // nothing to do, regardless of the state
142cdf0e10cSrcweir             return;
143cdf0e10cSrcweir 
144cdf0e10cSrcweir         DELETEZ( m_pContent );
145cdf0e10cSrcweir         m_eState = INVALID; // default to INVALID
146cdf0e10cSrcweir         m_sURL = _rURL;
147cdf0e10cSrcweir 
148cdf0e10cSrcweir         if ( m_sURL.getLength() )
149cdf0e10cSrcweir         {
150cdf0e10cSrcweir             try
151cdf0e10cSrcweir             {
152cdf0e10cSrcweir                 m_pContent = new ::ucbhelper::Content( _rURL, m_xCmdEnv );
153cdf0e10cSrcweir                 m_eState = UNKNOWN;
154cdf0e10cSrcweir                     // from now on, the state is unknown -> we cannot know for sure if the content
155cdf0e10cSrcweir                     // is really valid (some UCP's only tell this when asking for properties, not upon
156cdf0e10cSrcweir                     // creation)
157cdf0e10cSrcweir             }
158cdf0e10cSrcweir             catch( ContentCreationException& )
159cdf0e10cSrcweir             {
160cdf0e10cSrcweir             }
161cdf0e10cSrcweir             catch( Exception& )
162cdf0e10cSrcweir             {
163cdf0e10cSrcweir                 DBG_ERROR( "SmartContent::bindTo: unexpected exception caught!" );
164cdf0e10cSrcweir             }
165cdf0e10cSrcweir         }
166cdf0e10cSrcweir         else
167cdf0e10cSrcweir         {
168cdf0e10cSrcweir             m_eState = NOT_BOUND;
169cdf0e10cSrcweir         }
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 
172cdf0e10cSrcweir         // don't forget to reset the may internal used interaction handler ...
173cdf0e10cSrcweir         // But do it only for our own specialized interaction helper!
174cdf0e10cSrcweir         ::svt::OFilePickerInteractionHandler* pHandler = getOwnInteractionHandler();
175cdf0e10cSrcweir         if (pHandler)
176cdf0e10cSrcweir         {
177cdf0e10cSrcweir             pHandler->resetUseState();
178cdf0e10cSrcweir             pHandler->forgetRequest();
179cdf0e10cSrcweir         }
180cdf0e10cSrcweir     }
181cdf0e10cSrcweir 
182cdf0e10cSrcweir     //--------------------------------------------------------------------
implIs(const::rtl::OUString & _rURL,Type _eType)183cdf0e10cSrcweir     sal_Bool SmartContent::implIs( const ::rtl::OUString& _rURL, Type _eType )
184cdf0e10cSrcweir     {
185cdf0e10cSrcweir         // bind to this content
186cdf0e10cSrcweir         bindTo( _rURL );
187cdf0e10cSrcweir 
188cdf0e10cSrcweir         // did we survive this?
189cdf0e10cSrcweir         if ( isInvalid() || !isBound() )
190cdf0e10cSrcweir             return sal_False;
191cdf0e10cSrcweir 
192cdf0e10cSrcweir         DBG_ASSERT( m_pContent, "SmartContent::implIs: inconsistence!" );
193cdf0e10cSrcweir             // if, after an bindTo, we don't have a content, then we should be INVALID, or at least
194cdf0e10cSrcweir             // NOT_BOUND (the latter happens, for example, if somebody tries to ask for an empty URL)
195cdf0e10cSrcweir 
196cdf0e10cSrcweir         sal_Bool bIs = sal_False;
197cdf0e10cSrcweir         try
198cdf0e10cSrcweir         {
199cdf0e10cSrcweir             if ( Folder == _eType )
200cdf0e10cSrcweir                 bIs = m_pContent->isFolder();
201cdf0e10cSrcweir             else
202cdf0e10cSrcweir                 bIs = m_pContent->isDocument();
203cdf0e10cSrcweir 
204*07a3d7f1SPedro Giffuni             // from here on, we definitely know that the content is valid
205cdf0e10cSrcweir             m_eState = VALID;
206cdf0e10cSrcweir         }
207cdf0e10cSrcweir         catch( Exception& )
208cdf0e10cSrcweir         {
209*07a3d7f1SPedro Giffuni             // now we're definitely invalid
210cdf0e10cSrcweir             m_eState = INVALID;
211cdf0e10cSrcweir         }
212cdf0e10cSrcweir         return bIs;
213cdf0e10cSrcweir     }
214cdf0e10cSrcweir 
215cdf0e10cSrcweir     //--------------------------------------------------------------------
getTitle(::rtl::OUString & _rTitle)216cdf0e10cSrcweir     void SmartContent::getTitle( ::rtl::OUString& /* [out] */ _rTitle )
217cdf0e10cSrcweir     {
218cdf0e10cSrcweir         if ( !isBound() || isInvalid() )
219cdf0e10cSrcweir             return;
220cdf0e10cSrcweir 
221cdf0e10cSrcweir         try
222cdf0e10cSrcweir         {
223cdf0e10cSrcweir             ::rtl::OUString sTitle;
224cdf0e10cSrcweir             m_pContent->getPropertyValue( ::rtl::OUString::createFromAscii( "Title" ) ) >>= sTitle;
225cdf0e10cSrcweir             _rTitle =  sTitle;
226cdf0e10cSrcweir 
227*07a3d7f1SPedro Giffuni             // from here on, we definitely know that the content is valid
228cdf0e10cSrcweir             m_eState = VALID;
229cdf0e10cSrcweir         }
230cdf0e10cSrcweir         catch( ::com::sun::star::uno::Exception& )
231cdf0e10cSrcweir         {
232*07a3d7f1SPedro Giffuni             // now we're definitely invalid
233cdf0e10cSrcweir             m_eState = INVALID;
234cdf0e10cSrcweir         }
235cdf0e10cSrcweir     }
236cdf0e10cSrcweir 
237cdf0e10cSrcweir     //--------------------------------------------------------------------
hasParentFolder()238cdf0e10cSrcweir     sal_Bool SmartContent::hasParentFolder( )
239cdf0e10cSrcweir     {
240cdf0e10cSrcweir         if ( !isBound() || isInvalid() )
241cdf0e10cSrcweir             return sal_False;
242cdf0e10cSrcweir 
243cdf0e10cSrcweir         sal_Bool bRet = sal_False;
244cdf0e10cSrcweir         try
245cdf0e10cSrcweir         {
246cdf0e10cSrcweir             Reference< XChild > xChild( m_pContent->get(), UNO_QUERY );
247cdf0e10cSrcweir             if ( xChild.is() )
248cdf0e10cSrcweir             {
249cdf0e10cSrcweir                 Reference< XContent > xParent( xChild->getParent(), UNO_QUERY );
250cdf0e10cSrcweir                 if ( xParent.is() )
251cdf0e10cSrcweir                 {
252cdf0e10cSrcweir                     String aParentURL = String( xParent->getIdentifier()->getContentIdentifier() );
253cdf0e10cSrcweir                     bRet = ( aParentURL.Len() > 0 && aParentURL != (String)(m_pContent->getURL()) );
254cdf0e10cSrcweir 
255*07a3d7f1SPedro Giffuni                     // now we're definitely valid
256cdf0e10cSrcweir                     m_eState = VALID;
257cdf0e10cSrcweir                 }
258cdf0e10cSrcweir             }
259cdf0e10cSrcweir         }
260cdf0e10cSrcweir         catch( const Exception& )
261cdf0e10cSrcweir         {
262*07a3d7f1SPedro Giffuni             // now we're definitely invalid
263cdf0e10cSrcweir             m_eState = INVALID;
264cdf0e10cSrcweir         }
265cdf0e10cSrcweir         return bRet;
266cdf0e10cSrcweir     }
267cdf0e10cSrcweir 
268cdf0e10cSrcweir     //--------------------------------------------------------------------
canCreateFolder()269cdf0e10cSrcweir     sal_Bool SmartContent::canCreateFolder( )
270cdf0e10cSrcweir     {
271cdf0e10cSrcweir         if ( !isBound() || isInvalid() )
272cdf0e10cSrcweir             return sal_False;
273cdf0e10cSrcweir 
274cdf0e10cSrcweir         sal_Bool bRet = sal_False;
275cdf0e10cSrcweir         try
276cdf0e10cSrcweir         {
277cdf0e10cSrcweir             Sequence< ContentInfo > aInfo = m_pContent->queryCreatableContentsInfo();
278cdf0e10cSrcweir             const ContentInfo* pInfo = aInfo.getConstArray();
279cdf0e10cSrcweir             sal_Int32 nCount = aInfo.getLength();
280cdf0e10cSrcweir             for ( sal_Int32 i = 0; i < nCount; ++i, ++pInfo )
281cdf0e10cSrcweir             {
282cdf0e10cSrcweir                 // Simply look for the first KIND_FOLDER...
283cdf0e10cSrcweir                 if ( pInfo->Attributes & ContentInfoAttribute::KIND_FOLDER )
284cdf0e10cSrcweir                 {
285cdf0e10cSrcweir                     bRet = sal_True;
286cdf0e10cSrcweir                     break;
287cdf0e10cSrcweir                 }
288cdf0e10cSrcweir             }
289cdf0e10cSrcweir 
290*07a3d7f1SPedro Giffuni             // now we're definitely valid
291cdf0e10cSrcweir             m_eState = VALID;
292cdf0e10cSrcweir         }
293cdf0e10cSrcweir         catch( Exception& )
294cdf0e10cSrcweir         {
295*07a3d7f1SPedro Giffuni             // now we're definitely invalid
296cdf0e10cSrcweir             m_eState = INVALID;
297cdf0e10cSrcweir         }
298cdf0e10cSrcweir         return bRet;
299cdf0e10cSrcweir     }
300cdf0e10cSrcweir 
301cdf0e10cSrcweir //........................................................................
302cdf0e10cSrcweir } // namespace svt
303cdf0e10cSrcweir //........................................................................
304cdf0e10cSrcweir 
305