File: mozilla-helper.cpp

package info (click to toggle)
seahorse 2.22.3-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 19,092 kB
  • ctags: 4,220
  • sloc: ansic: 39,234; xml: 11,748; sh: 9,191; makefile: 795; cpp: 131
file content (181 lines) | stat: -rw-r--r-- 4,925 bytes parent folder | download
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
/*
 *  Copyright (C) 2000 Marco Pesenti Gritti
 *  Copyright (C) 2004 Jean-François Rameau
 *  Copyright (C) 2006 Adam Schreiber
 *
 *  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.
 *
 *  $Id: mozilla-helper.cpp 1401 2006-10-05 21:03:08Z sadam $
 */

#include "mozilla-config.h"
#include "config.h"

#include <glib.h>

#include <nsStringAPI.h>

#include <gtkmozembed.h>
#include <gtkmozembed_internal.h>
#include <nsCOMPtr.h>
#include <nsIDOMElement.h>
#include <nsIDOMHTMLInputElement.h>
#include <nsIDOMHTMLTextAreaElement.h>
#include <nsIDOMNSHTMLInputElement.h>
#include <nsIDOMNSHTMLTextAreaElement.h>
#include <nsIDOMWindow.h>
#include <nsISelection.h>
#include <nsIWebBrowserFocus.h>
#include <nsIWebBrowser.h>
#include <nsMemory.h>

#include "mozilla-helper.h"

template <class T>
void set_value (nsIDOMElement *aElement, const char *value)
{
	// Get full text in element
	nsString text; 
	nsCOMPtr<T> element (do_QueryInterface(aElement));

	nsString nsValue;
	NS_CStringToUTF16 (nsCString(value),
			   NS_CSTRING_ENCODING_UTF8, nsValue);

	element->SetValue (nsValue);
}

template <class T>
char * get_value (nsIDOMElement *aElement)
{
	// Get full text in element
	nsString text; 
	nsCOMPtr<T> element (do_QueryInterface(aElement));
	NS_ENSURE_TRUE (element, NULL);

	element->GetValue(text);
	if (text.IsEmpty ()) return NULL;

	nsCString cText; 
	NS_UTF16ToCString(text, NS_CSTRING_ENCODING_UTF8, cText);

	return g_strdup(cText.get());
}

extern "C" gboolean
mozilla_is_input (EphyEmbed *embed)
{
	nsCOMPtr<nsIWebBrowser> browser;
	gtk_moz_embed_get_nsIWebBrowser (GTK_MOZ_EMBED (embed),
			getter_AddRefs (browser));
	nsCOMPtr<nsIWebBrowserFocus> focus (do_QueryInterface(browser));
	if (!focus) return FALSE;

	nsCOMPtr<nsIDOMElement> domElement;
	focus->GetFocusedElement (getter_AddRefs(domElement));
	if (!domElement) return FALSE;

	nsCOMPtr<nsIDOMHTMLTextAreaElement> nodeAsArea (do_QueryInterface(domElement));
	if (nodeAsArea) return TRUE;

	nsCOMPtr<nsIDOMHTMLInputElement> nodeAsInput (do_QueryInterface(domElement));
	if (nodeAsInput) return TRUE;

	return FALSE;
}

extern "C" const char*
mozilla_get_text (EphyEmbed *embed)
{
    nsCOMPtr<nsIWebBrowser> browser;
	gtk_moz_embed_get_nsIWebBrowser (GTK_MOZ_EMBED (embed),
			getter_AddRefs (browser));
	nsCOMPtr<nsIWebBrowserFocus> focus (do_QueryInterface(browser));
	if (!focus) 
	    return NULL;

	nsCOMPtr<nsIDOMElement> domElement;
	focus->GetFocusedElement (getter_AddRefs(domElement));
	if (!domElement) 
	    return NULL;

	char *value;
	// Try with a textarea
	value = get_value <nsIDOMHTMLTextAreaElement> (domElement);
	if (value)   
		return value;

	// Then with any input
	// Take care of password fields
	nsString text;
	nsCOMPtr<nsIDOMHTMLInputElement> input (do_QueryInterface(domElement));
	if (text.Length () > 0) {
    	input->GetType (text);
    	const PRUnichar *str = text.get ();
    	if (!(str[0] == 't' && str[1] == 'e' && str[2] == 'x' && str[3] == 't' && str[4] == '\0')) 
    	    return NULL;

    	value = get_value <nsIDOMHTMLInputElement> (domElement);
    	if (value)
            return value;
    }
    
    return NULL;
}

extern "C" void
mozilla_set_text (EphyEmbed *embed, char *new_text)
{
    nsCOMPtr<nsIWebBrowser> browser;
	gtk_moz_embed_get_nsIWebBrowser (GTK_MOZ_EMBED (embed),
			getter_AddRefs (browser));
	nsCOMPtr<nsIWebBrowserFocus> focus (do_QueryInterface(browser));
	if (!focus) return;

	nsCOMPtr<nsIDOMElement> domElement;
	focus->GetFocusedElement (getter_AddRefs(domElement));
	if (!domElement) return;

	char *value;
	// Try with a textarea
	value = get_value <nsIDOMHTMLTextAreaElement> (domElement);
	if (value)
	{
		set_value <nsIDOMHTMLTextAreaElement> (domElement, new_text);

        g_free (new_text);
        
		return;
	}

	// Then with any input
	// Take care of password fields
	nsString text;
	nsCOMPtr<nsIDOMHTMLInputElement> input (do_QueryInterface(domElement));
	input->GetType (text);
	const PRUnichar *str = text.get ();
	if (!(str[0] == 't' && str[1] == 'e' && str[2] == 'x' && str[3] == 't' && str[4] == '\0'))  
	    return;

	value = get_value <nsIDOMHTMLInputElement> (domElement);
	if (value)
	{
		set_value <nsIDOMHTMLInputElement> (domElement, new_text);
		
        g_free (new_text);

		return;
	}
}