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

« back to all changes in this revision

Viewing changes to shortcuts/src/applet-disk-usage.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 Fabrice Rey (for any bug report, please mail me to fabounet@users.berlios.de)
 
26
 
 
27
*********************************************************************************/
 
28
#include <string.h>
 
29
#include <mntent.h>
 
30
#include <sys/types.h>
 
31
#include <sys/statfs.h>
 
32
#include <mntent.h>
 
33
 
 
34
#include <cairo-dock.h>
 
35
 
 
36
#include "applet-struct.h"
 
37
#include "applet-disk-usage.h"
 
38
 
 
39
 
 
40
void cd_shortcuts_get_fs_stat (const gchar *cDiskURI, CDDiskUsage *pDiskUsage)
 
41
{
 
42
        static struct statfs sts;
 
43
        const gchar *cMountPath = (strncmp (cDiskURI, "file://", 7) == 0 ? cDiskURI + 7 : cDiskURI);
 
44
        //g_print ("checking device on '%s'...\n", cMountPath);
 
45
        
 
46
        if (statfs (cMountPath, &sts) == 0)
 
47
        {
 
48
                if (pDiskUsage->iType == 0)
 
49
                        pDiskUsage->iType = sts.f_type;
 
50
                pDiskUsage->iPrevAvail = pDiskUsage->iAvail;
 
51
                pDiskUsage->iAvail = (long long)sts.f_bavail * sts.f_bsize;  // Blocs libres pour utilisateurs
 
52
                pDiskUsage->iFree  = (long long)sts.f_bfree  * sts.f_bsize;  // Blocs libres
 
53
                pDiskUsage->iTotal = (long long)sts.f_blocks * sts.f_bsize;  // Nombre total de blocs
 
54
                pDiskUsage->iUsed  = pDiskUsage->iTotal - pDiskUsage->iAvail;
 
55
                //g_print ("%lld / %lld\n", pDiskUsage->iAvail, pDiskUsage->iTotal);
 
56
        }
 
57
}
 
58
 
 
59
void cd_shortcuts_get_disk_usage (CairoDockModuleInstance *myApplet)
 
60
{
 
61
        const gchar *cMountPath;
 
62
        GList *pElement = myData.pDiskUsageList;
 
63
        CDDiskUsage *pDiskUsage;
 
64
        long long iAvail, iFree, iTotal, iUsed, iType;
 
65
        Icon *pIcon;
 
66
        GList *ic;
 
67
        GList *pIconsList = CD_APPLET_MY_ICONS_LIST;
 
68
        for (ic = pIconsList; ic != NULL; ic = ic->next)
 
69
        {
 
70
                pIcon = ic->data;
 
71
                if (pIcon->iType != 6)
 
72
                        break;
 
73
                //g_print ("%s () : %s\n", __func__, pIcon->acCommand);
 
74
                if (pIcon->acCommand != NULL)
 
75
                {
 
76
                        if (pElement != NULL)
 
77
                        {
 
78
                                pDiskUsage = pElement->data;
 
79
                                pElement = pElement->next;
 
80
                        }
 
81
                        else
 
82
                        {
 
83
                                pDiskUsage = g_new0 (CDDiskUsage, 1);
 
84
                                myData.pDiskUsageList = g_list_append (myData.pDiskUsageList, pDiskUsage);
 
85
                        }
 
86
                        
 
87
                        cd_shortcuts_get_fs_stat (pIcon->acCommand, pDiskUsage);
 
88
                }
 
89
        }
 
90
}
 
91
 
 
92
gboolean cd_shortcuts_update_disk_usage (CairoDockModuleInstance *myApplet)
 
93
{
 
94
        g_return_val_if_fail (myData.pDiskUsageList != NULL, TRUE);
 
95
        
 
96
        CairoContainer *pContainer = CD_APPLET_MY_ICONS_LIST_CONTAINER;
 
97
        GList *pElement = myData.pDiskUsageList;
 
98
        CDDiskUsage *pDiskUsage;
 
99
        Icon *pIcon;
 
100
        double fValue;
 
101
        GList *ic;
 
102
        GList *pIconsList = CD_APPLET_MY_ICONS_LIST;
 
103
        for (ic = pIconsList; ic != NULL; ic = ic->next)
 
104
        {
 
105
                pIcon = ic->data;
 
106
                if (pIcon->iType != 6)
 
107
                        break;
 
108
                if (pIcon->acCommand != NULL && pElement != NULL)
 
109
                {
 
110
                        pDiskUsage = pElement->data;
 
111
                        if (pDiskUsage->iPrevAvail != pDiskUsage->iAvail)
 
112
                        {
 
113
                                switch (myConfig.iDisplayType)
 
114
                                {
 
115
                                        case CD_SHOW_FREE_SPACE :
 
116
                                                fValue = (double) pDiskUsage->iAvail / pDiskUsage->iTotal;
 
117
                                                cairo_dock_set_size_as_quick_info (myDrawContext, pIcon, pContainer, pDiskUsage->iAvail);
 
118
                                        break ;
 
119
                                        case CD_SHOW_USED_SPACE :
 
120
                                                fValue = (double) - pDiskUsage->iUsed / pDiskUsage->iTotal;  // <0 => du vert au rouge.
 
121
                                                cairo_dock_set_size_as_quick_info (myDrawContext, pIcon, pContainer, pDiskUsage->iUsed);
 
122
                                        break ;
 
123
                                        case CD_SHOW_FREE_SPACE_PERCENT :
 
124
                                                fValue = (double) pDiskUsage->iAvail / pDiskUsage->iTotal;
 
125
                                                cairo_dock_set_quick_info_full (myDrawContext, pIcon, pContainer, "%.1f%%", 100.*fValue);
 
126
                                        break ;
 
127
                                        case CD_SHOW_USED_SPACE_PERCENT :
 
128
                                                fValue = (double) - pDiskUsage->iUsed / pDiskUsage->iTotal;  // <0 => du vert au rouge.
 
129
                                                cairo_dock_set_quick_info_full (myDrawContext, pIcon, pContainer, "%.1f%%", -100.*fValue);
 
130
                                        break ;
 
131
                                }
 
132
                                
 
133
                                if (myConfig.bDrawBar)
 
134
                                {
 
135
                                        int iWidth, iHeight;
 
136
                                        cairo_dock_get_icon_extent (pIcon, pContainer, &iWidth, &iHeight);
 
137
                                        cairo_surface_t *pSurface = cairo_dock_create_surface_for_icon (pIcon->acFileName, myDrawContext, iWidth, iHeight);;
 
138
                                        cairo_t *pIconContext = cairo_create (pIcon->pIconBuffer);
 
139
                                        
 
140
                                        cairo_dock_set_icon_surface_with_bar (pIconContext, pSurface, fValue, pIcon, pContainer);
 
141
                                        
 
142
                                        cairo_destroy (pIconContext);
 
143
                                        cairo_surface_destroy (pSurface);
 
144
                                }
 
145
                                
 
146
                                if (pDiskUsage->iPrevAvail != 0)
 
147
                                        cairo_dock_redraw_icon (pIcon, pContainer);
 
148
                        }
 
149
                        pElement = pElement->next;
 
150
                }
 
151
        }
 
152
        
 
153
        return TRUE;
 
154
}
 
155
 
 
156
 
 
157
void cd_shortcuts_stop_disk_periodic_task (CairoDockModuleInstance *myApplet)
 
158
{
 
159
        cairo_dock_stop_task (myData.pDiskTask);
 
160
        g_list_foreach (myData.pDiskUsageList, (GFunc) g_free, NULL);
 
161
        g_list_free (myData.pDiskUsageList);
 
162
        myData.pDiskUsageList = NULL;
 
163
}
 
164
 
 
165
void cd_shortcuts_launch_disk_periodic_task (CairoDockModuleInstance *myApplet)
 
166
{
 
167
        if (myConfig.iDisplayType != CD_SHOW_NOTHING)
 
168
        {
 
169
                if (myData.pDiskTask == NULL)
 
170
                {
 
171
                        myData.pDiskTask = cairo_dock_new_task (myConfig.iCheckInterval,
 
172
                                (CairoDockGetDataAsyncFunc) cd_shortcuts_get_disk_usage, 
 
173
                                (CairoDockUpdateSyncFunc) cd_shortcuts_update_disk_usage, 
 
174
                                myApplet);
 
175
                }
 
176
                cairo_dock_launch_task (myData.pDiskTask);
 
177
        }
 
178
}
 
179
 
 
180
 
 
181
 
 
182
void cd_shortcuts_get_fs_info (const gchar *cDiskURI, GString *sInfo)
 
183
{
 
184
        const gchar *cMountPath = (strncmp (cDiskURI, "file://", 7) == 0 ? cDiskURI + 7 : cDiskURI);
 
185
        struct mntent *me;
 
186
        FILE *mtab = setmntent ("/etc/mtab", "r");
 
187
        char *search_path;
 
188
        int match;
 
189
        char *slash;
 
190
 
 
191
        if (mtab == NULL)
 
192
        {
 
193
                cd_warning ("couldn't open /etc/mtab");
 
194
                return ;
 
195
        }
 
196
        
 
197
        gchar *cFsInfo = NULL;
 
198
        while ((me = getmntent (mtab)) != NULL)
 
199
        {
 
200
                if (me->mnt_dir && strcmp (me->mnt_dir, cMountPath) == 0)
 
201
                {
 
202
                        g_string_append_printf (sInfo, "Mount point : %s\nFile system : %s\nDevice : %s\nMount options : %s",
 
203
                                me->mnt_dir,
 
204
                                me->mnt_type,
 
205
                                me->mnt_fsname,
 
206
                                me->mnt_opts);
 
207
                        if (me->mnt_freq != 0)
 
208
                                g_string_append_printf (sInfo, "\nBackup frequency : %d days", me->mnt_freq);
 
209
                        break ;
 
210
                }
 
211
        }
 
212
        
 
213
        endmntent (mtab);
 
214
}