~ubuntu-branches/ubuntu/oneiric/lxappearance-obconf/oneiric

« back to all changes in this revision

Viewing changes to src/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Julien Lavergne
  • Date: 2010-11-13 00:25:20 UTC
  • Revision ID: james.westby@ubuntu.com-20101113002520-lsxiri8g5916wsql
Tags: upstream-0.0.1~git20101112
ImportĀ upstreamĀ versionĀ 0.0.1~git20101112

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
 
2
 
 
3
   main.c for ObConf, the configuration tool for Openbox
 
4
   Copyright (c) 2003-2008   Dana Jansens
 
5
   Copyright (c) 2003        Tim Riley
 
6
 
 
7
   Copyright (C) 2010        Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
 
8
 
 
9
   This program is free software; you can redistribute it and/or modify
 
10
   it under the terms of the GNU General Public License as published by
 
11
   the Free Software Foundation; either version 2 of the License, or
 
12
   (at your option) any later version.
 
13
 
 
14
   This program is distributed in the hope that it will be useful,
 
15
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
   GNU General Public License for more details.
 
18
 
 
19
   See the COPYING file for a copy of the GNU General Public License.
 
20
*/
 
21
 
 
22
/* This file is part of ObConf. It's taken by Hong Jen Yee on
 
23
 * 2010-08-07 and some modifications were done to make it a loadable
 
24
 * module of LXAppearance. */
 
25
 
 
26
#ifdef HAVE_CONFIG_H
 
27
#include <config.h>
 
28
#endif
 
29
 
 
30
#include "main.h"
 
31
#include "archive.h"
 
32
#include "theme.h"
 
33
#include "appearance.h"
 
34
#include "preview_update.h"
 
35
#include "tree.h"
 
36
#include <glib/gi18n-lib.h>
 
37
 
 
38
#include "lxappearance/lxappearance.h"
 
39
 
 
40
#include <gdk/gdkx.h>
 
41
 
 
42
GtkWidget *mainwin = NULL;
 
43
 
 
44
GtkBuilder* builder;
 
45
xmlDocPtr doc;
 
46
xmlNodePtr root;
 
47
RrInstance *rrinst;
 
48
gchar *obc_config_file = NULL;
 
49
 
 
50
static gchar *obc_theme_install = NULL;
 
51
static gchar *obc_theme_archive = NULL;
 
52
 
 
53
void obconf_error(gchar *msg, gboolean modal)
 
54
{
 
55
    GtkWidget *d;
 
56
 
 
57
    d = gtk_message_dialog_new(mainwin ? GTK_WINDOW(mainwin) : NULL,
 
58
                               GTK_DIALOG_DESTROY_WITH_PARENT,
 
59
                               GTK_MESSAGE_ERROR,
 
60
                               GTK_BUTTONS_CLOSE,
 
61
                               "%s", msg);
 
62
    gtk_window_set_title(GTK_WINDOW(d), "ObConf Error");
 
63
    if (modal)
 
64
        gtk_dialog_run(GTK_DIALOG(d));
 
65
    else {
 
66
        g_signal_connect_swapped(GTK_OBJECT(d), "response",
 
67
                                 G_CALLBACK(gtk_widget_destroy),
 
68
                                 GTK_OBJECT(d));
 
69
        gtk_widget_show(d);
 
70
    }
 
71
}
 
72
 
 
73
static gboolean get_all(Window win, Atom prop, Atom type, gint size,
 
74
                        guchar **data, guint *num)
 
75
{
 
76
    gboolean ret = FALSE;
 
77
    gint res;
 
78
    guchar *xdata = NULL;
 
79
    Atom ret_type;
 
80
    gint ret_size;
 
81
    gulong ret_items, bytes_left;
 
82
 
 
83
    res = XGetWindowProperty(GDK_DISPLAY(), win, prop, 0l, G_MAXLONG,
 
84
                             FALSE, type, &ret_type, &ret_size,
 
85
                             &ret_items, &bytes_left, &xdata);
 
86
    if (res == Success) {
 
87
        if (ret_size == size && ret_items > 0) {
 
88
            guint i;
 
89
 
 
90
            *data = g_malloc(ret_items * (size / 8));
 
91
            for (i = 0; i < ret_items; ++i)
 
92
                switch (size) {
 
93
                case 8:
 
94
                    (*data)[i] = xdata[i];
 
95
                    break;
 
96
                case 16:
 
97
                    ((guint16*)*data)[i] = ((gushort*)xdata)[i];
 
98
                    break;
 
99
                case 32:
 
100
                    ((guint32*)*data)[i] = ((gulong*)xdata)[i];
 
101
                    break;
 
102
                default:
 
103
                    g_assert_not_reached(); /* unhandled size */
 
104
                }
 
105
            *num = ret_items;
 
106
            ret = TRUE;
 
107
        }
 
108
        XFree(xdata);
 
109
    }
 
110
    return ret;
 
111
}
 
112
 
 
113
static gboolean prop_get_string_utf8(Window win, Atom prop, gchar **ret)
 
114
{
 
115
    gchar *raw;
 
116
    gchar *str;
 
117
    guint num;
 
118
 
 
119
    if (get_all(win, prop,
 
120
                gdk_x11_get_xatom_by_name("UTF8_STRING"),
 
121
                8,(guchar**)&raw, &num))
 
122
    {
 
123
        str = g_strndup(raw, num); /* grab the first string from the list */
 
124
        g_free(raw);
 
125
        if (g_utf8_validate(str, -1, NULL)) {
 
126
            *ret = str;
 
127
            return TRUE;
 
128
        }
 
129
        g_free(str);
 
130
    }
 
131
    return FALSE;
 
132
}
 
133
 
 
134
static void on_response(GtkDialog* dlg, int res, LXAppearance* app)
 
135
{
 
136
    if(res == GTK_RESPONSE_APPLY)
 
137
    {
 
138
        tree_apply();
 
139
    }
 
140
}
 
141
 
 
142
/* int main(int argc, char **argv) */
 
143
extern gboolean plugin_load(LXAppearance* app, GtkBuilder* lxappearance_builder)
 
144
{
 
145
    gchar *p;
 
146
    gboolean exit_with_error = FALSE;
 
147
 
 
148
    /* ABI compatibility check. */
 
149
    if(app->abi_version > LXAPPEARANCE_ABI_VERSION)
 
150
        return FALSE;
 
151
 
 
152
    /* detect openbox */
 
153
    const char* wm_name = gdk_x11_screen_get_window_manager_name(gtk_widget_get_screen(app->dlg));
 
154
    if(g_strcmp0(wm_name, "Openbox"))
 
155
        return FALSE; /* don't load the plugin if openbox is not in use. */
 
156
 
 
157
#ifdef ENABLE_NLS
 
158
    bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
 
159
    bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
 
160
#endif
 
161
 
 
162
    mainwin = app->dlg;
 
163
 
 
164
    builder = gtk_builder_new();
 
165
    gtk_builder_set_translation_domain(builder, GETTEXT_PACKAGE);
 
166
    g_debug(GLADEDIR"/obconf.glade");
 
167
    if(!gtk_builder_add_from_file(builder, GLADEDIR"/obconf.glade", NULL))
 
168
    {
 
169
        obconf_error(_("Failed to load the obconf.glade interface file. ObConf is probably not installed correctly."), TRUE);
 
170
        exit_with_error = TRUE;
 
171
    }
 
172
    gtk_builder_connect_signals(builder, NULL);
 
173
    gtk_box_pack_start(app->wm_page, get_widget("obconf_vbox"), TRUE, TRUE, 0);
 
174
    gtk_widget_show_all(app->wm_page);
 
175
 
 
176
    g_signal_connect(app->dlg, "response", G_CALLBACK(on_response), app);
 
177
 
 
178
    parse_paths_startup();
 
179
 
 
180
    if (!obc_config_file) {
 
181
        gchar *p;
 
182
 
 
183
        if (prop_get_string_utf8(GDK_ROOT_WINDOW(),
 
184
                                 gdk_x11_get_xatom_by_name("_OB_CONFIG_FILE"),
 
185
                                 &p))
 
186
        {
 
187
            obc_config_file = g_filename_from_utf8(p, -1, NULL, NULL, NULL);
 
188
            g_free(p);
 
189
        }
 
190
    }
 
191
 
 
192
    xmlIndentTreeOutput = 1;
 
193
    if (!parse_load_rc(obc_config_file, &doc, &root)) {
 
194
        obconf_error(_("Failed to load an rc.xml. Openbox is probably not installed correctly."), TRUE);
 
195
        exit_with_error = TRUE;
 
196
    }
 
197
 
 
198
    /* look for parsing errors */
 
199
    {
 
200
        xmlErrorPtr e = xmlGetLastError();
 
201
        if (e) {
 
202
            char *a = g_strdup_printf
 
203
                (_("Error while parsing the Openbox configuration file. Your configuration file is not valid XML.\n\nMessage: %s"),
 
204
                 e->message);
 
205
            obconf_error(a, TRUE);
 
206
            g_free(a);
 
207
            exit_with_error = TRUE;
 
208
        }
 
209
    }
 
210
 
 
211
    rrinst = RrInstanceNew(GDK_DISPLAY(), gdk_x11_get_default_screen());
 
212
    if (!exit_with_error) {
 
213
        theme_setup_tab();
 
214
        appearance_setup_tab();
 
215
        theme_load_all();
 
216
    }
 
217
    return !exit_with_error;
 
218
}
 
219
 
 
220
extern void plugin_unload(LXAppearance* app)
 
221
{
 
222
    preview_update_set_active_font(NULL);
 
223
    preview_update_set_inactive_font(NULL);
 
224
    preview_update_set_menu_header_font(NULL);
 
225
    preview_update_set_menu_item_font(NULL);
 
226
    preview_update_set_osd_font(NULL);
 
227
    preview_update_set_title_layout(NULL);
 
228
 
 
229
    RrInstanceFree(rrinst);
 
230
    parse_paths_shutdown();
 
231
 
 
232
    xmlFreeDoc(doc);
 
233
}