1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef __FRAMEWORK_PATTERN_FRAME_HXX_
29 #define __FRAMEWORK_PATTERN_FRAME_HXX_
30 
31 //_______________________________________________
32 // own includes
33 
34 #include <general.h>
35 
36 //_______________________________________________
37 // interface includes
38 #include <com/sun/star/frame/XFrame.hpp>
39 #include <com/sun/star/frame/XController.hpp>
40 #include <com/sun/star/frame/XModel.hpp>
41 #include <com/sun/star/lang/XComponent.hpp>
42 #include <com/sun/star/lang/DisposedException.hpp>
43 #include <com/sun/star/util/XCloseable.hpp>
44 
45 //_______________________________________________
46 // other includes
47 
48 //_______________________________________________
49 // namespaces
50 
51 #ifndef css
52 namespace css = ::com::sun::star;
53 #endif
54 
55 namespace framework{
56     namespace pattern{
57         namespace frame{
58 
59 //_______________________________________________
60 // definitions
61 
62 //-----------------------------------------------
63 inline css::uno::Reference< css::frame::XModel > extractFrameModel(const css::uno::Reference< css::frame::XFrame >& xFrame)
64 {
65     css::uno::Reference< css::frame::XModel >      xModel;
66     css::uno::Reference< css::frame::XController > xController;
67     if (xFrame.is())
68         xController = xFrame->getController();
69     if (xController.is())
70         xModel = xController->getModel();
71     return xModel;
72 }
73 
74 //-----------------------------------------------
75 /** @short  close (or dispose) the given resource.
76 
77     @descr  It try to close the given resource first.
78             Delegating of the ownership can be influenced from
79             outside. If closing isnt possible (because the
80             needed interface isnt available) dispose() is tried instead.
81             Al possible exception are handled inside.
82             So the user of this method has to look for the return value only.
83 
84     @attention  The given resource will not be cleared.
85                 But later using of it can produce an exception!
86 
87     @param  xResource
88             the object, which should be closed here.
89 
90     @param  bDelegateOwnerShip
91             used at the XCloseable->close() method to define
92             the right owner in case closing failed.
93 
94     @return [bool]
95             sal_True if closing failed.
96  */
97 inline sal_Bool closeIt(const css::uno::Reference< css::uno::XInterface >& xResource         ,
98                        sal_Bool                                     bDelegateOwnerShip)
99 {
100     css::uno::Reference< css::util::XCloseable > xClose  (xResource, css::uno::UNO_QUERY);
101     css::uno::Reference< css::lang::XComponent > xDispose(xResource, css::uno::UNO_QUERY);
102 
103     try
104     {
105         if (xClose.is())
106             xClose->close(bDelegateOwnerShip);
107         else
108         if (xDispose.is())
109             xDispose->dispose();
110         else
111             return sal_False;
112     }
113     catch(const css::util::CloseVetoException&)
114         { return sal_False; }
115     catch(const css::lang::DisposedException&)
116         {} // disposed is closed is ...
117     catch(const css::uno::RuntimeException&)
118         { throw; } // shouldnt be suppressed!
119     catch(const css::uno::Exception&)
120         { return sal_False;  } // ??? We defined to return a boolen value instead of throwing exceptions ...
121                                // (OK: RuntimeExceptions shouldnt be catched inside the core ..)
122 
123     return sal_True;
124 }
125 
126         } // namespace frame
127     } // namespace pattern
128 } // namespace framework
129 
130 #endif // __FRAMEWORK_PATTERN_FRAME_HXX_
131