~ubuntu-branches/ubuntu/utopic/cairo-dock/utopic

« back to all changes in this revision

Viewing changes to src/cairo-dock-widget-config-group.c

  • Committer: Matthieu Baerts
  • Date: 2012-08-18 12:02:27 UTC
  • mto: This revision was merged to the branch mainline in revision 30.
  • Revision ID: matttbe@gmail.com-20120818120227-egczdficyxkeitzf
Tags: upstream-3.0.99.beta1
ImportĀ upstreamĀ versionĀ 3.0.99.beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
* This file is a part of the Cairo-Dock project
 
3
*
 
4
* Copyright : (C) see the 'copyright' file.
 
5
* E-mail    : see the 'copyright' file.
 
6
*
 
7
* This program is free software; you can redistribute it and/or
 
8
* modify it under the terms of the GNU General Public License
 
9
* as published by the Free Software Foundation; either version 3
 
10
* of the License, or (at your option) any later version.
 
11
*
 
12
* This program is distributed in the hope that it will be useful,
 
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
* GNU General Public License for more details.
 
16
* You should have received a copy of the GNU General Public License
 
17
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
*/
 
19
 
 
20
#include "config.h"
 
21
#include "cairo-dock-struct.h"
 
22
#include "cairo-dock-keyfile-utilities.h"
 
23
#include "cairo-dock-module-manager.h"
 
24
#include "cairo-dock-module-factory.h"
 
25
#include "cairo-dock-gui-factory.h"
 
26
#include "cairo-dock-log.h"
 
27
#include "cairo-dock-X-manager.h"
 
28
#include "cairo-dock-widget-config-group.h"
 
29
 
 
30
#define CAIRO_DOCK_ICON_MARGIN 6
 
31
extern gchar *g_cConfFile;
 
32
 
 
33
 
 
34
static void _config_group_widget_apply (CDWidget *pCdWidget)
 
35
{
 
36
        ConfigGroupWidget *pConfigGroupWidget = CONFIG_GROUP_WIDGET (pCdWidget);
 
37
        
 
38
        // update the conf file.
 
39
        GKeyFile *pKeyFile = cairo_dock_open_key_file (g_cConfFile);
 
40
        g_return_if_fail (pKeyFile != NULL);
 
41
 
 
42
        cairo_dock_update_keyfile_from_widget_list (pKeyFile, pCdWidget->pWidgetList);
 
43
        cairo_dock_write_keys_to_file (pKeyFile, g_cConfFile);
 
44
        g_key_file_free (pKeyFile);
 
45
        
 
46
        // reload the associated managers.
 
47
        const gchar *cManagerName, *cModuleName;
 
48
        CairoDockModule *pModule;
 
49
        CairoDockModuleInstance *pExtraInstance;
 
50
        GldiManager *pManager;
 
51
        GSList *pExtraWidgetList;
 
52
        GKeyFile* pExtraKeyFile;
 
53
        GList *m, *e;
 
54
        GSList *w = pConfigGroupWidget->pExtraWidgets;
 
55
        for (m = pConfigGroupWidget->pManagers; m != NULL; m = m->next)
 
56
        {
 
57
                cManagerName = m->data;
 
58
                pManager = gldi_get_manager (cManagerName);
 
59
                g_return_if_fail (pManager != NULL);
 
60
                gldi_reload_manager (pManager, g_cConfFile);
 
61
                
 
62
                // reload the extensions too
 
63
                for (e = pManager->pExternalModules; e != NULL && w != NULL; e = e->next)
 
64
                {
 
65
                        // get the extension
 
66
                        cModuleName = e->data;
 
67
                        pModule = cairo_dock_find_module_from_name (cModuleName);
 
68
                        if (!pModule)
 
69
                                continue;
 
70
                        
 
71
                        pExtraInstance = pModule->pInstancesList->data;
 
72
                        if (pExtraInstance == NULL)
 
73
                                continue;
 
74
                        
 
75
                        // update its conf file
 
76
                        pExtraKeyFile = cairo_dock_open_key_file (pExtraInstance->cConfFilePath);
 
77
                        if (pExtraKeyFile == NULL)
 
78
                                continue;
 
79
                        
 
80
                        pExtraWidgetList = w->data;
 
81
                        w = w->next;
 
82
                        
 
83
                        cairo_dock_update_keyfile_from_widget_list (pExtraKeyFile, pExtraWidgetList);
 
84
                        if (pModule->pInterface->save_custom_widget != NULL)
 
85
                                pModule->pInterface->save_custom_widget (pExtraInstance, pKeyFile, pExtraWidgetList);
 
86
                        cairo_dock_write_keys_to_file (pExtraKeyFile, pExtraInstance->cConfFilePath);
 
87
                        g_key_file_free (pExtraKeyFile);
 
88
                        
 
89
                        // reload it
 
90
                        cairo_dock_reload_module_instance (pExtraInstance, TRUE);
 
91
                }
 
92
        }
 
93
}
 
94
 
 
95
static void _config_group_widget_reset (CDWidget *pCdWidget)
 
96
{
 
97
        ConfigGroupWidget *pConfigGroupWidget = CONFIG_GROUP_WIDGET (pCdWidget);
 
98
        g_free (pConfigGroupWidget->cGroupName);
 
99
        g_slist_foreach (pConfigGroupWidget->pExtraWidgets, (GFunc)cairo_dock_free_generated_widget_list, NULL);
 
100
        g_slist_free (pConfigGroupWidget->pExtraWidgets);
 
101
        memset (pCdWidget+1, 0, sizeof (ConfigGroupWidget) - sizeof (CDWidget));  // reset all our parameters.
 
102
}
 
103
 
 
104
ConfigGroupWidget *cairo_dock_config_group_widget_new (const gchar *cGroupName, GList *pManagers, const gchar *cTitle, const gchar *cIcon)
 
105
{
 
106
        ConfigGroupWidget *pConfigGroupWidget = g_new0 (ConfigGroupWidget, 1);
 
107
        pConfigGroupWidget->widget.iType = WIDGET_CONFIG_GROUP;
 
108
        pConfigGroupWidget->widget.apply = _config_group_widget_apply;
 
109
        pConfigGroupWidget->widget.reset = _config_group_widget_reset;
 
110
        pConfigGroupWidget->cGroupName = g_strdup (cGroupName);
 
111
        pConfigGroupWidget->pManagers = pManagers;
 
112
        
 
113
        // build its widget based on its config file.
 
114
        GKeyFile* pKeyFile = cairo_dock_open_key_file (g_cConfFile);
 
115
        g_return_val_if_fail (pKeyFile != NULL, NULL);
 
116
        
 
117
        GSList *pWidgetList = NULL;
 
118
        GPtrArray *pDataGarbage = g_ptr_array_new ();
 
119
        GtkWidget *pWidget = cairo_dock_build_group_widget (pKeyFile,
 
120
                cGroupName,
 
121
                NULL,  // gettext domain
 
122
                NULL,  // main window
 
123
                &pWidgetList,
 
124
                pDataGarbage,
 
125
                g_strdup (CAIRO_DOCK_SHARE_DATA_DIR"/"CAIRO_DOCK_CONF_FILE));
 
126
        pConfigGroupWidget->widget.pWidgetList = pWidgetList;
 
127
        pConfigGroupWidget->widget.pDataGarbage = pDataGarbage;
 
128
        
 
129
        // build the widgets of the extensions
 
130
        GtkWidget *pNoteBook = NULL;
 
131
        GKeyFile* pExtraKeyFile;
 
132
        CairoDockModule *pModule;
 
133
        CairoDockModuleInstance *pExtraInstance;
 
134
        GSList *pExtraWidgetList;
 
135
        gchar *cOriginalConfFilePath;
 
136
        GldiManager *pManager;
 
137
        const gchar *cManagerName, *cModuleName;
 
138
        GList *m, *e;
 
139
        for (m = pManagers; m != NULL; m = m->next)
 
140
        {
 
141
                cManagerName = m->data;
 
142
                pManager = gldi_get_manager (cManagerName);
 
143
                if (!pManager)
 
144
                        continue;
 
145
                
 
146
                for (e = pManager->pExternalModules; e != NULL; e = e->next)
 
147
                {
 
148
                        cModuleName = e->data;
 
149
                        pModule = cairo_dock_find_module_from_name (cModuleName);
 
150
                        if (!pModule)
 
151
                                continue;
 
152
                        
 
153
                        pExtraInstance = pModule->pInstancesList->data;
 
154
                        if (pExtraInstance == NULL)
 
155
                                continue;
 
156
                        
 
157
                        pExtraKeyFile = cairo_dock_open_key_file (pExtraInstance->cConfFilePath);
 
158
                        if (pExtraKeyFile == NULL)
 
159
                                continue;
 
160
                        
 
161
                        pExtraWidgetList = NULL;
 
162
                        cOriginalConfFilePath = g_strdup_printf ("%s/%s", pModule->pVisitCard->cShareDataDir, pModule->pVisitCard->cConfFileName);
 
163
                        pNoteBook = cairo_dock_build_key_file_widget (pExtraKeyFile,
 
164
                                pModule->pVisitCard->cGettextDomain,
 
165
                                NULL,
 
166
                                &pExtraWidgetList,
 
167
                                pDataGarbage,  // the garbage array can be mutualized with 'pConfigGroupWidget'
 
168
                                cOriginalConfFilePath);  /// TODO : fournir pNoteBook a la fonction pour fusionner les differents modules extra...
 
169
                        
 
170
                        pConfigGroupWidget->pExtraWidgets = g_slist_append (pConfigGroupWidget->pExtraWidgets, pExtraWidgetList);  // append, so that we can parse the list in the same order again.
 
171
                        
 
172
                        if (pModule->pInterface->load_custom_widget != NULL)
 
173
                                pModule->pInterface->load_custom_widget (pExtraInstance, pExtraKeyFile, pExtraWidgetList);
 
174
                        
 
175
                        ///g_free (cOriginalConfFilePath);
 
176
                        g_key_file_free (pExtraKeyFile);
 
177
                }
 
178
        }
 
179
        
 
180
        // on rajoute la page du module interne en 1er dans le notebook.
 
181
        if (pNoteBook != NULL)
 
182
        {
 
183
                GtkWidget *pLabel = gtk_label_new (cTitle);
 
184
                GtkWidget *pLabelContainer = NULL;
 
185
                GtkWidget *pAlign = NULL;
 
186
                if (cIcon != NULL && *cIcon != '\0')
 
187
                {
 
188
                        pLabelContainer = _gtk_hbox_new (CAIRO_DOCK_ICON_MARGIN);
 
189
                        pAlign = gtk_alignment_new (0., 0.5, 0., 0.);
 
190
                        gtk_container_add (GTK_CONTAINER (pAlign), pLabelContainer);
 
191
 
 
192
                        GtkWidget *pImage = _gtk_image_new_from_file (cIcon, GTK_ICON_SIZE_BUTTON);
 
193
                        gtk_container_add (GTK_CONTAINER (pLabelContainer),
 
194
                                pImage);
 
195
                        gtk_container_add (GTK_CONTAINER (pLabelContainer), pLabel);
 
196
                        gtk_widget_show_all (pLabelContainer);
 
197
                }
 
198
                
 
199
                GtkWidget *pScrolledWindow = gtk_scrolled_window_new (NULL, NULL);  // add scrollbars on the widget before putting it into the notebook.
 
200
                gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (pScrolledWindow), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
 
201
                gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (pScrolledWindow), pWidget);
 
202
                
 
203
                gtk_notebook_prepend_page (GTK_NOTEBOOK (pNoteBook), pScrolledWindow, (pAlign != NULL ? pAlign : pLabel));
 
204
                pWidget = pNoteBook;
 
205
        }
 
206
        
 
207
        pConfigGroupWidget->widget.pWidget = pWidget;
 
208
        
 
209
        g_key_file_free (pKeyFile);
 
210
        return pConfigGroupWidget;
 
211
}
 
212