~ubuntu-branches/ubuntu/saucy/cairo-dock-plug-ins/saucy

« back to all changes in this revision

Viewing changes to stack/src/applet-stack.c

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2009-08-26 21:07:39 UTC
  • Revision ID: james.westby@ubuntu.com-20090826210739-gyjuuqezrzuluao4
Tags: upstream-2.0.8.1
ImportĀ upstreamĀ versionĀ 2.0.8.1

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
/******************************************************************************
 
21
 
 
22
This file is a part of the cairo-dock program, 
 
23
released under the terms of the GNU General Public License.
 
24
 
 
25
Written by RĆ©my Robertson (for any bug report, please mail me to changfu@cairo-dock.org)
 
26
 
 
27
******************************************************************************/
 
28
#include <string.h>
 
29
#include <glib/gstdio.h>
 
30
 
 
31
#include "applet-struct.h"
 
32
#include "applet-load-icons.h"
 
33
#include "applet-stack.h"
 
34
 
 
35
 
 
36
void cd_stack_check_local (CairoDockModuleInstance *myApplet, GKeyFile *pKeyFile) {
 
37
        
 
38
        if (myConfig.cStackDir == NULL)  // applet nouvellement instanciee.
 
39
        {
 
40
                GString *sDirPath = g_string_new ("");
 
41
                
 
42
                int i = 0;
 
43
                do
 
44
                {
 
45
                        if (i == 0)
 
46
                                g_string_printf (sDirPath, "%s/stack", g_cCairoDockDataDir);
 
47
                        else
 
48
                                g_string_printf (sDirPath, "%s/stack-%d", g_cCairoDockDataDir, i);
 
49
                        i ++;
 
50
                        g_print ("stack : test de %s\n", sDirPath->str);
 
51
                } while (g_file_test (sDirPath->str, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_EXECUTABLE));
 
52
                
 
53
                myConfig.cStackDir = sDirPath->str;
 
54
                g_string_free (sDirPath, FALSE);
 
55
                g_key_file_set_string (pKeyFile, "Configuration", "stack dir", myConfig.cStackDir);
 
56
                cairo_dock_write_keys_to_file (pKeyFile, myApplet->cConfFilePath);
 
57
        }
 
58
        cd_debug ("Stack : reperoire local : %s", myConfig.cStackDir);
 
59
        
 
60
        if (! g_file_test (myConfig.cStackDir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_EXECUTABLE)) {
 
61
                g_mkdir_with_parents (myConfig.cStackDir, 7*8*8+7*8+5);
 
62
                cd_debug ("Stack local directory created (%s)", myConfig.cStackDir);
 
63
        }
 
64
}
 
65
 
 
66
void cd_stack_clear_stack (CairoDockModuleInstance *myApplet) {
 
67
        gchar *cCommand = g_strdup_printf("rm -rf \"%s\"/*", myConfig.cStackDir);
 
68
        cd_debug("Stack: will use '%s'", cCommand);
 
69
        int r = system (cCommand);
 
70
        g_free(cCommand);
 
71
        
 
72
        CD_APPLET_DELETE_MY_ICONS_LIST;
 
73
}
 
74
 
 
75
 
 
76
void cd_stack_remove_item (CairoDockModuleInstance *myApplet, Icon *pIcon)
 
77
{
 
78
        gchar *cFilePath = g_strdup_printf ("%s/%s", myConfig.cStackDir, pIcon->acDesktopFileName);
 
79
        cd_message ("removing %s...", cFilePath);
 
80
        g_remove (cFilePath);
 
81
        g_free (cFilePath);
 
82
        
 
83
        if (myDock)
 
84
        {
 
85
                cairo_dock_detach_icon_from_dock (pIcon, myIcon->pSubDock, FALSE);
 
86
                cairo_dock_update_dock_size (myIcon->pSubDock);
 
87
        }
 
88
        else
 
89
        {
 
90
                myDesklet->icons = g_list_remove (myDesklet->icons, pIcon);
 
91
                gtk_widget_queue_draw (myDesklet->pWidget);
 
92
        }
 
93
        cairo_dock_free_icon (pIcon);
 
94
}
 
95
 
 
96
 
 
97
 
 
98
Icon *cd_stack_create_item (CairoDockModuleInstance *myApplet, const gchar *cStackDirectory, const gchar *cContent)
 
99
{
 
100
        gchar *cName;
 
101
        double fOrder = 0;
 
102
        int iDate;
 
103
        
 
104
        if (cairo_dock_string_is_adress (cContent) || *cContent == '/')
 
105
        {
 
106
                if (strncmp (cContent, "http://", 7) == 0 || strncmp (cContent, "www", 3) == 0)
 
107
                {
 
108
                        gchar *buf = g_strdup (cContent);
 
109
                        gchar *str = strchr (buf, '?');
 
110
                        if (str != NULL)
 
111
                                *str = '\0';
 
112
                        if (str[strlen(str)-1] == '/')
 
113
                                str[strlen(str)-1] = '\0';
 
114
                        str = strrchr (buf, '/');
 
115
                        if (str != NULL && *(str+1) != '\0')
 
116
                        {
 
117
                                cName = g_strdup (str+1);
 
118
                                g_free (buf);
 
119
                        }
 
120
                        else
 
121
                        {
 
122
                                cName = buf;
 
123
                        }
 
124
                }
 
125
                else
 
126
                {
 
127
                        gchar *cFileName = (*cContent == '/' ? g_strdup (cContent) : g_filename_from_uri (cContent, NULL, NULL));  // virer l'extension ?
 
128
                        cName = g_path_get_basename (cFileName);
 
129
                        g_free (cFileName);
 
130
                }
 
131
        }
 
132
        else
 
133
        {
 
134
                cName = cairo_dock_cut_string (cContent, 15);  // 15 caracteres par defaut.
 
135
        }
 
136
        g_return_val_if_fail (cName != NULL, NULL);
 
137
        
 
138
        GList *pIconsList = (myDock ? (myIcon->pSubDock != NULL ? myIcon->pSubDock->icons : NULL) : myDesklet->icons);
 
139
        GList *ic;
 
140
        Icon *icon;
 
141
        for (ic = pIconsList; ic != NULL; ic = ic->next)
 
142
        {
 
143
                icon = ic->data;
 
144
                fOrder = MAX (fOrder, icon->fOrder);
 
145
        }
 
146
        
 
147
        iDate = time (NULL);
 
148
        
 
149
        GKeyFile *pKeyFile = g_key_file_new();
 
150
        g_key_file_set_string (pKeyFile, "Desktop Entry", "Name", cName);
 
151
        g_key_file_set_integer (pKeyFile, "Desktop Entry", "Date", iDate);
 
152
        g_key_file_set_double (pKeyFile, "Desktop Entry", "Order", fOrder);
 
153
        if (*cContent == '/')
 
154
        {
 
155
                gchar *cURI = g_filename_to_uri (cContent, NULL, NULL);
 
156
                if (cURI == NULL)
 
157
                {
 
158
                        g_key_file_free (pKeyFile);
 
159
                        g_free (cURI);
 
160
                        cd_warning ("stack : '%s' is not a valid adress", cContent);
 
161
                        return NULL;
 
162
                }
 
163
                g_key_file_set_string (pKeyFile, "Desktop Entry", "Content", cURI);
 
164
                g_free (cURI);
 
165
        }
 
166
        else
 
167
                g_key_file_set_string (pKeyFile, "Desktop Entry", "Content", cContent);
 
168
        
 
169
        GString *sConfFilePath = g_string_new ("");
 
170
        int i = 0;
 
171
        do
 
172
        {
 
173
                if (i == 0)
 
174
                        g_string_printf (sConfFilePath, "%s/%s", cStackDirectory, cName);
 
175
                else
 
176
                        g_string_printf (sConfFilePath, "%s/%s.%d", cStackDirectory, cName, i);
 
177
        } while (g_file_test (sConfFilePath->str, G_FILE_TEST_EXISTS));
 
178
        
 
179
        cairo_dock_write_keys_to_file (pKeyFile, sConfFilePath->str);
 
180
        
 
181
        Icon *pIcon = cd_stack_build_one_icon (myApplet, pKeyFile);
 
182
        if (pIcon != NULL)
 
183
                pIcon->acDesktopFileName = g_path_get_basename (sConfFilePath->str);
 
184
        
 
185
        g_key_file_free (pKeyFile);
 
186
        g_string_free (sConfFilePath, TRUE);
 
187
        return pIcon;
 
188
}
 
189
 
 
190
void cd_stack_create_and_load_item (CairoDockModuleInstance *myApplet, const gchar *cContent)
 
191
{
 
192
        Icon *pIcon = cd_stack_create_item (myApplet, myConfig.cStackDir, cContent);
 
193
        if (pIcon == NULL)  // peut arriver si l'icone est filtree.
 
194
                return ;
 
195
        
 
196
        if (myDock)
 
197
        {
 
198
                if (myIcon->pSubDock == NULL)
 
199
                {
 
200
                        GList *pStacksIconList = NULL;
 
201
                        pStacksIconList = g_list_prepend (pStacksIconList, pIcon);
 
202
                        CD_APPLET_CREATE_MY_SUBDOCK (pStacksIconList, myConfig.cRenderer);
 
203
                }
 
204
                else
 
205
                {
 
206
                        cairo_dock_load_one_icon_from_scratch (pIcon, CAIRO_CONTAINER (myIcon->pSubDock));
 
207
                        GCompareFunc pCompareFunc = NULL;
 
208
                        switch (myConfig.iSortType)
 
209
                        {
 
210
                                case CD_STACK_SORT_BY_DATE :
 
211
                                case CD_STACK_SORT_MANUALLY :
 
212
                                        pCompareFunc = (GCompareFunc) cairo_dock_compare_icons_order;
 
213
                                break;
 
214
                                case CD_STACK_SORT_BY_NAME :
 
215
                                        pCompareFunc = (GCompareFunc) cairo_dock_compare_icons_name;
 
216
                                break;
 
217
                                case CD_STACK_SORT_BY_TYPE :
 
218
                                default :
 
219
                                        pCompareFunc = (GCompareFunc) cairo_dock_compare_icons_extension;
 
220
                                break;
 
221
                        }
 
222
                        cairo_dock_insert_icon_in_dock_full (pIcon, myIcon->pSubDock, CAIRO_DOCK_UPDATE_DOCK_SIZE, CAIRO_DOCK_ANIMATE_ICON, ! CAIRO_DOCK_INSERT_SEPARATOR, pCompareFunc);
 
223
                }
 
224
        }
 
225
        else
 
226
        {
 
227
                GList *pStacksIconList = (myDock ? (myIcon->pSubDock != NULL ? myIcon->pSubDock->icons : NULL) : myDesklet->icons);
 
228
                pStacksIconList = cd_stack_insert_icon_in_list (myApplet, pStacksIconList, pIcon);
 
229
                
 
230
                cairo_dock_load_one_icon_from_scratch (pIcon, CAIRO_CONTAINER (myDesklet));
 
231
                myDesklet->icons = pStacksIconList;
 
232
                gtk_widget_queue_draw (myDesklet->pWidget);  /// il faudrait recharger le Tree non ?...
 
233
        }
 
234
}
 
235
 
 
236
void cd_stack_set_item_name (const gchar *cDesktopFilePath, const gchar *cName)
 
237
{
 
238
        cairo_dock_update_conf_file (cDesktopFilePath,
 
239
                G_TYPE_STRING, "Desktop Entry", "Name", cName,
 
240
                G_TYPE_INVALID);
 
241
}
 
242
 
 
243
void cd_stack_set_item_order (const gchar *cDesktopFilePath, double fOrder)
 
244
{
 
245
        cairo_dock_update_conf_file (cDesktopFilePath,
 
246
                G_TYPE_DOUBLE, "Desktop Entry", "Order", fOrder,
 
247
                G_TYPE_INVALID);
 
248
}