Blame telepathy-account-widgets/tp-account-widgets/tpaw-camera-monitor.c

Packit 79f644
/*
Packit 79f644
 * Copyright (C) 2011 Collabora Ltd.
Packit 79f644
 *
Packit 79f644
 * This library is free software; you can redistribute it and/or
Packit 79f644
 * modify it under the terms of the GNU Lesser General Public
Packit 79f644
 * License as published by the Free Software Foundation; either
Packit 79f644
 * version 2.1 of the License, or (at your option) any later version.
Packit 79f644
 *
Packit 79f644
 * This library is distributed in the hope that it will be useful,
Packit 79f644
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 79f644
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 79f644
 * Lesser General Public License for more details.
Packit 79f644
 *
Packit 79f644
 * You should have received a copy of the GNU Lesser General Public
Packit 79f644
 * License along with this library; if not, write to the Free Software
Packit 79f644
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Packit 79f644
 *
Packit 79f644
 * Authors: Emilio Pozuelo Monfort <emilio.pozuelo@collabora.co.uk>
Packit 79f644
 */
Packit 79f644
Packit 79f644
#include "config.h"
Packit 79f644
#include "tpaw-camera-monitor.h"
Packit 79f644
Packit 79f644
#include "cheese-camera-device-monitor.h"
Packit 79f644
Packit 79f644
#define DEBUG_FLAG TPAW_DEBUG_OTHER
Packit 79f644
#include "tpaw-debug.h"
Packit 79f644
Packit 79f644
struct _TpawCameraMonitorPrivate
Packit 79f644
{
Packit 79f644
  TpawCameraDeviceMonitor *tpaw_monitor;
Packit 79f644
  GQueue *cameras;
Packit 79f644
  gint num_cameras;
Packit 79f644
};
Packit 79f644
Packit 79f644
enum
Packit 79f644
{
Packit 79f644
  PROP_0,
Packit 79f644
  PROP_AVAILABLE,
Packit 79f644
};
Packit 79f644
Packit 79f644
enum
Packit 79f644
{
Packit 79f644
  CAMERA_ADDED,
Packit 79f644
  CAMERA_REMOVED,
Packit 79f644
  LAST_SIGNAL
Packit 79f644
};
Packit 79f644
Packit 79f644
static guint signals[LAST_SIGNAL];
Packit 79f644
Packit 79f644
G_DEFINE_TYPE (TpawCameraMonitor, tpaw_camera_monitor, G_TYPE_OBJECT);
Packit 79f644
Packit 79f644
static TpawCameraMonitor *manager_singleton = NULL;
Packit 79f644
Packit 79f644
static TpawCamera *
Packit 79f644
tpaw_camera_new (const gchar *id,
Packit 79f644
    const gchar *device,
Packit 79f644
    const gchar *name)
Packit 79f644
{
Packit 79f644
  TpawCamera *camera = g_slice_new (TpawCamera);
Packit 79f644
Packit 79f644
  camera->id = g_strdup (id);
Packit 79f644
  camera->device = g_strdup (device);
Packit 79f644
  camera->name = g_strdup (name);
Packit 79f644
Packit 79f644
  return camera;
Packit 79f644
}
Packit 79f644
Packit 79f644
static TpawCamera *
Packit 79f644
tpaw_camera_copy (TpawCamera *camera)
Packit 79f644
{
Packit 79f644
  return tpaw_camera_new (camera->id, camera->device, camera->name);
Packit 79f644
}
Packit 79f644
Packit 79f644
static void
Packit 79f644
tpaw_camera_free (TpawCamera *camera)
Packit 79f644
{
Packit 79f644
  g_free (camera->id);
Packit 79f644
  g_free (camera->device);
Packit 79f644
  g_free (camera->name);
Packit 79f644
Packit 79f644
  g_slice_free (TpawCamera, camera);
Packit 79f644
}
Packit 79f644
Packit 79f644
G_DEFINE_BOXED_TYPE (TpawCamera, tpaw_camera,
Packit 79f644
    tpaw_camera_copy, tpaw_camera_free)
Packit 79f644
Packit 79f644
static gint
Packit 79f644
tpaw_camera_find (gconstpointer a,
Packit 79f644
    gconstpointer b)
Packit 79f644
{
Packit 79f644
  const TpawCamera *camera = a;
Packit 79f644
  const gchar *id = b;
Packit 79f644
Packit 79f644
  return g_strcmp0 (camera->id, id);
Packit 79f644
}
Packit 79f644
Packit 79f644
static void
Packit 79f644
tpaw_camera_monitor_free_camera_foreach (gpointer data,
Packit 79f644
    gpointer user_data)
Packit 79f644
{
Packit 79f644
  tpaw_camera_free (data);
Packit 79f644
}
Packit 79f644
Packit 79f644
static void
Packit 79f644
on_camera_added (TpawCameraDeviceMonitor *device,
Packit 79f644
    gchar *id,
Packit 79f644
    gchar *filename,
Packit 79f644
    gchar *product_name,
Packit 79f644
    gint api_version,
Packit 79f644
    TpawCameraMonitor *self)
Packit 79f644
{
Packit 79f644
  TpawCamera *camera;
Packit 79f644
Packit 79f644
  if (self->priv->cameras == NULL)
Packit 79f644
    return;
Packit 79f644
Packit 79f644
  camera = tpaw_camera_new (id, filename, product_name);
Packit 79f644
Packit 79f644
  g_queue_push_tail (self->priv->cameras, camera);
Packit 79f644
Packit 79f644
  self->priv->num_cameras++;
Packit 79f644
Packit 79f644
  if (self->priv->num_cameras == 1)
Packit 79f644
    g_object_notify (G_OBJECT (self), "available");
Packit 79f644
Packit 79f644
  g_signal_emit (self, signals[CAMERA_ADDED], 0, camera);
Packit 79f644
}
Packit 79f644
Packit 79f644
static void
Packit 79f644
on_camera_removed (TpawCameraDeviceMonitor *device,
Packit 79f644
    gchar *id,
Packit 79f644
    TpawCameraMonitor *self)
Packit 79f644
{
Packit 79f644
  TpawCamera *camera;
Packit 79f644
  GList *l;
Packit 79f644
Packit 79f644
  if (self->priv->cameras == NULL)
Packit 79f644
    return;
Packit 79f644
Packit 79f644
  l = g_queue_find_custom (self->priv->cameras, id, tpaw_camera_find);
Packit 79f644
Packit 79f644
  g_return_if_fail (l != NULL);
Packit 79f644
Packit 79f644
  camera = l->data;
Packit 79f644
Packit 79f644
  g_queue_delete_link (self->priv->cameras, l);
Packit 79f644
Packit 79f644
  self->priv->num_cameras--;
Packit 79f644
Packit 79f644
  if (self->priv->num_cameras == 0)
Packit 79f644
    g_object_notify (G_OBJECT (self), "available");
Packit 79f644
Packit 79f644
  g_signal_emit (self, signals[CAMERA_REMOVED], 0, camera);
Packit 79f644
Packit 79f644
  tpaw_camera_free (camera);
Packit 79f644
}
Packit 79f644
Packit 79f644
const GList *
Packit 79f644
tpaw_camera_monitor_get_cameras (TpawCameraMonitor *self)
Packit 79f644
{
Packit 79f644
  if (self->priv->cameras != NULL)
Packit 79f644
    return self->priv->cameras->head;
Packit 79f644
  else
Packit 79f644
    return NULL;
Packit 79f644
}
Packit 79f644
Packit 79f644
static void
Packit 79f644
tpaw_camera_monitor_get_property (GObject *object,
Packit 79f644
    guint prop_id,
Packit 79f644
    GValue *value,
Packit 79f644
    GParamSpec *pspec)
Packit 79f644
{
Packit 79f644
  TpawCameraMonitor *self = (TpawCameraMonitor *) object;
Packit 79f644
Packit 79f644
  switch (prop_id)
Packit 79f644
    {
Packit 79f644
    case PROP_AVAILABLE:
Packit 79f644
      g_value_set_boolean (value, self->priv->num_cameras > 0);
Packit 79f644
      break;
Packit 79f644
    default:
Packit 79f644
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit 79f644
      break;
Packit 79f644
  }
Packit 79f644
}
Packit 79f644
Packit 79f644
static void
Packit 79f644
tpaw_camera_monitor_dispose (GObject *object)
Packit 79f644
{
Packit 79f644
  TpawCameraMonitor *self = TPAW_CAMERA_MONITOR (object);
Packit 79f644
Packit 79f644
  tp_clear_object (&self->priv->tpaw_monitor);
Packit 79f644
Packit 79f644
  g_queue_foreach (self->priv->cameras,
Packit 79f644
      tpaw_camera_monitor_free_camera_foreach, NULL);
Packit 79f644
  tp_clear_pointer (&self->priv->cameras, g_queue_free);
Packit 79f644
Packit 79f644
  G_OBJECT_CLASS (tpaw_camera_monitor_parent_class)->dispose (object);
Packit 79f644
}
Packit 79f644
Packit 79f644
static void
Packit 79f644
tpaw_camera_monitor_constructed (GObject *object)
Packit 79f644
{
Packit 79f644
  TpawCameraMonitor *self = (TpawCameraMonitor *) object;
Packit 79f644
Packit 79f644
  G_OBJECT_CLASS (tpaw_camera_monitor_parent_class)->constructed (object);
Packit 79f644
Packit 79f644
  tpaw_camera_device_monitor_coldplug (self->priv->tpaw_monitor);
Packit 79f644
}
Packit 79f644
Packit 79f644
static void
Packit 79f644
tpaw_camera_monitor_class_init (TpawCameraMonitorClass *klass)
Packit 79f644
{
Packit 79f644
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
Packit 79f644
Packit 79f644
  object_class->dispose = tpaw_camera_monitor_dispose;
Packit 79f644
  object_class->constructed = tpaw_camera_monitor_constructed;
Packit 79f644
  object_class->get_property = tpaw_camera_monitor_get_property;
Packit 79f644
Packit 79f644
  g_object_class_install_property (object_class, PROP_AVAILABLE,
Packit 79f644
      g_param_spec_boolean ("available", "Available",
Packit 79f644
      "Camera available", TRUE, G_PARAM_READABLE));
Packit 79f644
Packit 79f644
  signals[CAMERA_ADDED] =
Packit 79f644
      g_signal_new ("added", G_OBJECT_CLASS_TYPE (klass),
Packit 79f644
          G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
Packit 79f644
          0, NULL, NULL,
Packit 79f644
          g_cclosure_marshal_generic,
Packit 79f644
          G_TYPE_NONE, 1, TPAW_TYPE_CAMERA);
Packit 79f644
Packit 79f644
  signals[CAMERA_REMOVED] =
Packit 79f644
      g_signal_new ("removed", G_OBJECT_CLASS_TYPE (klass),
Packit 79f644
          G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
Packit 79f644
          0, NULL, NULL,
Packit 79f644
          g_cclosure_marshal_generic,
Packit 79f644
          G_TYPE_NONE, 1, TPAW_TYPE_CAMERA);
Packit 79f644
Packit 79f644
  g_type_class_add_private (object_class,
Packit 79f644
      sizeof (TpawCameraMonitorPrivate));
Packit 79f644
}
Packit 79f644
Packit 79f644
static void
Packit 79f644
tpaw_camera_monitor_init (TpawCameraMonitor *self)
Packit 79f644
{
Packit 79f644
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
Packit 79f644
      TPAW_TYPE_CAMERA_MONITOR, TpawCameraMonitorPrivate);
Packit 79f644
Packit 79f644
  self->priv->cameras = g_queue_new ();
Packit 79f644
Packit 79f644
  self->priv->tpaw_monitor = tpaw_camera_device_monitor_new ();
Packit 79f644
Packit 79f644
  g_signal_connect (self->priv->tpaw_monitor, "added",
Packit 79f644
      G_CALLBACK (on_camera_added), self);
Packit 79f644
  g_signal_connect (self->priv->tpaw_monitor, "removed",
Packit 79f644
      G_CALLBACK (on_camera_removed), self);
Packit 79f644
Packit 79f644
#ifndef HAVE_UDEV
Packit 79f644
  /* No udev, assume there are cameras present */
Packit 79f644
  self->priv->num_cameras = 1;
Packit 79f644
#endif
Packit 79f644
}
Packit 79f644
Packit 79f644
TpawCameraMonitor *
Packit 79f644
tpaw_camera_monitor_dup_singleton (void)
Packit 79f644
{
Packit 79f644
  GObject *retval;
Packit 79f644
Packit 79f644
  if (manager_singleton)
Packit 79f644
    {
Packit 79f644
      retval = g_object_ref (manager_singleton);
Packit 79f644
    }
Packit 79f644
  else
Packit 79f644
    {
Packit 79f644
      retval = g_object_new (TPAW_TYPE_CAMERA_MONITOR, NULL);
Packit 79f644
Packit 79f644
      manager_singleton = TPAW_CAMERA_MONITOR (retval);
Packit 79f644
      g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
Packit 79f644
    }
Packit 79f644
Packit 79f644
  return TPAW_CAMERA_MONITOR (retval);
Packit 79f644
}
Packit 79f644
Packit 79f644
TpawCameraMonitor *
Packit 79f644
tpaw_camera_monitor_new (void)
Packit 79f644
{
Packit 79f644
  return TPAW_CAMERA_MONITOR (
Packit 79f644
      g_object_new (TPAW_TYPE_CAMERA_MONITOR, NULL));
Packit 79f644
}
Packit 79f644
Packit 79f644
gboolean tpaw_camera_monitor_get_available (TpawCameraMonitor *self)
Packit 79f644
{
Packit 79f644
  g_return_val_if_fail (TPAW_IS_CAMERA_MONITOR (self), FALSE);
Packit 79f644
Packit 79f644
  return self->priv->num_cameras > 0;
Packit 79f644
}