aboutsummaryrefslogtreecommitdiffstats
path: root/embed/mozilla/EphyEventListener.cpp
blob: de696bf1e4b1834acc0f3b53a61f04947b4ecb68 (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
/*
 *  Copyright (C) 2000 Marco Pesenti Gritti
 *
 *  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 <nsCOMPtr.h>

#include "EphyEventListener.h"
#include "nsIDOMNode.h"
#include "nsIDOMElement.h"
#include "nsString.h"
#include "nsUnicharUtils.h"
#include "nsIDOMDocument.h"
#include "nsIURI.h"
#include "nsIDocument.h"
#include "nsIDOMEventTarget.h"
#include "nsIDOMEvent.h"

EphyEventListener::EphyEventListener(void)
{
    NS_INIT_ISUPPORTS();
    mOwner = nsnull;
}

EphyEventListener::~EphyEventListener()
{
}

NS_IMPL_ISUPPORTS1(EphyEventListener, nsIDOMEventListener)

nsresult
EphyEventListener::Init(EphyEmbed *aOwner)
{
    mOwner = aOwner;
    return NS_OK;
}

nsresult
EphyEventListener::HandleFaviconLink (nsIDOMNode *node)
{
    nsresult result;

    nsCOMPtr<nsIDOMElement> linkElement;
    linkElement = do_QueryInterface (node);
    if (!linkElement) return NS_ERROR_FAILURE;

    NS_NAMED_LITERAL_STRING(attr_rel, "rel");
    nsAutoString value;
    result = linkElement->GetAttribute (attr_rel, value);
    if (NS_FAILED(result)) return NS_ERROR_FAILURE;

    if (value.Equals(NS_LITERAL_STRING("SHORTCUT ICON"),
             nsCaseInsensitiveStringComparator()) ||
        value.Equals(NS_LITERAL_STRING("ICON"),
                 nsCaseInsensitiveStringComparator()))
    {
        NS_NAMED_LITERAL_STRING(attr_href, "href");
        nsAutoString value;
        result = linkElement->GetAttribute (attr_href, value);
        if (NS_FAILED (result) || value.IsEmpty())
            return NS_ERROR_FAILURE;

        nsCOMPtr<nsIDOMDocument> domDoc;
        result = node->GetOwnerDocument(getter_AddRefs(domDoc));
        if (NS_FAILED(result) || !domDoc) return NS_ERROR_FAILURE;

        nsCOMPtr<nsIDocument> doc = do_QueryInterface (domDoc);
        if(!doc) return NS_ERROR_FAILURE;

        nsCOMPtr<nsIURI> uri;
        result = doc->GetDocumentURL(getter_AddRefs(uri));
        if (NS_FAILED (result)) return NS_ERROR_FAILURE;

        const nsACString &link = NS_ConvertUCS2toUTF8(value);
        nsCAutoString favicon_url;
        result = uri->Resolve (link, favicon_url);
        if (NS_FAILED (result)) return NS_ERROR_FAILURE;
        
        char *url = g_strdup (favicon_url.get());
        g_signal_emit_by_name (mOwner, "ge_favicon", url);
        g_free (url);
    }

    return NS_OK;
}   

NS_IMETHODIMP
EphyEventListener::HandleEvent(nsIDOMEvent* aDOMEvent)
{
    nsCOMPtr<nsIDOMEventTarget> eventTarget;
    
    aDOMEvent->GetTarget(getter_AddRefs(eventTarget));
    
    nsresult result;
    nsCOMPtr<nsIDOMNode> node = do_QueryInterface(eventTarget, &result);
    if (NS_FAILED(result) || !node) return NS_ERROR_FAILURE;

    HandleFaviconLink (node);
    
    return NS_OK;
}