gpollfilemonitor.c 5.85 KB
Newer Older
1 2 3 4 5 6 7
/* GIO - GLib Input, Output and Streaming Library
 * 
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9 10 11 12 13 14 15
 *
 * This 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
16
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 18 19 20
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

21
#include "config.h"
22 23 24
#include <string.h>

#include "gpollfilemonitor.h"
25
#include "gfile.h"
26
#include "gfilemonitor.h"
27
#include "gfileinfo.h"
28

29

30 31 32 33 34 35 36 37
static gboolean g_poll_file_monitor_cancel (GFileMonitor* monitor);
static void schedule_poll_timeout (GPollFileMonitor* poll_monitor);

struct _GPollFileMonitor
{
  GFileMonitor parent_instance;
  GFile *file;
  GFileInfo *last_info;
38
  GSource *timeout;
39 40 41 42
};

#define POLL_TIME_SECS 5

43
#define g_poll_file_monitor_get_type _g_poll_file_monitor_get_type
44 45 46 47 48 49 50 51 52
G_DEFINE_TYPE (GPollFileMonitor, g_poll_file_monitor, G_TYPE_FILE_MONITOR)

static void
g_poll_file_monitor_finalize (GObject* object)
{
  GPollFileMonitor* poll_monitor;
  
  poll_monitor = G_POLL_FILE_MONITOR (object);

53
  g_poll_file_monitor_cancel (G_FILE_MONITOR (poll_monitor));
54
  g_object_unref (poll_monitor->file);
55
  g_clear_object (&poll_monitor->last_info);
56

57
  G_OBJECT_CLASS (g_poll_file_monitor_parent_class)->finalize (object);
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
}


static void
g_poll_file_monitor_class_init (GPollFileMonitorClass* klass)
{
  GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
  GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
  
  gobject_class->finalize = g_poll_file_monitor_finalize;

  file_monitor_class->cancel = g_poll_file_monitor_cancel;
}

static void
g_poll_file_monitor_init (GPollFileMonitor* poll_monitor)
{
}

static int
calc_event_type (GFileInfo *last,
		 GFileInfo *new)
{
  if (last == NULL && new == NULL)
    return -1;

  if (last == NULL && new != NULL)
    return G_FILE_MONITOR_EVENT_CREATED;
  
  if (last != NULL && new == NULL)
    return G_FILE_MONITOR_EVENT_DELETED;

90
  if (g_strcmp0 (g_file_info_get_etag (last), g_file_info_get_etag (new)))
91 92
    return G_FILE_MONITOR_EVENT_CHANGED;
  
93
  if (g_file_info_get_size (last) != g_file_info_get_size (new))
94 95 96 97 98 99
    return G_FILE_MONITOR_EVENT_CHANGED;

  return -1;
}

static void
100 101 102
got_new_info (GObject      *source_object,
              GAsyncResult *res,
              gpointer      user_data)
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
{
  GPollFileMonitor* poll_monitor = user_data;
  GFileInfo *info;
  int event;

  info = g_file_query_info_finish (poll_monitor->file, res, NULL);

  if (!g_file_monitor_is_cancelled (G_FILE_MONITOR (poll_monitor)))
    {
      event = calc_event_type (poll_monitor->last_info, info);

      if (event != -1)
	{
	  g_file_monitor_emit_event (G_FILE_MONITOR (poll_monitor),
				     poll_monitor->file,
				     NULL, event);
	  /* We're polling so slowly anyway, so always emit the done hint */
120
	  if (!g_file_monitor_is_cancelled (G_FILE_MONITOR (poll_monitor)) &&
121
             (event == G_FILE_MONITOR_EVENT_CHANGED || event == G_FILE_MONITOR_EVENT_CREATED))
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
	    g_file_monitor_emit_event (G_FILE_MONITOR (poll_monitor),
				       poll_monitor->file,
				       NULL, G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
	}
      
      if (poll_monitor->last_info)
	{
	  g_object_unref (poll_monitor->last_info);
	  poll_monitor->last_info = NULL;
	}
      
      if (info)
	poll_monitor->last_info = g_object_ref (info);
      
      schedule_poll_timeout (poll_monitor);
    }

  if (info)
    g_object_unref (info);
  
  g_object_unref (poll_monitor);
}

static gboolean
poll_file_timeout (gpointer data)
{
  GPollFileMonitor* poll_monitor = data;

150 151
  g_source_unref (poll_monitor->timeout);
  poll_monitor->timeout = NULL;
152

153
  g_file_query_info_async (poll_monitor->file, G_FILE_ATTRIBUTE_ETAG_VALUE "," G_FILE_ATTRIBUTE_STANDARD_SIZE,
154 155
			 0, 0, NULL, got_new_info, g_object_ref (poll_monitor));
  
156
  return G_SOURCE_REMOVE;
157 158 159 160 161
}

static void
schedule_poll_timeout (GPollFileMonitor* poll_monitor)
{
162 163 164 165
  poll_monitor->timeout = g_timeout_source_new_seconds (POLL_TIME_SECS);
  g_source_set_callback (poll_monitor->timeout, poll_file_timeout, poll_monitor, NULL);
  g_source_attach (poll_monitor->timeout, g_main_context_get_thread_default ());
}
166 167

static void
168 169 170
got_initial_info (GObject      *source_object,
                  GAsyncResult *res,
                  gpointer      user_data)
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
{
  GPollFileMonitor* poll_monitor = user_data;
  GFileInfo *info;

  info = g_file_query_info_finish (poll_monitor->file, res, NULL);

  poll_monitor->last_info = info;

  if (!g_file_monitor_is_cancelled (G_FILE_MONITOR (poll_monitor)))
    schedule_poll_timeout (poll_monitor);
  
  g_object_unref (poll_monitor);
}

/**
186
 * _g_poll_file_monitor_new:
187 188 189
 * @file: a #GFile.
 * 
 * Polls @file for changes.
190
 * 
Matthias Clasen's avatar
Matthias Clasen committed
191
 * Returns: a new #GFileMonitor for the given #GFile. 
192 193
 **/
GFileMonitor*
194
_g_poll_file_monitor_new (GFile *file)
195 196 197 198 199 200 201
{
  GPollFileMonitor* poll_monitor;
  
  poll_monitor = g_object_new (G_TYPE_POLL_FILE_MONITOR, NULL);

  poll_monitor->file = g_object_ref (file);

202
  g_file_query_info_async (file, G_FILE_ATTRIBUTE_ETAG_VALUE "," G_FILE_ATTRIBUTE_STANDARD_SIZE,
203 204 205 206 207 208 209 210 211 212 213 214
			   0, 0, NULL, got_initial_info, g_object_ref (poll_monitor));
  
  return G_FILE_MONITOR (poll_monitor);
}

static gboolean
g_poll_file_monitor_cancel (GFileMonitor* monitor)
{
  GPollFileMonitor *poll_monitor = G_POLL_FILE_MONITOR (monitor);
  
  if (poll_monitor->timeout)
    {
215 216 217
      g_source_destroy (poll_monitor->timeout);
      g_source_unref (poll_monitor->timeout);
      poll_monitor->timeout = NULL;
218 219 220 221
    }
  
  return TRUE;
}