xref: /AOO41X/main/connectivity/source/drivers/kab/KDEInit.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
30*cdf0e10cSrcweir #include "KDEInit.h"
31*cdf0e10cSrcweir #include <osl/diagnose.h>
32*cdf0e10cSrcweir #include <osl/process.h>
33*cdf0e10cSrcweir #include <shell/kde_headers.h>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir namespace connectivity
36*cdf0e10cSrcweir {
37*cdf0e10cSrcweir     namespace kab
38*cdf0e10cSrcweir     {
39*cdf0e10cSrcweir         // ===============================================================
40*cdf0e10cSrcweir         // = KDEInit
41*cdf0e10cSrcweir         // ===============================================================
42*cdf0e10cSrcweir         class KDEInit
43*cdf0e10cSrcweir         {
44*cdf0e10cSrcweir         private:
45*cdf0e10cSrcweir             /// KDE application if we own it
46*cdf0e10cSrcweir             static KApplication*    s_pKApplication;
47*cdf0e10cSrcweir             static bool             s_bDidInsertCatalogue;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir         public:
50*cdf0e10cSrcweir             static void Init();
51*cdf0e10cSrcweir             static void Shutdown();
52*cdf0e10cSrcweir         };
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir         // ---------------------------------------------------------------
55*cdf0e10cSrcweir         KApplication* KDEInit::s_pKApplication = NULL;
56*cdf0e10cSrcweir         bool          KDEInit::s_bDidInsertCatalogue = false;
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir         // ---------------------------------------------------------------
59*cdf0e10cSrcweir         void KDEInit::Init()
60*cdf0e10cSrcweir         {
61*cdf0e10cSrcweir             // TODO: All this is not thread-safe
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir             // we create a KDE application only if it is not already done
64*cdf0e10cSrcweir             if (KApplication::kApplication() == NULL)
65*cdf0e10cSrcweir             {
66*cdf0e10cSrcweir                 OSL_ENSURE(s_pKApplication == NULL, "KDEInit::Init: inconsistency in the application pointers!");
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir                 char *kabargs[1] = {(char*)"libkab1"};
69*cdf0e10cSrcweir                 KCmdLineArgs::init(1, kabargs, "KAddressBook", *kabargs, "Address Book driver", KAB_DRIVER_VERSION);
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir                 s_pKApplication = new KApplication(false, false);
72*cdf0e10cSrcweir             }
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir             // set language
75*cdf0e10cSrcweir             rtl_Locale *pProcessLocale;
76*cdf0e10cSrcweir             osl_getProcessLocale(&pProcessLocale);
77*cdf0e10cSrcweir             // sal_Unicode and QChar are (currently) both 16 bits characters
78*cdf0e10cSrcweir             QString aLanguage(
79*cdf0e10cSrcweir                 (const QChar *) pProcessLocale->Language->buffer,
80*cdf0e10cSrcweir                 (int) pProcessLocale->Language->length);
81*cdf0e10cSrcweir             KGlobal::locale()->setLanguage(aLanguage);
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir             // load KDE address book's localized messages
84*cdf0e10cSrcweir             KGlobal::locale()->insertCatalogue("kaddressbook");
85*cdf0e10cSrcweir             s_bDidInsertCatalogue = true;
86*cdf0e10cSrcweir         }
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir         // ---------------------------------------------------------------
89*cdf0e10cSrcweir         void KDEInit::Shutdown()
90*cdf0e10cSrcweir         {
91*cdf0e10cSrcweir             if ( s_bDidInsertCatalogue )
92*cdf0e10cSrcweir                 // this guard is necessary, since KDE 3.3 seems to crash if we remove a catalogue
93*cdf0e10cSrcweir                 // which we did not previously insert
94*cdf0e10cSrcweir                 KGlobal::locale()->removeCatalogue("kaddressbook");
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir             if ( s_pKApplication != NULL )
97*cdf0e10cSrcweir             {
98*cdf0e10cSrcweir                 delete s_pKApplication;
99*cdf0e10cSrcweir                 s_pKApplication = NULL;
100*cdf0e10cSrcweir             }
101*cdf0e10cSrcweir         }
102*cdf0e10cSrcweir     }
103*cdf0e10cSrcweir }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir // =======================================================================
106*cdf0e10cSrcweir namespace
107*cdf0e10cSrcweir {
108*cdf0e10cSrcweir     double normalizeVersion( unsigned int major, unsigned int minor )
109*cdf0e10cSrcweir     {
110*cdf0e10cSrcweir         return major + 1.0 * minor / 1000;
111*cdf0e10cSrcweir     }
112*cdf0e10cSrcweir }
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir // -----------------------------------------------------------------------
115*cdf0e10cSrcweir extern "C" SAL_DLLPUBLIC_EXPORT void SAL_CALL initKApplication()
116*cdf0e10cSrcweir {
117*cdf0e10cSrcweir     ::connectivity::kab::KDEInit::Init();
118*cdf0e10cSrcweir }
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir // -----------------------------------------------------------------------
121*cdf0e10cSrcweir extern "C" SAL_DLLPUBLIC_EXPORT void SAL_CALL shutdownKApplication()
122*cdf0e10cSrcweir {
123*cdf0e10cSrcweir     ::connectivity::kab::KDEInit::Shutdown();
124*cdf0e10cSrcweir }
125*cdf0e10cSrcweir // -----------------------------------------------------------------------
126*cdf0e10cSrcweir /** checks whether the KDE version on the system we're running at is supported
127*cdf0e10cSrcweir     by the driver
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir     Has to be called before any other code from this library, in particular,
130*cdf0e10cSrcweir     it has to be called before initKApplication()
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir     If this function returns <code>0</code>, then no other code from this library
133*cdf0e10cSrcweir     has to be called, else the results are unpredictable.
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir     @return
136*cdf0e10cSrcweir         <ul><li><code>0</code> if the KDE version is supportednon</li>
137*cdf0e10cSrcweir             <li>a negative value if the version is too old</li>
138*cdf0e10cSrcweir             <li>a positive value if the version is too new to know whether it works with this driver</li>
139*cdf0e10cSrcweir         </ul>
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir     #i60062# / 2006-01-06 / frank.schoenheit@sun.com
142*cdf0e10cSrcweir */
143*cdf0e10cSrcweir extern "C" SAL_DLLPUBLIC_EXPORT int SAL_CALL matchKDEVersion()
144*cdf0e10cSrcweir {
145*cdf0e10cSrcweir     double nMinVersion = normalizeVersion( MIN_KDE_VERSION_MAJOR, MIN_KDE_VERSION_MINOR );
146*cdf0e10cSrcweir     double nCurVersion = normalizeVersion( ::KDE::versionMajor(), ::KDE::versionMinor() );
147*cdf0e10cSrcweir     double nMaxVersion = normalizeVersion( MAX_KDE_VERSION_MAJOR, MAX_KDE_VERSION_MINOR );
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir     if ( nCurVersion < nMinVersion )
150*cdf0e10cSrcweir         return -1;
151*cdf0e10cSrcweir     if ( nCurVersion > nMaxVersion )
152*cdf0e10cSrcweir         return 1;
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir     return 0;
155*cdf0e10cSrcweir }
156