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 #include "precompiled_sd.hxx"
25 
26 #include "ToolPanelModule.hxx"
27 #include "ReadOnlyModeObserver.hxx"
28 #include "framework/FrameworkHelper.hxx"
29 
30 #include <com/sun/star/lang/XInitialization.hpp>
31 #include <com/sun/star/drawing/framework/XControllerManager.hpp>
32 
33 #include <comphelper/processfactory.hxx>
34 #include <cppuhelper/compbase1.hxx>
35 #include <boost/enable_shared_from_this.hpp>
36 
37 using namespace ::com::sun::star;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::drawing::framework;
40 using ::rtl::OUString;
41 using ::sd::framework::FrameworkHelper;
42 
43 
44 namespace sd { namespace framework {
45 
46 namespace {
47 
48 typedef ::cppu::WeakComponentImplHelper1 <
49       ::com::sun::star::frame::XStatusListener
50     > LocalReadOnlyModeObserverInterfaceBase;
51 
52 /** This local class enables or disables the ResourceManager of a
53     ToolPanelModule.  It connects to a ReadOnlyModeObserver and is called
54     when the state of the .uno:EditDoc command changes.  When either the
55     ResourceManager or the ReadOnlyModeObserver are disposed then the
56     LocalReadOnlyModeObserver disposes itself.  The link
57     between the ResourceManager and the ReadOnlyModeObserver is removed and
58     the ReadOnlyModeObserver typically looses its last reference and is
59     destroyed.
60 */
61 class LocalReadOnlyModeObserver
62     : private MutexOwner,
63       public LocalReadOnlyModeObserverInterfaceBase
64 {
65 public:
LocalReadOnlyModeObserver(const Reference<frame::XController> & rxController,const::rtl::Reference<ResourceManager> & rpResourceManager)66     LocalReadOnlyModeObserver (
67         const Reference<frame::XController>& rxController,
68         const ::rtl::Reference<ResourceManager>& rpResourceManager)
69         : MutexOwner(),
70           LocalReadOnlyModeObserverInterfaceBase(maMutex),
71           mpResourceManager(rpResourceManager),
72           mpObserver(new ReadOnlyModeObserver(rxController))
73     {
74         mpObserver->AddStatusListener(this);
75 
76         Reference<lang::XComponent> xComponent (
77             static_cast<XWeak*>(mpResourceManager.get()), UNO_QUERY);
78         if (xComponent.is())
79             xComponent->addEventListener(this);
80     }
81 
~LocalReadOnlyModeObserver(void)82     ~LocalReadOnlyModeObserver (void)
83     {
84     }
85 
disposing(void)86     virtual void SAL_CALL disposing (void)
87     {
88         Reference<lang::XComponent> xComponent (static_cast<XWeak*>(mpObserver.get()), UNO_QUERY);
89         if (xComponent.is())
90             xComponent->dispose();
91 
92         xComponent = Reference<lang::XComponent>(
93             static_cast<XWeak*>(mpResourceManager.get()), UNO_QUERY);
94         if (xComponent.is())
95             xComponent->removeEventListener(this);
96 
97     }
98 
disposing(const com::sun::star::lang::EventObject & rEvent)99     virtual void SAL_CALL disposing (const com::sun::star::lang::EventObject& rEvent)
100         throw(RuntimeException)
101     {
102         if (rEvent.Source == Reference<XInterface>(static_cast<XWeak*>(mpObserver.get())))
103         {
104             mpObserver = NULL;
105         }
106         else if (rEvent.Source == Reference<XInterface>(
107             static_cast<XWeak*>(mpResourceManager.get())))
108         {
109             mpResourceManager = NULL;
110         }
111         dispose();
112     }
113 
statusChanged(const com::sun::star::frame::FeatureStateEvent & rEvent)114     virtual void SAL_CALL statusChanged (const com::sun::star::frame::FeatureStateEvent& rEvent)
115         throw(RuntimeException)
116     {
117         bool bReadWrite (true);
118         if (rEvent.IsEnabled)
119             rEvent.State >>= bReadWrite;
120 
121         if (bReadWrite)
122             mpResourceManager->Enable();
123         else
124             mpResourceManager->Disable();
125     }
126 
127 private:
128     ::rtl::Reference<ResourceManager> mpResourceManager;
129     ::rtl::Reference<ReadOnlyModeObserver> mpObserver;
130 
131 };
132 }
133 
134 
135 
136 
137 //===== ToolPanelModule ====================================================
138 
Initialize(const Reference<frame::XController> & rxController)139 void ToolPanelModule::Initialize (const Reference<frame::XController>& rxController)
140 {
141     ::rtl::Reference<ResourceManager> pResourceManager (
142         new ResourceManager(
143             rxController,
144             FrameworkHelper::CreateResourceId(
145                 FrameworkHelper::msSidebarViewURL,
146                 FrameworkHelper::msSidebarPaneURL)));
147     pResourceManager->AddActiveMainView(FrameworkHelper::msImpressViewURL);
148     pResourceManager->AddActiveMainView(FrameworkHelper::msNotesViewURL);
149     pResourceManager->AddActiveMainView(FrameworkHelper::msHandoutViewURL);
150     pResourceManager->AddActiveMainView(FrameworkHelper::msSlideSorterURL);
151 
152     new LocalReadOnlyModeObserver(rxController, pResourceManager);
153 }
154 
155 
156 
157 
158 } } // end of namespace sd::framework
159