app-manager/localmedia.c
changeset 9 8d3b1ddf789c
child 10 8b50be3e2998
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/app-manager/localmedia.c	Sat Feb 20 12:11:02 2010 +0000
     1.3 @@ -0,0 +1,193 @@
     1.4 +/*
     1.5 + * Copyright (C) 2010  J. Ali Harlow <ali@juiblex.co.uk>
     1.6 + *
     1.7 + * This program is free software; you can redistribute it and/or modify
     1.8 + * it under the terms of the GNU General Public License as published by
     1.9 + * the Free Software Foundation; either version 2 of the License, or
    1.10 + * (at your option) any later version.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License along
    1.18 + * with this program; if not, write to the Free Software Foundation, Inc.,
    1.19 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    1.20 + */
    1.21 +
    1.22 +#include "config.h"
    1.23 +#include <stdlib.h>
    1.24 +#include <string.h>
    1.25 +#include <glib.h>
    1.26 +#include <gio/gio.h>
    1.27 +#include <gtk/gtk.h>
    1.28 +#include <plover-gtk/packagestore.h>
    1.29 +#include "localmedia.h"
    1.30 +
    1.31 +G_DEFINE_TYPE(PloverLocalMediaStore,plover_local_media_store,
    1.32 +  PLOVER_TYPE_PACKAGE_STORE);
    1.33 +
    1.34 +static void plover_local_media_store_dispose(GObject *obj)
    1.35 +{
    1.36 +    PloverLocalMediaStore *store=PLOVER_LOCAL_MEDIA_STORE(obj);
    1.37 +    if (store->monitor)
    1.38 +    {
    1.39 +	g_object_unref(store->monitor);
    1.40 +	store->monitor=NULL;
    1.41 +    }
    1.42 +    if (G_OBJECT_CLASS(plover_local_media_store_parent_class)->dispose)
    1.43 +	G_OBJECT_CLASS(plover_local_media_store_parent_class)->dispose(obj);
    1.44 +}
    1.45 +
    1.46 +static void
    1.47 +  plover_local_media_store_class_init(PloverLocalMediaStoreClass *klass)
    1.48 +{
    1.49 +    GObjectClass *oclass=G_OBJECT_CLASS(klass);
    1.50 +    oclass->dispose=plover_local_media_store_dispose;
    1.51 +}
    1.52 +
    1.53 +GtkTreeModel *plover_local_media_store_new(void)
    1.54 +{
    1.55 +    return g_object_new(PLOVER_TYPE_LOCAL_MEDIA_STORE,NULL);
    1.56 +}
    1.57 +
    1.58 +static void local_media_scan_mount(PloverLocalMediaStore *store,GMount *mount)
    1.59 +{
    1.60 +    GFile *root;
    1.61 +    gchar *path;
    1.62 +    PloverPackageSet *set;
    1.63 +    root=g_mount_get_root(mount);
    1.64 +    path=g_file_get_path(root);
    1.65 +    if (path)
    1.66 +    {
    1.67 +	set=plover_package_set_new_from_repository(path,NULL);
    1.68 +	if (set)
    1.69 +	{
    1.70 +	    g_object_set_data(G_OBJECT(mount),"plover-local-media-set",set);
    1.71 +	    plover_package_store_add_set(PLOVER_PACKAGE_STORE(store),set);
    1.72 +	    g_object_ref(mount);
    1.73 +	}
    1.74 +	g_free(path);
    1.75 +    }
    1.76 +    g_object_unref(root);
    1.77 +}
    1.78 +
    1.79 +static void local_media_mounted(GObject *source,GAsyncResult *res,gpointer data)
    1.80 +{
    1.81 +    GVolume *volume=G_VOLUME(source);
    1.82 +    GMount *mount;
    1.83 +    PloverLocalMediaStore *store=PLOVER_LOCAL_MEDIA_STORE(data);
    1.84 +    if (g_volume_mount_finish(volume,res,NULL))
    1.85 +    {
    1.86 +	mount=g_volume_get_mount(volume);
    1.87 +	if (mount)
    1.88 +	{
    1.89 +	    local_media_scan_mount(store,mount);
    1.90 +	    g_object_unref(mount);
    1.91 +	}
    1.92 +    }
    1.93 +    g_object_unref(volume);
    1.94 +}
    1.95 +
    1.96 +static void local_media_scan_drive(PloverLocalMediaStore *store,GDrive *drive)
    1.97 +{
    1.98 +    GVolume *volume;
    1.99 +    GMount *mount;
   1.100 +    GList *volumes,*link;
   1.101 +    if (g_drive_has_media(drive))
   1.102 +    {
   1.103 +	volumes=g_drive_get_volumes(drive);
   1.104 +	for(link=volumes;link;link=link->next)
   1.105 +	{
   1.106 +	    volume=G_VOLUME(link->data);
   1.107 +	    mount=g_volume_get_mount(volume);
   1.108 +	    if (mount)
   1.109 +	    {
   1.110 +		local_media_scan_mount(store,mount);
   1.111 +		g_object_unref(mount);
   1.112 +	    }
   1.113 +	    else if (!store->implicit_scan && g_volume_can_mount(volume))
   1.114 +		g_volume_mount(volume,G_MOUNT_MOUNT_NONE,NULL,NULL,
   1.115 +		  local_media_mounted,store);
   1.116 +	    g_object_unref(volume);
   1.117 +	}
   1.118 +	g_list_free(volumes);
   1.119 +    }
   1.120 +}
   1.121 +
   1.122 +static void local_media_polled(GObject *source,GAsyncResult *res,gpointer data)
   1.123 +{
   1.124 +    GDrive *drive=G_DRIVE(source);
   1.125 +    PloverLocalMediaStore *store=PLOVER_LOCAL_MEDIA_STORE(data);
   1.126 +    if (g_drive_poll_for_media_finish(drive,res,NULL))
   1.127 +	local_media_scan_drive(store,drive);
   1.128 +    g_object_unref(drive);
   1.129 +}
   1.130 +
   1.131 +void plover_local_media_store_scan(PloverLocalMediaStore *store)
   1.132 +{
   1.133 +    GList *drives,*link;
   1.134 +    GDrive *drive;
   1.135 +    g_return_if_fail(PLOVER_IS_LOCAL_MEDIA_STORE(store));
   1.136 +    drives=g_volume_monitor_get_connected_drives(store->monitor);
   1.137 +    for(link=drives;link;link=link->next)
   1.138 +    {
   1.139 +	drive=G_DRIVE(link->data);
   1.140 +	if (g_drive_is_media_removable(drive))
   1.141 +	{
   1.142 +	    if (!g_drive_is_media_check_automatic(drive) &&
   1.143 +	      g_drive_can_poll_for_media(drive) ||
   1.144 +	      !g_drive_has_media(drive) && !store->implicit_scan)
   1.145 +	    {
   1.146 +		g_object_ref(drive);
   1.147 +		g_drive_poll_for_media(drive,NULL,local_media_polled,store);
   1.148 +	    }
   1.149 +	    else
   1.150 +		local_media_scan_drive(store,drive);
   1.151 +	}
   1.152 +	g_object_unref(drive);
   1.153 +    }
   1.154 +    g_list_free(drives);
   1.155 +}
   1.156 +
   1.157 +static void local_media_mount_added(GVolumeMonitor *volume_monitor,
   1.158 +  GMount *mount,PloverLocalMediaStore *store)
   1.159 +{
   1.160 +    local_media_scan_mount(store,mount);
   1.161 +}
   1.162 +
   1.163 +static void local_media_mount_removed(GVolumeMonitor *volume_monitor,
   1.164 +  GMount *mount,PloverLocalMediaStore *store)
   1.165 +{
   1.166 +    PloverPackageSet *set=
   1.167 +      g_object_get_data(G_OBJECT(mount),"plover-local-media-set");
   1.168 +    if (set)
   1.169 +    {
   1.170 +	plover_package_store_remove_set(PLOVER_PACKAGE_STORE(store),set);
   1.171 +	g_object_set_data(G_OBJECT(mount),"plover-local-media-set",NULL);
   1.172 +	g_object_unref(set);
   1.173 +	g_object_unref(mount);
   1.174 +    }
   1.175 +}
   1.176 +
   1.177 +static void local_media_mount_changed(GVolumeMonitor *volume_monitor,
   1.178 +  GMount *mount,PloverLocalMediaStore *store)
   1.179 +{
   1.180 +    local_media_mount_removed(volume_monitor,mount,store);
   1.181 +    local_media_mount_added(volume_monitor,mount,store);
   1.182 +}
   1.183 +
   1.184 +static void plover_local_media_store_init(PloverLocalMediaStore *store)
   1.185 +{
   1.186 +    store->monitor=g_volume_monitor_get();
   1.187 +    store->implicit_scan=TRUE;
   1.188 +    plover_local_media_store_scan(store);
   1.189 +    store->implicit_scan=FALSE;
   1.190 +    g_signal_connect(store->monitor,"mount-added",
   1.191 +      G_CALLBACK(local_media_mount_added),store);
   1.192 +    g_signal_connect(store->monitor,"mount-changed",
   1.193 +      G_CALLBACK(local_media_mount_changed),store);
   1.194 +    g_signal_connect(store->monitor,"mount-removed",
   1.195 +      G_CALLBACK(local_media_mount_removed),store);
   1.196 +}