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