~sarahstrong/ubuntu/lucid/gedit/mypatch

« back to all changes in this revision

Viewing changes to plugins/browse/browse.c

  • Committer: Bazaar Package Importer
  • Author(s): Joe Drew
  • Date: 2002-01-13 02:13:27 UTC
  • Revision ID: james.westby@ubuntu.com-20020113021327-dukaa4n50oykvrjg
Tags: upstream-0.9.6
ImportĀ upstreamĀ versionĀ 0.9.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* browse.c - web browse plugin.
 
2
 *
 
3
 * Copyright (C) 1998 Alex Roberts
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2, or (at your option)
 
8
 * any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
18
 *
 
19
 * Converted to a gmodule plugin by Jason Leach <leach@wam.umd.edu>
 
20
 * Libgladify and better error recovery Chema Celorio <chema@celorio.com>
 
21
 *
 
22
 */
 
23
 
 
24
 
 
25
#include <config.h>
 
26
#include <gnome.h>
 
27
#include <glade/glade.h>
 
28
 
 
29
#include "window.h"
 
30
#include "document.h"
 
31
#include "plugin.h"
 
32
#include "view.h"
 
33
#include "utils.h"
 
34
 
 
35
#define GEDIT_PLUGIN_PROGRAM "lynx"
 
36
/* xgettext translators: !!!!!!!!!!!---------> the name of the plugin only.
 
37
   it is used to display "you can not use the [name] plugin without this program... */
 
38
#define GEDIT_PLUGIN_NAME  _("Browse")
 
39
 
 
40
static GtkWidget *location_label;
 
41
static GtkWidget *url_entry;
 
42
 
 
43
static void
 
44
gedit_plugin_destroy (PluginData *pd)
 
45
{
 
46
        g_free (pd->name);
 
47
}
 
48
 
 
49
static void
 
50
gedit_plugin_finish (GtkWidget *widget, gpointer data)
 
51
{
 
52
        gnome_dialog_close (GNOME_DIALOG(widget));
 
53
}
 
54
 
 
55
/* WE need to make this plugin non-blocking and to improve the error
 
56
   reporting. !! It is very bad !
 
57
   Chema
 
58
*/
 
59
   
 
60
static void
 
61
gedit_plugin_execute (GtkWidget *widget, gint button, gpointer data)
 
62
{
 
63
        int fdpipe[2];
 
64
        gchar *url;
 
65
        GeditDocument *doc;
 
66
        guint length, pos;
 
67
        char buff[1025];
 
68
        int pid;
 
69
        gchar *lynx_location;
 
70
 
 
71
        if (button == 0)
 
72
        {
 
73
                url = g_strdup (gtk_entry_get_text (GTK_ENTRY (url_entry)));
 
74
                
 
75
                if (!url || strlen (url) == 0)
 
76
                {
 
77
                        GnomeDialog *error_dialog;
 
78
                        error_dialog = GNOME_DIALOG (gnome_error_dialog_parented ("Please provide a valid URL.",
 
79
                                                                    gedit_window_active()));
 
80
                        gnome_dialog_run_and_close (error_dialog);
 
81
                        gdk_window_raise (widget->window);
 
82
                        return;
 
83
                }
 
84
                
 
85
                lynx_location = g_strdup (GTK_LABEL(location_label)->label);
 
86
                
 
87
                if (pipe (fdpipe) == -1)
 
88
                {
 
89
                        g_warning ("Could not open pipe\n");
 
90
                        return;
 
91
                }
 
92
  
 
93
                pid = fork();
 
94
                if (pid == 0)
 
95
                {
 
96
                        char *argv[4];
 
97
                        
 
98
                        close (1);
 
99
                        dup (fdpipe[1]);
 
100
                        close (fdpipe[0]);
 
101
                        close (fdpipe[1]);
 
102
      
 
103
                        argv[0] = "lynx";
 
104
                        argv[1] = "-dump";
 
105
                        argv[2] = url;
 
106
                        argv[3] = NULL;
 
107
                        execv (lynx_location, argv);
 
108
 
 
109
                        /* don't use asserts not reached ! Chema.
 
110
                           why crash gedit when you can display an error
 
111
                           and give the user a chance to save it's work ?
 
112
                        g_assert_not_reached ();
 
113
                        */
 
114
                        g_warning ("A undetermined PIPE problem occurred");
 
115
                        return;
 
116
                }
 
117
                close (fdpipe[1]);
 
118
 
 
119
                doc = gedit_document_new_with_title (url);
 
120
 
 
121
                length = 1;
 
122
                pos = 0;
 
123
                while (length > 0)
 
124
                {
 
125
                        buff [ length = read (fdpipe[0], buff, 1024) ] = 0;
 
126
                        if (length > 0)
 
127
                        {
 
128
                                gedit_document_insert_text (doc, buff, pos, FALSE);
 
129
                                pos += length;
 
130
                        }
 
131
                }
 
132
 
 
133
                gedit_view_set_position (gedit_view_active (), 0);
 
134
                /*
 
135
                  gnome_config_push_prefix ("/Editor_Plugins/Browse/");
 
136
                  gnome_config_set_string ("Url", url[0]);
 
137
                  gnome_config_pop_prefix ();
 
138
                  gnome_config_sync ();
 
139
                */
 
140
                g_free (url);
 
141
                g_free (lynx_location);
 
142
        }
 
143
        else
 
144
        {
 
145
                if(button == 2)
 
146
                {
 
147
                        static GnomeHelpMenuEntry help_entry = { "gedit", "plugins.html#browse" };
 
148
                        gnome_help_display (NULL, &help_entry);
 
149
 
 
150
                        return;
 
151
                }       
 
152
        }
 
153
        gnome_dialog_close (GNOME_DIALOG(widget));
 
154
}
 
155
 
 
156
static void
 
157
gedit_plugin_change_location (GtkWidget *button, gpointer userdata)
 
158
{
 
159
        GtkWidget *dialog;
 
160
        GtkWidget *label;
 
161
        gchar * new_location;
 
162
 
 
163
        gedit_debug (DEBUG_PLUGINS, "");
 
164
        dialog = userdata;
 
165
 
 
166
        new_location = gedit_plugin_program_location_change (GEDIT_PLUGIN_PROGRAM,
 
167
                                                             GEDIT_PLUGIN_NAME);
 
168
 
 
169
        if ( new_location == NULL)
 
170
        {
 
171
                gdk_window_raise (dialog->window);
 
172
                return;
 
173
        }
 
174
 
 
175
        /* We need to update the label */
 
176
        label  = gtk_object_get_data (GTK_OBJECT (dialog), "location_label");
 
177
        g_return_if_fail (label!=NULL);
 
178
        gtk_label_set_text (GTK_LABEL (label),
 
179
                            new_location);
 
180
        g_free (new_location);
 
181
 
 
182
        gdk_window_raise (dialog->window);      
 
183
 
 
184
}
 
185
 
 
186
static void
 
187
gedit_plugin_browse_create_dialog (void)
 
188
{
 
189
        GladeXML *gui;
 
190
        GtkWidget *dialog;
 
191
        GtkWidget *change_button;
 
192
        gchar *browser_location;
 
193
 
 
194
        gedit_debug (DEBUG_PLUGINS, "");
 
195
        
 
196
        /* xgettext translators: !!!!!!!!!!!---------> the name of the plugin only.
 
197
         it is used to display "you can not use the [name] plugin without this program... */
 
198
        browser_location = gedit_plugin_program_location_get (GEDIT_PLUGIN_PROGRAM,
 
199
                                                              GEDIT_PLUGIN_NAME,
 
200
                                                              FALSE);
 
201
        
 
202
        g_return_if_fail(browser_location != NULL);
 
203
            
 
204
        gui = glade_xml_new (GEDIT_GLADEDIR "/browse.glade", NULL);
 
205
 
 
206
        if (!gui)
 
207
        {
 
208
                g_warning ("Could not find %s", GEDIT_GLADEDIR "/browse.glade");
 
209
                return;
 
210
        }
 
211
 
 
212
        dialog         = glade_xml_get_widget (gui, "dialog");
 
213
        url_entry      = glade_xml_get_widget (gui, "url_entry");
 
214
        location_label = glade_xml_get_widget (gui, "location_label");
 
215
        change_button  = glade_xml_get_widget (gui, "change_button");
 
216
 
 
217
        g_return_if_fail (dialog != NULL);
 
218
        g_return_if_fail (url_entry != NULL);
 
219
        g_return_if_fail (location_label != NULL);
 
220
        g_return_if_fail (change_button != NULL);
 
221
        
 
222
        /* Set the location label */
 
223
        gtk_object_set_data (GTK_OBJECT (dialog), "location_label", location_label);
 
224
        gtk_label_set_text (GTK_LABEL (location_label),
 
225
                                        browser_location);
 
226
        g_free (browser_location);
 
227
 
 
228
        /* Connect the signals */
 
229
        gtk_signal_connect (GTK_OBJECT (dialog), "clicked",
 
230
                            GTK_SIGNAL_FUNC (gedit_plugin_execute), NULL);
 
231
        gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
 
232
                            GTK_SIGNAL_FUNC (gedit_plugin_finish), NULL);
 
233
        gtk_signal_connect (GTK_OBJECT (change_button), "clicked",
 
234
                            GTK_SIGNAL_FUNC (gedit_plugin_change_location), dialog);
 
235
 
 
236
        /* Set the dialog parent and modal type */ 
 
237
        gnome_dialog_set_parent         (GNOME_DIALOG (dialog),  gedit_window_active());
 
238
        gtk_window_set_modal            (GTK_WINDOW (dialog), TRUE);
 
239
        gnome_dialog_set_default        (GNOME_DIALOG (dialog), 0);
 
240
        gnome_dialog_editable_enters    (GNOME_DIALOG (dialog), GTK_EDITABLE (url_entry));
 
241
 
 
242
        /* Show everything then free the GladeXML memmory */
 
243
        gtk_widget_show_all (dialog);
 
244
        gtk_object_unref (GTK_OBJECT (gui));
 
245
}
 
246
 
 
247
gint
 
248
init_plugin (PluginData *pd)
 
249
{
 
250
        pd->destroy_plugin = gedit_plugin_destroy;
 
251
        pd->name = _("Browse");
 
252
        pd->desc = _("Web browse plugin");
 
253
        pd->long_desc = _("Web browse plugin");
 
254
        pd->author = "Alex Roberts <bse@error.fsnet.co.uk>";
 
255
        pd->needs_a_document = FALSE;
 
256
 
 
257
        pd->private_data = (gpointer)gedit_plugin_browse_create_dialog;
 
258
        pd->installed_by_default = TRUE;
 
259
 
 
260
        return PLUGIN_OK;
 
261
}