summaryrefslogtreecommitdiff
path: root/daemon/gvfsjobenumerate.c
blob: 90ff45209b44df5a15c88c6e87bb2d2e68bfb35c (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* 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
 * version 2 of the License, or (at your option) any later version.
 *
 * 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
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

#include <config.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

#include <glib.h>
#include <dbus/dbus.h>
#include <glib/gi18n.h>
#include "gvfsjobenumerate.h"
#include "gvfsdbusutils.h"
#include "gvfsdaemonprotocol.h"

G_DEFINE_TYPE (GVfsJobEnumerate, g_vfs_job_enumerate, G_VFS_TYPE_JOB_DBUS)

static void         run        (GVfsJob        *job);
static gboolean     try        (GVfsJob        *job);
static void         send_reply   (GVfsJob        *job);
static DBusMessage *create_reply (GVfsJob        *job,
				  DBusConnection *connection,
				  DBusMessage    *message);

static void
g_vfs_job_enumerate_finalize (GObject *object)
{
  GVfsJobEnumerate *job;

  job = G_VFS_JOB_ENUMERATE (object);

  g_free (job->filename);
  g_free (job->attributes);
  g_file_attribute_matcher_unref (job->attribute_matcher);
  g_free (job->object_path);
  g_free (job->uri);
  
  if (G_OBJECT_CLASS (g_vfs_job_enumerate_parent_class)->finalize)
    (*G_OBJECT_CLASS (g_vfs_job_enumerate_parent_class)->finalize) (object);
}

static void
g_vfs_job_enumerate_class_init (GVfsJobEnumerateClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GVfsJobClass *job_class = G_VFS_JOB_CLASS (klass);
  GVfsJobDBusClass *job_dbus_class = G_VFS_JOB_DBUS_CLASS (klass);
  
  gobject_class->finalize = g_vfs_job_enumerate_finalize;
  job_class->run = run;
  job_class->try = try;
  job_class->send_reply = send_reply;
  job_dbus_class->create_reply = create_reply;
}

static void
g_vfs_job_enumerate_init (GVfsJobEnumerate *job)
{
}

GVfsJob *
g_vfs_job_enumerate_new (DBusConnection *connection,
			 DBusMessage *message,
			 GVfsBackend *backend)
{
  GVfsJobEnumerate *job;
  DBusMessage *reply;
  DBusError derror;
  int path_len;
  const char *obj_path;
  const char *path_data;
  char *attributes, *uri;
  dbus_uint32_t flags;
  DBusMessageIter iter;
  
  dbus_message_iter_init (message, &iter);
  dbus_error_init (&derror);
  if (!_g_dbus_message_iter_get_args (&iter, &derror, 
				      DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
				      &path_data, &path_len,
				      DBUS_TYPE_STRING, &obj_path,
				      DBUS_TYPE_STRING, &attributes,
				      DBUS_TYPE_UINT32, &flags,
				      0))
    {
      reply = dbus_message_new_error (message,
				      derror.name,
                                      derror.message);
      dbus_error_free (&derror);

      dbus_connection_send (connection, reply, NULL);
      return NULL;
    }

  /* Optional uri arg for thumbnail info */
  if (!_g_dbus_message_iter_get_args (&iter, NULL,
				      DBUS_TYPE_STRING, &uri,
				      0))
    uri = NULL;

  job = g_object_new (G_VFS_TYPE_JOB_ENUMERATE,
		      "message", message,
		      "connection", connection,
		      NULL);
  
  job->object_path = g_strdup (obj_path);
  job->filename = g_strndup (path_data, path_len);
  job->backend = backend;
  job->attributes = g_strdup (attributes);
  job->attribute_matcher = g_file_attribute_matcher_new (attributes);
  job->flags = flags;
  job->uri = g_strdup (uri);
  
  return G_VFS_JOB (job);
}

static void
send_infos (GVfsJobEnumerate *job)
{
  if (!dbus_message_iter_close_container (&job->building_iter, &job->building_array_iter))
    _g_dbus_oom ();
  
  dbus_connection_send (g_vfs_job_dbus_get_connection (G_VFS_JOB_DBUS (job)),
			job->building_infos, NULL);
  dbus_message_unref (job->building_infos);
  job->building_infos = NULL;
  job->n_building_infos = 0;
}

void
g_vfs_job_enumerate_add_info (GVfsJobEnumerate *job,
			      GFileInfo *info)
{
  DBusMessage *message, *orig_message;
  char *uri, *escaped_name;
  
  if (job->building_infos == NULL)
    {
      orig_message = g_vfs_job_dbus_get_message (G_VFS_JOB_DBUS (job));
      
      message = dbus_message_new_method_call (dbus_message_get_sender (orig_message),
					      job->object_path,
					      G_VFS_DBUS_ENUMERATOR_INTERFACE,
					      G_VFS_DBUS_ENUMERATOR_OP_GOT_INFO);
      dbus_message_set_no_reply (message, TRUE);
      
      dbus_message_iter_init_append (message, &job->building_iter);
      
      if (!dbus_message_iter_open_container (&job->building_iter,
					     DBUS_TYPE_ARRAY,
					     G_FILE_INFO_TYPE_AS_STRING, 
					     &job->building_array_iter))
	_g_dbus_oom ();

      job->building_infos = message;
      job->n_building_infos = 0;
    }

  

  uri = NULL;
  if (job->uri != NULL &&
      g_file_info_get_name (info) != NULL)
    {
      escaped_name = g_uri_escape_string (g_file_info_get_name (info),
					  G_URI_RESERVED_CHARS_ALLOWED_IN_PATH,
					  FALSE);
      uri = g_build_path ("/", job->uri, escaped_name, NULL);
      g_free (escaped_name);
    }
  
  g_vfs_backend_add_auto_info (job->backend,
			       job->attribute_matcher,
			       info,
			       uri);
  g_free (uri);

  g_file_info_set_attribute_mask (info, job->attribute_matcher);
  
  _g_dbus_append_file_info (&job->building_array_iter, info);
  job->n_building_infos++;

  if (job->n_building_infos == 50)
    send_infos (job);
}

void
g_vfs_job_enumerate_add_infos (GVfsJobEnumerate *job,
			       const GList *infos)
{
  const GList *l;
  GFileInfo *info;

  for (l = infos; l != NULL; l = l->next)
    {
      info = l->data;
      g_vfs_job_enumerate_add_info (job, info);
    }
}

void
g_vfs_job_enumerate_done (GVfsJobEnumerate *job)
{
  DBusMessage *message, *orig_message;
  
  g_assert (!G_VFS_JOB (job)->failed);

  if (job->building_infos != NULL)
    send_infos (job);
  
  orig_message = g_vfs_job_dbus_get_message (G_VFS_JOB_DBUS (job));
  
  message = dbus_message_new_method_call (dbus_message_get_sender (orig_message),
					  job->object_path,
					  G_VFS_DBUS_ENUMERATOR_INTERFACE,
					  G_VFS_DBUS_ENUMERATOR_OP_DONE);
  dbus_message_set_no_reply (message, TRUE);

  dbus_connection_send (g_vfs_job_dbus_get_connection (G_VFS_JOB_DBUS (job)),
			message, NULL);
  dbus_message_unref (message);

  g_vfs_job_emit_finished (G_VFS_JOB (job));
}

static void
run (GVfsJob *job)
{
  GVfsJobEnumerate *op_job = G_VFS_JOB_ENUMERATE (job);
  GVfsBackendClass *class = G_VFS_BACKEND_GET_CLASS (op_job->backend);
  
  if (class->enumerate == NULL)
    {
      g_vfs_job_failed (job, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
			_("Operation not supported by backend"));
      return;
    }
  
  class->enumerate (op_job->backend,
		    op_job,
		    op_job->filename,
		    op_job->attribute_matcher,
		    op_job->flags);
}

static gboolean
try (GVfsJob *job)
{
  GVfsJobEnumerate *op_job = G_VFS_JOB_ENUMERATE (job);
  GVfsBackendClass *class = G_VFS_BACKEND_GET_CLASS (op_job->backend);
  
  if (class->try_enumerate == NULL)
    return FALSE;
  
  return class->try_enumerate (op_job->backend,
			       op_job,
			       op_job->filename,
			       op_job->attribute_matcher,
			       op_job->flags);
}

static void
send_reply (GVfsJob *job)
{
  GVfsJobDBus *dbus_job = G_VFS_JOB_DBUS (job);
  DBusMessage *reply;
  GVfsJobDBusClass *class;

  g_debug ("send_reply(%p), failed=%d (%s)\n", job, job->failed, job->failed?job->error->message:"");
  
  class = G_VFS_JOB_DBUS_GET_CLASS (job);
  
  if (job->failed) 
    reply = _dbus_message_new_from_gerror (dbus_job->message, job->error);
  else
    reply = class->create_reply (job, dbus_job->connection, dbus_job->message);
 
  g_assert (reply != NULL);

  /* Queues reply (threadsafely), actually sends it in mainloop */
  dbus_connection_send (dbus_job->connection, reply, NULL);
  dbus_message_unref (reply);
  
  if (job->failed)
    g_vfs_job_emit_finished (job);
}

/* Might be called on an i/o thread */
static DBusMessage *
create_reply (GVfsJob *job,
	      DBusConnection *connection,
	      DBusMessage *message)
{
  DBusMessage *reply;

  reply = dbus_message_new_method_return (message);

  return reply;
}