~cairo-dock-team/cairo-dock-core/popup_from_shortkey

« back to all changes in this revision

Viewing changes to src/icon-factory/cairo-dock-desktop-file-factory.c

  • Committer: Fabrice Rey
  • Date: 2013-06-04 22:58:28 UTC
  • Revision ID: fabounet03@gmail.com-20130604225828-x8lg3mv9135jr114
updated various parts of the core to the new API (new icons type, 'delete' method, simplified functions, etc)

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 <stdlib.h>
21
 
#include <string.h>
22
 
#include <cairo.h>
23
 
#include <gtk/gtk.h>
24
 
 
25
 
 
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"
34
 
 
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"
38
 
 
39
 
extern gchar *g_cCurrentThemePath;
40
 
extern gchar *g_cCurrentLaunchersPath;
41
 
 
42
 
 
43
 
static gchar *_cairo_dock_generate_desktop_filename (const gchar *cBaseName, gchar *cCairoDockDataDir)
44
 
{
45
 
        int iPrefixNumber = 0;
46
 
        GString *sFileName = g_string_new ("");
47
 
 
48
 
        do
49
 
        {
50
 
                iPrefixNumber ++;
51
 
                g_string_printf (sFileName, "%s/%02d%s", cCairoDockDataDir, iPrefixNumber, cBaseName);
52
 
        } while (iPrefixNumber < 99 && g_file_test (sFileName->str, G_FILE_TEST_EXISTS));
53
 
 
54
 
        g_string_free (sFileName, TRUE);
55
 
        if (iPrefixNumber == 99)
56
 
                return NULL;
57
 
        else
58
 
                return g_strdup_printf ("%02d%s", iPrefixNumber, cBaseName);
59
 
}
60
 
 
61
 
static inline const gchar *_cairo_dock_get_launcher_template_conf_file_path (CairoDockDesktopFileType iNewDesktopFileType)
62
 
{
63
 
        const gchar *cTemplateFile;
64
 
        switch (iNewDesktopFileType)
65
 
        {
66
 
                case CAIRO_DOCK_DESKTOP_FILE_FOR_LAUNCHER :
67
 
                        cTemplateFile = GLDI_SHARE_DATA_DIR"/"CAIRO_DOCK_LAUNCHER_CONF_FILE;
68
 
                break ;
69
 
                case CAIRO_DOCK_DESKTOP_FILE_FOR_CONTAINER :
70
 
                        cTemplateFile = GLDI_SHARE_DATA_DIR"/"CAIRO_DOCK_CONTAINER_CONF_FILE;
71
 
                break ;
72
 
                case CAIRO_DOCK_DESKTOP_FILE_FOR_SEPARATOR :
73
 
                        cTemplateFile = GLDI_SHARE_DATA_DIR"/"CAIRO_DOCK_SEPARATOR_CONF_FILE;
74
 
                break ;
75
 
                default:
76
 
                        cTemplateFile = NULL;
77
 
        }
78
 
        return cTemplateFile;
79
 
}
80
 
 
81
 
static gchar *_add_new_desktop_file (CairoDockDesktopFileType iLauncherType, const gchar *cOrigin, const gchar *cDockName, double fOrder, G_GNUC_UNUSED GError **erreur)
82
 
{
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);
86
 
        
87
 
        GKeyFile *pKeyFile = cairo_dock_open_key_file (cTemplateFile);
88
 
        g_return_val_if_fail (pKeyFile != NULL, NULL);
89
 
        
90
 
        //\__________________ fill the parameters
91
 
        gchar *cFilePath = NULL;
92
 
        if (cOrigin != NULL && *cOrigin != '/')  // transform the origin URI into a path or a file name.
93
 
        {
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.
96
 
                else
97
 
                        cFilePath = g_filename_from_uri (cOrigin, NULL, NULL);
98
 
        }
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:"");
102
 
        
103
 
        g_key_file_set_double (pKeyFile, "Desktop Entry", "Order", fOrder);
104
 
        
105
 
        g_key_file_set_string (pKeyFile, "Desktop Entry", "Container", cDockName);
106
 
        
107
 
        //\__________________ in the case of a script, set ourselves a valid name and command.
108
 
        if (cFilePath != NULL && g_str_has_suffix (cFilePath, ".sh"))
109
 
        {
110
 
                gchar *cName = g_path_get_basename (cFilePath);
111
 
                g_key_file_set_string (pKeyFile, "Desktop Entry", "Name", cName);
112
 
                g_free (cName);
113
 
                g_key_file_set_string (pKeyFile, "Desktop Entry", "Exec", cFilePath);
114
 
                g_key_file_set_boolean (pKeyFile, "Desktop Entry", "Terminal", TRUE);
115
 
        }
116
 
        
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)
119
 
        {
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"));
122
 
        }
123
 
        
124
 
        //\__________________ generate a unique and readable filename.
125
 
        gchar *cBaseName = (cFilePath ?
126
 
                *cFilePath == '/' ?
127
 
                        g_path_get_basename (cFilePath) :
128
 
                        g_strdup (cFilePath) :
129
 
                g_path_get_basename (cTemplateFile));
130
 
 
131
 
        if (! g_str_has_suffix (cBaseName, ".desktop")) // if we have a script (.sh file) => add '.desktop'
132
 
        {
133
 
                gchar *cTmpBaseName = g_strdup_printf ("%s.desktop", cBaseName);
134
 
                g_free (cBaseName);
135
 
                cBaseName = cTmpBaseName;
136
 
        }
137
 
 
138
 
        gchar *cNewDesktopFileName = _cairo_dock_generate_desktop_filename (cBaseName, g_cCurrentLaunchersPath);
139
 
        g_free (cBaseName);
140
 
        
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);
145
 
        
146
 
        g_free (cFilePath);
147
 
        g_key_file_free (pKeyFile);
148
 
        return cNewDesktopFileName;
149
 
}
150
 
 
151
 
gchar *cairo_dock_add_desktop_file_from_uri (const gchar *cURI, const gchar *cDockName, double fOrder, GError **erreur)
152
 
{
153
 
        if (! (cURI == NULL || g_str_has_suffix (cURI, ".desktop") || g_str_has_suffix (cURI, ".sh")))
154
 
                return NULL;
155
 
        return _add_new_desktop_file (CAIRO_DOCK_DESKTOP_FILE_FOR_LAUNCHER, cURI, cDockName, fOrder, erreur);
156
 
}
157
 
 
158
 
gchar *cairo_dock_add_desktop_file_from_type (CairoDockDesktopFileType iLauncherType, const gchar *cDockName, double fOrder, GError **erreur)
159
 
{
160
 
        return _add_new_desktop_file (iLauncherType, NULL, cDockName, fOrder, erreur);
161
 
}
162
 
 
163
 
 
164
 
void cairo_dock_update_launcher_key_file (GKeyFile *pKeyFile, const gchar *cDesktopFilePath, CairoDockDesktopFileType iLauncherType)
165
 
{
166
 
        const gchar *cTemplateFile = _cairo_dock_get_launcher_template_conf_file_path (iLauncherType);
167
 
        cd_debug ("%s (%s)", __func__, cTemplateFile);
168
 
        
169
 
        cairo_dock_upgrade_conf_file (cDesktopFilePath, pKeyFile, cTemplateFile);  // update keys
170
 
}
171
 
 
172
 
 
173
 
void cairo_dock_write_container_name_in_conf_file (Icon *pIcon, const gchar *cParentDockName)
174
 
{
175
 
        if (pIcon->cDesktopFileName != NULL)  // lanceur ou separateur.
176
 
        {
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,
180
 
                        G_TYPE_INVALID);
181
 
                g_free (cDesktopFilePath);
182
 
        }
183
 
        else if (CAIRO_DOCK_IS_APPLET (pIcon))
184
 
        {
185
 
                cairo_dock_update_conf_file (pIcon->pModuleInstance->cConfFilePath,
186
 
                        G_TYPE_STRING, "Icon", "dock name", cParentDockName,
187
 
                        G_TYPE_INVALID);
188
 
        }
189
 
}
190
 
 
191
 
void cairo_dock_write_order_in_conf_file (Icon *pIcon, double fOrder)
192
 
{
193
 
        if (pIcon->cDesktopFileName != NULL)  // lanceur ou separateur.
194
 
        {
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,
198
 
                        G_TYPE_INVALID);
199
 
                g_free (cDesktopFilePath);
200
 
        }
201
 
        else if (CAIRO_DOCK_IS_APPLET (pIcon))
202
 
        {
203
 
                cairo_dock_update_conf_file (pIcon->pModuleInstance->cConfFilePath,
204
 
                        G_TYPE_DOUBLE, "Icon", "order", fOrder,
205
 
                        G_TYPE_INVALID);
206
 
        }
207
 
}
208
 
 
209
 
 
210
 
// should not be here, and probably not be used either (only applets does).
211
 
void cairo_dock_remove_html_spaces (gchar *cString)
212
 
{
213
 
        gchar *str = cString;
214
 
        do
215
 
        {
216
 
                str = g_strstr_len (str, -1, "%20");
217
 
                if (str == NULL)
218
 
                        break ;
219
 
                *str = ' ';
220
 
                str ++;
221
 
                strcpy (str, str+2);
222
 
        }
223
 
        while (TRUE);
224
 
}