summaryrefslogtreecommitdiff
path: root/client/gdaemonfilemonitor.c
blob: 294a9c60b1d51a2fbf450a7807e0c2586811d627 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <config.h>
#include <string.h>

#include "gdaemonfilemonitor.h"
#include <gio/gfilemonitor.h>
#include <gvfsdaemondbus.h>
#include <gvfsdaemonprotocol.h>
#include "gdbusutils.h"
#include "gmountspec.h"
#include "gdaemonfile.h"

#define OBJ_PATH_PREFIX "/org/gtk/vfs/client/filemonitor/"

/* atomic */
static volatile gint path_counter = 1;

static gboolean g_daemon_file_monitor_cancel (GFileMonitor* monitor);
static DBusHandlerResult g_daemon_file_monitor_dbus_filter (DBusConnection     *connection,
							    DBusMessage        *message,
							    void               *user_data);


struct _GDaemonFileMonitor
{
  GFileMonitor parent_instance;

  char *object_path;
  char *remote_obj_path;
  char *remote_id;
};

G_DEFINE_TYPE (GDaemonFileMonitor, g_daemon_file_monitor, G_TYPE_FILE_MONITOR)

static void
g_daemon_file_monitor_finalize (GObject* object)
{
  GDaemonFileMonitor *daemon_monitor;
  
  daemon_monitor = G_DAEMON_FILE_MONITOR (object);

  _g_dbus_unregister_vfs_filter (daemon_monitor->object_path);
  
  g_free (daemon_monitor->object_path);
  g_free (daemon_monitor->remote_id);
  g_free (daemon_monitor->remote_obj_path);
  
  if (G_OBJECT_CLASS (g_daemon_file_monitor_parent_class)->finalize)
    (*G_OBJECT_CLASS (g_daemon_file_monitor_parent_class)->finalize) (object);
}

static void
g_daemon_file_monitor_class_init (GDaemonFileMonitorClass* klass)
{
  GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
  GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
  
  gobject_class->finalize = g_daemon_file_monitor_finalize;

  file_monitor_class->cancel = g_daemon_file_monitor_cancel;
}

static void
g_daemon_file_monitor_init (GDaemonFileMonitor* daemon_monitor)
{
  gint id;
  
  id = g_atomic_int_exchange_and_add (&path_counter, 1);

  daemon_monitor->object_path = g_strdup_printf (OBJ_PATH_PREFIX"%d", id);
  
  _g_dbus_register_vfs_filter (daemon_monitor->object_path,
			       g_daemon_file_monitor_dbus_filter,
			       G_OBJECT (daemon_monitor));
}

GFileMonitor*
g_daemon_file_monitor_new (const char *remote_id,
				const char *remote_obj_path)
{
  GDaemonFileMonitor* daemon_monitor;
  DBusMessage *message;
  
  daemon_monitor = g_object_new (G_TYPE_DAEMON_FILE_MONITOR, NULL);

  daemon_monitor->remote_id = g_strdup (remote_id);
  daemon_monitor->remote_obj_path = g_strdup (remote_obj_path);

  message =
    dbus_message_new_method_call (daemon_monitor->remote_id,
				  daemon_monitor->remote_obj_path,
				  G_VFS_DBUS_MONITOR_INTERFACE,
				  G_VFS_DBUS_MONITOR_OP_SUBSCRIBE);

  _g_dbus_message_append_args (message, DBUS_TYPE_OBJECT_PATH,
			       &daemon_monitor->object_path, 0);

  _g_vfs_daemon_call_async (message,
			    NULL, NULL,
			    NULL);

  dbus_message_unref (message);
  
  return G_FILE_MONITOR (daemon_monitor);
}

static DBusHandlerResult
g_daemon_file_monitor_dbus_filter (DBusConnection     *connection,
				   DBusMessage        *message,
				   void               *user_data)
{
  GDaemonFileMonitor *monitor = G_DAEMON_FILE_MONITOR (user_data);
  const char *member;
  guint32 event_type;
  DBusMessageIter iter;
  GMountSpec *spec1, *spec2;
  char *path1, *path2;
  GFile *file1, *file2;
  
  member = dbus_message_get_member (message);

  if (strcmp (member, G_VFS_DBUS_MONITOR_CLIENT_OP_CHANGED) == 0)
    {
      dbus_message_iter_init (message, &iter);
      
      if (!_g_dbus_message_iter_get_args (&iter, NULL,
					  DBUS_TYPE_UINT32, &event_type,
					  0))
	return DBUS_HANDLER_RESULT_HANDLED;
      
      spec1 = g_mount_spec_from_dbus (&iter);
      if (!_g_dbus_message_iter_get_args (&iter, NULL,
					  G_DBUS_TYPE_CSTRING, &path1,
					  0))
	{
	  g_mount_spec_unref (spec1);
	  return DBUS_HANDLER_RESULT_HANDLED;
	}

      file1 = g_daemon_file_new (spec1, path1);
      
      g_mount_spec_unref (spec1);
      g_free (path1);

      file2 = NULL;
      
      spec2 = g_mount_spec_from_dbus (&iter);
      if (spec2) {
	if (_g_dbus_message_iter_get_args (&iter, NULL,
					   G_DBUS_TYPE_CSTRING, &path2,
					   0))
	  {
	    file2 = g_daemon_file_new (spec2, path2);

	    g_free (path2);
	  }
	
	g_mount_spec_unref (spec2);
      }

      g_file_monitor_emit_event (G_FILE_MONITOR (monitor),
				 file1, file2,
				 event_type);
      
      return DBUS_HANDLER_RESULT_HANDLED;
    }

  return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}


static gboolean
g_daemon_file_monitor_cancel (GFileMonitor* monitor)
{
  GDaemonFileMonitor *daemon_monitor = G_DAEMON_FILE_MONITOR (monitor);
  DBusMessage *message;
  
  message =
    dbus_message_new_method_call (daemon_monitor->remote_id,
				  daemon_monitor->remote_obj_path,
				  G_VFS_DBUS_MONITOR_INTERFACE,
				  G_VFS_DBUS_MONITOR_OP_UNSUBSCRIBE);

  _g_vfs_daemon_call_async (message,
			    NULL, NULL, 
			    NULL);
  
  dbus_message_unref (message);
  
  return TRUE;
}