File: appearancegtk2.cpp

package info (click to toggle)
kde-gtk-config 3%3A2.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,408 kB
  • ctags: 230
  • sloc: cpp: 1,429; ansic: 210; makefile: 14; sh: 2
file content (160 lines) | stat: -rw-r--r-- 5,851 bytes parent folder | download | duplicates (2)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* KDE GTK Configuration Module
 * 
 * Copyright 2011 José Antonio Sanchez Reynaga <joanzare@gmail.com>
 * Copyright 2011 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) version 3, or any
 * later version accepted by the membership of KDE e.V. (or its
 * successor approved by the membership of KDE e.V.), which shall
 * act as a proxy defined in Section 6 of version 3 of the license.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "appearancegtk2.h"
#include <QFile>
#include <KDebug>
#include <QStringList>
#include <QDir>
#include <qdiriterator.h>
#include <KProcess>
#include <KStandardDirs>

bool AppearanceGTK2::loadSettings(const QString& path)
{
    QFile configFile(path);
    
    bool canRead = configFile.open(QIODevice::ReadOnly | QIODevice::Text);
    
    if(canRead) {
        kDebug() << "The gtk2 config file exists...";
        QMap<QString, QString> foundSettings = readSettingsTuples(&configFile);

        m_settings["theme"] = foundSettings["gtk-theme-name"];
        m_settings["icon"] = foundSettings["gtk-icon-theme-name"];
        m_settings["icon_fallback"] = foundSettings["gtk-fallback-icon-theme"];
        m_settings["font"] = foundSettings["gtk-font-name"];
        m_settings["toolbar_style"] = foundSettings.value("gtk-toolbar-style", "GTK_TOOLBAR_ICONS");
        m_settings["show_icons_buttons"] = foundSettings.value("gtk-button-images", "0");
        m_settings["show_icons_menus"] = foundSettings.value("gtk-menu-images", "0");
    }
    
    return canRead;
}

QString AppearanceGTK2::themesGtkrcFile(const QString& themeName) const
{
    QStringList themes=installedThemes();
    themes=themes.filter(QRegExp("/"+themeName+"/?$"));
    if(themes.size()==1) {
        QDirIterator it(themes.first(), QDirIterator::Subdirectories);
        while(it.hasNext()) {
            it.next();
            if(it.fileName()=="gtkrc") {
                kDebug() << "\tgtkrc file found at : " << it.filePath();
                return it.filePath();
            }
        }
    }
    
    return QString();
}

bool AppearanceGTK2::saveSettings(const QString& gtkrcFile)
{
    QFile gtkrc(gtkrcFile);
    gtkrc.remove();

    if(!gtkrc.open(QIODevice::WriteOnly | QIODevice::Text)) {
        kDebug() << "There was unable to write the file .gtkrc-2.0";
        return false;
    }

    QTextStream flow(&gtkrc);

    flow << "# File created by KDE Gtk Config" << "\n"
         << "# Configs for GTK2 programs \n\n";

    QString themeGtkrcFile=themesGtkrcFile(getTheme());
    
    //TODO: is this really needed?
    if(!themeGtkrcFile.isEmpty())
        flow << "include \"" << themeGtkrcFile << "\"\n"; //We include the theme's gtkrc file
    
    if(QFile::exists("/etc/gtk-2.0/gtkrc"))
        flow  << "include \"/etc/gtk-2.0/gtkrc\"\n"; //We include the /etc's config file

    int nameEnd = m_settings["font"].lastIndexOf(QRegExp(" ([0-9]+|bold|italic)"));
    QString fontFamily = m_settings["font"].left(nameEnd);

    //TODO: is this really needed?
    flow << "style \"user-font\" \n"
            << "{\n"
            << "\tfont_name=\""<< fontFamily << "\"\n"
            << "}\n";
    
    flow << "widget_class \"*\" style \"user-font\"\n";
    flow << "gtk-font-name=\"" << m_settings["font"] << "\"\n";
    flow << "gtk-theme-name=\"" << m_settings["theme"] << "\"\n";
    flow << "gtk-icon-theme-name=\""<< m_settings["icon"] << "\"\n";
    flow << "gtk-fallback-icon-theme=\"" << m_settings["icon_fallback"] << "\"\n";
    flow << "gtk-toolbar-style=" << m_settings["toolbar_style"] << "\n";
    flow << "gtk-menu-images=" << m_settings["show_icons_menus"] << "\n";
    flow << "gtk-button-images=" << m_settings["show_icons_buttons"] << "\n";
    
    //we're done with the  ~/.gtk-2.0 file
    gtkrc.close();
    
    //TODO: do we really need the linked file?
    if(QFile::remove(gtkrcFile+"-kde4"))
        kDebug() << "ready to create the symbolic link";
    
    if( !QFile::link(gtkrcFile, gtkrcFile+"-kde4") )
        kDebug() << "Couldn't create the symboling link to .gtkrc-2.0-kde4 :(";
    else
        kDebug() << "Symbolic link created for .gtkrc-2.0-kde4 :D";
    
    if(gtkrcFile==defaultConfigFile())
        KProcess::startDetached(KStandardDirs::findExe("reload_gtk_apps"));
    
    return true;
}

QString AppearanceGTK2::defaultConfigFile() const
{
    return QDir::homePath()+"/.gtkrc-2.0";
}

QStringList AppearanceGTK2::installedThemes() const
{
    QFileInfoList availableThemes;
    foreach(const QString& themesDir, KGlobal::dirs()->findDirs("xdgdata-apps", "../themes")) {
        QDir root(themesDir);
        availableThemes += root.entryInfoList(QDir::NoDotAndDotDot|QDir::AllDirs);
    }
    
    //Check if there are themes installed by the user
    QDir user(QDir::homePath()+"/.themes");
    availableThemes += user.entryInfoList(QDir::NoDotAndDotDot|QDir::AllDirs);

    //we just want actual themes
    QStringList paths;
    for(QFileInfoList::const_iterator it=availableThemes.constBegin(); it!=availableThemes.constEnd(); ++it) {
        bool hasGtkrc = QDir(it->filePath()).exists("gtk-2.0");

        //If it doesn't exist, we don't want it on the list
        if(hasGtkrc)
            paths += it->filePath();
    }

    return paths;
}