1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_connectivity.hxx"
26 #include "KDEInit.h"
27 #include <osl/diagnose.h>
28 #include <osl/process.h>
29 #include <shell/kde_headers.h>
30 
31 namespace connectivity
32 {
33     namespace kab
34     {
35         // ===============================================================
36         // = KDEInit
37         // ===============================================================
38         class KDEInit
39         {
40         private:
41             /// KDE application if we own it
42             static KApplication*    s_pKApplication;
43             static bool             s_bDidInsertCatalogue;
44 
45         public:
46             static void Init();
47             static void Shutdown();
48         };
49 
50         // ---------------------------------------------------------------
51         KApplication* KDEInit::s_pKApplication = NULL;
52         bool          KDEInit::s_bDidInsertCatalogue = false;
53 
54         // ---------------------------------------------------------------
Init()55         void KDEInit::Init()
56         {
57             // TODO: All this is not thread-safe
58 
59             // we create a KDE application only if it is not already done
60             if (KApplication::kApplication() == NULL)
61             {
62                 OSL_ENSURE(s_pKApplication == NULL, "KDEInit::Init: inconsistency in the application pointers!");
63 
64                 char *kabargs[1] = {(char*)"libkab1"};
65                 KCmdLineArgs::init(1, kabargs, "KAddressBook", *kabargs, "Address Book driver", KAB_DRIVER_VERSION);
66 
67                 s_pKApplication = new KApplication(false, false);
68             }
69 
70             // set language
71             rtl_Locale *pProcessLocale;
72             osl_getProcessLocale(&pProcessLocale);
73             // sal_Unicode and QChar are (currently) both 16 bits characters
74             QString aLanguage(
75                 (const QChar *) pProcessLocale->Language->buffer,
76                 (int) pProcessLocale->Language->length);
77             KGlobal::locale()->setLanguage(aLanguage);
78 
79             // load KDE address book's localized messages
80             KGlobal::locale()->insertCatalogue("kaddressbook");
81             s_bDidInsertCatalogue = true;
82         }
83 
84         // ---------------------------------------------------------------
Shutdown()85         void KDEInit::Shutdown()
86         {
87             if ( s_bDidInsertCatalogue )
88                 // this guard is necessary, since KDE 3.3 seems to crash if we remove a catalogue
89                 // which we did not previously insert
90                 KGlobal::locale()->removeCatalogue("kaddressbook");
91 
92             if ( s_pKApplication != NULL )
93             {
94                 delete s_pKApplication;
95                 s_pKApplication = NULL;
96             }
97         }
98     }
99 }
100 
101 // =======================================================================
102 namespace
103 {
normalizeVersion(unsigned int major,unsigned int minor)104     double normalizeVersion( unsigned int major, unsigned int minor )
105     {
106         return major + 1.0 * minor / 1000;
107     }
108 }
109 
110 // -----------------------------------------------------------------------
initKApplication()111 extern "C" SAL_DLLPUBLIC_EXPORT void SAL_CALL initKApplication()
112 {
113     ::connectivity::kab::KDEInit::Init();
114 }
115 
116 // -----------------------------------------------------------------------
shutdownKApplication()117 extern "C" SAL_DLLPUBLIC_EXPORT void SAL_CALL shutdownKApplication()
118 {
119     ::connectivity::kab::KDEInit::Shutdown();
120 }
121 // -----------------------------------------------------------------------
122 /** checks whether the KDE version on the system we're running at is supported
123     by the driver
124 
125     Has to be called before any other code from this library, in particular,
126     it has to be called before initKApplication()
127 
128     If this function returns <code>0</code>, then no other code from this library
129     has to be called, else the results are unpredictable.
130 
131     @return
132         <ul><li><code>0</code> if the KDE version is supportednon</li>
133             <li>a negative value if the version is too old</li>
134             <li>a positive value if the version is too new to know whether it works with this driver</li>
135         </ul>
136 
137     #i60062# / 2006-01-06 / frank.schoenheit@sun.com
138 */
matchKDEVersion()139 extern "C" SAL_DLLPUBLIC_EXPORT int SAL_CALL matchKDEVersion()
140 {
141     double nMinVersion = normalizeVersion( MIN_KDE_VERSION_MAJOR, MIN_KDE_VERSION_MINOR );
142     double nCurVersion = normalizeVersion( ::KDE::versionMajor(), ::KDE::versionMinor() );
143     double nMaxVersion = normalizeVersion( MAX_KDE_VERSION_MAJOR, MAX_KDE_VERSION_MINOR );
144 
145     if ( nCurVersion < nMinVersion )
146         return -1;
147     if ( nCurVersion > nMaxVersion )
148         return 1;
149 
150     return 0;
151 }
152