88
#define CD_EXPAND_FIELD_CODES //comment this line to switch off the arguments parsing.
89
static gchar * cd_expand_field_codes(const gchar* cCommand, GKeyFile* keyfile) // Thanks to Tristan Moody for this patch !
91
//g_print ("%s (%s)\n", __func__, cCommand);
92
gchar* cCommandExpanded = NULL;
93
#ifdef CD_EXPAND_FIELD_CODES
94
gchar* cField = strchr (cCommand, '%');
96
gchar* cFieldCodeToken = NULL;
97
GError* erreur = NULL;
99
// Break out immediately if there are no field codes
102
cCommandExpanded = g_strdup(cCommand);
103
return cCommandExpanded;
106
// parse all the %x tokens, replacing them with the value they represent.
107
// Exec=krusader -caption "%c" %i %m
108
GString *sExpandedcCommand = g_string_new ("");
109
g_string_append_len (sExpandedcCommand, cCommand, cField - cCommand/** - (*(cField-1) != ' ')*/); // take the command until the first % (not included).
110
while ( cField != NULL )
112
cField ++; // jump to the code.
113
gboolean bAddQuote = FALSE;
114
switch ( *cField ) // Make sure field code is valid
119
case 'U': //Is there any reason to expect these codes in a launcher for the main menu?
120
cd_warning("Unexpected field code %%%c in exec string '%s' : cannot handle file or url codes in the menu.", *cField, cCommand);
127
case 'm': //Deprecated field codes ignored and stripped, per freedesktop spec
128
cd_warning("Deprecated field code %%%c ignored in exec string '%s'.", *cField, cCommand);
131
cFieldCodeToken = g_key_file_get_locale_string (keyfile, "Desktop Entry", "Name", NULL, &erreur);
134
cd_warning ("Error while expanding %c in exec string '%s' : %s", *cField, cCommand, erreur->message);
135
g_error_free (erreur);
138
if (*(cField-2) == ' ') // add quotes if not present.
140
gchar *tmp = cFieldCodeToken;
141
cFieldCodeToken = g_strdup_printf ("\"%s\"", cFieldCodeToken);
146
cFieldCodeToken = g_key_file_get_locale_string (keyfile, "Desktop Entry", "Icon", NULL, NULL); // Icon key not required. If not found, no error message necessary.
147
if (cFieldCodeToken != NULL)
149
gchar *tmp = cFieldCodeToken;
150
cFieldCodeToken = g_strdup_printf ("--icon \"%s\"", cFieldCodeToken);
155
cd_warning("Field code %%k not handled yet");
157
case '%': // %% is a literal % sign.
158
cFieldCodeToken = g_strdup("%");
159
//cField ++; // to avoid capturing this %-sign as the beginning of the next field code
162
cd_warning("Invalid field code %%%c in exec string '%s'", *cField, cCommand);
163
break; // we'll try to launch it anyway.
166
if (cFieldCodeToken != NULL) // there is a token to add to the command.
168
g_string_append_printf (sExpandedcCommand, "%s", cFieldCodeToken);
169
g_free (cFieldCodeToken);
170
cFieldCodeToken = NULL;
173
///if (*(cField+1) != ' ' && *(cField+1) != '\0') // on enleve les eventuels '"'.
175
cField = strchr(cField + 1, '%'); // next field.
176
// we append everything between the current filed and the next field.
178
g_string_append_len (sExpandedcCommand, cFieldLast+1, cField - cFieldLast - 1);
180
g_string_append (sExpandedcCommand, cFieldLast+1);
182
cCommandExpanded = sExpandedcCommand->str;
183
g_string_free (sExpandedcCommand, FALSE);
185
gchar *str = strchr (cCommand, '%');
187
cCommandExpanded = g_strndup (cCommand, str - cCommand);
189
cCommandExpanded = g_strdup (cCommand);
190
#endif //CD_EXPAND_FIELD_CODES
191
g_print ("cCommandExpanded : %s\n", cCommandExpanded);
192
return cCommandExpanded;
88
194
static void _launch_from_file (const gchar *cDesktopFilePath)
196
//\____________ On ouvre le .desktop
90
197
GError *erreur = NULL;
91
198
GKeyFile* keyfile = g_key_file_new();
92
g_key_file_load_from_file (keyfile, cDesktopFilePath, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &erreur);
199
g_key_file_load_from_file (keyfile, cDesktopFilePath, 0, &erreur); //skip comments and translations.
93
200
if (erreur != NULL)
95
202
cd_warning ("while trying to read %s : %s", cDesktopFilePath, erreur->message);
96
203
g_error_free (erreur);
206
//\____________ On recupere la commande.
99
207
gchar *cCommand = g_key_file_get_string (keyfile, "Desktop Entry", "Exec", &erreur);
100
208
if (erreur != NULL)
103
211
g_error_free (erreur);
106
gchar *cWorkingDirectory = NULL;
107
if (cCommand != NULL)
109
gchar *str = strchr (cCommand, '%');
214
g_return_if_fail (cCommand != NULL);
216
//\____________ On gere les arguments de la forme %x.
217
gchar *cCommandExpanded = NULL;
218
#ifdef CD_EXPAND_FIELD_CODES
219
cCommandExpanded = cd_expand_field_codes(cCommand, keyfile);
221
gchar *str = strchr (cCommand, '%');
224
#endif //CD_EXPAND_FIELD_CODES
226
//\____________ On gere le lancement dans un terminal.
227
gboolean bExecInTerminal = g_key_file_get_boolean (keyfile, "Desktop Entry", "Terminal", NULL);
228
if (bExecInTerminal) // on le fait apres l'expansion.
230
gchar *cOldCommand = cCommand;
231
const gchar *cTerm = g_getenv ("COLORTERM");
232
if (cTerm != NULL && strlen (cTerm) > 1) // on filtre les cas COLORTERM=1 ou COLORTERM=y. ce qu'on veut c'est le nom d'un terminal.
233
cCommand = g_strdup_printf ("$COLORTERM -e \"%s\"", cOldCommand);
234
else if (g_getenv ("TERM") != NULL)
235
cCommand = g_strdup_printf ("$TERM -e \"%s\"", cOldCommand);
237
cCommand = g_strdup_printf ("xterm -e \"%s\"", cOldCommand);
238
g_free (cOldCommand);
241
//\____________ On recupere le repertoire d'execution.
242
gchar *cWorkingDirectory = g_key_file_get_string (keyfile, "Desktop Entry", "Path", NULL);
243
if (cWorkingDirectory != NULL && *cWorkingDirectory == '\0')
112
245
g_free (cWorkingDirectory);
113
cWorkingDirectory = g_key_file_get_string (keyfile, "Desktop Entry", "Path", NULL);
114
if (cWorkingDirectory != NULL && *cWorkingDirectory == '\0')
116
g_free (cWorkingDirectory);
117
cWorkingDirectory = NULL;
120
cairo_dock_launch_command_full (cCommand, cWorkingDirectory);
246
cWorkingDirectory = NULL;
249
//\____________ On lance le tout.
250
cairo_dock_launch_command_full (cCommandExpanded, cWorkingDirectory);
121
251
g_free (cCommand);
252
g_free (cCommandExpanded);
122
253
g_free (cWorkingDirectory);
124
255
static void _launch_from_basename (const gchar *cDesktopFileName)