summaryrefslogtreecommitdiffstats
path: root/kicker/kicker/kicker-3.4-reverseLayout.cpp
blob: 97a35aff6d174c60b49a766f2c96dde1c6b5783e (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
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
#include <tqfile.h>
#include <tqmap.h>
#include <tqregexp.h>
#include <tqstring.h>
#include <tqtextstream.h>

#include <kapplication.h>
#include <kcmdlineargs.h>
#include <kglobal.h>
#include <klocale.h>
#include <kprocess.h>
#include <ktempfile.h>

struct AppletInfo
{
    double freeSpace;
    TQString configFile;
    TQString desktopFile;
};
typedef TQMap<TQString, AppletInfo> AppletInfoMap;

int main(int argc, char** argv)
{
    // We must disguise as Kicker in order to obtain the correct reverseLayout setting.
    KCmdLineArgs::init(argc, argv, "kicker", "", "", "", false);
    KApplication app(false, false);

    TQStringList stretchableApplets;
    stretchableApplets << "taskbarapplet.desktop";

    TQTextStream in (stdin,  IO_ReadOnly);
    TQTextStream out(stdout, IO_WriteOnly);

    TQStringList appletIds;
    AppletInfoMap applets;

    TQRegExp rxGroup("^\\[(.+)\\]$");
    TQRegExp rxKeyValue("([^=]+)=[ \t]*([^\n]+)");
    TQString currentGroup;

    TQString line;
    while (!(line = in.readLine()).isNull())
    {
        if (rxGroup.search(line) != -1)
        {
            currentGroup = rxGroup.cap(1);
            continue;
        }

        if (rxKeyValue.search(line) != -1)
        {
            TQString key   = rxKeyValue.cap(1);
            TQString value = rxKeyValue.cap(2);

            if (key == "Applets")
            {
                appletIds = TQStringList::split(",", value);
            }
            else if (key == "FreeSpace")
            {
                applets[currentGroup].freeSpace = value.toDouble();
            }
            else if (key == "ConfigFile")
            {
                applets[currentGroup].configFile = value;
            }
            else if (key == "DesktopFile")
            {
                applets[currentGroup].desktopFile = value;
            }
        }
    }

    if (TQApplication::reverseLayout())
    {
        // Reverse appletIds
        TQStringList appletIdsRev;
        TQStringList::ConstIterator it;
        for (it = appletIds.begin(); it != appletIds.end(); ++it)
        {
            appletIdsRev.prepend(*it);
        }
        appletIds = appletIdsRev;

        // Adjust the FreeSpace values
        for (it = appletIds.begin(); it != appletIds.end(); ++it)
        {
            applets[*it].freeSpace = 1 - applets[*it].freeSpace;

            // Take care of stretchable applets.
            if (stretchableApplets.contains(applets[*it].desktopFile))
            {
                if (it != appletIds.begin())
                {
                    applets[*it].freeSpace = applets[*(--it)].freeSpace; 
                    ++it;
                }
                else
                {
                    applets[*it].freeSpace = 0;
                }
            }
        }
    }

    // Write the changed entries to stdout.
    if (!appletIds.empty())
    {
        out << "[General]" << endl;
        out << "Applets2=" << appletIds.join(",") << endl;
        TQStringList::ConstIterator it;
        for (it = appletIds.begin(); it != appletIds.end(); ++it)
        {
            out << "[" << *it << "]" << endl;
            out << "FreeSpace2=" << applets[*it].freeSpace << endl;
        }
    }

    // Build a list of childpanel config files.
    TQStringList childPanelConfigFiles;
    AppletInfoMap::ConstIterator it2;
    TQStringList::ConstIterator it;
    for (it2 = applets.begin(); it2 != applets.end(); ++it2)
    {
        if (it2.data().desktopFile == "childpanelextension.desktop")
        {
            childPanelConfigFiles << it2.data().configFile;
        }
    }

    if (!childPanelConfigFiles.isEmpty())
    {
        // Create a temporary kconf_update .upd file for updating the childpanels
        KTempFile tempFile(TQString::null, ".upd");
        TQTextStream* upd = tempFile.textStream();
        for (it = childPanelConfigFiles.begin(); it != childPanelConfigFiles.end(); ++it)
        {
            *upd << "Id=kde_3.4_reverseLayout" << endl;
            *upd << "File=" << *it << endl;
            *upd << "Script=kicker-3.4-reverseLayout" << endl;
            *upd << endl;
        }
        tempFile.close();

        // Run kconf_update on the childpanel config files.
        KProcess kconf_update;
        kconf_update << "kconf_update" << tempFile.name();
        kconf_update.start(KProcess::Block);

        tempFile.unlink();
    }
}