1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libintl.h>
#include <sys/stat.h>
#include <gtk/gtk.h>
#include <libgnomevfs/gnome-vfs.h>
#include <gpe/spacing.h>
#define _(x) gettext(x)
#define FILE_MTAB "/etc/mtab"
typedef struct
{
gchar *name;
gchar *path;
gboolean mountable;
GtkWidget *item;
}
t_storage;
static t_storage *storages = NULL;
static int num_storage = 0;
/*static time_t mtab_time = 0;*/
/* checks if a tree is mounted */
static gboolean
is_mounted(gchar *path)
{
FILE *mtab = fopen(FILE_MTAB, "r");
gboolean result = FALSE;
if (mtab)
{
char buf[255];
char target[255];
while (fgets(buf, 255, mtab))
{
if (sscanf(buf, "%*s %s %*s %*s %*i %*i", target))
if (!strcmp(target, path))
{
result = TRUE;
break;
}
}
fclose(mtab);
}
return result;
}
gboolean
do_scheduled_update(void)
{
int i;
for (i=0; i<num_storage; i++)
{
if (storages[i].mountable)
{
if (is_mounted(storages[i].path))
gtk_widget_show(storages[i].item);
else
gtk_widget_hide(storages[i].item);
}
}
return FALSE;
}
static void
mountpoint_changed (GnomeVFSMonitorHandle *handle,
const gchar *monitor_uri,
const gchar *info_uri,
GnomeVFSMonitorEventType event_type,
gpointer user_data)
{
if (event_type == GNOME_VFS_MONITOR_EVENT_CHANGED)
{
do_scheduled_update();
}
}
static void
activate_item(int i)
{
if ((i < 0) || (i > num_storage-1))
return;
if (GTK_IS_TOGGLE_BUTTON(storages[i].item))
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(storages[i].item), TRUE);
else if (GTK_IS_RADIO_MENU_ITEM(storages[i].item))
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(storages[i].item), TRUE);
}
void
set_active_item(char* path)
{
int i;
if (g_str_has_prefix(path, storages[0].path)) /* are we in $HOME? */
{
activate_item(0);
return;
}
else if (strcmp("/", path))
{
for (i = 2; i < num_storage; i++)
{
if (g_str_has_prefix(path, storages[i].path))
{
activate_item(i);
return;
}
}
}
/* nothing? activate filesystem button per default */
activate_item(1);
}
static void
filesystem_toggled (GtkToggleButton *togglebutton, gpointer user_data)
{
t_storage *st = user_data;
if (GTK_IS_TOGGLE_BUTTON(togglebutton))
{
if (gtk_toggle_button_get_active(togglebutton))
browse_directory(st->path);
}
else if (GTK_IS_RADIO_MENU_ITEM(togglebutton))
{
if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(togglebutton)))
browse_directory(st->path);
}
}
void
add_storage(gchar* name, const gchar *path, gboolean isdevice)
{
/* populate struct */
num_storage++;
storages = realloc(storages, sizeof(t_storage) * num_storage);
storages[num_storage - 1].name = name;
storages[num_storage - 1].path = g_strdup(path);
storages[num_storage - 1].mountable = isdevice;
}
/* searches for interesting devices in fstab */
void
scan_fstab(void)
{
FILE *fstab = fopen("/etc/fstab", "r");
if (fstab)
{
char buf[255];
while (fgets(buf, 255, fstab))
{
char path[128];
if (sscanf(buf, "%*s %128s %*s %*s %*d %*d", path))
{
if (strstr(path, "card") || strstr(path, "mmc"))
add_storage(_("MMC-Card"), path, TRUE);
else if (strstr(path, "pc"))
add_storage(_("PC-Card"), path, TRUE);
else if (strstr(path, "cf") || strstr(path, "hda"))
add_storage(_("CF-Card"), path, TRUE);
else if (strstr(path, "usb") && !strstr(path, "proc"))
add_storage(_("USB-Device"), path, TRUE);
else if (strstr(path, "cdrom"))
add_storage(_("CDROM"), path, TRUE);
}
}
fclose(fstab);
}
}
gboolean
check_mount(void)
{
/* struct stat sdat;*/
do_scheduled_update();
/*
if (!stat(FILE_MTAB, &sdat))
{
if (mtab_time != sdat.st_ctime)
{
do_scheduled_update();
mtab_time = sdat.st_ctime;
}
}
*/
return TRUE;
}
GtkWidget *
build_storage_menu(gboolean wide)
{
GnomeVFSMonitorHandle *dir_monitor = NULL;
GtkWidget *vmenu;
GtkWidget *item = NULL;
int i, ret;
/* add defaults */
add_storage(_("My Documents"), g_get_home_dir(), FALSE); /* if you change this order, change below too */
add_storage(_("Filesystem"), "/", FALSE);
scan_fstab();
/* create widgets depending on usable display size */
if (wide)
{
vmenu = gtk_hbox_new(FALSE, gpe_get_boxspacing());
gtk_container_set_border_width(GTK_CONTAINER(vmenu), gpe_get_border());
item = gtk_radio_button_new_with_label(NULL, storages[0].name);
storages[0].item = item;
gtk_toggle_button_set_mode(GTK_TOGGLE_BUTTON(item), FALSE);
GTK_WIDGET_UNSET_FLAGS(item, GTK_CAN_FOCUS);
gtk_box_pack_start(GTK_BOX(vmenu), item, FALSE, TRUE, 0);
g_signal_connect_after(G_OBJECT(item), "clicked", G_CALLBACK(filesystem_toggled), &storages[0]);
for (i = 1; i < num_storage; i++)
{
item = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(item), storages[i].name);
storages[i].item = item;
gtk_toggle_button_set_mode(GTK_TOGGLE_BUTTON(item), FALSE);
GTK_WIDGET_UNSET_FLAGS(item, GTK_CAN_FOCUS);
gtk_box_pack_start(GTK_BOX(vmenu), item, FALSE, TRUE, 0);
g_signal_connect_after(G_OBJECT(item), "clicked", G_CALLBACK(filesystem_toggled), &storages[i]);
}
}
else
{
vmenu = gtk_menu_new();
item = gtk_radio_menu_item_new_with_label(NULL, storages[0].name);
storages[0].item = item;
gtk_menu_append(vmenu, item);
g_signal_connect(G_OBJECT(item), "toggled", G_CALLBACK(filesystem_toggled), &storages[0]);
for (i = 1; i < num_storage; i++)
{
item = gtk_radio_menu_item_new_with_label_from_widget(GTK_RADIO_MENU_ITEM(item), storages[i].name);
storages[i].item = item;
gtk_menu_append(vmenu, item);
g_signal_connect(G_OBJECT(item), "toggled", G_CALLBACK(filesystem_toggled), &storages[i]);
}
}
/* set initial activation status */
do_scheduled_update();
/* install monitor to get info if mtab changes */
if ((ret = gnome_vfs_monitor_add(&dir_monitor,
FILE_MTAB,
GNOME_VFS_MONITOR_FILE,
(GnomeVFSMonitorCallback)mountpoint_changed,
NULL)) != GNOME_VFS_OK)
{
fprintf(stderr, "Installing monitor failed: %s\n",
gnome_vfs_result_to_string(ret));
fprintf(stderr, "Polling...\n");
g_timeout_add(1000, (GSourceFunc)check_mount, NULL);
}
return vmenu;
}
|