Update ooo320-m1
[ooovba.git] / odk / examples / cpp / complextoolbarcontrols / MyListener.cxx
blob1c17a0da8e11b3e50dda616461b6686b87ae4119
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: MyListener.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 #include "MyListener.h"
33 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 #include <com/sun/star/frame/XModel.hpp>
35 #include <com/sun/star/lang/XServiceInfo.hpp>
36 #include <com/sun/star/document/XEventBroadcaster.hpp>
38 namespace css = ::com::sun::star;
40 /*-----------------------------------------------------
41 20.11.2003 11:31
42 -----------------------------------------------------*/
43 MyListener::MyListener(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR)
44 : m_xSMGR(xSMGR)
47 /*-----------------------------------------------------
48 20.11.2003 11:32
49 -----------------------------------------------------*/
50 MyListener::~MyListener()
53 /*-----------------------------------------------------
54 20.11.2003 12:04
55 -----------------------------------------------------*/
56 css::uno::Any SAL_CALL MyListener::execute(const css::uno::Sequence< css::beans::NamedValue >& lArguments)
57 throw (css::lang::IllegalArgumentException,
58 css::uno::Exception,
59 css::uno::RuntimeException)
61 css::uno::Sequence< css::beans::NamedValue > lEnv;
63 sal_Int32 i = 0;
64 sal_Int32 c = lArguments.getLength();
65 const css::beans::NamedValue* p = lArguments.getConstArray();
66 for (i=0; i<c; ++i)
68 if (p[i].Name.equalsAscii("Environment"))
70 p[i].Value >>= lEnv;
71 break;
75 css::uno::Reference< css::frame::XModel > xModel;
77 c = lEnv.getLength();
78 p = lEnv.getConstArray();
79 for (i=0; i<c; ++i)
81 if (p[i].Name.equalsAscii("Model"))
83 p[i].Value >>= xModel;
84 break;
86 if (p[i].Name.equalsAscii("Frame"))
88 css::uno::Reference< css::frame::XController > xController;
89 css::uno::Reference< css::frame::XFrame > xFrame;
90 p[i].Value >>= xFrame;
91 if (xFrame.is())
92 xController = xFrame->getController();
93 if (xController.is())
94 xModel = xController->getModel();
95 break;
99 if (!xModel.is())
100 return css::uno::Any();
102 css::uno::Reference< css::lang::XServiceInfo > xInfo(xModel, css::uno::UNO_QUERY);
103 sal_Bool bCalc = xInfo->supportsService(::rtl::OUString::createFromAscii("com.sun.star.sheet.SpreadsheetDocument"));
104 sal_Bool bWriter = (
105 xInfo->supportsService(::rtl::OUString::createFromAscii("com.sun.star.text.TextDocument" )) &&
106 !xInfo->supportsService(::rtl::OUString::createFromAscii("com.sun.star.text.WebDocument" )) &&
107 !xInfo->supportsService(::rtl::OUString::createFromAscii("com.sun.star.text.GlobalDocument"))
110 // Wir interessieren uns nur für Writer und Calc. Werden hier aber für
111 // alle neu geöffneten Dokumente benachrichtigt ...
112 if (!bCalc && !bWriter)
113 return css::uno::Any();
115 void* pListener = 0;
116 if (bCalc)
117 pListener = (void*)(new CalcListener(m_xSMGR));
118 else
119 if (bWriter)
120 pListener = (void*)(new WriterListener(m_xSMGR));
122 css::uno::Reference< css::document::XEventListener > xDocListener (static_cast< css::document::XEventListener* >(pListener), css::uno::UNO_QUERY);
123 css::uno::Reference< css::document::XEventBroadcaster > xDocBroadcaster (xModel , css::uno::UNO_QUERY);
125 xDocBroadcaster->addEventListener(xDocListener);
127 return css::uno::Any();
130 /*-----------------------------------------------------
131 20.11.2003 12:13
132 -----------------------------------------------------*/
133 ::rtl::OUString SAL_CALL MyListener::getImplementationName()
134 throw (css::uno::RuntimeException)
136 return ::rtl::OUString::createFromAscii(MYLISTENER_IMPLEMENTATIONNAME);
139 /*-----------------------------------------------------
140 20.11.2003 12:13
141 -----------------------------------------------------*/
142 css::uno::Sequence< ::rtl::OUString > SAL_CALL MyListener::getSupportedServiceNames()
143 throw (css::uno::RuntimeException)
145 css::uno::Sequence< ::rtl::OUString > lNames(1);
146 lNames[0] = ::rtl::OUString::createFromAscii(MYLISTENER_SERVICENAME);
147 return lNames;
150 /*-----------------------------------------------------
151 20.11.2003 12:14
152 -----------------------------------------------------*/
153 sal_Bool SAL_CALL MyListener::supportsService(const ::rtl::OUString& sServiceName)
154 throw (css::uno::RuntimeException)
156 return (
157 sServiceName.equalsAscii(MYLISTENER_SERVICENAME) ||
158 sServiceName.equalsAscii("com.sun.star.task.Job" )
162 /*-----------------------------------------------------
163 20.11.2003 11:31
164 -----------------------------------------------------*/
165 css::uno::Reference< css::uno::XInterface > MyListener::st_createInstance(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR)
167 MyListener* pListener = new MyListener(xSMGR);
168 css::uno::Reference< css::uno::XInterface > xListener(static_cast< css::task::XJob* >(pListener), css::uno::UNO_QUERY);
169 return xListener;