/* * Copyright (C) 2010 J. Ali Harlow * * 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 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., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "config.h" #include #include #include #include #include #include #include "localmedia.h" G_DEFINE_TYPE(PloverLocalMediaStore,plover_local_media_store, PLOVER_TYPE_PACKAGE_STORE); static void plover_local_media_store_dispose(GObject *obj) { PloverLocalMediaStore *store=PLOVER_LOCAL_MEDIA_STORE(obj); if (store->monitor) { g_object_unref(store->monitor); store->monitor=NULL; } if (G_OBJECT_CLASS(plover_local_media_store_parent_class)->dispose) G_OBJECT_CLASS(plover_local_media_store_parent_class)->dispose(obj); } static void plover_local_media_store_class_init(PloverLocalMediaStoreClass *klass) { GObjectClass *oclass=G_OBJECT_CLASS(klass); oclass->dispose=plover_local_media_store_dispose; } GtkTreeModel *plover_local_media_store_new(void) { return g_object_new(PLOVER_TYPE_LOCAL_MEDIA_STORE,NULL); } static void local_media_scan_mount(PloverLocalMediaStore *store,GMount *mount) { GFile *root; gchar *path; PloverPackageSet *set; root=g_mount_get_root(mount); path=g_file_get_path(root); if (path) { set=plover_package_set_new_from_repository(path,NULL); if (set) { g_object_set_data(G_OBJECT(mount),"plover-local-media-set",set); plover_package_store_add_set(PLOVER_PACKAGE_STORE(store),set); g_object_ref(mount); } g_free(path); } g_object_unref(root); } static void local_media_mounted(GObject *source,GAsyncResult *res,gpointer data) { GVolume *volume=G_VOLUME(source); GMount *mount; PloverLocalMediaStore *store=PLOVER_LOCAL_MEDIA_STORE(data); if (g_volume_mount_finish(volume,res,NULL)) { mount=g_volume_get_mount(volume); if (mount) { local_media_scan_mount(store,mount); g_object_unref(mount); } } g_object_unref(volume); } static void local_media_scan_drive(PloverLocalMediaStore *store,GDrive *drive) { GVolume *volume; GMount *mount; GList *volumes,*link; if (g_drive_has_media(drive)) { volumes=g_drive_get_volumes(drive); for(link=volumes;link;link=link->next) { volume=G_VOLUME(link->data); mount=g_volume_get_mount(volume); if (mount) { local_media_scan_mount(store,mount); g_object_unref(mount); } else if (!store->implicit_scan && g_volume_can_mount(volume)) g_volume_mount(volume,G_MOUNT_MOUNT_NONE,NULL,NULL, local_media_mounted,store); g_object_unref(volume); } g_list_free(volumes); } } static void local_media_polled(GObject *source,GAsyncResult *res,gpointer data) { GDrive *drive=G_DRIVE(source); PloverLocalMediaStore *store=PLOVER_LOCAL_MEDIA_STORE(data); if (g_drive_poll_for_media_finish(drive,res,NULL)) local_media_scan_drive(store,drive); g_object_unref(drive); } void plover_local_media_store_scan(PloverLocalMediaStore *store) { GList *drives,*link; GDrive *drive; g_return_if_fail(PLOVER_IS_LOCAL_MEDIA_STORE(store)); drives=g_volume_monitor_get_connected_drives(store->monitor); for(link=drives;link;link=link->next) { drive=G_DRIVE(link->data); if (g_drive_is_media_removable(drive)) { if (!g_drive_is_media_check_automatic(drive) && g_drive_can_poll_for_media(drive) || !g_drive_has_media(drive) && !store->implicit_scan) { g_object_ref(drive); g_drive_poll_for_media(drive,NULL,local_media_polled,store); } else local_media_scan_drive(store,drive); } g_object_unref(drive); } g_list_free(drives); } static void local_media_mount_added(GVolumeMonitor *volume_monitor, GMount *mount,PloverLocalMediaStore *store) { local_media_scan_mount(store,mount); } static void local_media_mount_removed(GVolumeMonitor *volume_monitor, GMount *mount,PloverLocalMediaStore *store) { PloverPackageSet *set= g_object_get_data(G_OBJECT(mount),"plover-local-media-set"); if (set) { plover_package_store_remove_set(PLOVER_PACKAGE_STORE(store),set); g_object_set_data(G_OBJECT(mount),"plover-local-media-set",NULL); g_object_unref(set); g_object_unref(mount); } } static void local_media_mount_changed(GVolumeMonitor *volume_monitor, GMount *mount,PloverLocalMediaStore *store) { local_media_mount_removed(volume_monitor,mount,store); local_media_mount_added(volume_monitor,mount,store); } static void plover_local_media_store_init(PloverLocalMediaStore *store) { store->monitor=g_volume_monitor_get(); store->implicit_scan=TRUE; plover_local_media_store_scan(store); store->implicit_scan=FALSE; g_signal_connect(store->monitor,"mount-added", G_CALLBACK(local_media_mount_added),store); g_signal_connect(store->monitor,"mount-changed", G_CALLBACK(local_media_mount_changed),store); g_signal_connect(store->monitor,"mount-removed", G_CALLBACK(local_media_mount_removed),store); }