1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef __FRAMEWORK_PATTERN_FRAME_HXX_
25 #define __FRAMEWORK_PATTERN_FRAME_HXX_
26 
27 //_______________________________________________
28 // own includes
29 
30 #include <general.h>
31 
32 //_______________________________________________
33 // interface includes
34 #include <com/sun/star/frame/XFrame.hpp>
35 #include <com/sun/star/frame/XController.hpp>
36 #include <com/sun/star/frame/XModel.hpp>
37 #include <com/sun/star/lang/XComponent.hpp>
38 #include <com/sun/star/lang/DisposedException.hpp>
39 #include <com/sun/star/util/XCloseable.hpp>
40 
41 //_______________________________________________
42 // other includes
43 
44 //_______________________________________________
45 // namespaces
46 
47 #ifndef css
48 namespace css = ::com::sun::star;
49 #endif
50 
51 namespace framework{
52     namespace pattern{
53         namespace frame{
54 
55 //_______________________________________________
56 // definitions
57 
58 //-----------------------------------------------
extractFrameModel(const css::uno::Reference<css::frame::XFrame> & xFrame)59 inline css::uno::Reference< css::frame::XModel > extractFrameModel(const css::uno::Reference< css::frame::XFrame >& xFrame)
60 {
61     css::uno::Reference< css::frame::XModel >      xModel;
62     css::uno::Reference< css::frame::XController > xController;
63     if (xFrame.is())
64         xController = xFrame->getController();
65     if (xController.is())
66         xModel = xController->getModel();
67     return xModel;
68 }
69 
70 //-----------------------------------------------
71 /** @short  close (or dispose) the given resource.
72 
73     @descr  It try to close the given resource first.
74             Delegating of the ownership can be influenced from
75             outside. If closing isnt possible (because the
76             needed interface isnt available) dispose() is tried instead.
77             Al possible exception are handled inside.
78             So the user of this method has to look for the return value only.
79 
80     @attention  The given resource will not be cleared.
81                 But later using of it can produce an exception!
82 
83     @param  xResource
84             the object, which should be closed here.
85 
86     @param  bDelegateOwnerShip
87             used at the XCloseable->close() method to define
88             the right owner in case closing failed.
89 
90     @return [bool]
91             sal_True if closing failed.
92  */
closeIt(const css::uno::Reference<css::uno::XInterface> & xResource,sal_Bool bDelegateOwnerShip)93 inline sal_Bool closeIt(const css::uno::Reference< css::uno::XInterface >& xResource         ,
94                        sal_Bool                                     bDelegateOwnerShip)
95 {
96     css::uno::Reference< css::util::XCloseable > xClose  (xResource, css::uno::UNO_QUERY);
97     css::uno::Reference< css::lang::XComponent > xDispose(xResource, css::uno::UNO_QUERY);
98 
99     try
100     {
101         if (xClose.is())
102             xClose->close(bDelegateOwnerShip);
103         else
104         if (xDispose.is())
105             xDispose->dispose();
106         else
107             return sal_False;
108     }
109     catch(const css::util::CloseVetoException&)
110         { return sal_False; }
111     catch(const css::lang::DisposedException&)
112         {} // disposed is closed is ...
113     catch(const css::uno::RuntimeException&)
114         { throw; } // shouldnt be suppressed!
115     catch(const css::uno::Exception&)
116         { return sal_False;  } // ??? We defined to return a boolen value instead of throwing exceptions ...
117                                // (OK: RuntimeExceptions shouldnt be catched inside the core ..)
118 
119     return sal_True;
120 }
121 
122         } // namespace frame
123     } // namespace pattern
124 } // namespace framework
125 
126 #endif // __FRAMEWORK_PATTERN_FRAME_HXX_
127