tree.cpp 4.75 KB
Newer Older
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
/* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-

   tree.c for ObConf, the configuration tool for Openbox
   Copyright (c) 2003-2007   Dana Jansens

   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.

   See the COPYING file for a copy of the GNU General Public License.
*/

#include <QX11Info>
#include <QMessageBox>

#include "tree.h"
#include "obconf-qt.h"

#include <obt/xml.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>

xmlNodePtr tree_get_node(const gchar *path, const gchar *def)
{
    xmlNodePtr n, c;
    gchar **nodes;
    gchar **it, **next;

    n = obt_xml_root(parse_i);

    nodes = g_strsplit(path, "/", 0);
    for (it = nodes; *it; it = next) {
        gchar **attrs;
        gboolean ok = FALSE;

        attrs = g_strsplit(*it, ":", 0);
        next = it + 1;

        /* match attributes */
        c = obt_xml_find_node(n->children, attrs[0]);
        while (c && !ok) {
            gint i;

            ok = TRUE;
            for (i = 1; attrs[i]; ++i) {
                gchar **eq = g_strsplit(attrs[i], "=", 2);
                if (eq[1] && !obt_xml_attr_contains(c, eq[0], eq[1]))
                    ok = FALSE;
                g_strfreev(eq);
            }
            if (!ok)
                c = obt_xml_find_node(c->next, attrs[0]);
        }

        if (!c) {
            gint i;

            c = xmlNewTextChild(n, NULL, (xmlChar*)attrs[0], (xmlChar*)(*next ? NULL : def));

            for (i = 1; attrs[i]; ++i) {
                gchar **eq = g_strsplit(attrs[i], "=", 2);
                if (eq[1])
                  xmlNewProp(c, (xmlChar*)eq[0], (xmlChar*)eq[1]);
                g_strfreev(eq);
            }
        }
        n = c;

        g_strfreev(attrs);
    }

    g_strfreev(nodes);

    return n;
}

void tree_delete_node(const gchar *path)
{
    xmlNodePtr n;

    n = tree_get_node(path, NULL);
    xmlUnlinkNode(n);
    xmlFreeNode(n);
}

void tree_apply()
{
    gchar *p, *d;
    gboolean err = FALSE;

    if (obc_config_file)
        p = g_strdup(obc_config_file);
    else
        p = g_build_filename(obt_paths_config_home(paths), "openbox",
                             "rc.xml", NULL);

    d = g_path_get_dirname(p);
    obt_paths_mkdir_path(d, 0700);
    g_free(d);

    if (!obt_xml_save_file(parse_i, p, TRUE)) {
        gchar *s;
109
        s = g_strdup_printf("An error occurred while saving the "
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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
                            "config file '%s'", p);
        // obconf_error(s, FALSE);
        QMessageBox::critical(NULL, QString(), QString::fromUtf8(s));
        g_free(s);
    }
    g_free(p);

    if (!err) {
        XEvent ce;

        ce.xclient.type = ClientMessage;
        ce.xclient.display = QX11Info::display();
        ce.xclient.message_type = XInternAtom(ce.xclient.display, "_OB_CONTROL", false);
        Window root = QX11Info::appRootWindow();
        ce.xclient.window = root;
        ce.xclient.format = 32;
        ce.xclient.data.l[0] = 1; /* reconfigure */
        ce.xclient.data.l[1] = 0;
        ce.xclient.data.l[2] = 0;
        ce.xclient.data.l[3] = 0;
        ce.xclient.data.l[4] = 0;
        XSendEvent(ce.xclient.display, root, FALSE,
                   SubstructureNotifyMask | SubstructureRedirectMask,
                   &ce);
    }
}

void tree_set_string(const gchar *node, const gchar *value)
{
    xmlNodePtr n;

    n = tree_get_node(node, NULL);
    xmlNodeSetContent(n, (const xmlChar*) value);

    tree_apply();
}

void tree_set_int(const gchar *node, const gint value)
{
    xmlNodePtr n;
    gchar *s;

    n = tree_get_node(node, NULL);
    s = g_strdup_printf("%d", value);
    xmlNodeSetContent(n, (const xmlChar*) s);
    g_free(s);

    tree_apply();
}

void tree_set_bool(const gchar *node, const gboolean value)
{
    xmlNodePtr n;

    n = tree_get_node(node, NULL);
    xmlNodeSetContent(n, (const xmlChar*) (value ? "yes" : "no"));

    tree_apply();
}

gchar* tree_get_string(const gchar *node, const gchar *def)
{
    xmlNodePtr n;

    n = tree_get_node(node, def);
    return obt_xml_node_string(n);
}

gint tree_get_int(const gchar *node, gint def)
{
    xmlNodePtr n;
    gchar *d;

    d = g_strdup_printf("%d", def);
    n = tree_get_node(node, d);
    g_free(d);
    return obt_xml_node_int(n);
}

gboolean tree_get_bool(const gchar *node, gboolean def)
{
    xmlNodePtr n;

    n = tree_get_node(node, (def ? "yes" : "no"));
    return obt_xml_node_bool(n);
}