1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
)
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
;
40 sal_Int32 c
= lArguments
.getLength();
41 const css::beans::NamedValue
* p
= lArguments
.getConstArray();
44 if ( p
[i
].Name
== "Environment" )
51 css::uno::Reference
< css::frame::XModel
> xModel
;
54 p
= lEnv
.getConstArray();
57 if ( p
[i
].Name
== "Model" )
59 p
[i
].Value
>>= xModel
;
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
;
68 xController
= xFrame
->getController();
70 xModel
= xController
->getModel();
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();
91 pListener
= (void*)(new CalcListener(m_xSMGR
));
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
);
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
);
128 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */