Debian Codegraph @

dustbin/src/applet-notifications.c

raw
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
23 #include "applet-struct.h"
24 #include "applet-trashes-manager.h"
25 #include "applet-notifications.h"
26
27 static void _on_answer_delete_trash (int iClickedButton, GtkWidget *pInteractiveWidget, gpointer data, CairoDialog *pDialog)
28 {
29 CD_APPLET_ENTER;
30 if (iClickedButton == 0 || iClickedButton == -1) // ok button or Enter.
31 {
32 cairo_dock_fm_empty_trash ();
33 }
34 CD_APPLET_LEAVE ();
35 }
36 static void _cd_dustbin_delete_trash (GtkMenuItem *menu_item, GldiModuleInstance *myApplet)
37 {
38 if (myConfig.bAskBeforeDelete)
39 {
40 gldi_dialog_show_with_question (D_("You're about to delete all files in all dustbins. Sure ?"),
41 myIcon, myContainer,
42 "same icon",
43 (CairoDockActionOnAnswerFunc) _on_answer_delete_trash, NULL, (GFreeFunc)NULL);
44 }
45 else
46 {
47 cairo_dock_fm_empty_trash ();
48 }
49 }
50
51 static void _cd_dustbin_show_trash (GtkMenuItem *menu_item, GldiModuleInstance *myApplet)
52 {
53 cairo_dock_fm_launch_uri ("trash:/"/**myData.cDustbinPath*/); // on force l'utilisation de trash:/ ici, car on sait que tous les backends sauront l'ouvrir.
54 }
55
56
57 static void _free_info_dialog (GldiModuleInstance *myApplet)
58 {
59 myData.pInfoDialog = NULL;
60 if (myData.pInfoTask != NULL)
61 {
62 gldi_task_discard (myData.pInfoTask);
63 myData.pInfoTask = NULL;
64 }
65 }
66
67 static void _free_shared_memory (CDSharedMemory *pSharedMemory)
68 {
69 cd_debug ("free dustbin SM");
70 g_free (pSharedMemory->cDustbinPath);
71 g_free (pSharedMemory);
72 }
73 static void _measure_trash (CDSharedMemory *pSharedMemory)
74 {
75 pSharedMemory->iMeasure = cairo_dock_fm_measure_diretory (pSharedMemory->cDustbinPath,
76 (pSharedMemory->iQuickInfoType == CD_DUSTBIN_INFO_WEIGHT ? 0 : 1),
77 TRUE,
78 pSharedMemory->bDiscard);
79 }
80 static gboolean _display_result (CDSharedMemory *pSharedMemory)
81 {
82 if (myData.pInfoDialog != NULL)
83 {
84 int iSize=-1, iNbFiles=-1, iTrashes=-1;
85 if (pSharedMemory->iQuickInfoType == CD_DUSTBIN_INFO_WEIGHT)
86 {
87 iSize = myData.iMeasure;
88 iNbFiles = pSharedMemory->iMeasure;
89 }
90 else
91 {
92 iSize = pSharedMemory->iMeasure;
93 if (pSharedMemory->iQuickInfoType == CD_DUSTBIN_INFO_NB_FILES)
94 iNbFiles = myData.iMeasure;
95 else
96 {
97 gint iCancel = 0;
98 iTrashes = cairo_dock_fm_measure_diretory (myData.cDustbinPath, 0, FALSE, &iCancel); // ca c'est rapide.
99 }
100 }
101
102 gldi_dialog_set_message_printf (myData.pInfoDialog, "%s :\n %d %s\n %.2f %s",
103 D_("The trash contains"),
104 iNbFiles > -1 ? iNbFiles : iTrashes,
105 iNbFiles > -1 ? D_("files") : D_("elements"),
106 (iSize > 1e6 ? (iSize >> 10) / 1024. : iSize / 1024.),
107 (iSize > 1e6 ? D_("Mo") : D_("Ko")));
108 }
109 gldi_task_discard (myData.pInfoTask);
110 myData.pInfoTask = NULL;
111
112 return FALSE;
113 }
114 static void _cd_dustbin_show_info (GtkMenuItem *menu_item, GldiModuleInstance *myApplet)
115 {
116 if (myData.pInfoDialog != NULL)
117 {
118 gldi_object_unref (GLDI_OBJECT(myData.pInfoDialog));
119 myData.pInfoDialog = NULL;
120 }
121 g_return_if_fail (myData.pInfoTask == NULL);
122
123 CairoDialogAttr attr;
124 memset (&attr, 0, sizeof (CairoDialogAttr));
125 attr.cImageFilePath = "same icon";
126 attr.cText = g_strdup_printf ("%s ...\n\n", D_("Counting total size and files number..."));
127 attr.pFreeDataFunc = (GFreeFunc)_free_info_dialog;
128 attr.pIcon = myIcon;
129 attr.pContainer = myContainer;
130 attr.pUserData = myApplet;
131 myData.pInfoDialog = gldi_dialog_new (&attr);
132
133 // launch the task and update the dialog when finished.
134 CDSharedMemory *pSharedMemory = g_new0 (CDSharedMemory, 1);
135 pSharedMemory->cDustbinPath = g_strdup (myData.cDustbinPath);
136 pSharedMemory->iQuickInfoType = myConfig.iQuickInfoType;
137 myData.pInfoTask = gldi_task_new_full (0,
138 (GldiGetDataAsyncFunc) _measure_trash,
139 (GldiUpdateSyncFunc) _display_result,
140 (GFreeFunc) _free_shared_memory,
141 pSharedMemory);
142 pSharedMemory->bDiscard = &myData.pInfoTask->bDiscard;
143
144 gldi_task_launch (myData.pInfoTask);
145 }
146
147 CD_APPLET_ON_BUILD_MENU_BEGIN
148 CD_APPLET_ADD_IN_MENU_WITH_STOCK_AND_DATA (D_("Show Trash (click)"), GLDI_ICON_NAME_OPEN, _cd_dustbin_show_trash, CD_APPLET_MY_MENU, NULL);
149 gchar *cLabel = g_strdup_printf ("%s (%s)", D_("Empty Trash"), D_("middle-click"));
150 CD_APPLET_ADD_IN_MENU_WITH_STOCK_AND_DATA (cLabel, GLDI_ICON_NAME_DELETE, _cd_dustbin_delete_trash, CD_APPLET_MY_MENU, NULL);
151 g_free (cLabel);
152
153 CD_APPLET_ADD_IN_MENU_WITH_STOCK (D_("Display dustbins information"), GLDI_ICON_NAME_INFO, _cd_dustbin_show_info, CD_APPLET_MY_MENU);
154 CD_APPLET_ON_BUILD_MENU_END
155
156
157 static void _cd_dustbin_action_after_unmount (gboolean bMounting, gboolean bSuccess, const gchar *cName, gpointer data)
158 {
159 g_return_if_fail (myIcon != NULL && ! bMounting);
160 gchar *cMessage;
161 if (bSuccess)
162 {
163 cMessage = g_strdup_printf (D_("%s successfully unmounted"), cName);
164 }
165 else
166 {
167 cMessage = g_strdup_printf (D_("Failed to unmount %s"), cName);
168
169 }
170 gldi_dialogs_remove_on_icon (myIcon);
171 gldi_dialog_show_temporary (cMessage, myIcon, myContainer, 4000);
172 g_free (cMessage);
173 }
174 CD_APPLET_ON_DROP_DATA_BEGIN
175 cd_message (" '%s' --> a la poubelle !", CD_APPLET_RECEIVED_DATA);
176 gchar *cName=NULL, *cURI=NULL, *cIconName=NULL;
177 gboolean bIsDirectory;
178 int iVolumeID = 0;
179 double fOrder;
180 if (cairo_dock_fm_get_file_info (CD_APPLET_RECEIVED_DATA,
181 &cName,
182 &cURI,
183 &cIconName,
184 &bIsDirectory,
185 &iVolumeID,
186 &fOrder,
187 0))
188 {
189 if (iVolumeID > 0)
190 {
191 gldi_dialog_show_temporary_with_icon (D_("Unmouting this volume ..."), myIcon, myContainer, 15000., "same icon"); // le dialogue sera enleve lorsque le volume sera demonte.
192 cairo_dock_fm_unmount_full (cURI, iVolumeID, (CairoDockFMMountCallback) _cd_dustbin_action_after_unmount, myApplet);
193 }
194 else
195 cairo_dock_fm_delete_file (cURI, FALSE);
196 }
197 else
198 {
199 cd_warning ("can't get info for '%s'", CD_APPLET_RECEIVED_DATA);
200 }
201 g_free (cName);
202 g_free (cURI);
203 g_free (cIconName);
204 CD_APPLET_ON_DROP_DATA_END
205
206
207 CD_APPLET_ON_CLICK_BEGIN
208 _cd_dustbin_show_trash (NULL, myApplet);
209 CD_APPLET_ON_CLICK_END
210
211
212 CD_APPLET_ON_MIDDLE_CLICK_BEGIN
213 _cd_dustbin_delete_trash (NULL, myApplet);
214 CD_APPLET_ON_MIDDLE_CLICK_END