Database: sqlite default to appdata kworship.db
[kworship.git] / unipresent / openoffice.org / UpOoBridge.cpp
blob13f156a486631ac937598c8d46af355dbbcc7a82
1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
5 * KWorship is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * KWorship is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with KWorship. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 /**
21 * @file UpOoBridge.cpp
22 * @brief Bridge to OpenOffice.org.
23 * @author James Hogan <james@albanarts.com>
26 #include "UpOoBridge.h"
27 #include "compiler.h"
29 #include <KStandardDirs>
30 #include <KLocale>
31 #include <KDebug>
33 #include <QByteArray>
35 #include <cppuhelper/bootstrap.hxx>
36 #include <osl/file.hxx>
37 #include <osl/process.h>
38 #include <com/sun/star/beans/XPropertySet.hpp>
39 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
40 #include <com/sun/star/frame/XComponentLoader.hpp>
41 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
42 #include <com/sun/star/registry/XSimpleRegistry.hpp>
44 using namespace com::sun::star::beans;
45 using namespace com::sun::star::bridge;
46 using namespace com::sun::star::frame;
47 using namespace com::sun::star::lang;
48 using namespace com::sun::star::registry;
49 using namespace com::sun::star::uno;
50 using namespace rtl;
53 * Constructors + destructor
56 /// Primary constructor.
57 UpOoBridge::UpOoBridge()
58 : m_valid(false)
60 QByteArray connectionBaCore = "socket,host=localhost,port=2083;urp;StarOffice.ServiceManager";
61 QByteArray connectionBa = "uno:" + connectionBaCore;
62 #define TYPESDB "unipresent/openoffice.org/types.rdb"
63 #define BACKEND_NAME i18n("OpenOffice.org backend:")
64 QString rdbFileBa = KStandardDirs::locate("data", TYPESDB);
65 if (rdbFileBa.isNull())
67 kdError() << BACKEND_NAME << i18n("couldn't find data %1").arg(TYPESDB);
68 return;
71 OUString connectionString(OUString::createFromAscii(connectionBa));
72 OUString rdbFile(OUString::createFromAscii(rdbFileBa.toAscii()));
73 bool tryAgain = false;
76 // Connect
77 Reference<XSimpleRegistry> registry = ::cppu::createSimpleRegistry();
78 registry->open(rdbFile, sal_True, sal_False);
79 Reference<XComponentContext> componentContext = ::cppu::bootstrap_InitialComponentContext(registry);
80 Reference<XMultiComponentFactory> componentFactory = componentContext->getServiceManager();
81 Reference<XUnoUrlResolver> resolver(componentFactory->createInstanceWithContext(
82 OUString::createFromAscii("com.sun.star.bridge.UnoUrlResolver"),
83 componentContext), UNO_QUERY);
84 if (!resolver.is())
86 kdError() << BACKEND_NAME << i18n("could not create instance of XUnoUrlResolver");
87 return;
89 try
91 m_serviceManager = resolver->resolve(connectionString);
92 tryAgain = false;
94 catch (Exception& exception)
96 // If we've already tried it again, just give up.
97 if (unlikely(tryAgain))
99 // not valid
100 OUString message = exception.Message;
101 kdDebug() << BACKEND_NAME << QString::fromUtf16((const sal_Unicode*)message, message.getLength()) << i18n("(start OpenOffice.org with %1)").arg(QString()+"soffice \"-accept=" + connectionBaCore + "\"");
102 return;
104 else
106 // Start openoffice
107 tryAgain = true;
108 continue;
111 } while (tryAgain);
113 m_valid = true;
116 /// Destructor.
117 UpOoBridge::~UpOoBridge()
122 * Main interface
125 /// Find whether the bridge is valid.
126 bool UpOoBridge::isValid() const
128 return m_valid;
131 /// Get the service manager.
132 XInterface* UpOoBridge::serviceManager()
134 return m_serviceManager.get();