xref: /trunk/main/odk/examples/DevelopersGuide/ProfUNO/CppBinding/office_connect.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *************************************************************************/
34 
35 #include <stdio.h>
36 
37 #include <sal/main.h>
38 
39 #include <cppuhelper/bootstrap.hxx>
40 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
41 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
43 
44 using namespace com::sun::star::uno;
45 using namespace com::sun::star::lang;
46 using namespace com::sun::star::bridge;
47 using namespace rtl;
48 using namespace cppu;
49 
50 SAL_IMPLEMENT_MAIN()
51 {
52     // create the initial component context
53     Reference< XComponentContext > rComponentContext =
54         defaultBootstrap_InitialComponentContext();
55 
56     // retrieve the servicemanager from the context
57     Reference< XMultiComponentFactory > rServiceManager =
58         rComponentContext->getServiceManager();
59 
60     // instantiate a sample service with the servicemanager.
61     Reference< XInterface > rInstance =
62         rServiceManager->createInstanceWithContext(
63             OUString::createFromAscii("com.sun.star.bridge.UnoUrlResolver" ),
64             rComponentContext );
65 
66     // Query for the XUnoUrlResolver interface
67     Reference< XUnoUrlResolver > rResolver( rInstance, UNO_QUERY );
68 
69     if( ! rResolver.is() )
70     {
71         printf( "Error: Couldn't instantiate com.sun.star.bridge.UnoUrlResolver service\n" );
72         return 1;
73     }
74     try
75     {
76         // resolve the uno-url
77         rInstance = rResolver->resolve( OUString::createFromAscii(
78             "uno:socket,host=localhost,port=2083;urp;StarOffice.ServiceManager" ) );
79 
80         if( ! rInstance.is() )
81         {
82             printf( "StarOffice.ServiceManager is not exported from remote counterpart\n" );
83             return 1;
84         }
85 
86         // query for the simpler XMultiServiceFactory interface, sufficient for scripting
87         Reference< XMultiServiceFactory > rOfficeServiceManager (rInstance, UNO_QUERY);
88 
89         if( ! rInstance.is() )
90         {
91             printf( "XMultiServiceFactory interface is not exported for StarOffice.ServiceManager\n" );
92             return 1;
93         }
94 
95         printf( "Connected sucessfully to the office\n" );
96     }
97     catch( Exception &e )
98     {
99         OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
100         printf( "Error: %s\n", o.pData->buffer );
101         return 1;
102     }
103     return 0;
104 }
105