2
* This file is a part of the Cairo-Dock project
4
* Copyright : (C) see the 'copyright' file.
5
* E-mail : see the 'copyright' file.
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.
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/>.
26
#include "gldi-config.h"
27
#include "cairo-dock-icon-factory.h"
28
#include "cairo-dock-themes-manager.h" // cairo_dock_update_conf_file
29
#include "cairo-dock-keyfile-utilities.h"
30
#include "cairo-dock-config.h"
31
#include "cairo-dock-log.h"
32
#include "cairo-dock-module-instance-manager.h"
33
#include "cairo-dock-desktop-file-factory.h"
35
#define CAIRO_DOCK_LAUNCHER_CONF_FILE "launcher.desktop"
36
#define CAIRO_DOCK_CONTAINER_CONF_FILE "container.desktop"
37
#define CAIRO_DOCK_SEPARATOR_CONF_FILE "separator.desktop"
39
extern gchar *g_cCurrentThemePath;
40
extern gchar *g_cCurrentLaunchersPath;
43
static gchar *_cairo_dock_generate_desktop_filename (const gchar *cBaseName, gchar *cCairoDockDataDir)
45
int iPrefixNumber = 0;
46
GString *sFileName = g_string_new ("");
51
g_string_printf (sFileName, "%s/%02d%s", cCairoDockDataDir, iPrefixNumber, cBaseName);
52
} while (iPrefixNumber < 99 && g_file_test (sFileName->str, G_FILE_TEST_EXISTS));
54
g_string_free (sFileName, TRUE);
55
if (iPrefixNumber == 99)
58
return g_strdup_printf ("%02d%s", iPrefixNumber, cBaseName);
61
static inline const gchar *_cairo_dock_get_launcher_template_conf_file_path (CairoDockDesktopFileType iNewDesktopFileType)
63
const gchar *cTemplateFile;
64
switch (iNewDesktopFileType)
66
case CAIRO_DOCK_DESKTOP_FILE_FOR_LAUNCHER :
67
cTemplateFile = GLDI_SHARE_DATA_DIR"/"CAIRO_DOCK_LAUNCHER_CONF_FILE;
69
case CAIRO_DOCK_DESKTOP_FILE_FOR_CONTAINER :
70
cTemplateFile = GLDI_SHARE_DATA_DIR"/"CAIRO_DOCK_CONTAINER_CONF_FILE;
72
case CAIRO_DOCK_DESKTOP_FILE_FOR_SEPARATOR :
73
cTemplateFile = GLDI_SHARE_DATA_DIR"/"CAIRO_DOCK_SEPARATOR_CONF_FILE;
81
static gchar *_add_new_desktop_file (CairoDockDesktopFileType iLauncherType, const gchar *cOrigin, const gchar *cDockName, double fOrder, G_GNUC_UNUSED GError **erreur)
83
//\__________________ open the template.
84
const gchar *cTemplateFile = _cairo_dock_get_launcher_template_conf_file_path (iLauncherType);
85
g_return_val_if_fail (cTemplateFile != NULL, NULL);
87
GKeyFile *pKeyFile = cairo_dock_open_key_file (cTemplateFile);
88
g_return_val_if_fail (pKeyFile != NULL, NULL);
90
//\__________________ fill the parameters
91
gchar *cFilePath = NULL;
92
if (cOrigin != NULL && *cOrigin != '/') // transform the origin URI into a path or a file name.
94
if (strncmp (cOrigin, "application://", 14) == 0) // Ubuntu >= 11.04: it's now an "app" URI
95
cFilePath = g_strdup (cOrigin + 14); // in this case we don't have the actual path of the .desktop, but that doesn't matter.
97
cFilePath = g_filename_from_uri (cOrigin, NULL, NULL);
99
else // no origin or already a path.
100
cFilePath = g_strdup (cOrigin);
101
g_key_file_set_string (pKeyFile, "Desktop Entry", "Origin", cFilePath?cFilePath:"");
103
g_key_file_set_double (pKeyFile, "Desktop Entry", "Order", fOrder);
105
g_key_file_set_string (pKeyFile, "Desktop Entry", "Container", cDockName);
107
//\__________________ in the case of a script, set ourselves a valid name and command.
108
if (cFilePath != NULL && g_str_has_suffix (cFilePath, ".sh"))
110
gchar *cName = g_path_get_basename (cFilePath);
111
g_key_file_set_string (pKeyFile, "Desktop Entry", "Name", cName);
113
g_key_file_set_string (pKeyFile, "Desktop Entry", "Exec", cFilePath);
114
g_key_file_set_boolean (pKeyFile, "Desktop Entry", "Terminal", TRUE);
117
//\__________________ in the case of a custom launcher, set a command (the launcher would be invalid without).
118
if (cFilePath == NULL && iLauncherType == CAIRO_DOCK_DESKTOP_FILE_FOR_LAUNCHER)
120
g_key_file_set_string (pKeyFile, "Desktop Entry", "Exec", _("Enter a command"));
121
g_key_file_set_string (pKeyFile, "Desktop Entry", "Name", _("New launcher"));
124
//\__________________ generate a unique and readable filename.
125
gchar *cBaseName = (cFilePath ?
127
g_path_get_basename (cFilePath) :
128
g_strdup (cFilePath) :
129
g_path_get_basename (cTemplateFile));
131
if (! g_str_has_suffix (cBaseName, ".desktop")) // if we have a script (.sh file) => add '.desktop'
133
gchar *cTmpBaseName = g_strdup_printf ("%s.desktop", cBaseName);
135
cBaseName = cTmpBaseName;
138
gchar *cNewDesktopFileName = _cairo_dock_generate_desktop_filename (cBaseName, g_cCurrentLaunchersPath);
141
//\__________________ write the keys.
142
gchar *cNewDesktopFilePath = g_strdup_printf ("%s/%s", g_cCurrentLaunchersPath, cNewDesktopFileName);
143
cairo_dock_write_keys_to_conf_file (pKeyFile, cNewDesktopFilePath);
144
g_free (cNewDesktopFilePath);
147
g_key_file_free (pKeyFile);
148
return cNewDesktopFileName;
151
gchar *cairo_dock_add_desktop_file_from_uri (const gchar *cURI, const gchar *cDockName, double fOrder, GError **erreur)
153
if (! (cURI == NULL || g_str_has_suffix (cURI, ".desktop") || g_str_has_suffix (cURI, ".sh")))
155
return _add_new_desktop_file (CAIRO_DOCK_DESKTOP_FILE_FOR_LAUNCHER, cURI, cDockName, fOrder, erreur);
158
gchar *cairo_dock_add_desktop_file_from_type (CairoDockDesktopFileType iLauncherType, const gchar *cDockName, double fOrder, GError **erreur)
160
return _add_new_desktop_file (iLauncherType, NULL, cDockName, fOrder, erreur);
164
void cairo_dock_update_launcher_key_file (GKeyFile *pKeyFile, const gchar *cDesktopFilePath, CairoDockDesktopFileType iLauncherType)
166
const gchar *cTemplateFile = _cairo_dock_get_launcher_template_conf_file_path (iLauncherType);
167
cd_debug ("%s (%s)", __func__, cTemplateFile);
169
cairo_dock_upgrade_conf_file (cDesktopFilePath, pKeyFile, cTemplateFile); // update keys
173
void cairo_dock_write_container_name_in_conf_file (Icon *pIcon, const gchar *cParentDockName)
175
if (pIcon->cDesktopFileName != NULL) // lanceur ou separateur.
177
gchar *cDesktopFilePath = *pIcon->cDesktopFileName == '/' ? g_strdup (pIcon->cDesktopFileName) : g_strdup_printf ("%s/%s", g_cCurrentLaunchersPath, pIcon->cDesktopFileName);
178
cairo_dock_update_conf_file (cDesktopFilePath,
179
G_TYPE_STRING, "Desktop Entry", "Container", cParentDockName,
181
g_free (cDesktopFilePath);
183
else if (CAIRO_DOCK_IS_APPLET (pIcon))
185
cairo_dock_update_conf_file (pIcon->pModuleInstance->cConfFilePath,
186
G_TYPE_STRING, "Icon", "dock name", cParentDockName,
191
void cairo_dock_write_order_in_conf_file (Icon *pIcon, double fOrder)
193
if (pIcon->cDesktopFileName != NULL) // lanceur ou separateur.
195
gchar *cDesktopFilePath = *pIcon->cDesktopFileName == '/' ? g_strdup (pIcon->cDesktopFileName) : g_strdup_printf ("%s/%s", g_cCurrentLaunchersPath, pIcon->cDesktopFileName);
196
cairo_dock_update_conf_file (cDesktopFilePath,
197
G_TYPE_DOUBLE, "Desktop Entry", "Order", fOrder,
199
g_free (cDesktopFilePath);
201
else if (CAIRO_DOCK_IS_APPLET (pIcon))
203
cairo_dock_update_conf_file (pIcon->pModuleInstance->cConfFilePath,
204
G_TYPE_DOUBLE, "Icon", "order", fOrder,
210
// should not be here, and probably not be used either (only applets does).
211
void cairo_dock_remove_html_spaces (gchar *cString)
213
gchar *str = cString;
216
str = g_strstr_len (str, -1, "%20");