cid#1640468 Dereference after null check
[LibreOffice.git] / odk / examples / cpp / complextoolbarcontrols / MyListener.cxx
blobc49083a97b5eb88f6ddbf258c43428c6f0dfb1cc
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)
37 css::uno::Sequence< css::beans::NamedValue > lEnv;
39 sal_Int32 i = 0;
40 sal_Int32 c = lArguments.getLength();
41 const css::beans::NamedValue* p = lArguments.getConstArray();
42 for (i=0; i<c; ++i)
44 if ( p[i].Name == "Environment" )
46 p[i].Value >>= lEnv;
47 break;
51 css::uno::Reference< css::frame::XModel > xModel;
53 c = lEnv.getLength();
54 p = lEnv.getConstArray();
55 for (i=0; i<c; ++i)
57 if ( p[i].Name == "Model" )
59 p[i].Value >>= xModel;
60 break;
62 if ( p[i].Name == "Frame" )
64 css::uno::Reference< css::frame::XController > xController;
65 css::uno::Reference< css::frame::XFrame > xFrame;
66 p[i].Value >>= xFrame;
67 if (xFrame.is())
68 xController = xFrame->getController();
69 if (xController.is())
70 xModel = xController->getModel();
71 break;
75 if (!xModel.is())
76 return css::uno::Any();
78 css::uno::Reference< css::lang::XServiceInfo > xInfo(xModel, css::uno::UNO_QUERY);
79 sal_Bool bCalc = xInfo->supportsService("com.sun.star.sheet.SpreadsheetDocument");
80 sal_Bool bWriter = ( xInfo->supportsService("com.sun.star.text.TextDocument") &&
81 !xInfo->supportsService("com.sun.star.text.WebDocument") &&
82 !xInfo->supportsService("com.sun.star.text.GlobalDocument") );
84 // We are interested only in Writer and Calc. However, here we are
85 // notified of all newly opened Documents...
86 if (!bCalc && !bWriter)
87 return css::uno::Any();
89 void* pListener = 0;
90 if (bCalc)
91 pListener = (void*)(new CalcListener(m_xSMGR));
92 else
93 if (bWriter)
94 pListener = (void*)(new WriterListener(m_xSMGR));
96 css::uno::Reference< css::document::XEventListener > xDocListener (static_cast< css::document::XEventListener* >(pListener), css::uno::UNO_QUERY);
97 css::uno::Reference< css::document::XEventBroadcaster > xDocBroadcaster (xModel , css::uno::UNO_QUERY);
99 xDocBroadcaster->addEventListener(xDocListener);
101 return css::uno::Any();
104 ::rtl::OUString SAL_CALL MyListener::getImplementationName()
106 return ::rtl::OUString( MYLISTENER_IMPLEMENTATIONNAME );
109 css::uno::Sequence< ::rtl::OUString > SAL_CALL MyListener::getSupportedServiceNames()
111 css::uno::Sequence< ::rtl::OUString > lNames(1);
112 lNames[0] = ::rtl::OUString( MYLISTENER_SERVICENAME );
113 return lNames;
116 sal_Bool SAL_CALL MyListener::supportsService(const ::rtl::OUString& sServiceName)
118 return cppu::supportsService(this, sServiceName);
121 css::uno::Reference< css::uno::XInterface > MyListener::st_createInstance(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR)
123 MyListener* pListener = new MyListener(xSMGR);
124 css::uno::Reference< css::uno::XInterface > xListener(static_cast< css::task::XJob* >(pListener), css::uno::UNO_QUERY);
125 return xListener;
128 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */