build fix
[LibreOffice.git] / connectivity / source / drivers / kab / KDEInit.cxx
blob5317c6241d4b5047e71d54c48574b5828861e7c0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include "KDEInit.h"
21 #include <osl/diagnose.h>
22 #include <osl/process.h>
24 #include <shell/kde_headers.h>
26 namespace connectivity
28 namespace kab
31 // = KDEInit
33 class KDEInit
35 private:
36 /// KDE application if we own it
37 static KApplication* s_pKApplication;
38 static bool s_bDidInsertCatalogue;
40 public:
41 static void Init();
42 static void Shutdown();
46 KApplication* KDEInit::s_pKApplication = nullptr;
47 bool KDEInit::s_bDidInsertCatalogue = false;
50 void KDEInit::Init()
52 // TODO: All this is not thread-safe
54 // we create a KDE application only if it is not already done
55 if (KApplication::kApplication() == nullptr)
57 OSL_ENSURE(s_pKApplication == nullptr, "KDEInit::Init: inconsistency in the application pointers!");
59 char *kabargs[1] = {const_cast<char*>("libkab1")};
60 KCmdLineArgs::init(1, kabargs, "KAddressBook", *kabargs, "Address Book driver", KAB_DRIVER_VERSION);
62 s_pKApplication = new KApplication(false, false);
65 /* FIXME-BCP47: what slumbering dogs may we wake up here? */
66 // set language
67 rtl_Locale *pProcessLocale;
68 osl_getProcessLocale(&pProcessLocale);
69 // sal_Unicode and QChar are (currently) both 16 bits characters
70 QString aLanguage(
71 reinterpret_cast<QChar *>(pProcessLocale->Language->buffer),
72 (int) pProcessLocale->Language->length);
73 KGlobal::locale()->setLanguage(aLanguage);
75 // load KDE address book's localized messages
76 KGlobal::locale()->insertCatalogue("kaddressbook");
77 s_bDidInsertCatalogue = true;
81 void KDEInit::Shutdown()
83 if ( s_bDidInsertCatalogue )
84 // this guard is necessary, since KDE 3.3 seems to crash if we remove a catalogue
85 // which we did not previously insert
86 KGlobal::locale()->removeCatalogue("kaddressbook");
88 if ( s_pKApplication != nullptr )
90 delete s_pKApplication;
91 s_pKApplication = nullptr;
98 namespace
100 double normalizeVersion( unsigned int major, unsigned int minor )
102 return major + 1.0 * minor / 1000;
107 extern "C" SAL_DLLPUBLIC_EXPORT void SAL_CALL initKApplication()
109 ::connectivity::kab::KDEInit::Init();
113 extern "C" SAL_DLLPUBLIC_EXPORT void SAL_CALL shutdownKApplication()
115 ::connectivity::kab::KDEInit::Shutdown();
118 /** checks whether the KDE version on the system we're running at is supported
119 by the driver
121 Has to be called before any other code from this library, in particular,
122 it has to be called before initKApplication()
124 If this function returns <code>0</code>, then no other code from this library
125 has to be called, else the results are unpredictable.
127 @return
128 <ul><li><code>0</code> if the KDE version is supportednon</li>
129 <li>a negative value if the version is too old</li>
130 <li>a positive value if the version is too new to know whether it works with this driver</li>
131 </ul>
133 #i60062# / 2006-01-06 / frank.schoenheit@sun.com
135 extern "C" SAL_DLLPUBLIC_EXPORT int SAL_CALL matchKDEVersion()
137 double nMinVersion = normalizeVersion( MIN_KDE_VERSION_MAJOR, MIN_KDE_VERSION_MINOR );
138 double nCurVersion = normalizeVersion( ::KDE::versionMajor(), ::KDE::versionMinor() );
139 double nMaxVersion = normalizeVersion( MAX_KDE_VERSION_MAJOR, MAX_KDE_VERSION_MINOR );
141 if ( nCurVersion < nMinVersion )
142 return -1;
143 if ( nCurVersion > nMaxVersion )
144 return 1;
146 return 0;
149 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */