summaryrefslogtreecommitdiff
path: root/libnautilus-extensions/nautilus-search-async.c
blob: a39e9085fc98e2480f7755039cf58a82170fe368 (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-

   nautilus-search-async.c :  The search process
 
   Copyright (C) 1999, 2000 Eazel, Inc.
  
   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.
  
   This program 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
   General Public License for more details.
  
   You should have received a copy of the GNU General Public
   License along with this program; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
  
   Author: Rebecca Schulman <rebecka@eazel.com>
*/

#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <fcntl.h>
#include <unistd.h>
#include <gtk/gtkmain.h>
#include <gdk/gdk.h>
#include <libgnomevfs/gnome-vfs-types.h>
#include <nautilus-directory-private.h>
#include <libmedusa/medusa-search-service.h>
#include <libmedusa/medusa-search-service-private.h>
#include <glib.h>

#include "nautilus-search-async.h"

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif



static GnomeVFSResult   request_search                    (char *search_uri);
static int              initialize_socket                 (struct sockaddr_un *daemon_address);
static int              get_key_from_cookie               (void);
void                    parse_results                     (char *transmission);
static void             search_results_received_callback  (GIOChannel *source,
							   GIOCondition condition,
							   gpointer data);

/* this procedure is meant to mimic the behavior of the
   async load directory uri calls, so it takes
   the same arguments */
GnomeVFSResult
nautilus_async_medusa_search (GnomeVFSAsyncHandle **handle_return,
			      char *search_uri_text,
			      GnomeVFSAsyncDirectoryLoadCallback callback,
			      gpointer data)
{
	GnomeVFSResult result;

	g_return_val_if_fail (handle_return != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
	g_return_val_if_fail (search_uri_text != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
	g_return_val_if_fail (nautilus_uri_is_search_uri (search_uri_text) == TRUE,
			      GNOME_VFS_ERROR_BAD_PARAMETERS);

	/* the sending of the request is done synchronously,
	   and the nautilus receives the results over a channel. */
	result = request_search (search_uri_text);
	
	return result;
}

static GnomeVFSResult
request_search (char *search_uri) 
{
	int search_request_port;
	struct sockaddr_un *server_address;
	char cookie_request[MAX_LINE];
	char request_field[MAX_LINE];
	int key;
	GIOChannel *result_channel;

	
	printf ("help!! I'm trying to run a search!\n");
	/* FIXME:  This crap will be replaced by
	   the new medusa search service API */
	g_return_val_if_fail (search_uri != NULL, GNOME_VFS_ERROR_INVALID_URI);
	g_return_val_if_fail (nautilus_uri_is_search_uri (search_uri), 
			      GNOME_VFS_ERROR_INVALID_URI);


	/* For now run a dummy search */
	search_request_port = initialize_socket (server_address);


	/* Send request for cookie */
	sprintf (cookie_request, "%s\t%d\t%d\n", COOKIE_REQUEST, getuid (), getpid());
	printf ("Sending %s\n", cookie_request);
	g_return_val_if_fail (write (search_request_port, cookie_request, 
				     strlen (cookie_request)) > 0,
			      GNOME_VFS_ERROR_DIRECTORY_BUSY);

	

	key = get_key_from_cookie ();
	printf ("Got cookie %d from cookie file\n", key);
	sprintf (request_field, "%d %d %d\tFile_Name ^ tmp\n", getuid (), getpid (), key);
	
	printf ("Sending %s", request_field);
	g_return_val_if_fail (write (search_request_port, request_field, 
				     strlen(request_field)) > 0, 
			      GNOME_VFS_ERROR_DIRECTORY_BUSY);
	
	memset (request_field, 0, MAX_LINE);
	sprintf (request_field,"%d %d %d\tDONE\n", getuid (), getpid (), key);
	g_return_val_if_fail (write (search_request_port, request_field, 
				     strlen(request_field)) > 0, 
			      GNOME_VFS_ERROR_DIRECTORY_BUSY);
	
	/* Results are gotten in a different place */
	/* Set up a watch on the result socket */
	/* FIXME:  Is there a right macro here for these function casts? */
	result_channel = g_io_channel_unix_new (search_request_port);
	g_io_add_watch (result_channel,
			G_IO_IN,
			(GIOFunc) search_results_received_callback,
			search_uri);

	return GNOME_VFS_OK;
}




static int
initialize_socket (struct sockaddr_un *daemon_address)
{
  int search_request_port;
    
  search_request_port = socket (AF_LOCAL, SOCK_STREAM, 0);
  g_return_val_if_fail (search_request_port != -1, 3);

  daemon_address->sun_family = AF_LOCAL;
  /* FIXME:  This number (108) sucks, but it has no #define in the header.
     What to do? (POSIX requires 100 bytes at least, here)  */
  snprintf (daemon_address->sun_path, 100, "%s", SEARCH_SOCKET_PATH);

  g_return_val_if_fail (connect (search_request_port, (struct sockaddr *) daemon_address,
			     SUN_LEN (daemon_address)) != -1, -1);
  
  

  return search_request_port;
}


static int
get_key_from_cookie ()
{
  char file_name[MAX_LINE];
  int cookie_fd, key;
  /* Go look for cookie */
  sprintf (file_name, "%s/%d_%d", COOKIE_PATH, getuid (), getpid ());
  printf ("Looking in cookie file %s\n", file_name);

  cookie_fd = open (file_name, O_RDONLY);
  /* Keep looking if cookie file isn't created yet */
  while (cookie_fd == -1) {
    cookie_fd = open (file_name, O_RDONLY);
  }
  read (cookie_fd, &key, sizeof (int));
  close (cookie_fd);
  
  return key;
}



void
parse_results (char *transmission)
{
  char *line;
  line = transmission;
#ifdef SEARCH_DAEMON_DEBUG
  /* printf ("Received %s\n", line); */
#endif
  while ((line - 1) != NULL && *line != 0) {
    if (strncmp (line, SEARCH_FILE_TRANSMISSION, strlen (SEARCH_FILE_TRANSMISSION)) == 0) {
      printf ("next file is %s", line);
    }
    else if (strncmp (line, SEARCH_END_TRANSMISSION, strlen (SEARCH_END_TRANSMISSION)) == 0) {
      exit (1);
    }
    line = strchr (line, 0);
    line++;
  }
}

static void
search_results_received_callback (GIOChannel *source,
				  GIOCondition condition,
				  gpointer data)
{
	
	g_assert (condition == G_IO_IN);
	/* Make sure source is a valid file descriptor */
	printf ("Received search results\n");

}