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