Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / odk / examples / cpp / complextoolbarcontrols / MyListener.cxx
blob82f8d01a303edad601afd670c30a08c979e32a4a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "MyListener.h"
22 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 #include <com/sun/star/frame/XModel.hpp>
24 #include <com/sun/star/lang/XServiceInfo.hpp>
25 #include <com/sun/star/document/XEventBroadcaster.hpp>
26 #include <cppuhelper/supportsservice.hxx>
28 MyListener::MyListener(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR)
29 : m_xSMGR(xSMGR)
32 MyListener::~MyListener()
35 css::uno::Any SAL_CALL MyListener::execute(const css::uno::Sequence< css::beans::NamedValue >& lArguments)
36 throw (css::lang::IllegalArgumentException,
37 css::uno::Exception,
38 css::uno::RuntimeException)
40 css::uno::Sequence< css::beans::NamedValue > lEnv;
42 sal_Int32 i = 0;
43 sal_Int32 c = lArguments.getLength();
44 const css::beans::NamedValue* p = lArguments.getConstArray();
45 for (i=0; i<c; ++i)
47 if ( p[i].Name == "Environment" )
49 p[i].Value >>= lEnv;
50 break;
54 css::uno::Reference< css::frame::XModel > xModel;
56 c = lEnv.getLength();
57 p = lEnv.getConstArray();
58 for (i=0; i<c; ++i)
60 if ( p[i].Name == "Model" )
62 p[i].Value >>= xModel;
63 break;
65 if ( p[i].Name == "Frame" )
67 css::uno::Reference< css::frame::XController > xController;
68 css::uno::Reference< css::frame::XFrame > xFrame;
69 p[i].Value >>= xFrame;
70 if (xFrame.is())
71 xController = xFrame->getController();
72 if (xController.is())
73 xModel = xController->getModel();
74 break;
78 if (!xModel.is())
79 return css::uno::Any();
81 css::uno::Reference< css::lang::XServiceInfo > xInfo(xModel, css::uno::UNO_QUERY);
82 sal_Bool bCalc = xInfo->supportsService("com.sun.star.sheet.SpreadsheetDocument");
83 sal_Bool bWriter = ( xInfo->supportsService("com.sun.star.text.TextDocument") &&
84 !xInfo->supportsService("com.sun.star.text.WebDocument") &&
85 !xInfo->supportsService("com.sun.star.text.GlobalDocument") );
87 // We are interested only in Writer and Calc. However, here we are
88 // notified of all newly opened Documents...
89 if (!bCalc && !bWriter)
90 return css::uno::Any();
92 void* pListener = 0;
93 if (bCalc)
94 pListener = (void*)(new CalcListener(m_xSMGR));
95 else
96 if (bWriter)
97 pListener = (void*)(new WriterListener(m_xSMGR));
99 css::uno::Reference< css::document::XEventListener > xDocListener (static_cast< css::document::XEventListener* >(pListener), css::uno::UNO_QUERY);
100 css::uno::Reference< css::document::XEventBroadcaster > xDocBroadcaster (xModel , css::uno::UNO_QUERY);
102 xDocBroadcaster->addEventListener(xDocListener);
104 return css::uno::Any();
107 ::rtl::OUString SAL_CALL MyListener::getImplementationName()
108 throw (css::uno::RuntimeException)
110 return ::rtl::OUString( MYLISTENER_IMPLEMENTATIONNAME );
113 css::uno::Sequence< ::rtl::OUString > SAL_CALL MyListener::getSupportedServiceNames()
114 throw (css::uno::RuntimeException)
116 css::uno::Sequence< ::rtl::OUString > lNames(1);
117 lNames[0] = ::rtl::OUString( MYLISTENER_SERVICENAME );
118 return lNames;
121 sal_Bool SAL_CALL MyListener::supportsService(const ::rtl::OUString& sServiceName)
122 throw (css::uno::RuntimeException)
124 return cppu::supportsService(this, sServiceName);
127 css::uno::Reference< css::uno::XInterface > MyListener::st_createInstance(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR)
129 MyListener* pListener = new MyListener(xSMGR);
130 css::uno::Reference< css::uno::XInterface > xListener(static_cast< css::task::XJob* >(pListener), css::uno::UNO_QUERY);
131 return xListener;
134 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */