~cairo-dock-team/ubuntu/oneiric/cairo-dock/2.3.0-3

« back to all changes in this revision

Viewing changes to src/gldit/cairo-dock-themes-manager.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthieu Baerts (matttbe)
  • Date: 2010-08-09 23:26:12 UTC
  • mto: This revision was merged to the branch mainline in revision 13.
  • Revision ID: james.westby@ubuntu.com-20100809232612-pocdxliaxjdetm37
Tags: upstream-2.2.0~0beta4
ImportĀ upstreamĀ versionĀ 2.2.0~0beta4

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-config.h"
 
32
#include "cairo-dock-keyfile-utilities.h"
 
33
#include "cairo-dock-dock-manager.h"
 
34
#include "cairo-dock-modules.h"
 
35
#include "cairo-dock-backends-manager.h"
 
36
#include "cairo-dock-dialog-manager.h"
 
37
#include "cairo-dock-gui-manager.h"
 
38
#include "cairo-dock-task.h"
 
39
#include "cairo-dock-log.h"
 
40
#include "cairo-dock-packages.h"
 
41
#include "cairo-dock-themes-manager.h"
 
42
 
 
43
#define CAIRO_DOCK_MODIFIED_THEME_FILE ".cairo-dock-need-save"
 
44
#define CAIRO_DOCK_DISTANT_THEMES_DIR "themes2.2"
 
45
 
 
46
gchar *g_cCairoDockDataDir = NULL;  // le repertoire racine contenant tout.
 
47
gchar *g_cCurrentThemePath = NULL;  // le chemin vers le repertoire du theme courant.
 
48
gchar *g_cExtrasDirPath = NULL;  // le chemin vers le repertoire des extra.
 
49
gchar *g_cThemesDirPath = NULL;  // le chemin vers le repertoire des themes.
 
50
gchar *g_cCurrentLaunchersPath = NULL;  // le chemin vers le repertoire des lanceurs du theme courant.
 
51
gchar *g_cCurrentIconsPath = NULL;  // le chemin vers le repertoire des icones du theme courant.
 
52
gchar *g_cCurrentPlugInsPath = NULL;  // le chemin vers le repertoire des plug-ins du theme courant.
 
53
gchar *g_cConfFile = NULL;  // le chemin du fichier de conf.
 
54
int g_iMajorVersion, g_iMinorVersion, g_iMicroVersion;  // version de la lib.
 
55
 
 
56
extern CairoDock *g_pMainDock;
 
57
 
 
58
#define CAIRO_DOCK_LOCAL_EXTRAS_DIR "extras"
 
59
#define CAIRO_DOCK_LAUNCHERS_DIR "launchers"
 
60
#define CAIRO_DOCK_PLUG_INS_DIR "plug-ins"
 
61
#define CAIRO_DOCK_LOCAL_ICONS_DIR "icons"
 
62
 
 
63
void cairo_dock_mark_current_theme_as_modified (gboolean bModified)
 
64
{
 
65
        gchar *cModifiedFile = g_strdup_printf ("%s/%s", g_cCairoDockDataDir, CAIRO_DOCK_MODIFIED_THEME_FILE);
 
66
 
 
67
        g_file_set_contents (cModifiedFile,
 
68
                (bModified ? "1" : "0"),
 
69
                -1,
 
70
                NULL);
 
71
 
 
72
        g_free (cModifiedFile);
 
73
}
 
74
 
 
75
gboolean cairo_dock_current_theme_need_save (void)
 
76
{
 
77
        gchar *cModifiedFile = g_strdup_printf ("%s/%s", g_cCairoDockDataDir, CAIRO_DOCK_MODIFIED_THEME_FILE);
 
78
        gsize length = 0;
 
79
        gchar *cContent = NULL;
 
80
        g_file_get_contents (cModifiedFile,
 
81
                &cContent,
 
82
                &length,
 
83
                NULL);
 
84
        g_free (cModifiedFile);
 
85
        gboolean bNeedSave;
 
86
        if (length > 0)
 
87
                bNeedSave = (*cContent == '1');
 
88
        else
 
89
                bNeedSave = FALSE;
 
90
        g_free (cContent);
 
91
        return bNeedSave;
 
92
}
 
93
 
 
94
 
 
95
gboolean cairo_dock_export_current_theme (const gchar *cNewThemeName, gboolean bSaveBehavior, gboolean bSaveLaunchers)
 
96
{
 
97
        g_return_val_if_fail (cNewThemeName != NULL, FALSE);
 
98
        
 
99
        cairo_dock_extract_package_type_from_name (cNewThemeName);
 
100
        
 
101
        cd_message ("on sauvegarde dans %s", cNewThemeName);
 
102
        GString *sCommand = g_string_new ("");
 
103
        gboolean bThemeSaved = FALSE;
 
104
        int r;
 
105
        gchar *cNewThemePath = g_strdup_printf ("%s/%s", g_cThemesDirPath, cNewThemeName);
 
106
        if (g_file_test (cNewThemePath, G_FILE_TEST_EXISTS))  // on ecrase un theme existant.
 
107
        {
 
108
                cd_debug ("  le theme existant sera mis a jour");
 
109
                gchar *cQuestion = g_strdup_printf (_("Are you sure you want to overwrite theme %s?"), cNewThemeName);
 
110
                int answer = cairo_dock_ask_general_question_and_wait (cQuestion);
 
111
                g_free (cQuestion);
 
112
                if (answer == GTK_RESPONSE_YES)
 
113
                {
 
114
                        //\___________________ On traite le fichier de conf.
 
115
                        gchar *cNewConfFilePath = g_strdup_printf ("%s/%s", cNewThemePath, CAIRO_DOCK_CONF_FILE);
 
116
                        if (bSaveBehavior)
 
117
                        {
 
118
                                g_string_printf (sCommand, "/bin/cp \"%s\" \"%s\"", g_cConfFile, cNewConfFilePath);
 
119
                                cd_message ("%s", sCommand->str);
 
120
                                r = system (sCommand->str);
 
121
                        }
 
122
                        else
 
123
                        {
 
124
                                cairo_dock_replace_keys_by_identifier (cNewConfFilePath, g_cConfFile, '+');
 
125
                        }
 
126
                        g_free (cNewConfFilePath);
 
127
                        
 
128
                        //\___________________ On traite les lanceurs.
 
129
                        if (bSaveLaunchers)
 
130
                        {
 
131
                                g_string_printf (sCommand, "rm -f \"%s/%s\"/*", cNewThemePath, CAIRO_DOCK_LAUNCHERS_DIR);
 
132
                                cd_message ("%s", sCommand->str);
 
133
                                r = system (sCommand->str);
 
134
                                
 
135
                                g_string_printf (sCommand, "cp \"%s\"/* \"%s/%s\"", g_cCurrentLaunchersPath, cNewThemePath, CAIRO_DOCK_LAUNCHERS_DIR);
 
136
                                cd_message ("%s", sCommand->str);
 
137
                                r = system (sCommand->str);
 
138
                        }
 
139
                        
 
140
                        //\___________________ On traite tous le reste.
 
141
                        /// TODO : traiter les .conf des applets comme celui du dock...
 
142
                        g_string_printf (sCommand, "find \"%s\" -mindepth 1 -maxdepth 1  ! -name '*.conf' ! -name \"%s\" -exec /bin/cp -r '{}' \"%s\" \\;", g_cCurrentThemePath, CAIRO_DOCK_LAUNCHERS_DIR, cNewThemePath);
 
143
                        cd_message ("%s", sCommand->str);
 
144
                        r = system (sCommand->str);
 
145
 
 
146
                        bThemeSaved = TRUE;
 
147
                }
 
148
        }
 
149
        else  // sinon on sauvegarde le repertoire courant tout simplement.
 
150
        {
 
151
                cd_debug ("  creation du nouveau theme (%s)", cNewThemePath);
 
152
 
 
153
                if (g_mkdir (cNewThemePath, 7*8*8+7*8+5) == 0)
 
154
                {
 
155
                        g_string_printf (sCommand, "cp -r \"%s\"/* \"%s\"", g_cCurrentThemePath, cNewThemePath);
 
156
                        cd_message ("%s", sCommand->str);
 
157
                        r = system (sCommand->str);
 
158
 
 
159
                        bThemeSaved = TRUE;
 
160
                }
 
161
                else
 
162
                        cd_warning ("couldn't create %s", cNewThemePath);
 
163
        }
 
164
        
 
165
        g_free (cNewThemePath);
 
166
        if (bThemeSaved)
 
167
        {
 
168
                cairo_dock_mark_current_theme_as_modified (FALSE);
 
169
        }
 
170
        
 
171
        g_string_free (sCommand, TRUE);
 
172
        
 
173
        return bThemeSaved;
 
174
}
 
175
 
 
176
gboolean cairo_dock_package_current_theme (const gchar *cThemeName)
 
177
{
 
178
        g_return_val_if_fail (cThemeName != NULL, FALSE);
 
179
        
 
180
        cairo_dock_extract_package_type_from_name (cThemeName);
 
181
        
 
182
        cd_message ("building theme package ...");
 
183
        int r;
 
184
        if (g_file_test (CAIRO_DOCK_SHARE_DATA_DIR"/../../bin/cairo-dock-package-theme", G_FILE_TEST_EXISTS))
 
185
        {
 
186
                gchar *cCommand;
 
187
                const gchar *cTerm = g_getenv ("TERM");
 
188
                if (cTerm == NULL || *cTerm == '\0')
 
189
                        cCommand = g_strdup_printf ("xterm -e %s \"%s\"", "cairo-dock-package-theme", cThemeName);
 
190
                else
 
191
                        cCommand = g_strdup_printf ("$TERM -e '%s \"%s\"'", "cairo-dock-package-theme", cThemeName);
 
192
                r = system (cCommand);
 
193
                g_free (cCommand);
 
194
                return TRUE;
 
195
        }
 
196
        else
 
197
        {
 
198
                cd_warning ("the package builder script was not found !");
 
199
                return FALSE;
 
200
        }
 
201
}
 
202
gchar *cairo_dock_depackage_theme (const gchar *cPackagePath)
 
203
{
 
204
        gchar *cNewThemePath = NULL;
 
205
        if (*cPackagePath == '/' || strncmp (cPackagePath, "file://", 7) == 0)  // paquet en local.
 
206
        {
 
207
                cd_debug (" paquet local\n");
 
208
                gchar *cFilePath = (*cPackagePath == '/' ? g_strdup (cPackagePath) : g_filename_from_uri (cPackagePath, NULL, NULL));
 
209
                cNewThemePath = cairo_dock_uncompress_file (cFilePath, g_cThemesDirPath, NULL);
 
210
                g_free (cFilePath);
 
211
        }
 
212
        else  // paquet distant.
 
213
        {
 
214
                cd_debug (" paquet distant\n");
 
215
                gchar *str = strrchr (cPackagePath, '/');
 
216
                if (str != NULL)
 
217
                {
 
218
                        *str = '\0';
 
219
                        cNewThemePath = cairo_dock_download_file (cPackagePath, "", str+1, g_cThemesDirPath, NULL);
 
220
                        if (cNewThemePath == NULL)
 
221
                        {
 
222
                                cairo_dock_show_temporary_dialog_with_icon_printf (_("Could not access remote file %s/%s. Maybe the server is down.\nPlease retry later or contact us at glx-dock.org."), NULL, NULL, 0, NULL, cPackagePath, str+1);
 
223
                        }
 
224
                }
 
225
        }
 
226
        return cNewThemePath;
 
227
}
 
228
 
 
229
gboolean cairo_dock_delete_themes (gchar **cThemesList)
 
230
{
 
231
        g_return_val_if_fail (cThemesList != NULL && cThemesList[0] != NULL, FALSE);
 
232
        
 
233
        GString *sCommand = g_string_new ("");
 
234
        gboolean bThemeDeleted = FALSE;
 
235
        
 
236
        if (cThemesList[1] == NULL)
 
237
                g_string_printf (sCommand, _("Are you sure you want to delete theme %s?"), cThemesList[0]);
 
238
        else
 
239
                g_string_printf (sCommand, _("Are you sure you want to delete these themes?"));
 
240
        int answer = cairo_dock_ask_general_question_and_wait (sCommand->str);
 
241
        if (answer == GTK_RESPONSE_YES)
 
242
        {
 
243
                gchar *cThemeName;
 
244
                int i, r;
 
245
                for (i = 0; cThemesList[i] != NULL; i ++)
 
246
                {
 
247
                        cThemeName = cThemesList[i];
 
248
                        if (*cThemeName == '\0')
 
249
                                continue;
 
250
                        cairo_dock_extract_package_type_from_name (cThemeName);
 
251
                        
 
252
                        bThemeDeleted = TRUE;
 
253
                        g_string_printf (sCommand, "rm -rf \"%s/%s/%s\"", g_cCairoDockDataDir, CAIRO_DOCK_THEMES_DIR, cThemeName);
 
254
                        r = system (sCommand->str);  // g_rmdir n'efface qu'un repertoire vide.
 
255
                }
 
256
        }
 
257
        
 
258
        g_string_free (sCommand, TRUE);
 
259
        return bThemeDeleted;
 
260
}
 
261
 
 
262
static gboolean _find_module_from_user_data_dir (gchar *cModuleName, CairoDockModule *pModule, const gchar *cUserDataDirName)
 
263
{
 
264
        if (pModule->pVisitCard->cUserDataDir && strcmp (cUserDataDirName, pModule->pVisitCard->cUserDataDir) == 0)
 
265
                return TRUE;
 
266
        return FALSE;
 
267
}
 
268
gboolean cairo_dock_import_theme (const gchar *cThemeName, gboolean bLoadBehavior, gboolean bLoadLaunchers)
 
269
{
 
270
        //\___________________ On obtient le chemin du nouveau theme (telecharge ou decompresse si necessaire).
 
271
        gchar *cNewThemeName = g_strdup (cThemeName);
 
272
        gchar *cNewThemePath = NULL;
 
273
        
 
274
        int length = strlen (cNewThemeName);
 
275
        if (cNewThemeName[length-1] == '\n')
 
276
                cNewThemeName[--length] = '\0';  // on vire le retour chariot final.
 
277
        if (cNewThemeName[length-1] == '\r')
 
278
                cNewThemeName[--length] = '\0';
 
279
        cd_debug ("cNewThemeName : '%s'", cNewThemeName);
 
280
        
 
281
        if (g_str_has_suffix (cNewThemeName, ".tar.gz") || g_str_has_suffix (cNewThemeName, ".tar.bz2") || g_str_has_suffix (cNewThemeName, ".tgz"))  // c'est un paquet.
 
282
        {
 
283
                cd_debug ("c'est un paquet");
 
284
                cNewThemePath = cairo_dock_depackage_theme (cNewThemeName);
 
285
                
 
286
                g_return_val_if_fail (cNewThemePath != NULL, FALSE);
 
287
                gchar *tmp = cNewThemeName;
 
288
                cNewThemeName = g_path_get_basename (cNewThemePath);
 
289
                g_free (tmp);
 
290
        }
 
291
        else  // c'est un theme officiel.
 
292
        {
 
293
                cd_debug ("c'est un theme officiel");
 
294
                cNewThemePath = cairo_dock_get_package_path (cNewThemeName, CAIRO_DOCK_SHARE_THEMES_DIR, g_cThemesDirPath, CAIRO_DOCK_DISTANT_THEMES_DIR, CAIRO_DOCK_ANY_PACKAGE);
 
295
        }
 
296
        g_return_val_if_fail (cNewThemePath != NULL && g_file_test (cNewThemePath, G_FILE_TEST_EXISTS), FALSE);
 
297
        //g_print ("cNewThemePath : %s ; cNewThemeName : %s\n", cNewThemePath, cNewThemeName);
 
298
        
 
299
        //\___________________ On charge les parametres de comportement.
 
300
        GString *sCommand = g_string_new ("");
 
301
        int r;
 
302
        cd_message ("Applying changes ...");
 
303
        if (g_pMainDock == NULL || bLoadBehavior)
 
304
        {
 
305
                g_string_printf (sCommand, "/bin/cp \"%s\"/%s \"%s\"", cNewThemePath, CAIRO_DOCK_CONF_FILE, g_cCurrentThemePath);
 
306
                cd_message ("%s", sCommand->str);
 
307
                r = system (sCommand->str);
 
308
        }
 
309
        else
 
310
        {
 
311
                gchar *cNewConfFilePath = g_strdup_printf ("%s/%s", cNewThemePath, CAIRO_DOCK_CONF_FILE);
 
312
                cairo_dock_replace_keys_by_identifier (g_cConfFile, cNewConfFilePath, '+');
 
313
                g_free (cNewConfFilePath);
 
314
        }
 
315
        
 
316
        //\___________________ On charge les .conf des autres docks racines.
 
317
        g_string_printf (sCommand, "find \"%s\" -mindepth 1 -maxdepth 1 -name '*.conf' ! -name '%s' -exec /bin/cp '{}' \"%s\" \\;", cNewThemePath, CAIRO_DOCK_CONF_FILE, g_cCurrentThemePath);
 
318
        cd_debug ("%s", sCommand->str);
 
319
        r = system (sCommand->str);
 
320
        
 
321
        //\___________________ On charge les lanceurs.
 
322
        if (bLoadLaunchers)
 
323
        {
 
324
                g_string_printf (sCommand, "rm -f \"%s\"/*", g_cCurrentIconsPath);
 
325
                cd_debug ("%s", sCommand->str);
 
326
                r = system (sCommand->str);
 
327
                g_string_printf (sCommand, "rm -f \"%s\"/.*", g_cCurrentIconsPath);
 
328
                cd_debug ("%s", sCommand->str);
 
329
                r = system (sCommand->str);
 
330
        }
 
331
        
 
332
        //\___________________ On charge les icones.
 
333
        gchar *cNewLocalIconsPath = g_strdup_printf ("%s/%s", cNewThemePath, CAIRO_DOCK_LOCAL_ICONS_DIR);
 
334
        if (! g_file_test (cNewLocalIconsPath, G_FILE_TEST_IS_DIR))  // c'est un ancien theme, on deplace les icones vers le repertoire 'icons'.
 
335
        {
 
336
                g_string_printf (sCommand, "find \"%s/%s\" -mindepth 1 ! -name '*.desktop' -exec /bin/cp '{}' '%s' \\;", cNewThemePath, CAIRO_DOCK_LAUNCHERS_DIR, g_cCurrentIconsPath);
 
337
        }
 
338
        else
 
339
        {
 
340
                g_string_printf (sCommand, "for f in \"%s\"/* ; do rm -f \"%s/`basename \"${f%%.*}\"`\"*; done;", cNewLocalIconsPath, g_cCurrentIconsPath);  // on efface les doublons car sinon on pourrait avoir x.png et x.svg ensemble et le dock ne saurait pas lequel choisir.
 
341
                cd_debug ("%s", sCommand->str);
 
342
                r = system (sCommand->str);
 
343
                
 
344
                g_string_printf (sCommand, "cp \"%s\"/* \"%s\"", cNewLocalIconsPath, g_cCurrentIconsPath);
 
345
        }
 
346
        cd_debug ("%s", sCommand->str);
 
347
        r = system (sCommand->str);
 
348
        g_free (cNewLocalIconsPath);
 
349
        
 
350
        //\___________________ On charge les extras.
 
351
        g_string_printf (sCommand, "%s/%s", cNewThemePath, CAIRO_DOCK_LOCAL_EXTRAS_DIR);
 
352
        if (g_file_test (sCommand->str, G_FILE_TEST_IS_DIR))
 
353
        {
 
354
                g_string_printf (sCommand, "cp -r \"%s/%s\"/* \"%s\"", cNewThemePath, CAIRO_DOCK_LOCAL_EXTRAS_DIR, g_cExtrasDirPath);
 
355
                cd_debug ("%s", sCommand->str);
 
356
                r = system (sCommand->str);
 
357
        }
 
358
        
 
359
        //\___________________ On charge les lanceurs si necessaire, en effacant ceux existants.
 
360
        if (! g_file_test (g_cCurrentLaunchersPath, G_FILE_TEST_EXISTS))
 
361
        {
 
362
                gchar *command = g_strdup_printf ("mkdir -p \"%s\"", g_cCurrentLaunchersPath);
 
363
                r = system (command);
 
364
                g_free (command);
 
365
        }
 
366
        if (g_pMainDock == NULL || bLoadLaunchers)
 
367
        {
 
368
                g_string_printf (sCommand, "rm -f \"%s\"/*.desktop", g_cCurrentLaunchersPath);
 
369
                cd_debug ("%s", sCommand->str);
 
370
                r = system (sCommand->str);
 
371
 
 
372
                g_string_printf (sCommand, "cp \"%s/%s\"/*.desktop \"%s\"", cNewThemePath, CAIRO_DOCK_LAUNCHERS_DIR, g_cCurrentLaunchersPath);
 
373
                cd_debug ("%s", sCommand->str);
 
374
                r = system (sCommand->str);
 
375
        }
 
376
        
 
377
        //\___________________ On remplace tous les autres fichiers par les nouveaux.
 
378
        g_string_printf (sCommand, "find \"%s\" -mindepth 1 -maxdepth 1  ! -name '*.conf' -type f -exec rm -f '{}' \\;", g_cCurrentThemePath);  // efface tous les fichiers du theme mais sans toucher aux lanceurs et aux plug-ins.
 
379
        cd_debug ("%s", sCommand->str);
 
380
        r = system (sCommand->str);
 
381
 
 
382
        if (g_pMainDock == NULL || bLoadBehavior)
 
383
        {
 
384
                g_string_printf (sCommand, "find \"%s\"/* -prune ! -name '*.conf' ! -name %s -exec /bin/cp -r '{}' \"%s\" \\;", cNewThemePath, CAIRO_DOCK_LAUNCHERS_DIR, g_cCurrentThemePath);  // copie tous les fichiers du nouveau theme sauf les lanceurs et le .conf, dans le repertoire du theme courant. Ecrase les fichiers de memes noms.
 
385
                cd_debug ("%s", sCommand->str);
 
386
                r = system (sCommand->str);
 
387
        }
 
388
        else
 
389
        {
 
390
                // on copie tous les fichiers du nouveau theme sauf les lanceurs et les .conf (dock et plug-ins).
 
391
                g_string_printf (sCommand, "find \"%s\" -mindepth 1 ! -name '*.conf' ! -path '%s/%s*' ! -type d -exec cp -p {} \"%s\" \\;", cNewThemePath, cNewThemePath, CAIRO_DOCK_LAUNCHERS_DIR, g_cCurrentThemePath);
 
392
                cd_debug ("%s", sCommand->str);
 
393
                r = system (sCommand->str);
 
394
                
 
395
                // on parcours les .conf des plug-ins, on les met a jour, et on fusionne avec le theme courant.
 
396
                gchar *cNewPlugInsDir = g_strdup_printf ("%s/%s", cNewThemePath, CAIRO_DOCK_PLUG_INS_DIR);  // repertoire des plug-ins du nouveau theme.
 
397
                GDir *dir = g_dir_open (cNewPlugInsDir, 0, NULL);  // NULL si ce theme n'a pas de repertoire 'plug-ins'.
 
398
                const gchar* cModuleDirName;
 
399
                gchar *cConfFilePath, *cNewConfFilePath, *cUserDataDirPath, *cConfFileName;
 
400
                do
 
401
                {
 
402
                        cModuleDirName = g_dir_read_name (dir);  // nom du repertoire du theme (pas forcement egal au nom du theme)
 
403
                        if (cModuleDirName == NULL)
 
404
                                break ;
 
405
                        
 
406
                        // on cree le repertoire du plug-in dans le theme courant.
 
407
                        cd_debug ("  installing %s's config", cModuleDirName);
 
408
                        cUserDataDirPath = g_strdup_printf ("%s/%s", g_cCurrentPlugInsPath, cModuleDirName);  // repertoire du plug-in dans le theme courant.
 
409
                        if (! g_file_test (cUserDataDirPath, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
 
410
                        {
 
411
                                cd_debug ("    directory %s doesn't exist, it will be created.", cUserDataDirPath);
 
412
                                
 
413
                                g_string_printf (sCommand, "mkdir -p \"%s\"", cUserDataDirPath);
 
414
                                r = system (sCommand->str);
 
415
                        }
 
416
                        
 
417
                        // on recupere le nom et le chemin du .conf du plug-in dans le nouveau theme.
 
418
                        cConfFileName = g_strdup_printf ("%s.conf", cModuleDirName);
 
419
                        cNewConfFilePath = g_strdup_printf ("%s/%s/%s", cNewPlugInsDir, cModuleDirName, cConfFileName);
 
420
                        if (! g_file_test (cNewConfFilePath, G_FILE_TEST_EXISTS))
 
421
                        {
 
422
                                g_free (cConfFileName);
 
423
                                g_free (cNewConfFilePath);
 
424
                                CairoDockModule *pModule = cairo_dock_foreach_module ((GHRFunc) _find_module_from_user_data_dir, (gpointer) cModuleDirName);
 
425
                                if (pModule == NULL)  // du coup, dans ce cas-la, on ne charge pas des plug-ins non utilises par l'utilisateur.
 
426
                                {
 
427
                                        cd_warning ("couldn't find the module owning '%s', this file will be ignored.");
 
428
                                        continue;
 
429
                                }
 
430
                                cConfFileName = g_strdup (pModule->pVisitCard->cConfFileName);
 
431
                                cNewConfFilePath = g_strdup_printf ("%s/%s/%s", cNewPlugInsDir, cModuleDirName, cConfFileName);
 
432
                        }
 
433
                        cConfFilePath = g_strdup_printf ("%s/%s", cUserDataDirPath, cConfFileName);  // chemin du .conf dans le theme courant.
 
434
                        
 
435
                        // on fusionne les 2 .conf.
 
436
                        if (! g_file_test (cConfFilePath, G_FILE_TEST_EXISTS))
 
437
                        {
 
438
                                cd_debug ("    no conf file %s, we will take the theme's one", cConfFilePath);
 
439
                                g_string_printf (sCommand, "cp \"%s\" \"%s\"", cNewConfFilePath, cConfFilePath);
 
440
                                r = system (sCommand->str);
 
441
                        }
 
442
                        else
 
443
                        {
 
444
                                cairo_dock_replace_keys_by_identifier (cConfFilePath, cNewConfFilePath, '+');
 
445
                        }
 
446
                        g_free (cNewConfFilePath);
 
447
                        g_free (cConfFilePath);
 
448
                        g_free (cUserDataDirPath);
 
449
                        g_free (cConfFileName);
 
450
                }
 
451
                while (1);
 
452
                g_dir_close (dir);
 
453
                g_free (cNewPlugInsDir);
 
454
        }
 
455
        
 
456
        // precaution probablement inutile.
 
457
        g_string_printf (sCommand, "chmod -R 777 \"%s\"", g_cCurrentThemePath);
 
458
        r = system (sCommand->str);
 
459
        
 
460
        g_string_free (sCommand, TRUE);
 
461
        return TRUE;
 
462
}
 
463
 
 
464
 
 
465
static void _import_theme (gpointer *pSharedMemory)
 
466
{
 
467
        pSharedMemory[5] = GINT_TO_POINTER (cairo_dock_import_theme (pSharedMemory[0], GPOINTER_TO_INT (pSharedMemory[1]), GPOINTER_TO_INT (pSharedMemory[2])));
 
468
}
 
469
static gboolean _finish_import (gpointer *pSharedMemory)
 
470
{
 
471
        if (! pSharedMemory[5])
 
472
                cd_warning ("Couldn't import the theme %s.", pSharedMemory[0]);
 
473
        
 
474
        GFunc pCallback = pSharedMemory[3];
 
475
        pCallback (pSharedMemory[5], pSharedMemory[4]);
 
476
        return FALSE;
 
477
}
 
478
static void _discard_import (gpointer *pSharedMemory)
 
479
{
 
480
        g_free (pSharedMemory[0]);
 
481
        g_free (pSharedMemory);
 
482
}
 
483
CairoDockTask *cairo_dock_import_theme_async (const gchar *cThemeName, gboolean bLoadBehavior, gboolean bLoadLaunchers, GFunc pCallback, gpointer data)
 
484
{
 
485
        gpointer *pSharedMemory = g_new0 (gpointer, 6);
 
486
        pSharedMemory[0] = g_strdup (cThemeName);
 
487
        pSharedMemory[1] = GINT_TO_POINTER (bLoadBehavior);
 
488
        pSharedMemory[2] = GINT_TO_POINTER (bLoadLaunchers);
 
489
        pSharedMemory[3] = pCallback;
 
490
        pSharedMemory[4] = data;
 
491
        CairoDockTask *pTask = cairo_dock_new_task_full (0, (CairoDockGetDataAsyncFunc) _import_theme, (CairoDockUpdateSyncFunc) _finish_import, (GFreeFunc) _discard_import, pSharedMemory);
 
492
        cairo_dock_launch_task (pTask);
 
493
        return pTask;
 
494
}
 
495
 
 
496
 
 
497
void cairo_dock_load_current_theme (void)
 
498
{
 
499
        cd_message ("%s ()", __func__);
 
500
 
 
501
        //\___________________ On libere toute la memoire allouee par tout le monde (stoppe aussi tous les timeout).
 
502
        cairo_dock_free_all ();
 
503
 
 
504
        //\___________________ On cree le dock principal.
 
505
        CairoDock *pMainDock = cairo_dock_create_dock (CAIRO_DOCK_MAIN_DOCK_NAME, NULL);  // on ne lui assigne pas de vues, puisque la vue par defaut des docks principaux sera definie plus tard.
 
506
 
 
507
        //\___________________ On lit son fichier de conf et on charge tout.
 
508
        cairo_dock_load_config (g_cConfFile, pMainDock);  // chargera des valeurs par defaut si le fichier de conf fourni est incorrect.
 
509
}
 
510
 
 
511
 
 
512
#define _check_dir(cDirPath) \
 
513
        if (! g_file_test (cDirPath, G_FILE_TEST_IS_DIR)) {\
 
514
                if (g_mkdir (cDirPath, 7*8*8+7*8+7) != 0) {\
 
515
                        cd_warning ("couldn't create directory %s", cDirPath);\
 
516
                        g_free (cDirPath);\
 
517
                        cDirPath = NULL; } }
 
518
 
 
519
void cairo_dock_set_paths (gchar *cRootDataDirPath, gchar *cExtraDirPath, gchar *cThemesDirPath, gchar *cCurrentThemeDirPath, gchar *cThemeServerAdress)
 
520
{
 
521
        //\___________________ On initialise les chemins de l'appli.
 
522
        g_cCairoDockDataDir = cRootDataDirPath;  // le repertoire racine contenant tout.
 
523
        _check_dir (g_cCairoDockDataDir);
 
524
        g_cCurrentThemePath = cCurrentThemeDirPath;  // le chemin vers le repertoire du theme courant.
 
525
        _check_dir (g_cCurrentThemePath);
 
526
        g_cExtrasDirPath = cExtraDirPath;  // le chemin vers le repertoire des extra.
 
527
        _check_dir (g_cExtrasDirPath);
 
528
        g_cThemesDirPath = cThemesDirPath;  // le chemin vers le repertoire des themes.
 
529
        _check_dir (g_cThemesDirPath);
 
530
        
 
531
        g_cCurrentLaunchersPath = g_strdup_printf ("%s/%s", g_cCurrentThemePath, CAIRO_DOCK_LAUNCHERS_DIR);
 
532
        _check_dir (g_cCurrentLaunchersPath);
 
533
        g_cCurrentIconsPath = g_strdup_printf ("%s/%s", g_cCurrentThemePath, CAIRO_DOCK_LOCAL_ICONS_DIR);
 
534
        _check_dir (g_cCurrentIconsPath);
 
535
        g_cCurrentPlugInsPath = g_strdup_printf ("%s/%s", g_cCurrentThemePath, CAIRO_DOCK_PLUG_INS_DIR);
 
536
        _check_dir (g_cCurrentPlugInsPath);
 
537
        g_cConfFile = g_strdup_printf ("%s/%s", g_cCurrentThemePath, CAIRO_DOCK_CONF_FILE);
 
538
        
 
539
        //\___________________ On initialise le gestionnaire de paquets.
 
540
        cairo_dock_init_package_manager (cThemeServerAdress);
 
541
        
 
542
        //\___________________ On initialise les numeros de version.
 
543
        cairo_dock_get_version_from_string (GLDI_VERSION, &g_iMajorVersion, &g_iMinorVersion, &g_iMicroVersion);
 
544
}