~helene-verhaeghe27/cairo-dock-core/bugfix

« back to all changes in this revision

Viewing changes to src/cairo-dock-gui-themes.c

  • Committer: Fabrice Rey
  • Date: 2012-08-04 22:44:55 UTC
  • Revision ID: fabounet03@gmail.com-20120804224455-i4qtut3p9cv1f4si
GUI: finished to implement the reload of the GUI

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 <string.h>
21
 
#include <unistd.h>
22
 
#define __USE_XOPEN_EXTENDED
23
 
#include <stdlib.h>
24
 
#include <sys/stat.h>
25
 
#define __USE_POSIX
26
 
#include <time.h>
27
 
#include <glib/gstdio.h>
28
 
#include <glib/gi18n.h>
29
 
 
30
 
#include "config.h"
31
 
#include "cairo-dock-keyfile-utilities.h"
32
 
#include "cairo-dock-file-manager.h"  // cairo_dock_copy_file
33
 
#include "cairo-dock-packages.h"
34
 
#include "cairo-dock-log.h"
35
 
#include "cairo-dock-gui-manager.h"
36
 
#include "cairo-dock-gui-factory.h"
37
 
#include "cairo-dock-log.h"
38
 
#include "cairo-dock-config.h"  // cairo_dock_load_current_theme
39
 
#include "cairo-dock-themes-manager.h"
40
 
#include "cairo-dock-gui-commons.h"
41
 
#include "cairo-dock-gui-themes.h"
42
 
 
43
 
#define CAIRO_DOCK_THEME_PANEL_WIDTH 1000
44
 
#define CAIRO_DOCK_THEME_PANEL_HEIGHT 550
45
 
 
46
 
extern gchar *g_cThemesDirPath;
47
 
 
48
 
static GtkWidget *s_pThemeManager = NULL;
49
 
static CairoDockTask *s_pImportTask = NULL;
50
 
 
51
 
 
52
 
static gchar *cairo_dock_build_temporary_themes_conf_file (void)
53
 
{
54
 
        //\___________________ On cree un fichier de conf temporaire.
55
 
        const gchar *cTmpDir = g_get_tmp_dir ();
56
 
        gchar *cTmpConfFile = g_strdup_printf ("%s/cairo-dock-init.XXXXXX", cTmpDir);
57
 
        int fds = mkstemp (cTmpConfFile);
58
 
        if (fds == -1)
59
 
        {
60
 
                cd_warning ("can't create a temporary file in %s", cTmpDir);
61
 
                g_free (cTmpConfFile);
62
 
                return NULL;
63
 
        }
64
 
        
65
 
        //\___________________ On copie le fichier de conf par defaut dedans.
66
 
        cairo_dock_copy_file (CAIRO_DOCK_SHARE_DATA_DIR"/themes.conf", cTmpConfFile);
67
 
        /*gchar *cCommand = g_strdup_printf ("cp \"%s\" \"%s\"", CAIRO_DOCK_SHARE_DATA_DIR"/themes.conf", cTmpConfFile);
68
 
        int r = system (cCommand);
69
 
        g_free (cCommand);*/
70
 
 
71
 
        close(fds);
72
 
        return cTmpConfFile;
73
 
}
74
 
 
75
 
 
76
 
static void _cairo_dock_fill_model_with_themes (const gchar *cThemeName, CairoDockPackage *pTheme, GtkListStore *pModele)
77
 
{
78
 
        GtkTreeIter iter;
79
 
        memset (&iter, 0, sizeof (GtkTreeIter));
80
 
        gtk_list_store_append (GTK_LIST_STORE (pModele), &iter);
81
 
        
82
 
        gtk_list_store_set (GTK_LIST_STORE (pModele), &iter,
83
 
                CAIRO_DOCK_MODEL_NAME, pTheme->cDisplayedName,
84
 
                CAIRO_DOCK_MODEL_RESULT, cThemeName, -1);
85
 
}
86
 
static void _cairo_dock_activate_one_element (GtkCellRendererToggle * cell_renderer, gchar * path, GtkTreeModel * model)
87
 
{
88
 
        GtkTreeIter iter;
89
 
        if (! gtk_tree_model_get_iter_from_string (model, &iter, path))
90
 
                return ;
91
 
        gboolean bState;
92
 
        gtk_tree_model_get (model, &iter, CAIRO_DOCK_MODEL_ACTIVE, &bState, -1);
93
 
 
94
 
        gtk_list_store_set (GTK_LIST_STORE (model), &iter, CAIRO_DOCK_MODEL_ACTIVE, !bState, -1);
95
 
}
96
 
static void _make_widgets (GtkWidget *pThemeManager, GKeyFile *pKeyFile)
97
 
{
98
 
        cairo_dock_make_tree_view_for_delete_themes (pThemeManager);
99
 
}
100
 
 
101
 
 
102
 
static void _load_theme (gboolean bSuccess, GtkWidget *pWaitingDialog)
103
 
{
104
 
        if (s_pThemeManager == NULL)  // si l'utilisateur a ferme la fenetre entre-temps, on considere qu'il a abandonne.
105
 
                return ;
106
 
        if (bSuccess)
107
 
        {
108
 
                cairo_dock_load_current_theme ();
109
 
                
110
 
                cairo_dock_set_status_message (s_pThemeManager, "");
111
 
                cairo_dock_reload_generic_gui (s_pThemeManager);
112
 
        }
113
 
        else
114
 
                cairo_dock_set_status_message (s_pThemeManager, _("Could not import the theme."));
115
 
        gtk_widget_destroy (pWaitingDialog);
116
 
}
117
 
 
118
 
static void on_theme_destroy (gchar *cInitConfFile)
119
 
{
120
 
        cd_debug ("");
121
 
        g_remove (cInitConfFile);
122
 
        g_free (cInitConfFile);
123
 
        s_pThemeManager = NULL;
124
 
}
125
 
 
126
 
static gboolean on_theme_apply (gchar *cInitConfFile)
127
 
{
128
 
        cd_debug ("%s (%s)", __func__, cInitConfFile);
129
 
        GError *erreur = NULL;
130
 
        int r;  // resultat de system().
131
 
        
132
 
        //\___________________ On recupere l'onglet courant.
133
 
        GtkWidget * pMainVBox = gtk_bin_get_child (GTK_BIN (s_pThemeManager));
134
 
        GList *children = gtk_container_get_children (GTK_CONTAINER (pMainVBox));
135
 
        g_return_val_if_fail (children != NULL, FALSE);
136
 
        GtkWidget *pGroupWidget = children->data;
137
 
        g_list_free (children);
138
 
        int iNumPage = gtk_notebook_get_current_page (GTK_NOTEBOOK (pGroupWidget));
139
 
        
140
 
        //\_______________ On recupere l'onglet courant.
141
 
        GKeyFile* pKeyFile = cairo_dock_open_key_file (cInitConfFile);
142
 
        g_return_val_if_fail (pKeyFile != NULL, FALSE);
143
 
        
144
 
        //\_______________ On effectue les actions correspondantes.
145
 
        gboolean bReloadWindow = FALSE;
146
 
        switch (iNumPage)
147
 
        {
148
 
                case 0:  // load a theme
149
 
                        cairo_dock_set_status_message (s_pThemeManager, _("Importing theme ..."));
150
 
                        cairo_dock_load_theme (pKeyFile, (GFunc) _load_theme, s_pThemeManager);  // bReloadWindow reste a FALSE, on ne rechargera la fenetre que lorsque le theme aura ete importe.
151
 
                break;
152
 
                
153
 
                case 1:  // save current theme
154
 
                        bReloadWindow = cairo_dock_save_current_theme (pKeyFile);
155
 
                        if (bReloadWindow)
156
 
                                cairo_dock_set_status_message (s_pThemeManager, _("Theme has been saved"));
157
 
                break;
158
 
                
159
 
                case 2:  // delete some themes
160
 
                        bReloadWindow = cairo_dock_delete_user_themes (pKeyFile);
161
 
                        if (bReloadWindow)
162
 
                                cairo_dock_set_status_message (s_pThemeManager, _("Themes have been deleted"));
163
 
                break;
164
 
        }
165
 
        g_key_file_free (pKeyFile);
166
 
        
167
 
        return !bReloadWindow;
168
 
}
169
 
 
170
 
void cairo_dock_manage_themes (void)
171
 
{
172
 
        if (s_pThemeManager != NULL)
173
 
        {
174
 
                gtk_window_present (GTK_WINDOW (s_pThemeManager));
175
 
                return ;
176
 
        }
177
 
        
178
 
        gchar *cInitConfFile = cairo_dock_build_temporary_themes_conf_file ();  // sera supprime a la destruction de la fenetre.
179
 
        
180
 
        //\___________________ On laisse l'utilisateur l'editer.
181
 
        const gchar *cPresentedGroup = (cairo_dock_current_theme_need_save () ? "Save" : NULL);
182
 
        const gchar *cTitle = _("Manage Themes");
183
 
        
184
 
        s_pThemeManager = cairo_dock_build_generic_gui_full (cInitConfFile,
185
 
                NULL, cTitle,
186
 
                CAIRO_DOCK_THEME_PANEL_WIDTH, CAIRO_DOCK_THEME_PANEL_HEIGHT,
187
 
                (CairoDockApplyConfigFunc) on_theme_apply,
188
 
                cInitConfFile,
189
 
                (GFreeFunc) on_theme_destroy,
190
 
                _make_widgets,
191
 
                NULL);
192
 
}