Blame gio/gpollfilemonitor.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 * 
Packit ae235b
 * Copyright (C) 2006-2007 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Alexander Larsson <alexl@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
#include "gpollfilemonitor.h"
Packit ae235b
#include "gfile.h"
Packit ae235b
#include "gfilemonitor.h"
Packit ae235b
#include "gfileinfo.h"
Packit ae235b
Packit ae235b
Packit ae235b
static gboolean g_poll_file_monitor_cancel (GFileMonitor* monitor);
Packit ae235b
static void schedule_poll_timeout (GPollFileMonitor* poll_monitor);
Packit ae235b
Packit ae235b
struct _GPollFileMonitor
Packit ae235b
{
Packit ae235b
  GFileMonitor parent_instance;
Packit ae235b
  GFile *file;
Packit ae235b
  GFileInfo *last_info;
Packit ae235b
  GSource *timeout;
Packit ae235b
};
Packit ae235b
Packit ae235b
#define POLL_TIME_SECS 5
Packit ae235b
Packit ae235b
#define g_poll_file_monitor_get_type _g_poll_file_monitor_get_type
Packit ae235b
G_DEFINE_TYPE (GPollFileMonitor, g_poll_file_monitor, G_TYPE_FILE_MONITOR)
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_poll_file_monitor_finalize (GObject* object)
Packit ae235b
{
Packit ae235b
  GPollFileMonitor* poll_monitor;
Packit ae235b
  
Packit ae235b
  poll_monitor = G_POLL_FILE_MONITOR (object);
Packit ae235b
Packit ae235b
  g_poll_file_monitor_cancel (G_FILE_MONITOR (poll_monitor));
Packit ae235b
  g_object_unref (poll_monitor->file);
Packit ae235b
  g_clear_object (&poll_monitor->last_info);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_poll_file_monitor_parent_class)->finalize (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_poll_file_monitor_class_init (GPollFileMonitorClass* klass)
Packit ae235b
{
Packit ae235b
  GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
Packit ae235b
  GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
Packit ae235b
  
Packit ae235b
  gobject_class->finalize = g_poll_file_monitor_finalize;
Packit ae235b
Packit ae235b
  file_monitor_class->cancel = g_poll_file_monitor_cancel;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_poll_file_monitor_init (GPollFileMonitor* poll_monitor)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
static int
Packit ae235b
calc_event_type (GFileInfo *last,
Packit ae235b
		 GFileInfo *new)
Packit ae235b
{
Packit ae235b
  if (last == NULL && new == NULL)
Packit ae235b
    return -1;
Packit ae235b
Packit ae235b
  if (last == NULL && new != NULL)
Packit ae235b
    return G_FILE_MONITOR_EVENT_CREATED;
Packit ae235b
  
Packit ae235b
  if (last != NULL && new == NULL)
Packit ae235b
    return G_FILE_MONITOR_EVENT_DELETED;
Packit ae235b
Packit ae235b
  if (g_strcmp0 (g_file_info_get_etag (last), g_file_info_get_etag (new)))
Packit ae235b
    return G_FILE_MONITOR_EVENT_CHANGED;
Packit ae235b
  
Packit ae235b
  if (g_file_info_get_size (last) != g_file_info_get_size (new))
Packit ae235b
    return G_FILE_MONITOR_EVENT_CHANGED;
Packit ae235b
Packit ae235b
  return -1;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
got_new_info (GObject      *source_object,
Packit ae235b
              GAsyncResult *res,
Packit ae235b
              gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GPollFileMonitor* poll_monitor = user_data;
Packit ae235b
  GFileInfo *info;
Packit ae235b
  int event;
Packit ae235b
Packit ae235b
  info = g_file_query_info_finish (poll_monitor->file, res, NULL);
Packit ae235b
Packit ae235b
  if (!g_file_monitor_is_cancelled (G_FILE_MONITOR (poll_monitor)))
Packit ae235b
    {
Packit ae235b
      event = calc_event_type (poll_monitor->last_info, info);
Packit ae235b
Packit ae235b
      if (event != -1)
Packit ae235b
	{
Packit ae235b
	  g_file_monitor_emit_event (G_FILE_MONITOR (poll_monitor),
Packit ae235b
				     poll_monitor->file,
Packit ae235b
				     NULL, event);
Packit ae235b
	  /* We're polling so slowly anyway, so always emit the done hint */
Packit ae235b
	  if (!g_file_monitor_is_cancelled (G_FILE_MONITOR (poll_monitor)) &&
Packit ae235b
             (event == G_FILE_MONITOR_EVENT_CHANGED || event == G_FILE_MONITOR_EVENT_CREATED))
Packit ae235b
	    g_file_monitor_emit_event (G_FILE_MONITOR (poll_monitor),
Packit ae235b
				       poll_monitor->file,
Packit ae235b
				       NULL, G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
Packit ae235b
	}
Packit ae235b
      
Packit ae235b
      if (poll_monitor->last_info)
Packit ae235b
	{
Packit ae235b
	  g_object_unref (poll_monitor->last_info);
Packit ae235b
	  poll_monitor->last_info = NULL;
Packit ae235b
	}
Packit ae235b
      
Packit ae235b
      if (info)
Packit ae235b
	poll_monitor->last_info = g_object_ref (info);
Packit ae235b
      
Packit ae235b
      schedule_poll_timeout (poll_monitor);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (info)
Packit ae235b
    g_object_unref (info);
Packit ae235b
  
Packit ae235b
  g_object_unref (poll_monitor);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
poll_file_timeout (gpointer data)
Packit ae235b
{
Packit ae235b
  GPollFileMonitor* poll_monitor = data;
Packit ae235b
Packit ae235b
  g_source_unref (poll_monitor->timeout);
Packit ae235b
  poll_monitor->timeout = NULL;
Packit ae235b
Packit ae235b
  g_file_query_info_async (poll_monitor->file, G_FILE_ATTRIBUTE_ETAG_VALUE "," G_FILE_ATTRIBUTE_STANDARD_SIZE,
Packit ae235b
			 0, 0, NULL, got_new_info, g_object_ref (poll_monitor));
Packit ae235b
  
Packit ae235b
  return G_SOURCE_REMOVE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
schedule_poll_timeout (GPollFileMonitor* poll_monitor)
Packit ae235b
{
Packit ae235b
  poll_monitor->timeout = g_timeout_source_new_seconds (POLL_TIME_SECS);
Packit ae235b
  g_source_set_callback (poll_monitor->timeout, poll_file_timeout, poll_monitor, NULL);
Packit ae235b
  g_source_attach (poll_monitor->timeout, g_main_context_get_thread_default ());
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
got_initial_info (GObject      *source_object,
Packit ae235b
                  GAsyncResult *res,
Packit ae235b
                  gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GPollFileMonitor* poll_monitor = user_data;
Packit ae235b
  GFileInfo *info;
Packit ae235b
Packit ae235b
  info = g_file_query_info_finish (poll_monitor->file, res, NULL);
Packit ae235b
Packit ae235b
  poll_monitor->last_info = info;
Packit ae235b
Packit ae235b
  if (!g_file_monitor_is_cancelled (G_FILE_MONITOR (poll_monitor)))
Packit ae235b
    schedule_poll_timeout (poll_monitor);
Packit ae235b
  
Packit ae235b
  g_object_unref (poll_monitor);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * _g_poll_file_monitor_new:
Packit ae235b
 * @file: a #GFile.
Packit ae235b
 * 
Packit ae235b
 * Polls @file for changes.
Packit ae235b
 * 
Packit ae235b
 * Returns: a new #GFileMonitor for the given #GFile. 
Packit ae235b
 **/
Packit ae235b
GFileMonitor*
Packit ae235b
_g_poll_file_monitor_new (GFile *file)
Packit ae235b
{
Packit ae235b
  GPollFileMonitor* poll_monitor;
Packit ae235b
  
Packit ae235b
  poll_monitor = g_object_new (G_TYPE_POLL_FILE_MONITOR, NULL);
Packit ae235b
Packit ae235b
  poll_monitor->file = g_object_ref (file);
Packit ae235b
Packit ae235b
  g_file_query_info_async (file, G_FILE_ATTRIBUTE_ETAG_VALUE "," G_FILE_ATTRIBUTE_STANDARD_SIZE,
Packit ae235b
			   0, 0, NULL, got_initial_info, g_object_ref (poll_monitor));
Packit ae235b
  
Packit ae235b
  return G_FILE_MONITOR (poll_monitor);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_poll_file_monitor_cancel (GFileMonitor* monitor)
Packit ae235b
{
Packit ae235b
  GPollFileMonitor *poll_monitor = G_POLL_FILE_MONITOR (monitor);
Packit ae235b
  
Packit ae235b
  if (poll_monitor->timeout)
Packit ae235b
    {
Packit ae235b
      g_source_destroy (poll_monitor->timeout);
Packit ae235b
      g_source_unref (poll_monitor->timeout);
Packit ae235b
      poll_monitor->timeout = NULL;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return TRUE;
Packit ae235b
}