1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #include <stdio.h>
24 
25 #include <com/sun/star/lang/IllegalArgumentException.hpp>
26 
27 #include "testlistener.hxx"
28 
29 #define U2S(s) OUStringToOString(s, RTL_TEXTENCODING_UTF8).getStr()
30 
31 
32 using ::com::sun::star::lang::XMultiServiceFactory;
33 using ::com::sun::star::lang::IllegalArgumentException;
34 
35 
36 namespace DOM { namespace events
37 {
38 
_getInstance(const Reference<XMultiServiceFactory> & rSMgr)39     Reference< XInterface > CTestListener::_getInstance(const Reference< XMultiServiceFactory >& rSMgr)
40     {
41         // XXX
42         // return static_cast< XXPathAPI* >(new CTestListener());
43         return Reference< XInterface >(static_cast<XEventListener*>(new CTestListener(rSMgr)));
44     }
45 
46     const char* CTestListener::aImplementationName = "com.sun.star.comp.xml.dom.events.TestListener";
47     const char* CTestListener::aSupportedServiceNames[] = {
48         "com.sun.star.comp.xml.dom.events.TestListener",
49         NULL
50     };
51 
_getImplementationName()52     OUString CTestListener::_getImplementationName()
53     {
54 	    return OUString::createFromAscii(aImplementationName);
55     }
_getSupportedServiceNames()56     Sequence<OUString> CTestListener::_getSupportedServiceNames()
57     {
58 	    Sequence<OUString> aSequence;
59 	    for (int i=0; aSupportedServiceNames[i]!=NULL; i++) {
60 		    aSequence.realloc(i+1);
61 		    aSequence[i]=(OUString::createFromAscii(aSupportedServiceNames[i]));
62 	    }
63 	    return aSequence;
64     }
65 
getSupportedServiceNames()66     Sequence< OUString > SAL_CALL CTestListener::getSupportedServiceNames()
67         throw (RuntimeException)
68     {
69         return CTestListener::_getSupportedServiceNames();
70     }
71 
getImplementationName()72     OUString SAL_CALL CTestListener::getImplementationName()
73         throw (RuntimeException)
74     {
75         return CTestListener::_getImplementationName();
76     }
77 
supportsService(const OUString & aServiceName)78     sal_Bool SAL_CALL CTestListener::supportsService(const OUString& aServiceName)
79         throw (RuntimeException)
80     {
81         Sequence< OUString > supported = CTestListener::_getSupportedServiceNames();
82         for (sal_Int32 i=0; i<supported.getLength(); i++)
83         {
84             if (supported[i] == aServiceName) return sal_True;
85         }
86         return sal_False;
87     }
88 
89     // --- XInitialize
90 
initialize(const Sequence<Any> & args)91     void SAL_CALL CTestListener::initialize(const Sequence< Any >& args) throw(RuntimeException)
92     {
93         if (args.getLength() < 3) throw IllegalArgumentException(
94             OUString::createFromAscii("Wrong number of arguments"), Reference< XInterface >(), 0);
95 
96         Reference <XEventTarget> aTarget;
97         if(! (args[0] >>= aTarget)) throw IllegalArgumentException(
98                 OUString::createFromAscii("Illegal argument 1"), Reference< XInterface >(), 1);
99 
100         OUString aType;
101         if (! (args[1] >>= aType))
102             throw IllegalArgumentException(OUString::createFromAscii("Illegal argument 2"), Reference< XInterface >(), 2);
103 
104         sal_Bool bCapture = sal_False;
105         if(! (args[2]  >>=  bCapture)) throw IllegalArgumentException(
106             OUString::createFromAscii("Illegal argument 3"), Reference< XInterface >(), 3);
107 
108         if(! (args[3] >>= m_name)) m_name = OUString::createFromAscii("<unnamed listener>");
109 
110         m_target = aTarget;
111         m_type = aType;
112         m_capture = bCapture;
113 
114         m_target->addEventListener(m_type, Reference< XEventListener >(this), m_capture);
115 
116 
117     }
118 
~CTestListener()119     CTestListener::~CTestListener()
120     {
121         fprintf(stderr, "CTestListener::~CTestListener()\n");
122         if( m_target.is())
123             m_target->removeEventListener(m_type, Reference< XEventListener >(this), m_capture);
124     }
125 
126     // --- XEventListener
127 
handleEvent(const Reference<XEvent> & evt)128     void SAL_CALL CTestListener::handleEvent(const Reference< XEvent >& evt) throw (RuntimeException)
129     {
130         FILE* f = fopen("C:\\listener.out", "a");
131         fprintf(f, "CTestListener::handleEvent in %s\n", U2S(m_name));
132         fprintf(f, "    type: %s\n\n", OUStringToOString(evt->getType(), RTL_TEXTENCODING_ASCII_US).getStr());
133         fclose(f);
134 
135     }
136 
137 }}
138