summaryrefslogtreecommitdiff
path: root/src/utils/iconprovider.cpp
blob: 9830fa293ab47a3cb5ec85ad0df02a36d11ce187 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (C) 2008  Lukas Lalinsky
// Copyright (C) 2008  Nokia Corporation and/or its subsidiary(-ies).
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#include <cstdlib>
#include <QDebug>
#include <QIcon>
#include <QDir>
#include <QVariant>
#include <QLibrary>
#include <QApplication>
#include "iconprovider.h"

using namespace std;

#ifdef Q_WS_X11
extern "C" {
    struct GConfClient;
    struct GError;
    typedef void (*Ptr_g_type_init)();
    typedef GConfClient* (*Ptr_gconf_client_get_default)();
    typedef char* (*Ptr_gconf_client_get_string)(GConfClient*, const char*, GError **);
    typedef void (*Ptr_g_object_unref)(void *);
    typedef void (*Ptr_g_error_free)(GError *);
    typedef void (*Ptr_g_free)(void*);
}

static Ptr_g_type_init p_g_type_init = 0;
static Ptr_gconf_client_get_default p_gconf_client_get_default = 0;
static Ptr_gconf_client_get_string p_gconf_client_get_string = 0;
static Ptr_g_object_unref p_g_object_unref = 0;
static Ptr_g_error_free p_g_error_free = 0;
static Ptr_g_free p_g_free = 0;
#endif

void IconProvider::gnomeLookupIconTheme()
{
#ifdef Q_WS_X11
    if (themeName.isEmpty()) {
        //resolve glib and gconf functions
        p_g_type_init =              (Ptr_g_type_init)QLibrary::resolve(QLatin1String("gobject-2.0"), 0, "g_type_init");
        p_gconf_client_get_default = (Ptr_gconf_client_get_default)QLibrary::resolve(QLatin1String("gconf-2"), 4, "gconf_client_get_default");
        p_gconf_client_get_string =  (Ptr_gconf_client_get_string)QLibrary::resolve(QLatin1String("gconf-2"), 4, "gconf_client_get_string");
        p_g_object_unref =           (Ptr_g_object_unref)QLibrary::resolve(QLatin1String("gobject-2.0"), 0, "g_object_unref");
        p_g_error_free =             (Ptr_g_error_free)QLibrary::resolve(QLatin1String("glib-2.0"), 0, "g_error_free");
        p_g_free =                   (Ptr_g_free)QLibrary::resolve(QLatin1String("glib-2.0"), 0, "g_free");

        if (p_g_type_init &&
             p_gconf_client_get_default &&
             p_gconf_client_get_string &&
             p_g_object_unref &&
             p_g_error_free &&
             p_g_free) {

            p_g_type_init();
            GConfClient* client = p_gconf_client_get_default();
            GError *err = 0;
            char *str = p_gconf_client_get_string(client, "/desktop/gnome/interface/icon_theme", &err);
            if (!err) {
                themeName = QString::fromUtf8(str);
                p_g_free(str);
            }
            p_g_object_unref(client);
            if (err)
                p_g_error_free (err);
        }
        if (themeName.isEmpty())
            themeName = QLatin1String("gnome");
    }
#endif
}

IconProvider::IconProvider()
{
#ifdef Q_WS_X11
	if (getenv("GNOME_DESKTOP_SESSION_ID")) {
		gnomeLookupIconTheme();
	}
	else if (getenv("KDE_FULL_SESSION")) {
		// FIXME
	}
	if (!themeName.isEmpty()) {
		iconDirs << QDir::homePath() + "/.icons/";
		iconDirs << "/usr/share/icons/";
	}
#endif
}

QIcon
IconProvider::findIcon(int size, const QString &name)
{
#ifdef Q_WS_X11
	IconProvider *iconProvider = instance();
	QString sizeString = QString("/%1x%2/").arg(size).arg(size);
	foreach(QString path, iconProvider->iconDirs) {
		QString fullPath = path + iconProvider->themeName + sizeString + "actions/" + name;
		if (QFile::exists(fullPath)) {
			return QIcon(fullPath);
		}
	}
#endif
	return QIcon(":/icons" + sizeString + name);
}