summaryrefslogtreecommitdiff
path: root/components/loser/sidebar/nautilus-sidebar-loser.c
blob: a10d6ca21573a0fec054c8b7690b66088c0cf77e (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* 
 * Copyright (C) 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: Maciej Stachowiak <mjs@eazel.com>
 */

/* nautilus-sidebar-loser.c - loser sidebar view component. This
   component fails on demand, either controlled by env variables
   during startup, or using toolbar buttons or menu items.  */

#include <config.h>
#include "nautilus-sidebar-loser.h"

#include <bonobo/bonobo-control.h>
#include <gtk/gtksignal.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <libgnome/gnome-i18n.h>
#include <libgnomeui/gnome-stock.h>
#include <libnautilus/nautilus-bonobo-ui.h>
#include <eel/eel-gtk-macros.h>
#include <stdio.h>
#include <stdlib.h>


/* A NautilusSidebarLoser's private information. */
struct NautilusSidebarLoserDetails {
	char *uri;
	NautilusView *nautilus_view;
};

static void nautilus_sidebar_loser_initialize_class (NautilusSidebarLoserClass *klass);
static void nautilus_sidebar_loser_initialize       (NautilusSidebarLoser      *view);
static void nautilus_sidebar_loser_destroy          (GtkObject                      *object);

EEL_DEFINE_CLASS_BOILERPLATE (NautilusSidebarLoser, nautilus_sidebar_loser, GTK_TYPE_LABEL)
     
static void loser_load_location_callback      (NautilusView         *nautilus_view,
					       const char           *location,
					       NautilusSidebarLoser *view);
static void loser_merge_bonobo_items_callback (BonoboObject         *control,
					       gboolean              state,
					       gpointer              user_data);
static void nautilus_sidebar_loser_fail       (void);
static void ensure_fail_env                   (void);

static void
nautilus_sidebar_loser_initialize_class (NautilusSidebarLoserClass *klass)
{
	GtkObjectClass *object_class;
	
	object_class = GTK_OBJECT_CLASS (klass);
	
	object_class->destroy = nautilus_sidebar_loser_destroy;
}

static void
nautilus_sidebar_loser_initialize (NautilusSidebarLoser *view)
{
	view->details = g_new0 (NautilusSidebarLoserDetails, 1);
	
	gtk_label_set_text (GTK_LABEL (view), g_strdup ("(none)"));
	
	view->details->nautilus_view = nautilus_view_new (GTK_WIDGET (view));
	
	gtk_signal_connect (GTK_OBJECT (view->details->nautilus_view), 
			    "load_location",
			    GTK_SIGNAL_FUNC (loser_load_location_callback), 
			    view);

	/* 
	 * Get notified when our bonobo control is activated so we
	 * can merge menu & toolbar items into Nautilus's UI.
	 */
        gtk_signal_connect (GTK_OBJECT (nautilus_view_get_bonobo_control
					(view->details->nautilus_view)),
                            "activate",
                            loser_merge_bonobo_items_callback,
                            view);
	
	gtk_widget_show (GTK_WIDGET (view));
}

static void
nautilus_sidebar_loser_destroy (GtkObject *object)
{
	NautilusSidebarLoser *view;
	
	view = NAUTILUS_SIDEBAR_LOSER (object);
	
	g_free (view->details->uri);
	g_free (view->details);
	
	EEL_CALL_PARENT (GTK_OBJECT_CLASS, destroy, (object));
}

/**
 * nautilus_sidebar_loser_get_nautilus_view:
 *
 * Return the NautilusView object associated with this view; this
 * is needed to export the view via CORBA/Bonobo.
 * @view: NautilusSidebarLoser to get the nautilus_view from..
 * 
 **/
NautilusView *
nautilus_sidebar_loser_get_nautilus_view (NautilusSidebarLoser *view)
{
	return view->details->nautilus_view;
}

/**
 * nautilus_sidebar_loser_load_uri:
 *
 * Load the resource pointed to by the specified URI.
 * @view: NautilusSidebarLoser to get the nautilus_view from.
 * 
 **/
void
nautilus_sidebar_loser_load_uri (NautilusSidebarLoser *view,
				       const char               *uri)
{
	char *label_text;
	
	g_free (view->details->uri);
	view->details->uri = g_strdup (uri);

	label_text = g_strdup_printf (_("%s\n\nLoser sidebar."), uri);
	gtk_label_set_text (GTK_LABEL (view), label_text);
	g_free (label_text);
}

static void
loser_load_location_callback (NautilusView  *nautilus_view, 
			      const char *location,
			      NautilusSidebarLoser *view)
{
	g_assert (nautilus_view == view->details->nautilus_view);
	
	nautilus_sidebar_loser_maybe_fail ("pre-underway");
	
	/* It's mandatory to send an underway message once the
	 * component starts loading, otherwise nautilus will assume it
	 * failed. In a real component, this will probably happen in
	 * some sort of callback from whatever loading mechanism it is
	 * using to load the data; this component loads no data, so it
	 * gives the progress update here.
	 */
	nautilus_view_report_load_underway (nautilus_view);
	
	nautilus_sidebar_loser_maybe_fail ("pre-load");

	/* Do the actual load. */
	nautilus_sidebar_loser_load_uri (view, location);
	
	nautilus_sidebar_loser_maybe_fail ("pre-done");

	/* It's mandatory to call report_load_complete once the
	 * component is done loading successfully, or
	 * report_load_failed if it completes unsuccessfully. In a
	 * real component, this will probably happen in some sort of
	 * callback from whatever loading mechanism it is using to
	 * load the data; this component loads no data, so it gives
	 * the progress update here.
	 */
	nautilus_view_report_load_complete (nautilus_view);

	nautilus_sidebar_loser_maybe_fail ("post-done");
}

static void
bonobo_loser_callback (BonoboUIComponent *ui, gpointer user_data, const char *verb)
{
        g_assert (NAUTILUS_IS_SIDEBAR_LOSER (user_data));

	nautilus_sidebar_loser_fail ();
	gtk_label_set_text (GTK_LABEL (user_data), _("You have tried to kill the Sidebar Loser"));
}

static void
loser_merge_bonobo_items_callback (BonoboObject *control, gboolean state, gpointer user_data)
{
 	NautilusSidebarLoser *view;
	BonoboUIComponent *ui_component;
	BonoboUIVerb verbs [] = {
		BONOBO_UI_VERB ("Kill Sidebar Loser", bonobo_loser_callback),
		BONOBO_UI_VERB_END
	};
	
	nautilus_sidebar_loser_maybe_fail ("pre-merge");

	g_assert (NAUTILUS_IS_SIDEBAR_LOSER (user_data));

	view = NAUTILUS_SIDEBAR_LOSER (user_data);

	if (state) {
		ui_component = nautilus_view_set_up_ui (view->details->nautilus_view,
							DATADIR,
							"nautilus-sidebar-loser-ui.xml",
							"nautilus-sidebar-loser");

		bonobo_ui_component_add_verb_list_with_data (ui_component, verbs, view);
	} else {
		/* Do nothing. */
	}

	nautilus_sidebar_loser_maybe_fail ("post-merge");


        /* 
         * Note that we do nothing if state is FALSE. Nautilus sidebar views are activated
         * when installed, but never explicitly deactivated. When the view changes to another,
         * the sidebar view object is destroyed, which ends up calling bonobo_ui_handler_unset_container,
         * which removes its merged menu & toolbar items.
         */
}

static char *failure_mode = NULL;
static char *failure_point = NULL;
static gboolean env_checked = FALSE;

void
nautilus_sidebar_loser_maybe_fail (const char *location)
{
	ensure_fail_env ();
	
	if (strcasecmp (location, failure_point) == 0) {
		nautilus_sidebar_loser_fail ();
	}
}
				   


static void
nautilus_sidebar_loser_fail (void)
{
	ensure_fail_env ();
	
	if (strcasecmp (failure_mode, "hang") == 0) {
		while (1) {
		}
	} else if (strcasecmp (failure_mode, "exit") == 0) {
		exit (0);
	} else if (strcasecmp (failure_mode, "error-exit") == 0) {
		exit (-1);
	} else if (strcasecmp (failure_mode, "crash") == 0) {
		abort ();
	} else {
		puts ("XXX - would fail now, if NAUTILUS_SIDEBAR_LOSER_MODE were set properly.");
	}
}


static void
ensure_fail_env (void)
{
	if (!env_checked) {
		failure_mode = g_getenv ("NAUTILUS_SIDEBAR_LOSER_MODE");
		if (failure_mode == NULL) {
			failure_mode = "";
		}
		
		failure_point = g_getenv ("NAUTILUS_SIDEBAR_LOSER_PLACE");
		if (failure_point == NULL) {
			failure_point = "";
		}
		
		env_checked = TRUE;
	}
}