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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* gnome-vfs-volume-monitor.c - Central volume handling.
Copyright (C) 2003 Red Hat, Inc
The Gnome Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Gnome Library 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 Library General Public
License along with the Gnome Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Author: Alexander Larsson <alexl@redhat.com>
*/
#include <config.h>
#include <string.h>
#include "gnome-vfs-private.h"
#include "gnome-vfs-volume-monitor.h"
#include "gnome-vfs-volume-monitor-private.h"
#ifdef USE_DAEMON
#include "gnome-vfs-volume-monitor-client.h"
#endif
#ifdef G_OS_WIN32
#define _WIN32_WINNT 0x0501
#include <windows.h>
#endif
#include <glib/gstdio.h>
static void gnome_vfs_volume_monitor_class_init (GnomeVFSVolumeMonitorClass *klass);
static void gnome_vfs_volume_monitor_init (GnomeVFSVolumeMonitor *volume_monitor);
static void gnome_vfs_volume_monitor_finalize (GObject *object);
enum
{
VOLUME_MOUNTED,
VOLUME_PRE_UNMOUNT,
VOLUME_UNMOUNTED,
DRIVE_CONNECTED,
DRIVE_DISCONNECTED,
LAST_SIGNAL
};
static GObjectClass *parent_class = NULL;
static guint volume_monitor_signals [LAST_SIGNAL] = { 0 };
GType
gnome_vfs_volume_monitor_get_type (void)
{
static GType volume_monitor_type = 0;
if (!volume_monitor_type) {
const GTypeInfo volume_monitor_info = {
sizeof (GnomeVFSVolumeMonitorClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) gnome_vfs_volume_monitor_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (GnomeVFSVolumeMonitor),
0, /* n_preallocs */
(GInstanceInitFunc) gnome_vfs_volume_monitor_init
};
volume_monitor_type =
g_type_register_static (G_TYPE_OBJECT, "GnomeVFSVolumeMonitor",
&volume_monitor_info, 0);
}
return volume_monitor_type;
}
static void
gnome_vfs_volume_monitor_class_init (GnomeVFSVolumeMonitorClass *class)
{
GObjectClass *o_class;
parent_class = g_type_class_peek_parent (class);
o_class = (GObjectClass *) class;
/* GObject signals */
o_class->finalize = gnome_vfs_volume_monitor_finalize;
/**
* GnomeVFSVolumeMonitor::volume-mounted:
* @volume_monitor: the #GnomeVFSVolumeMonitor which received the signal.
* @volume: the #GnomeVFSVolume that has been mounted.
*
* This signal is emitted after the #GnomeVFSVolume @volume has been mounted.
*
* When the @volume is mounted, it is present in the @volume_monitor's list of mounted
* volumes, which can be queried using gnome_vfs_volume_monitor_get_mounted_volumes().
*
* If the @volume has an associated #GnomeVFSDrive, it also appears in the drive's
* list of mounted volumes, which can be queried using gnome_vfs_drive_get_mounted_volumes().
**/
volume_monitor_signals[VOLUME_MOUNTED] =
g_signal_new ("volume_mounted",
G_TYPE_FROM_CLASS (o_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GnomeVFSVolumeMonitorClass, volume_mounted),
NULL, NULL,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE, 1,
GNOME_VFS_TYPE_VOLUME);
/**
* GnomeVFSVolumeMonitor::volume-pre-unmount:
* @volume_monitor: the #GnomeVFSVolumeMonitor which received the signal.
* @volume: the #GnomeVFSVolume that is about to be unmounted.
*
* This signal is emitted when the #GnomeVFSVolume @volume is about to be unmounted.
*
* When the @volume is unmounted, it is removed from the @volume_monitor's list of mounted
* volumes, which can be queried using gnome_vfs_volume_monitor_get_mounted_volumes().
*
* If the @volume has an associated #GnomeVFSDrive, it is also removed from in the drive's
* list of mounted volumes, which can be queried using gnome_vfs_drive_get_mounted_volumes().
*
* When a client application receives this signal, it must free all resources
* associated with the @volume, for instance cancel all pending file operations
* on the @volume, and cancel all pending file monitors using gnome_vfs_monitor_cancel().
**/
volume_monitor_signals[VOLUME_PRE_UNMOUNT] =
g_signal_new ("volume_pre_unmount",
G_TYPE_FROM_CLASS (o_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GnomeVFSVolumeMonitorClass, volume_pre_unmount),
NULL, NULL,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE, 1,
GNOME_VFS_TYPE_VOLUME);
/**
* GnomeVFSVolumeMonitor::volume-unmounted:
* @volume_monitor: the #GnomeVFSVolumeMonitor which received the signal.
* @volume: the #GnomeVFSVolume that has been unmounted.
*
* This signal is emitted after the #GnomeVFSVolume @volume has been unmounted.
*
* When the @volume is unmounted, it is removed from the @volume_monitor's list of mounted
* volumes, which can be queried using gnome_vfs_volume_monitor_get_mounted_volumes().
*
* If the @volume has an associated #GnomeVFSDrive, it is also removed from in the drive's
* list of mounted volumes, which can be queried using gnome_vfs_drive_get_mounted_volumes().
**/
volume_monitor_signals[VOLUME_UNMOUNTED] =
g_signal_new ("volume_unmounted",
G_TYPE_FROM_CLASS (o_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GnomeVFSVolumeMonitorClass, volume_unmounted),
NULL, NULL,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE, 1,
GNOME_VFS_TYPE_VOLUME);
/**
* GnomeVFSVolumeMonitor::drive-connected:
* @volume_monitor: the #GnomeVFSVolumeMonitor which received the signal.
* @drive: the #GnomeVFSDrive that has been connected.
*
* This signal is emitted when the #GnomeVFSDrive @drive has been connected.
*
* When the @drive is connected, it is present in the @volume_monitor's list of connected
* drives, which can be queried using gnome_vfs_volume_monitor_get_connected_drives().
**/
volume_monitor_signals[DRIVE_CONNECTED] =
g_signal_new ("drive_connected",
G_TYPE_FROM_CLASS (o_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GnomeVFSVolumeMonitorClass, drive_connected),
NULL, NULL,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE, 1,
GNOME_VFS_TYPE_DRIVE);
/**
* GnomeVFSVolumeMonitor::drive-disconnected:
* @volume_monitor: the #GnomeVFSVolumeMonitor which received the signal.
* @drive: the #GnomeVFSDrive that has been disconnected.
*
* This signal is emitted after the #GnomeVFSDrive @drive has been disconnected.
*
* When the @drive is disconnected, it is removed from the @volume_monitor's list of connected
* drives, which can be queried using gnome_vfs_volume_monitor_get_connected_drives().
**/
volume_monitor_signals[DRIVE_DISCONNECTED] =
g_signal_new ("drive_disconnected",
G_TYPE_FROM_CLASS (o_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GnomeVFSVolumeMonitorClass, drive_disconnected),
NULL, NULL,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE, 1,
GNOME_VFS_TYPE_DRIVE);
}
static void
gnome_vfs_volume_monitor_init (GnomeVFSVolumeMonitor *volume_monitor)
{
volume_monitor->priv = g_new0 (GnomeVFSVolumeMonitorPrivate, 1);
volume_monitor->priv->mutex = g_mutex_new ();
}
G_LOCK_DEFINE_STATIC (volume_monitor_ref);
/**
* gnome_vfs_volume_monitor_ref:
* @volume_monitor: the #GnomeVFSVolumeMonitor, or %NULL.
*
* Increases the refcount of @volume_monitor by one, if it is not %NULL.
*
* You shouldn't use this function unless you know what you are doing:
* #GnomeVFSVolumeMonitor is to be used as a singleton object, see
* gnome_vfs_get_volume_monitor() for more details.
*
* Returns: @volume_monitor with its refcount increased by one,
* or %NULL if @volume_monitor is %NULL.
*
* Since: 2.6
*/
GnomeVFSVolumeMonitor *
gnome_vfs_volume_monitor_ref (GnomeVFSVolumeMonitor *volume_monitor)
{
if (volume_monitor == NULL) {
return NULL;
}
G_LOCK (volume_monitor_ref);
g_object_ref (volume_monitor);
G_UNLOCK (volume_monitor_ref);
return volume_monitor;
}
/**
* gnome_vfs_volume_monitor_unref:
* @volume_monitor: #GnomeVFSVolumeMonitor, or %NULL.
*
* Decreases the refcount of @volume_monitor by one, if it is not %NULL.
*
* You shouldn't use this function unless you know what you are doing:
* #GnomeVFSVolumeMonitor is to be used as a singleton object, see
* gnome_vfs_get_volume_monitor() for more details.
*
* Since: 2.6
*/
void
gnome_vfs_volume_monitor_unref (GnomeVFSVolumeMonitor *volume_monitor)
{
if (volume_monitor == NULL) {
return;
}
G_LOCK (volume_monitor_ref);
g_object_unref (volume_monitor);
G_UNLOCK (volume_monitor_ref);
}
/* Remeber that this could be running on a thread other
* than the main thread */
static void
gnome_vfs_volume_monitor_finalize (GObject *object)
{
GnomeVFSVolumeMonitor *volume_monitor = (GnomeVFSVolumeMonitor *) object;
GnomeVFSVolumeMonitorPrivate *priv;
priv = volume_monitor->priv;
g_list_foreach (priv->mtab_volumes,
(GFunc)gnome_vfs_volume_unref, NULL);
g_list_free (priv->mtab_volumes);
g_list_foreach (priv->server_volumes,
(GFunc)gnome_vfs_volume_unref, NULL);
g_list_free (priv->server_volumes);
g_list_foreach (priv->vfs_volumes,
(GFunc)gnome_vfs_volume_unref, NULL);
g_list_free (priv->vfs_volumes);
g_list_foreach (priv->fstab_drives,
(GFunc)gnome_vfs_drive_unref, NULL);
g_list_free (priv->fstab_drives);
g_list_foreach (priv->vfs_drives,
(GFunc)gnome_vfs_drive_unref, NULL);
g_list_free (priv->vfs_drives);
g_mutex_free (priv->mutex);
g_free (priv);
volume_monitor->priv = NULL;
if (G_OBJECT_CLASS (parent_class)->finalize)
(* G_OBJECT_CLASS (parent_class)->finalize) (object);
}
#ifndef USE_DAEMON
#ifdef G_OS_WIN32
static void
handle_volume (GnomeVFSVolumeMonitor *volume_monitor,
wchar_t *volume_device)
{
wchar_t path_names[MAX_PATH*10];
DWORD path_names_len;
if (GetVolumePathNamesForVolumeNameW (volume_device,
path_names, G_N_ELEMENTS (path_names),
&path_names_len)) {
GnomeVFSVolume *volume;
GnomeVFSVolumePrivate *volume_priv;
GnomeVFSDrive *drive;
GnomeVFSDrivePrivate *drive_priv;
UINT drive_type;
wchar_t volume_name[MAX_PATH];
wchar_t file_system_name[MAX_PATH];
gchar *tmp;
DWORD file_system_flags;
drive_type = GetDriveTypeW (volume_device);
if (drive_type == DRIVE_UNKNOWN ||
drive_type == DRIVE_NO_ROOT_DIR)
return;
drive = g_object_new (GNOME_VFS_TYPE_DRIVE, NULL);
drive_priv = drive->priv;
drive_priv->device_type = GNOME_VFS_DEVICE_TYPE_WINDOWS;
drive_priv->device_path = g_utf16_to_utf8 (volume_device, -1, NULL, NULL, NULL);
drive_priv->activation_uri = g_filename_to_uri (drive_priv->device_path, NULL, NULL);
volume_name[0] = '\0';
file_system_name[0] = '\0';
GetVolumeInformationW (volume_device, volume_name, G_N_ELEMENTS (volume_name),
NULL, NULL,
&file_system_flags,
file_system_name, G_N_ELEMENTS (file_system_name));
if (volume_name[0])
drive_priv->display_name = g_utf16_to_utf8 (volume_name, -1, NULL, NULL, NULL);
else if (path_names[0])
drive_priv->display_name = g_utf16_to_utf8 (path_names, -1, NULL, NULL, NULL);
else
drive_priv->display_name = g_strdup (drive_priv->device_path);
tmp = g_utf8_casefold (drive_priv->display_name, -1);
drive_priv->display_name_key = g_utf8_collate_key (tmp, -1);
g_free (tmp);
/* ??? */
drive_priv->is_user_visible = TRUE;
drive_priv->must_eject_at_unmount = FALSE;
_gnome_vfs_volume_monitor_connected (volume_monitor, drive);
gnome_vfs_drive_unref (drive);
volume = g_object_new (GNOME_VFS_TYPE_VOLUME, NULL);
volume_priv = volume->priv;
volume_priv->volume_type = GNOME_VFS_VOLUME_TYPE_MOUNTPOINT;
volume_priv->device_type = GNOME_VFS_DEVICE_TYPE_WINDOWS;
volume_priv->drive = drive;
gnome_vfs_drive_add_mounted_volume_private (drive, volume);
volume_priv->activation_uri = g_strdup (drive_priv->activation_uri);
volume_priv->filesystem_type = g_utf16_to_utf8 (file_system_name, -1, NULL, NULL, NULL);
volume_priv->display_name = g_strdup (drive_priv->display_name);
volume_priv->display_name_key = g_strdup (drive_priv->display_name_key);
volume_priv->is_user_visible = TRUE;
volume_priv->is_read_only = FALSE;
volume_priv->device_path = g_strdup (drive_priv->device_path);
volume_priv->unix_device = 0;
_gnome_vfs_volume_monitor_mounted (volume_monitor, volume);
gnome_vfs_volume_unref (volume);
}
}
#endif
/* Without gnome-vfs-daemon, initialise the volume data just once and hope it
* doesn't change too much while the application is running.
*/
static void
initialise_the_volume_monitor (GnomeVFSVolumeMonitor *volume_monitor)
{
#ifdef G_OS_WIN32
wchar_t volume_device[MAX_PATH];
HANDLE volume;
BOOL more = TRUE;
volume = FindFirstVolumeW (volume_device, G_N_ELEMENTS (volume_device));
if (volume == INVALID_HANDLE_VALUE)
return;
while (more) {
handle_volume (volume_monitor, volume_device);
more = FindNextVolumeW (volume, volume_device, G_N_ELEMENTS (volume_device));
}
FindVolumeClose (volume);
#else
#error Add code here for once-only initialisation of the volume pseudo-monitor
#endif
}
#endif
G_LOCK_DEFINE_STATIC (the_volume_monitor);
static GnomeVFSVolumeMonitor *the_volume_monitor = NULL;
static gboolean volume_monitor_was_shutdown = FALSE;
GnomeVFSVolumeMonitor *
_gnome_vfs_get_volume_monitor_internal (gboolean create)
{
G_LOCK (the_volume_monitor);
if (the_volume_monitor == NULL &&
create &&
!volume_monitor_was_shutdown) {
#ifdef USE_DAEMON
if (gnome_vfs_get_is_daemon ()) {
the_volume_monitor = g_object_new (gnome_vfs_get_daemon_volume_monitor_type (), NULL);
} else {
the_volume_monitor = g_object_new (GNOME_VFS_TYPE_VOLUME_MONITOR_CLIENT, NULL);
}
#else
the_volume_monitor = g_object_new (GNOME_VFS_TYPE_VOLUME_MONITOR, NULL);
initialise_the_volume_monitor (the_volume_monitor);
#endif
}
G_UNLOCK (the_volume_monitor);
return the_volume_monitor;
}
/**
* gnome_vfs_get_volume_monitor:
*
* Returns a pointer to the #GnomeVFSVolumeMonitor singleton.
* #GnomeVFSVolumeMonitor is a singleton, this means it is guaranteed to
* exist and be valid until gnome_vfs_shutdown() is called. Consequently,
* it doesn't need to be refcounted since gnome-vfs will hold a reference to
* it until it is shut down.
*
* Returns: a pointer to the #GnomeVFSVolumeMonitor singleton.
*
* Since: 2.6
*/
GnomeVFSVolumeMonitor *
gnome_vfs_get_volume_monitor (void)
{
return _gnome_vfs_get_volume_monitor_internal (TRUE);
}
void
_gnome_vfs_volume_monitor_shutdown (void)
{
G_LOCK (the_volume_monitor);
if (the_volume_monitor != NULL) {
#ifdef USE_DAEMON
if (!gnome_vfs_get_is_daemon ()) {
gnome_vfs_volume_monitor_client_shutdown_private (GNOME_VFS_VOLUME_MONITOR_CLIENT (the_volume_monitor));
}
#endif
gnome_vfs_volume_monitor_unref (the_volume_monitor);
the_volume_monitor = NULL;
}
volume_monitor_was_shutdown = TRUE;
G_UNLOCK (the_volume_monitor);
}
#ifdef USE_HAL
GnomeVFSVolume *
_gnome_vfs_volume_monitor_find_volume_by_hal_udi (GnomeVFSVolumeMonitor *volume_monitor,
const char *hal_udi)
{
GList *l;
GnomeVFSVolume *vol, *ret;
/* Doesn't need locks, only called internally on main thread and doesn't write */
ret = NULL;
for (l = volume_monitor->priv->mtab_volumes; l != NULL; l = l->next) {
vol = l->data;
if (vol->priv != NULL && vol->priv->hal_udi != NULL &&
strcmp (vol->priv->hal_udi, hal_udi) == 0) {
ret = vol;
break;
}
}
/* burn:/// and cdda:// optical discs are by the hal backend added as VFS_MOUNT */
if (ret == NULL) {
for (l = volume_monitor->priv->vfs_volumes; l != NULL; l = l->next) {
vol = l->data;
if (vol->priv != NULL && vol->priv->hal_drive_udi != NULL &&
strcmp (vol->priv->hal_udi, hal_udi) == 0) {
ret = vol;
break;
}
}
}
return ret;
}
GnomeVFSDrive *
_gnome_vfs_volume_monitor_find_drive_by_hal_udi (GnomeVFSVolumeMonitor *volume_monitor,
const char *hal_udi)
{
GList *l;
GnomeVFSDrive *drive, *ret;
/* Doesn't need locks, only called internally on main thread and doesn't write */
ret = NULL;
for (l = volume_monitor->priv->fstab_drives; l != NULL; l = l->next) {
drive = l->data;
if (drive->priv != NULL && drive->priv->hal_udi != NULL &&
strcmp (drive->priv->hal_udi, hal_udi) == 0) {
ret = drive;
break;
}
}
return ret;
}
GnomeVFSVolume *
_gnome_vfs_volume_monitor_find_volume_by_hal_drive_udi (GnomeVFSVolumeMonitor *volume_monitor,
const char *hal_drive_udi)
{
GList *l;
GnomeVFSVolume *vol, *ret;
/* Doesn't need locks, only called internally on main thread and doesn't write */
ret = NULL;
for (l = volume_monitor->priv->mtab_volumes; l != NULL; l = l->next) {
vol = l->data;
if (vol->priv != NULL && vol->priv->hal_drive_udi != NULL &&
strcmp (vol->priv->hal_drive_udi, hal_drive_udi) == 0) {
ret = vol;
break;
}
}
/* burn:/// and cdda:// optical discs are by the hal backend added as VFS_MOUNT */
if (ret == NULL) {
for (l = volume_monitor->priv->vfs_volumes; l != NULL; l = l->next) {
vol = l->data;
if (vol->priv != NULL && vol->priv->hal_drive_udi != NULL &&
strcmp (vol->priv->hal_drive_udi, hal_drive_udi) == 0) {
ret = vol;
break;
}
}
}
return ret;
}
GnomeVFSDrive *
_gnome_vfs_volume_monitor_find_drive_by_hal_drive_udi (GnomeVFSVolumeMonitor *volume_monitor,
const char *hal_drive_udi)
{
GList *l;
GnomeVFSDrive *drive, *ret;
/* Doesn't need locks, only called internally on main thread and doesn't write */
ret = NULL;
for (l = volume_monitor->priv->fstab_drives; l != NULL; l = l->next) {
drive = l->data;
if (drive->priv != NULL && drive->priv->hal_drive_udi != NULL &&
strcmp (drive->priv->hal_drive_udi, hal_drive_udi) == 0) {
ret = drive;
break;
}
}
return ret;
}
#endif /* USE_HAL */
GnomeVFSVolume *
_gnome_vfs_volume_monitor_find_volume_by_device_path (GnomeVFSVolumeMonitor *volume_monitor,
const char *device_path)
{
GList *l;
GnomeVFSVolume *vol, *ret;
/* Doesn't need locks, only called internally on main thread and doesn't write */
ret = NULL;
for (l = volume_monitor->priv->mtab_volumes; l != NULL; l = l->next) {
vol = l->data;
if (vol->priv != NULL &&
vol->priv->device_path != NULL && /* Hmm */
strcmp (vol->priv->device_path, device_path) == 0) {
ret = vol;
break;
}
}
return ret;
}
GnomeVFSDrive *
_gnome_vfs_volume_monitor_find_drive_by_device_path (GnomeVFSVolumeMonitor *volume_monitor,
const char *device_path)
{
GList *l;
GnomeVFSDrive *drive, *ret;
/* Doesn't need locks, only called internally on main thread and doesn't write */
ret = NULL;
for (l = volume_monitor->priv->fstab_drives; l != NULL; l = l->next) {
drive = l->data;
if (drive->priv != NULL &&
drive->priv->device_path != NULL && /* Hmm */
strcmp (drive->priv->device_path, device_path) == 0) {
ret = drive;
break;
}
}
return ret;
}
GnomeVFSVolume *
_gnome_vfs_volume_monitor_find_mtab_volume_by_activation_uri (GnomeVFSVolumeMonitor *volume_monitor,
const char *activation_uri)
{
GList *l;
GnomeVFSVolume *vol, *ret;
/* Doesn't need locks, only called internally on main thread and doesn't write */
ret = NULL;
for (l = volume_monitor->priv->mtab_volumes; l != NULL; l = l->next) {
vol = l->data;
if (vol->priv->activation_uri != NULL &&
strcmp (vol->priv->activation_uri, activation_uri) == 0) {
ret = vol;
break;
}
}
return ret;
}
GnomeVFSDrive *
_gnome_vfs_volume_monitor_find_fstab_drive_by_activation_uri (GnomeVFSVolumeMonitor *volume_monitor,
const char *activation_uri)
{
GList *l;
GnomeVFSDrive *drive, *ret;
/* Doesn't need locks, only called internally on main thread and doesn't write */
ret = NULL;
for (l = volume_monitor->priv->fstab_drives; l != NULL; l = l->next) {
drive = l->data;
if (drive->priv->activation_uri != NULL &&
strcmp (drive->priv->activation_uri, activation_uri) == 0) {
ret = drive;
break;
}
}
return ret;
}
GnomeVFSVolume *
_gnome_vfs_volume_monitor_find_connected_server_by_gconf_id (GnomeVFSVolumeMonitor *volume_monitor,
const char *id)
{
GList *l;
GnomeVFSVolume *vol, *ret;
/* Doesn't need locks, only called internally on main thread and doesn't write */
ret = NULL;
for (l = volume_monitor->priv->server_volumes; l != NULL; l = l->next) {
vol = l->data;
if (vol->priv->gconf_id != NULL &&
strcmp (vol->priv->gconf_id, id) == 0) {
ret = vol;
break;
}
}
return ret;
}
/**
* gnome_vfs_volume_monitor_get_volume_by_id:
* @volume_monitor: a #GnomeVFSVolumeMonitor.
* @id: #GnomeVFSVolume id to look for.
*
* Looks for a #GnomeVFSVolume whose id is @id. A valid @volume_monitor to pass
* to this function can be acquired using gnome_vfs_get_volume_monitor().
*
* Returns: the #GnomeVFSVolume corresponding to @id, or %NULL if no
* #GnomeVFSVolume with a matching id could be found. The caller owns a
* reference on the returned volume, and must call gnome_vfs_volume_unref()
* when it no longer needs it.
*
* Since: 2.6
*/
GnomeVFSVolume *
gnome_vfs_volume_monitor_get_volume_by_id (GnomeVFSVolumeMonitor *volume_monitor,
gulong id)
{
GList *l;
GnomeVFSVolume *vol;
g_mutex_lock (volume_monitor->priv->mutex);
for (l = volume_monitor->priv->mtab_volumes; l != NULL; l = l->next) {
vol = l->data;
if (vol->priv->id == id) {
gnome_vfs_volume_ref (vol);
g_mutex_unlock (volume_monitor->priv->mutex);
return vol;
}
}
for (l = volume_monitor->priv->server_volumes; l != NULL; l = l->next) {
vol = l->data;
if (vol->priv->id == id) {
gnome_vfs_volume_ref (vol);
g_mutex_unlock (volume_monitor->priv->mutex);
return vol;
}
}
for (l = volume_monitor->priv->vfs_volumes; l != NULL; l = l->next) {
vol = l->data;
if (vol->priv->id == id) {
gnome_vfs_volume_ref (vol);
g_mutex_unlock (volume_monitor->priv->mutex);
return vol;
}
}
g_mutex_unlock (volume_monitor->priv->mutex);
return NULL;
}
/**
* gnome_vfs_volume_monitor_get_drive_by_id:
* @volume_monitor: a #GnomeVFSVolumeMonitor.
* @id: the #GnomeVFSVolume id to look for.
*
* Looks for a #GnomeVFSDrive whose id is @id. A valid @volume_monitor to pass
* to this function can be acquired using gnome_vfs_get_volume_monitor()
*
* Returns: the #GnomeVFSDrive corresponding to @id, or %NULL if no
* #GnomeVFSDrive with a matching id could be found. The caller owns a
* reference on the returned drive, and must call gnome_vfs_drive_unref()
* when it no longer needs it.
*
* Since: 2.6
*/
GnomeVFSDrive *
gnome_vfs_volume_monitor_get_drive_by_id (GnomeVFSVolumeMonitor *volume_monitor,
gulong id)
{
GList *l;
GnomeVFSDrive *drive;
g_mutex_lock (volume_monitor->priv->mutex);
for (l = volume_monitor->priv->fstab_drives; l != NULL; l = l->next) {
drive = l->data;
if (drive->priv->id == id) {
gnome_vfs_drive_ref (drive);
g_mutex_unlock (volume_monitor->priv->mutex);
return drive;
}
}
for (l = volume_monitor->priv->vfs_drives; l != NULL; l = l->next) {
drive = l->data;
if (drive->priv->id == id) {
gnome_vfs_drive_ref (drive);
g_mutex_unlock (volume_monitor->priv->mutex);
return drive;
}
}
g_mutex_unlock (volume_monitor->priv->mutex);
return NULL;
}
void
_gnome_vfs_volume_monitor_unmount_all (GnomeVFSVolumeMonitor *volume_monitor)
{
GList *l, *volumes;
GnomeVFSVolume *volume;
volumes = gnome_vfs_volume_monitor_get_mounted_volumes (volume_monitor);
for (l = volumes; l != NULL; l = l->next) {
volume = l->data;
_gnome_vfs_volume_monitor_unmounted (volume_monitor, volume);
gnome_vfs_volume_unref (volume);
}
g_list_free (volumes);
}
void
_gnome_vfs_volume_monitor_disconnect_all (GnomeVFSVolumeMonitor *volume_monitor)
{
GList *l, *drives;
GnomeVFSDrive *drive;
drives = gnome_vfs_volume_monitor_get_connected_drives (volume_monitor);
for (l = drives; l != NULL; l = l->next) {
drive = l->data;
_gnome_vfs_volume_monitor_disconnected (volume_monitor, drive);
gnome_vfs_drive_unref (drive);
}
g_list_free (drives);
}
/**
* gnome_vfs_volume_monitor_emit_pre_unmount:
* @volume_monitor: a #GnomeVFSVolumeMonitor.
* @volume: a #GnomeVFSVolume.
*
* Emits the "pre-unmount" signal on @volume.
*
* Since: 2.6
*/
void
gnome_vfs_volume_monitor_emit_pre_unmount (GnomeVFSVolumeMonitor *volume_monitor,
GnomeVFSVolume *volume)
{
g_signal_emit (volume_monitor, volume_monitor_signals[VOLUME_PRE_UNMOUNT], 0, volume);
}
void
_gnome_vfs_volume_monitor_mounted (GnomeVFSVolumeMonitor *volume_monitor,
GnomeVFSVolume *volume)
{
gnome_vfs_volume_ref (volume);
g_mutex_lock (volume_monitor->priv->mutex);
switch (volume->priv->volume_type) {
case GNOME_VFS_VOLUME_TYPE_MOUNTPOINT:
volume_monitor->priv->mtab_volumes = g_list_prepend (volume_monitor->priv->mtab_volumes,
volume);
break;
case GNOME_VFS_VOLUME_TYPE_CONNECTED_SERVER:
volume_monitor->priv->server_volumes = g_list_prepend (volume_monitor->priv->server_volumes,
volume);
break;
case GNOME_VFS_VOLUME_TYPE_VFS_MOUNT:
volume_monitor->priv->vfs_volumes = g_list_prepend (volume_monitor->priv->vfs_volumes,
volume);
break;
default:
g_assert_not_reached ();
}
volume->priv->is_mounted = 1;
g_mutex_unlock (volume_monitor->priv->mutex);
g_signal_emit (volume_monitor, volume_monitor_signals[VOLUME_MOUNTED], 0, volume);
}
void
_gnome_vfs_volume_monitor_unmounted (GnomeVFSVolumeMonitor *volume_monitor,
GnomeVFSVolume *volume)
{
GnomeVFSDrive *drive;
g_mutex_lock (volume_monitor->priv->mutex);
volume_monitor->priv->mtab_volumes = g_list_remove (volume_monitor->priv->mtab_volumes, volume);
volume_monitor->priv->server_volumes = g_list_remove (volume_monitor->priv->server_volumes, volume);
volume_monitor->priv->vfs_volumes = g_list_remove (volume_monitor->priv->vfs_volumes, volume);
volume->priv->is_mounted = 0;
g_mutex_unlock (volume_monitor->priv->mutex);
g_signal_emit (volume_monitor, volume_monitor_signals[VOLUME_UNMOUNTED], 0, volume);
drive = volume->priv->drive;
if (drive != NULL) {
gnome_vfs_volume_unset_drive_private (volume, drive);
gnome_vfs_drive_remove_volume_private (drive, volume);
}
gnome_vfs_volume_unref (volume);
}
void
_gnome_vfs_volume_monitor_connected (GnomeVFSVolumeMonitor *volume_monitor,
GnomeVFSDrive *drive)
{
gnome_vfs_drive_ref (drive);
g_mutex_lock (volume_monitor->priv->mutex);
volume_monitor->priv->fstab_drives = g_list_prepend (volume_monitor->priv->fstab_drives, drive);
drive->priv->is_connected = 1;
g_mutex_unlock (volume_monitor->priv->mutex);
g_signal_emit (volume_monitor, volume_monitor_signals[DRIVE_CONNECTED], 0, drive);
}
void
_gnome_vfs_volume_monitor_disconnected (GnomeVFSVolumeMonitor *volume_monitor,
GnomeVFSDrive *drive)
{
GList *vol_list;
GList *current_vol;
g_mutex_lock (volume_monitor->priv->mutex);
volume_monitor->priv->fstab_drives = g_list_remove (volume_monitor->priv->fstab_drives, drive);
drive->priv->is_connected = 0;
g_mutex_unlock (volume_monitor->priv->mutex);
vol_list = gnome_vfs_drive_get_mounted_volumes (drive);
for (current_vol = vol_list; current_vol != NULL; current_vol = current_vol->next) {
GnomeVFSVolume *volume;
volume = GNOME_VFS_VOLUME (vol_list->data);
gnome_vfs_volume_unset_drive_private (volume, drive);
gnome_vfs_drive_remove_volume_private (drive, volume);
}
g_list_free (vol_list);
g_signal_emit (volume_monitor, volume_monitor_signals[DRIVE_DISCONNECTED], 0, drive);
gnome_vfs_drive_unref (drive);
}
/**
* gnome_vfs_volume_monitor_get_mounted_volumes:
* @volume_monitor: a #GnomeVFSVolumeMonitor.
*
* Gets the list of all the mounted #GnomeVFSVolume volumes.
*
* Returns: #GList of #GnomeVFSVolume. The #GnomeVFSVolume objects must be
* unreffed by the caller when no longer needed with gnome_vfs_volume_unref()
* and the #GList must be freed.
*
* Since: 2.6
*/
GList *
gnome_vfs_volume_monitor_get_mounted_volumes (GnomeVFSVolumeMonitor *volume_monitor)
{
GList *ret;
g_mutex_lock (volume_monitor->priv->mutex);
ret = g_list_copy (volume_monitor->priv->mtab_volumes);
ret = g_list_concat (ret,
g_list_copy (volume_monitor->priv->server_volumes));
ret = g_list_concat (ret,
g_list_copy (volume_monitor->priv->vfs_volumes));
g_list_foreach (ret,
(GFunc)gnome_vfs_volume_ref, NULL);
g_mutex_unlock (volume_monitor->priv->mutex);
return ret;
}
/**
* gnome_vfs_volume_monitor_get_connected_drives:
* @volume_monitor: a #GnomeVFSVolumeMonitor.
*
* Returns a #GList of all drives connected to the machine.
* The #GnomeVFSDrive objects must be unreffed by the caller when
* no longer needed with gnome_vfs_drive_unref() and the #GList must
* be freed.
*
* Returns: a #GList of all connected drives.
*
* Since: 2.6
*/
GList *
gnome_vfs_volume_monitor_get_connected_drives (GnomeVFSVolumeMonitor *volume_monitor)
{
GList *ret;
g_mutex_lock (volume_monitor->priv->mutex);
ret = g_list_copy (volume_monitor->priv->fstab_drives);
g_list_foreach (ret,
(GFunc)gnome_vfs_drive_ref, NULL);
g_mutex_unlock (volume_monitor->priv->mutex);
return ret;
}
static gboolean
volume_name_is_unique (GnomeVFSVolumeMonitor *volume_monitor,
const char *name)
{
GList *l;
GnomeVFSVolume *volume;
for (l = volume_monitor->priv->mtab_volumes; l != NULL; l = l->next) {
volume = l->data;
if (volume->priv->is_user_visible && strcmp (volume->priv->display_name, name) == 0) {
return FALSE;
}
}
for (l = volume_monitor->priv->server_volumes; l != NULL; l = l->next) {
volume = l->data;
if (volume->priv->is_user_visible && strcmp (volume->priv->display_name, name) == 0) {
return FALSE;
}
}
for (l = volume_monitor->priv->vfs_volumes; l != NULL; l = l->next) {
volume = l->data;
if (volume->priv->is_user_visible && strcmp (volume->priv->display_name, name) == 0) {
return FALSE;
}
}
return TRUE;
}
char *
_gnome_vfs_volume_monitor_uniquify_volume_name (GnomeVFSVolumeMonitor *volume_monitor,
const char *name)
{
int index;
char *unique_name;
index = 1;
unique_name = g_strdup (name);
while (!volume_name_is_unique (volume_monitor, unique_name)) {
g_free (unique_name);
index++;
unique_name = g_strdup_printf ("%s (%d)", name, index);
}
return unique_name;
}
static gboolean
drive_name_is_unique (GnomeVFSVolumeMonitor *volume_monitor,
const char *name)
{
GList *l;
GnomeVFSDrive *drive;
for (l = volume_monitor->priv->fstab_drives; l != NULL; l = l->next) {
drive = l->data;
if (drive->priv->is_user_visible && strcmp (drive->priv->display_name, name) == 0) {
return FALSE;
}
}
for (l = volume_monitor->priv->vfs_drives; l != NULL; l = l->next) {
drive = l->data;
if (drive->priv->is_user_visible && strcmp (drive->priv->display_name, name) == 0) {
return FALSE;
}
}
return TRUE;
}
char *
_gnome_vfs_volume_monitor_uniquify_drive_name (GnomeVFSVolumeMonitor *volume_monitor,
const char *name)
{
int index;
char *unique_name;
index = 1;
unique_name = g_strdup (name);
while (!drive_name_is_unique (volume_monitor, unique_name)) {
g_free (unique_name);
index++;
unique_name = g_strdup_printf ("%s (%d)", name, index);
}
return unique_name;
}
/**
* gnome_vfs_volume_monitor_get_volume_for_path:
* @volume_monitor: a #GnomeVFSVolumeMonitor.
* @path: string representing a path.
*
* Returns the #GnomeVFSVolume corresponding to @path, or %NULL.
*
* The volume referring to @path is found by calling %stat on @path,
* and then iterating through the list of volumes that refer
* to currently mounted local file systems.
* The first volume in this list maching the @path's UNIX device
* is returned.
*
* If the %stat on @path was not successful, or no volume matches @path,
* or %NULL is returned.
*
* Returns: the #GnomeVFSVolume corresponding to the @path, or %NULL. Volume returned
* must be unreffed by the caller with gnome_vfs_volume_unref().
*
* Since: 2.6
*/
GnomeVFSVolume *
gnome_vfs_volume_monitor_get_volume_for_path (GnomeVFSVolumeMonitor *volume_monitor,
const char *path)
{
struct stat statbuf;
dev_t device;
GList *l;
GnomeVFSVolume *volume, *res;
if (g_stat (path, &statbuf) != 0)
return NULL;
device = statbuf.st_dev;
res = NULL;
g_mutex_lock (volume_monitor->priv->mutex);
for (l = volume_monitor->priv->mtab_volumes; l != NULL; l = l->next) {
volume = l->data;
if (volume->priv->unix_device == device) {
res = gnome_vfs_volume_ref (volume);
break;
}
}
g_mutex_unlock (volume_monitor->priv->mutex);
return res;
}
|