Merge remote-tracking branch 'origin/api_changes'
[profile/ivi/qtbase.git] / src / dbus / qdbus_symbols.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtDBus module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtCore/qglobal.h>
43 #ifndef QT_BOOTSTRAPPED
44 #include <QtCore/qlibrary.h>
45 #endif
46 #include <QtCore/qmutex.h>
47 #include <private/qmutexpool_p.h>
48
49 #ifndef QT_NO_DBUS
50
51 QT_BEGIN_NAMESPACE
52
53 void (*qdbus_resolve_me(const char *name))();
54
55 #if !defined QT_LINKED_LIBDBUS
56
57 #ifndef QT_BOOTSTRAPPED
58 static QLibrary *qdbus_libdbus = 0;
59
60 void qdbus_unloadLibDBus()
61 {
62     delete qdbus_libdbus;
63     qdbus_libdbus = 0;
64 }
65 #endif
66
67 bool qdbus_loadLibDBus()
68 {
69 #ifndef QT_BOOTSTRAPPED
70 #ifdef QT_BUILD_INTERNAL
71     // this is to simulate a library load failure for our autotest suite.
72     if (!qgetenv("QT_SIMULATE_DBUS_LIBFAIL").isEmpty())
73         return false;
74 #endif
75
76     static volatile bool triedToLoadLibrary = false;
77 #ifndef QT_NO_THREAD
78     QMutexLocker locker(QMutexPool::globalInstanceGet((void *)&qdbus_resolve_me));
79 #endif
80     QLibrary *&lib = qdbus_libdbus;
81     if (triedToLoadLibrary)
82         return lib && lib->isLoaded();
83
84     lib = new QLibrary;
85     triedToLoadLibrary = true;
86
87     static int majorversions[] = { 3, 2, -1 };
88     lib->unload();
89     lib->setFileName(QLatin1String("dbus-1"));
90     for (uint i = 0; i < sizeof(majorversions) / sizeof(majorversions[0]); ++i) {
91         lib->setFileNameAndVersion(lib->fileName(), majorversions[i]);
92         if (lib->load() && lib->resolve("dbus_connection_open_private"))
93             return true;
94
95         lib->unload();
96     }
97
98     delete lib;
99     lib = 0;
100     return false;
101 #else
102     return true;
103 #endif
104 }
105
106 #ifndef QT_BOOTSTRAPPED
107 void (*qdbus_resolve_conditionally(const char *name))()
108 {
109     if (qdbus_loadLibDBus())
110         return qdbus_libdbus->resolve(name);
111     return 0;
112 }
113 #endif
114
115 void (*qdbus_resolve_me(const char *name))()
116 {
117 #ifndef QT_BOOTSTRAPPED
118     if (!qdbus_loadLibDBus())
119         qFatal("Cannot find libdbus-1 in your system to resolve symbol '%s'.", name);
120
121     QFunctionPointer ptr = qdbus_libdbus->resolve(name);
122     if (!ptr)
123         qFatal("Cannot resolve '%s' in your libdbus-1.", name);
124
125     return ptr;
126 #else
127     Q_UNUSED(name);
128     return 0;
129 #endif
130 }
131
132 #ifndef QT_BOOTSTRAPPED
133 Q_DESTRUCTOR_FUNCTION(qdbus_unloadLibDBus)
134 #endif
135
136 #endif // QT_LINKED_LIBDBUS
137
138 QT_END_NAMESPACE
139
140 #endif // QT_NO_DBUS