Debian Codegraph @

dustbin/src/applet-trashes-manager.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 #include <glib/gstdio.h>
23
24 #include "cairo-dock.h"
25
26 #include "applet-struct.h"
27 #include "applet-trashes-manager.h"
28
29
30 static void _free_shared_memory (CDSharedMemory *pSharedMemory)
31 {
32 g_free (pSharedMemory->cDustbinPath);
33 g_free (pSharedMemory);
34 }
35
36 static void cd_dustbin_measure_trash (CDSharedMemory *pSharedMemory)
37 {
38 pSharedMemory->iMeasure = cairo_dock_fm_measure_diretory (pSharedMemory->cDustbinPath,
39 (pSharedMemory->iQuickInfoType == CD_DUSTBIN_INFO_WEIGHT ? 1 : 0),
40 (pSharedMemory->iQuickInfoType == CD_DUSTBIN_INFO_WEIGHT || pSharedMemory->iQuickInfoType == CD_DUSTBIN_INFO_NB_FILES),
41 pSharedMemory->bDiscard);
42 }
43
44 static gboolean cd_dustbin_display_result (CDSharedMemory *pSharedMemory)
45 {
46 myData.iMeasure = pSharedMemory->iMeasure;
47 //g_print ("trash measure : %d\n", myData.iMeasure);
48
49 if (myData.iMeasure == 0)
50 {
51 CD_APPLET_SET_QUICK_INFO_ON_MY_ICON (NULL);
52 if (myData.bDisplayFullIcon)
53 {
54 myData.bDisplayFullIcon = FALSE;
55 CD_APPLET_SET_IMAGE_ON_MY_ICON (myConfig.cEmptyUserImage);
56 }
57 }
58 else
59 {
60 if (! myData.bDisplayFullIcon)
61 {
62 myData.bDisplayFullIcon = TRUE;
63 CD_APPLET_SET_IMAGE_ON_MY_ICON (myConfig.cFullUserImage);
64 }
65
66 if (myConfig.iQuickInfoType == CD_DUSTBIN_INFO_NB_TRASHES)
67 {
68 CD_APPLET_SET_QUICK_INFO_ON_MY_ICON_PRINTF ("%zd%s", myData.iMeasure, (myDesklet ? D_(" trashe(s)") : ""));
69 }
70 else if (myConfig.iQuickInfoType == CD_DUSTBIN_INFO_NB_FILES)
71 {
72 CD_APPLET_SET_QUICK_INFO_ON_MY_ICON_PRINTF ("%zd%s", myData.iMeasure, (myDesklet ? D_(" file(s)") : ""));
73 }
74 else if (myConfig.iQuickInfoType == CD_DUSTBIN_INFO_WEIGHT)
75 {
76 CD_APPLET_SET_SIZE_AS_QUICK_INFO (myData.iMeasure);
77 }
78 else // on vire les "..."
79 {
80 CD_APPLET_SET_QUICK_INFO_ON_MY_ICON (NULL);
81 }
82 }
83
84 CD_APPLET_REDRAW_MY_ICON;
85 return TRUE;
86 }
87
88 static void cd_dustbin_on_file_event (CairoDockFMEventType iEventType, const gchar *cURI, GldiModuleInstance *myApplet)
89 {
90 g_return_if_fail (cURI != NULL);
91 //g_print ("%s (%s, %d)\n", __func__, cURI, myData.iMeasure);
92 switch (iEventType)
93 {
94 case CAIRO_DOCK_FILE_DELETED :
95 case CAIRO_DOCK_FILE_CREATED :
96 if (gldi_task_is_running (myData.pTask)) // task is running, cancel it since files count has changed, no need to finish this measure.
97 {
98 //g_print ("cancel measure\n");
99 gldi_task_discard (myData.pTask);
100
101 CDSharedMemory *pSharedMemory = g_new0 (CDSharedMemory, 1);
102 pSharedMemory->cDustbinPath = g_strdup (myData.cDustbinPath);
103 pSharedMemory->iQuickInfoType = myConfig.iQuickInfoType;
104 myData.pTask = gldi_task_new_full (0,
105 (GldiGetDataAsyncFunc) cd_dustbin_measure_trash,
106 (GldiUpdateSyncFunc) cd_dustbin_display_result,
107 (GFreeFunc) _free_shared_memory,
108 pSharedMemory);
109 pSharedMemory->bDiscard = &myData.pTask->bDiscard;
110 }
111 else if (myConfig.iQuickInfoType == CD_DUSTBIN_INFO_WEIGHT || myConfig.iQuickInfoType == CD_DUSTBIN_INFO_NB_FILES) // task was not running, so no waiting message; let's add it before we launch the task.
112 {
113 CD_APPLET_SET_QUICK_INFO_ON_MY_ICON_PRINTF ("%s...", (myDesklet ? D_("calculating") : ""));
114 }
115 gldi_task_launch_delayed (myData.pTask, 500);
116 break ;
117
118 default :
119 break;
120 }
121 }
122
123 void cd_dustbin_start (GldiModuleInstance *myApplet)
124 {
125 // get the trash folder if not already done.
126 if (myData.cDustbinPath == NULL)
127 myData.cDustbinPath = cairo_dock_fm_get_trash_path (NULL, NULL);
128 // monitor this folder.
129 if (myData.cDustbinPath != NULL)
130 {
131 // try monitoring the trash folder.
132 myData.bMonitoringOK = cairo_dock_fm_add_monitor_full (myData.cDustbinPath, TRUE, NULL, (CairoDockFMMonitorCallback) cd_dustbin_on_file_event, myApplet);
133 if (! myData.bMonitoringOK)
134 {
135 cd_message ("dustbin : can't monitor trash folder\n we'll check it periodically");
136 }
137
138 // measure the trash content once, to get the initial stats.
139 CDSharedMemory *pSharedMemory = g_new0 (CDSharedMemory, 1);
140 pSharedMemory->cDustbinPath = g_strdup (myData.cDustbinPath);
141 pSharedMemory->iQuickInfoType = myConfig.iQuickInfoType;
142 myData.pTask = gldi_task_new_full (myData.bMonitoringOK ? 0 : 10, // si le monitoring de fichiers n'est pas disponible, on execute la tache periodiquement.
143 (GldiGetDataAsyncFunc) cd_dustbin_measure_trash,
144 (GldiUpdateSyncFunc) cd_dustbin_display_result,
145 (GFreeFunc) _free_shared_memory,
146 pSharedMemory);
147 pSharedMemory->bDiscard = &myData.pTask->bDiscard;
148
149 gldi_task_launch (myData.pTask); // on la lance meme si on n'affiche rien, pour savoir si le nombre de fichiers est nul ou non.
150 if (myConfig.iQuickInfoType == CD_DUSTBIN_INFO_WEIGHT || myConfig.iQuickInfoType == CD_DUSTBIN_INFO_NB_FILES) // operation potentiellement longue => on met un petit message discret.
151 {
152 CD_APPLET_SET_QUICK_INFO_ON_MY_ICON_PRINTF ("%s...", (myDesklet ? D_("calculating") : ""));
153 }
154 }
155 else // no trash, set a N/A icon.
156 {
157 CD_APPLET_SET_IMAGE_ON_MY_ICON (myConfig.cEmptyUserImage);
158 CD_APPLET_SET_QUICK_INFO_ON_MY_ICON ("N/A");
159 }
160 }
161
162 void cd_dustbin_stop (GldiModuleInstance *myApplet)
163 {
164 gldi_task_discard (myData.pTask);
165 myData.pTask = NULL;
166
167 if (myData.bMonitoringOK)
168 {
169 cairo_dock_fm_remove_monitor_full (myData.cDustbinPath, TRUE, NULL);
170 }
171
172 gldi_object_unref (GLDI_OBJECT(myData.pInfoDialog));
173 myData.pInfoDialog = NULL;
174 }