File: kconf_update_migrate_from_kde3_icon_theme.cpp

package info (click to toggle)
kde4libs 4:4.14.2-5+deb8u2
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 82,428 kB
  • ctags: 99,415
  • sloc: cpp: 761,864; xml: 12,344; ansic: 6,295; java: 4,060; perl: 2,938; yacc: 2,507; python: 1,207; sh: 1,179; ruby: 337; lex: 278; makefile: 29
file content (157 lines) | stat: -rw-r--r-- 5,271 bytes parent folder | download | duplicates (7)
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
/*
   Copyright (c) 2011, Modestas Vainius <modax@debian.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include <QtCore/QString>
#include <QtCore/QLatin1String>
#include <QtCore/QStringList>
#include <QtCore/QTextStream>
#include <QtCore/QRegExp>
#include <QtCore/QHash>

#include <kicontheme.h>
#include <kiconloader.h>
#include <kaboutdata.h>
#include <kcmdlineargs.h>
#include <kapplication.h>
#include <kdebug.h>

struct IconCheck {
    QString name;
    QString kde4IconName;
    KIconLoader::Context context;

    IconCheck(const QString &name, const QString &kde4IconName,
            KIconLoader::Context context)
        : name(name), kde4IconName(kde4IconName), context(context)
    {
    }
};

bool isIconThemeKde4Compat(const QString &themeName=QString())
{
    KIconTheme *theme = new KIconTheme(
            themeName.isEmpty() ? KIconTheme::current() : themeName);
    if (!theme->isValid()) {
        // When the theme is invalid (i.e. icon directory does not exist),
        // KIconLoader will load default one. As this might be temporary,
        // do not anything
        delete theme;
        return true;
    }

    /* Check if the theme is not for KDE 3. Do this by checking
       if some standard KDE4 icons which have different names
       from KDE 3, actually exist.

        |----------|------------|------------------|
        | Action   | KDE 3 name | KDE 4 name       |
        |----------|------------|------------------|
        | Open     | fileopen   | document-open    |
        | Save     | filesave   | document-save    |
        | Save As  | filesaveas | document-save-as |
        |----------|------------|------------------|

    */

    QList<struct IconCheck> iconChecks;
    iconChecks <<
        IconCheck(QLatin1String("Open"),  QLatin1String("document-open"), KIconLoader::Action) <<
        IconCheck(QLatin1String("Save"),  QLatin1String("document-save"), KIconLoader::Action) <<
        IconCheck(QLatin1String("SaveAs"), QLatin1String("document-save-as"), KIconLoader::Action);

    int success = 0;
    QHash<KIconLoader::Context, QStringList> icons;
    foreach(IconCheck iconCheck, iconChecks) {
        if (!icons.contains(iconCheck.context)) {
            icons[iconCheck.context] = theme->queryIcons(32, iconCheck.context);
        }
        const QStringList &iconList = icons.value(iconCheck.context);
        if (!iconList.filter(iconCheck.kde4IconName).isEmpty())
            success++;
        else
            kDebug() << "Standard KDE 4 icon" << iconCheck.kde4IconName << "for"
                << iconCheck.name << "could not be found in the current icon theme"
                << theme->name();
    }

    if (success < iconChecks.size())
    {
        kDebug() << theme->internalName() << "theme is not KDE 4 compatible.";
    }

    delete theme;
    return (success == iconChecks.size());
}

int main(int argc, char *argv[])
{
    KAboutData aboutData(
        "kconf_update_migrate_from_kde3_icon_theme", 0,
        ki18n("kconf_update for icon theme: migrate from KDE 3"),
        "1.0",
        ki18n("kconf_update utility which resets global icon theme if "
              "current one is not KDE 4 compatible"),
        KAboutData::License_GPL,
        ki18n("Copyright (C) 2011 Modestas Vainius"),
        ki18n(QByteArray()),
        "http://pkg-kde.alioth.debian.org/",
        "debian-qt-kde@lists.debian.org");
    KCmdLineArgs::init( argc, argv, &aboutData );
    KApplication app(false);


    QTextStream in(stdin, QIODevice::ReadOnly);
    QTextStream out(stdout, QIODevice::WriteOnly);
    QRegExp themeRe("^\\s*(Theme(\\[[^]]+\\])?)\\s*=(.*)");
    QRegExp spacesRe("^\\s+$");

    QString line, key, themeName;
    QStringList keysToDelete;
    bool deleteGroup = true;

    while (!in.atEnd()) {
        line = in.readLine();
        if (line.isEmpty() || spacesRe.exactMatch(line))
            continue;

        if (themeRe.indexIn(line) > -1) {
            key = themeRe.cap(1);
            themeName = themeRe.cap(3).trimmed();
            if (!isIconThemeKde4Compat(themeName)) {
                keysToDelete.append(key);
                continue;
            }
        }

        // It means some valid keys exist, so don't delete the whole group
        deleteGroup = false;
    }

    if (!keysToDelete.isEmpty()) {
        if (deleteGroup) {
            out << "# DELETEGROUP" << endl;
        } else {
            foreach (key, keysToDelete) {
                out << "# DELETE " << key << endl;
            }
        }
    }

    return 0;
}