summaryrefslogtreecommitdiff
path: root/src/nautilus-applicable-views.c
blob: afc2c93de0eb697e388a31f35a4d1375186aeb69 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */

/*
 *  Nautilus
 *
 *  Copyright (C) 1999, 2000 Red Hat, Inc.
 *  Copyright (C) 2000, 2001 Eazel, Inc.
 *
 *  Nautilus 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.
 *
 *  Nautilus 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Authors: Elliot Lee <sopwith@redhat.com>
 *           Maciej Stachowiak <mjs@eazel.com>
 *           Darin Adler <darin@eazel.com>
 *
 */


/* #define DEBUG_MJS 1 */

/* nautilus-applicable-views.c: Implementation of routines for mapping a location
   change request to a set of views and actual URL to be loaded. */

#include <config.h>
#include "nautilus-applicable-views.h"

#include <libgnomevfs/gnome-vfs-result.h>
#include <libnautilus-extensions/nautilus-directory.h>
#include <libnautilus-extensions/nautilus-file.h>
#include <libnautilus-extensions/nautilus-mime-actions.h>
#include <libnautilus-extensions/nautilus-view-identifier.h>

struct NautilusDetermineViewHandle {
        NautilusDetermineViewHandle **early_completion_hook;
	NautilusDetermineViewCallback callback;
	gpointer callback_data;
        NautilusFile *file;
        NautilusDirectory *directory;
};

static NautilusDetermineViewResult
get_view_result_from_gnome_vfs_result (GnomeVFSResult gnome_vfs_result)
{
        switch (gnome_vfs_result) {
        case GNOME_VFS_OK:
                return NAUTILUS_DETERMINE_VIEW_OK;
        case GNOME_VFS_ERROR_NOT_FOUND:
                return NAUTILUS_DETERMINE_VIEW_NOT_FOUND;
        case GNOME_VFS_ERROR_INVALID_URI:
                return NAUTILUS_DETERMINE_VIEW_INVALID_URI;
        case GNOME_VFS_ERROR_NOT_SUPPORTED:
                return NAUTILUS_DETERMINE_VIEW_UNSUPPORTED_SCHEME;
	case GNOME_VFS_ERROR_LOGIN_FAILED:
		return NAUTILUS_DETERMINE_VIEW_LOGIN_FAILED;
	case GNOME_VFS_ERROR_SERVICE_NOT_AVAILABLE:	
		return NAUTILUS_DETERMINE_VIEW_SERVICE_NOT_AVAILABLE;
	case GNOME_VFS_ERROR_ACCESS_DENIED:	
		return NAUTILUS_DETERMINE_VIEW_ACCESS_DENIED;
        case GNOME_VFS_ERROR_HOST_NOT_FOUND:
                return NAUTILUS_DETERMINE_VIEW_HOST_NOT_FOUND;
	case GNOME_VFS_ERROR_HOST_HAS_NO_ADDRESS:
		return NAUTILUS_DETERMINE_VIEW_HOST_HAS_NO_ADDRESS;
        case GNOME_VFS_ERROR_GENERIC:
        case GNOME_VFS_ERROR_INTERNAL:
                /* These two have occurred at least once in the web browser component */
                return NAUTILUS_DETERMINE_VIEW_UNSPECIFIC_ERROR;
        default:
                /* Whenever this message fires, we should consider adding a specific case
                 * to make the error as comprehensible as possible to the user. Please
                 * bug me (sullivan@eazel.com) if you see this fire and don't have the
                 * inclination to immediately make a good message yourself (tell me
                 * what GnomeVFSResult code the message reported, and what caused it to
                 * fire).
                 */
                g_warning ("in nautilus-applicable-views.c, got unhandled GnomeVFSResult %d (%s). "
                           "If this is a legitimate get_file_info result, please tell "
                           "sullivan@eazel.com so he can "
                	   "write a decent user-level error message for it.", 
                	   gnome_vfs_result,
                	   gnome_vfs_result_to_string (gnome_vfs_result));
                return NAUTILUS_DETERMINE_VIEW_UNSPECIFIC_ERROR;
        }
}

static void
got_file_info_callback (NautilusFile *file,
                        gpointer callback_data)
{
        NautilusDetermineViewHandle *handle;
        GnomeVFSResult vfs_result_code;
        NautilusDetermineViewResult result_code;
        NautilusViewIdentifier *default_id;
        OAF_ServerInfo *default_component;
        
        handle = (NautilusDetermineViewHandle *) callback_data;
        
        g_assert (handle->file == file);
	default_id = NULL;
        
        vfs_result_code = nautilus_file_get_file_info_result (file);
        if (vfs_result_code == GNOME_VFS_OK
            || vfs_result_code == GNOME_VFS_ERROR_NOT_SUPPORTED
            || vfs_result_code == GNOME_VFS_ERROR_INVALID_URI) {
                default_component = nautilus_mime_get_default_component_for_file (handle->file);
                if (default_component != NULL) {
                        default_id = nautilus_view_identifier_new_from_content_view (default_component);
                        CORBA_free (default_component);
                        if (default_id != NULL) {
                                vfs_result_code = GNOME_VFS_OK;
                        }
                }
        }
        
        if (vfs_result_code == GNOME_VFS_OK && default_id == NULL) {
                /* If the complete list is non-empty, the default shouldn't have been NULL */
                g_assert (!nautilus_mime_has_any_components_for_file (handle->file));
                result_code = NAUTILUS_DETERMINE_VIEW_NO_HANDLER_FOR_TYPE;
        } else {
                result_code = get_view_result_from_gnome_vfs_result (vfs_result_code);
 	}

        (* handle->callback) (handle,
                              result_code,
                              default_id,
                              handle->callback_data);
        
        nautilus_view_identifier_free (default_id);

        nautilus_determine_initial_view_cancel (handle);
}

static void
got_minimum_file_info_callback (NautilusFile *file,
                                gpointer callback_data)
{
        NautilusDetermineViewHandle *handle;
        GList *attributes;
        
        handle = (NautilusDetermineViewHandle *) callback_data;

        g_assert (handle->file == file);

        /* We start monitoring files here so we get a single load of
         * the directory instead of multiple ones. The concept is that
         * our load of the directory is shared both with the
         * possible call_when_ready below and with other stuff needed by
         * components.
         */
        nautilus_directory_file_monitor_add (handle->directory, handle,
                                             TRUE, TRUE, NULL, FALSE);
        
        if (nautilus_mime_actions_file_needs_full_file_attributes (file)
            && nautilus_file_get_file_info_result (file) == GNOME_VFS_OK) {
                attributes = nautilus_mime_actions_get_full_file_attributes ();
                nautilus_file_call_when_ready (file, attributes,
                                               got_file_info_callback,
                                               handle);
                g_list_free (attributes);
        } else {
                got_file_info_callback (file, handle);
        }
}

NautilusDetermineViewHandle *
nautilus_determine_initial_view (const char *location,
                                 NautilusDetermineViewCallback callback,
                                 gpointer callback_data)
{
        NautilusDetermineViewHandle *handle;
        GList *attributes;

        g_return_val_if_fail (location != NULL, NULL);
        g_return_val_if_fail (callback != NULL, NULL);

        handle = g_new0 (NautilusDetermineViewHandle, 1);
        
        handle->early_completion_hook = &handle;

        handle->callback = callback;
        handle->callback_data = callback_data;
        
        handle->file = nautilus_file_get (location);
        handle->directory = nautilus_directory_get (location);

        attributes = nautilus_mime_actions_get_minimum_file_attributes ();
        nautilus_file_call_when_ready (handle->file, attributes,
                                       got_minimum_file_info_callback, handle);
        g_list_free (attributes);

        if (handle != NULL) {
                handle->early_completion_hook = NULL;
        }
        
        return handle;
}

void
nautilus_determine_initial_view_cancel (NautilusDetermineViewHandle *handle)
{
        g_return_if_fail (handle != NULL);
        g_return_if_fail (handle->early_completion_hook == NULL
                          || *handle->early_completion_hook == handle);
        g_return_if_fail (handle->callback != NULL);

        if (handle->early_completion_hook != NULL) {
                *handle->early_completion_hook = NULL;
        }

        nautilus_file_cancel_call_when_ready
                (handle->file, got_minimum_file_info_callback, handle);
        nautilus_file_cancel_call_when_ready
                (handle->file, got_file_info_callback, handle);
        nautilus_directory_file_monitor_remove
                (handle->directory, handle);

        nautilus_file_unref (handle->file);
        nautilus_directory_unref (handle->directory);

        g_free (handle);
}