aboutsummaryrefslogtreecommitdiffstats
path: root/embed/mozilla/ExternalProtocolService.cpp
blob: 728cd0f5c4ecbd636e6948995f243a14858e61d8 (plain) (blame)
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
/*
 *  Copyright (C) 2001 Philip Langdale
 *
 *  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, 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.
 */

#include <gtk/gtk.h>
#include <libgnome/gnome-exec.h>
#include <libgnome/gnome-i18n.h>
#include <libgnome/gnome-url.h>

#include <nsString.h>
#include <nsXPIDLString.h>
#include <nsCOMPtr.h>
#include <nsIURI.h>
#include <nsIDOMWindow.h>
#include <nsIWindowWatcher.h>
#include <nsIServiceManager.h>
#include <nsXPComFactory.h>

#include "ephy-prefs.h"
#include "eel-gconf-extensions.h"

#include "ExternalProtocolService.h"

#define WINDOWWATCHER_CONTRACTID "@mozilla.org/embedcomp/window-watcher;1"

/* Implementation file */
NS_IMPL_ISUPPORTS1(GExternalProtocolService, nsIExternalProtocolService)

GExternalProtocolService::GExternalProtocolService()
{
  NS_INIT_ISUPPORTS();
  /* member initializers and constructor code */
}

GExternalProtocolService::~GExternalProtocolService()
{
  /* destructor code */
}

/* boolean externalProtocolHandlerExists (in string aProtocolScheme); */
NS_IMETHODIMP GExternalProtocolService::
        ExternalProtocolHandlerExists(const char *aProtocolScheme,
                          PRBool *_retval)
{
    /* build the config key */
    char *key = g_strconcat ("/desktop/gnome/url-handlers/",
                 aProtocolScheme,
                 "/command", NULL);

    char *tmp = eel_gconf_get_string(key);
    g_free (key);

    *_retval = (tmp != NULL);
    g_free (tmp);

    return NS_OK;
}

/* void loadUrl (in nsIURI aURL); */
NS_IMETHODIMP GExternalProtocolService::LoadUrl(nsIURI *aURL)
{
    nsCAutoString cSpec;
    aURL->GetSpec (cSpec);
    nsCAutoString cScheme;
    aURL->GetScheme (cScheme);

    if (cScheme.Equals("http"))
    {
        nsresult rv;
        nsCOMPtr<nsIWindowWatcher> ww;
        ww = do_GetService(WINDOWWATCHER_CONTRACTID, &rv);
        if (NS_SUCCEEDED(rv))
        {
            nsCOMPtr<nsIDOMWindow> newWin;
            rv = ww->OpenWindow(nsnull, cSpec.get(),
                        nsnull, nsnull, nsnull,
                        getter_AddRefs(newWin));
            if (NS_SUCCEEDED(rv)) return NS_OK;
        }
    }

    /* build the config key */
    const nsCAutoString key(NS_LITERAL_CSTRING("/desktop/gnome/url-handlers/") +
                cScheme + NS_LITERAL_CSTRING("/command"));

    /* find it */
    char *result = eel_gconf_get_string(key.get());
    if (result)
    {
        gnome_url_show(cSpec.get(), NULL);
        g_free (result);
        return NS_OK;
    }

    /* no luck, so offer the user the option of trying the
     * default handler -- we don't do this automatically in
     * case the default handler is erroneously set to epiphany */
    result = eel_gconf_get_string("/desktop/gnome/url-handlers/unknown/command");

    /* check there is a default */
    {
        GtkWidget *dialog;
        
        /* throw the error */
        dialog = gtk_message_dialog_new (NULL, (GtkDialogFlags)0, 
                         GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
                             _("Galeon cannot handle this protocol,\n"
                                 "and no GNOME default handler is set"));
        gtk_dialog_run (GTK_DIALOG(dialog));
        gtk_widget_destroy (dialog);
        
        /* don't let mozilla try blindly */
        return NS_ERROR_FAILURE;
    }
    g_free (result);

    /* offer the choice */
    GtkWidget *dialog = gtk_message_dialog_new (NULL, (GtkDialogFlags)0, 
                            GTK_MESSAGE_QUESTION,
                            GTK_BUTTONS_YES_NO,
                            _("The protocol specified "
                             "is not recognised.\n\n"
                             "Would you like to try "
                             "the GNOME default?"));
    
    int ret = gtk_dialog_run (GTK_DIALOG(dialog));
    gtk_widget_destroy (dialog);

    if (ret == 0)
    {
        gnome_url_show(cSpec.get(), NULL);      
        return NS_OK;
    }
    else
    {
        return NS_ERROR_FAILURE;
    }

    return NS_OK;
}

NS_DEF_FACTORY (GExternalProtocolService, GExternalProtocolService);

/**
 * NS_NewExternalProtocolServiceFactory:
 */ 
nsresult NS_NewExternalProtocolServiceFactory(nsIFactory** aFactory)
{
    NS_ENSURE_ARG_POINTER(aFactory);
    *aFactory = nsnull;

    nsGExternalProtocolServiceFactory *result = new nsGExternalProtocolServiceFactory;
    if (result == NULL)
    {
        return NS_ERROR_OUT_OF_MEMORY;
    }
    
    NS_ADDREF(result);
    *aFactory = result;

    return NS_OK;
}