xref: /aoo42x/main/pyuno/demo/hello_world_comp.py (revision bd8ef897)
1*bd8ef897SAndrew Rist# *************************************************************
2*bd8ef897SAndrew Rist#
3*bd8ef897SAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
4*bd8ef897SAndrew Rist#  or more contributor license agreements.  See the NOTICE file
5*bd8ef897SAndrew Rist#  distributed with this work for additional information
6*bd8ef897SAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
7*bd8ef897SAndrew Rist#  to you under the Apache License, Version 2.0 (the
8*bd8ef897SAndrew Rist#  "License"); you may not use this file except in compliance
9*bd8ef897SAndrew Rist#  with the License.  You may obtain a copy of the License at
10*bd8ef897SAndrew Rist#
11*bd8ef897SAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
12*bd8ef897SAndrew Rist#
13*bd8ef897SAndrew Rist#  Unless required by applicable law or agreed to in writing,
14*bd8ef897SAndrew Rist#  software distributed under the License is distributed on an
15*bd8ef897SAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*bd8ef897SAndrew Rist#  KIND, either express or implied.  See the License for the
17*bd8ef897SAndrew Rist#  specific language governing permissions and limitations
18*bd8ef897SAndrew Rist#  under the License.
19*bd8ef897SAndrew Rist#
20*bd8ef897SAndrew Rist# *************************************************************
21*bd8ef897SAndrew Rist
22cdf0e10cSrcweirimport uno
23cdf0e10cSrcweirimport unohelper
24cdf0e10cSrcweir
25cdf0e10cSrcweirfrom com.sun.star.task import XJobExecutor
26cdf0e10cSrcweir
27cdf0e10cSrcweir# implement a UNO component by deriving from the standard unohelper.Base class
28cdf0e10cSrcweir# and from the interface(s) you want to implement.
29cdf0e10cSrcweirclass HelloWorldJob( unohelper.Base, XJobExecutor ):
30cdf0e10cSrcweir    def __init__( self, ctx ):
31cdf0e10cSrcweir        # store the component context for later use
32cdf0e10cSrcweir        self.ctx = ctx
33cdf0e10cSrcweir
34cdf0e10cSrcweir    def trigger( self, args ):
35cdf0e10cSrcweir        # note: args[0] == "HelloWorld", see below config settings
36cdf0e10cSrcweir
37cdf0e10cSrcweir        # retrieve the desktop object
38cdf0e10cSrcweir        desktop = self.ctx.ServiceManager.createInstanceWithContext(
39cdf0e10cSrcweir            "com.sun.star.frame.Desktop", self.ctx )
40cdf0e10cSrcweir
41cdf0e10cSrcweir        # get current document model
42cdf0e10cSrcweir        model = desktop.getCurrentComponent()
43cdf0e10cSrcweir
44cdf0e10cSrcweir	# access the document's text property
45cdf0e10cSrcweir	text = model.Text
46cdf0e10cSrcweir
47cdf0e10cSrcweir	# create a cursor
48cdf0e10cSrcweir	cursor = text.createTextCursor()
49cdf0e10cSrcweir
50cdf0e10cSrcweir	# insert the text into the document
51cdf0e10cSrcweir	text.insertString( cursor, "Hello World", 0 )
52cdf0e10cSrcweir
53cdf0e10cSrcweir# pythonloader looks for a static g_ImplementationHelper variable
54cdf0e10cSrcweirg_ImplementationHelper = unohelper.ImplementationHelper()
55cdf0e10cSrcweir
56cdf0e10cSrcweir#
57cdf0e10cSrcweirg_ImplementationHelper.addImplementation( \
58cdf0e10cSrcweir	HelloWorldJob,                        # UNO object class
59cdf0e10cSrcweir	"org.openoffice.comp.pyuno.demo.HelloWorld", # implemenation name
60cdf0e10cSrcweir	("com.sun.star.task.Job",),)          # list of implemented services
61cdf0e10cSrcweir	                                      # (the only service)
62