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>
27 MyListener::MyListener(const css::uno::Reference
< css::lang::XMultiServiceFactory
>& xSMGR
)
32 MyListener::~MyListener()
36 css::uno::Any SAL_CALL
MyListener::execute(const css::uno::Sequence
< css::beans::NamedValue
>& lArguments
)
37 throw (css::lang::IllegalArgumentException
,
39 css::uno::RuntimeException
)
41 css::uno::Sequence
< css::beans::NamedValue
> lEnv
;
44 sal_Int32 c
= lArguments
.getLength();
45 const css::beans::NamedValue
* p
= lArguments
.getConstArray();
48 if ( p
[i
].Name
== "Environment" )
55 css::uno::Reference
< css::frame::XModel
> xModel
;
58 p
= lEnv
.getConstArray();
61 if ( p
[i
].Name
== "Model" )
63 p
[i
].Value
>>= xModel
;
66 if ( p
[i
].Name
== "Frame" )
68 css::uno::Reference
< css::frame::XController
> xController
;
69 css::uno::Reference
< css::frame::XFrame
> xFrame
;
70 p
[i
].Value
>>= xFrame
;
72 xController
= xFrame
->getController();
74 xModel
= xController
->getModel();
80 return css::uno::Any();
82 css::uno::Reference
< css::lang::XServiceInfo
> xInfo(xModel
, css::uno::UNO_QUERY
);
83 sal_Bool bCalc
= xInfo
->supportsService("com.sun.star.sheet.SpreadsheetDocument");
84 sal_Bool bWriter
= ( xInfo
->supportsService("com.sun.star.text.TextDocument") &&
85 !xInfo
->supportsService("com.sun.star.text.WebDocument") &&
86 !xInfo
->supportsService("com.sun.star.text.GlobalDocument") );
88 // We are interested only in Writer and Calc. However, here we are
89 // notified of all newly opened Documents...
90 if (!bCalc
&& !bWriter
)
91 return css::uno::Any();
95 pListener
= (void*)(new CalcListener(m_xSMGR
));
98 pListener
= (void*)(new WriterListener(m_xSMGR
));
100 css::uno::Reference
< css::document::XEventListener
> xDocListener (static_cast< css::document::XEventListener
* >(pListener
), css::uno::UNO_QUERY
);
101 css::uno::Reference
< css::document::XEventBroadcaster
> xDocBroadcaster (xModel
, css::uno::UNO_QUERY
);
103 xDocBroadcaster
->addEventListener(xDocListener
);
105 return css::uno::Any();
109 ::rtl::OUString SAL_CALL
MyListener::getImplementationName()
110 throw (css::uno::RuntimeException
)
112 return ::rtl::OUString( MYLISTENER_IMPLEMENTATIONNAME
);
116 css::uno::Sequence
< ::rtl::OUString
> SAL_CALL
MyListener::getSupportedServiceNames()
117 throw (css::uno::RuntimeException
)
119 css::uno::Sequence
< ::rtl::OUString
> lNames(1);
120 lNames
[0] = ::rtl::OUString( MYLISTENER_SERVICENAME
);
125 sal_Bool SAL_CALL
MyListener::supportsService(const ::rtl::OUString
& sServiceName
)
126 throw (css::uno::RuntimeException
)
128 return sServiceName
== MYLISTENER_SERVICENAME
||
129 sServiceName
== "com.sun.star.task.Job";
133 css::uno::Reference
< css::uno::XInterface
> MyListener::st_createInstance(const css::uno::Reference
< css::lang::XMultiServiceFactory
>& xSMGR
)
135 MyListener
* pListener
= new MyListener(xSMGR
);
136 css::uno::Reference
< css::uno::XInterface
> xListener(static_cast< css::task::XJob
* >(pListener
), css::uno::UNO_QUERY
);
140 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */