aboutsummaryrefslogtreecommitdiffstats
path: root/embed/mozilla/EphyBrowser.cpp
diff options
context:
space:
mode:
authorChristian Persch <chpe@cvs.gnome.org>2004-01-04 20:52:05 +0800
committerChristian Persch <chpe@src.gnome.org>2004-01-04 20:52:05 +0800
commit753b29514d19fd54d51c2346bc6bb08ff1093cac (patch)
treee15206e10a0f54f52bcd5c2fd245d0d21f8750ee /embed/mozilla/EphyBrowser.cpp
parent1fe471d0ab202613f6802751f5604420e23727d1 (diff)
downloadgsoc2013-epiphany-753b29514d19fd54d51c2346bc6bb08ff1093cac.tar
gsoc2013-epiphany-753b29514d19fd54d51c2346bc6bb08ff1093cac.tar.gz
gsoc2013-epiphany-753b29514d19fd54d51c2346bc6bb08ff1093cac.tar.bz2
gsoc2013-epiphany-753b29514d19fd54d51c2346bc6bb08ff1093cac.tar.lz
gsoc2013-epiphany-753b29514d19fd54d51c2346bc6bb08ff1093cac.tar.xz
gsoc2013-epiphany-753b29514d19fd54d51c2346bc6bb08ff1093cac.tar.zst
gsoc2013-epiphany-753b29514d19fd54d51c2346bc6bb08ff1093cac.zip
Add API to check if an EphyEmbed has forms with user input in them.
2004-01-04 Christian Persch <chpe@cvs.gnome.org> * embed/ephy-embed.c: (ephy_embed_has_modified_forms): * embed/ephy-embed.h: * embed/mozilla/EphyBrowser.cpp: * embed/mozilla/EphyBrowser.h: * embed/mozilla/mozilla-embed.cpp: Add API to check if an EphyEmbed has forms with user input in them. Currently it required one modified textarea, or two modified text fields. * src/ephy-notebook.c: (ephy_notebook_class_init), (close_button_clicked_cb): * src/ephy-notebook.h: * src/ephy-window.c: (confirm_close_with_modified_forms), (ephy_window_delete_event_cb), (tab_delete_cb), (setup_notebook), (ephy_window_init), (ephy_window_remove_tab): When closing a window or tab, check if there is unsubmitted user input in form fields, and if so, warn the user before closing. Fixes bug #119857.
Diffstat (limited to 'embed/mozilla/EphyBrowser.cpp')
-rw-r--r--embed/mozilla/EphyBrowser.cpp128
1 files changed, 127 insertions, 1 deletions
diff --git a/embed/mozilla/EphyBrowser.cpp b/embed/mozilla/EphyBrowser.cpp
index c8c82c62c..afed11d8a 100644
--- a/embed/mozilla/EphyBrowser.cpp
+++ b/embed/mozilla/EphyBrowser.cpp
@@ -74,6 +74,9 @@
#include "nsIDOMHTMLDocument.h"
#include "nsIDOMHTMLCollection.h"
#include "nsIDOMHTMLElement.h"
+#include "nsIDOMHTMLFormElement.h"
+#include "nsIDOMHTMLInputElement.h"
+#include "nsIDOMHTMLTextAreaElement.h"
#include "nsIDeviceContext.h"
#include "nsIPresContext.h"
#include "nsIAtom.h"
@@ -498,7 +501,6 @@ nsresult EphyBrowser::GetZoom (float *aZoom)
nsresult EphyBrowser::GetDocument (nsIDOMDocument **aDOMDocument)
{
- nsCOMPtr<nsIDOMDocument> domDocument;
return mDOMWindow->GetDocument (aDOMDocument);
}
@@ -867,3 +869,127 @@ nsresult EphyBrowser::GetCommandState (const char *command, PRBool *enabled)
return cmdManager->IsCommandEnabled (command, nsnull, enabled);
}
+
+#define NUM_MODIFIED_TEXTFIELDS_REQUIRED 2
+
+nsresult EphyBrowser::GetDocumentHasModifiedForms (nsIDOMDocument *aDomDoc, PRUint32 *aNumTextFields, PRBool *aHasTextArea)
+{
+ nsCOMPtr<nsIDOMHTMLDocument> htmlDoc = do_QueryInterface(aDomDoc);
+ NS_ENSURE_TRUE (htmlDoc, NS_ERROR_FAILURE);
+
+ nsCOMPtr<nsIDOMHTMLCollection> forms;
+ htmlDoc->GetForms (getter_AddRefs (forms));
+ if (!forms) return NS_OK; /* it's ok not to have any forms */
+
+ PRUint32 formNum;
+ forms->GetLength (&formNum);
+
+ /* check all forms */
+ for (PRUint32 formIndex = 0; formIndex < formNum; formIndex++)
+ {
+ nsCOMPtr<nsIDOMNode> formNode;
+ forms->Item (formIndex, getter_AddRefs (formNode));
+ if (!formNode) continue;
+
+ nsCOMPtr<nsIDOMHTMLFormElement> formElement = do_QueryInterface (formNode);
+ if (!formElement) continue;
+
+ nsCOMPtr<nsIDOMHTMLCollection> formElements;
+ formElement->GetElements (getter_AddRefs (formElements));
+ if (!formElements) continue;
+
+ PRUint32 elementNum;
+ formElements->GetLength (&elementNum);
+
+ /* check all input elements in the form for user input */
+ for (PRUint32 elementIndex = 0; elementIndex < elementNum; elementIndex++)
+ {
+ nsCOMPtr<nsIDOMNode> domNode;
+ formElements->Item (elementIndex, getter_AddRefs (domNode));
+ if (!domNode) continue;
+
+ nsCOMPtr<nsIDOMHTMLTextAreaElement> areaElement = do_QueryInterface (domNode);
+ if (areaElement)
+ {
+ nsAutoString default_text, user_text;
+ areaElement->GetDefaultValue (default_text);
+ areaElement->GetValue (user_text);
+ if (Compare (user_text, default_text) != 0)
+ {
+ *aHasTextArea = PR_TRUE;
+ return NS_OK;
+ }
+
+ continue;
+ }
+
+ nsCOMPtr<nsIDOMHTMLInputElement> inputElement = do_QueryInterface(domNode);
+ if (!inputElement) continue;
+
+ nsAutoString type;
+ inputElement->GetType(type);
+
+ if (type.EqualsIgnoreCase("text"))
+ {
+ nsAutoString default_text, user_text;
+ inputElement->GetDefaultValue (default_text);
+ inputElement->GetValue (user_text);
+ if (Compare (user_text, default_text) != 0)
+ {
+ (*aNumTextFields)++;
+ if (*aNumTextFields >= NUM_MODIFIED_TEXTFIELDS_REQUIRED)
+ {
+ return NS_OK;
+ }
+ }
+ }
+ }
+ }
+
+ return NS_OK;
+}
+
+nsresult EphyBrowser::GetHasModifiedForms (PRBool *modified)
+{
+ *modified = PR_FALSE;
+
+ nsCOMPtr<nsIDocShell> rootDocShell = do_GetInterface (mWebBrowser);
+ NS_ENSURE_TRUE (rootDocShell, NS_ERROR_FAILURE);
+
+ nsCOMPtr<nsISimpleEnumerator> enumerator;
+ rootDocShell->GetDocShellEnumerator(nsIDocShellTreeItem::typeContent,
+ nsIDocShell::ENUMERATE_FORWARDS,
+ getter_AddRefs(enumerator));
+ NS_ENSURE_TRUE (enumerator, NS_ERROR_FAILURE);
+
+ PRBool hasMore;
+ PRBool hasTextArea = PR_FALSE;
+ PRUint32 numTextFields = 0;
+ while (NS_SUCCEEDED(enumerator->HasMoreElements(&hasMore)) && hasMore)
+ {
+ nsCOMPtr<nsISupports> element;
+ enumerator->GetNext (getter_AddRefs(element));
+ if (!element) continue;
+
+ nsCOMPtr<nsIDocShell> docShell = do_QueryInterface (element);
+ if (!docShell) continue;
+
+ nsCOMPtr<nsIContentViewer> contentViewer;
+ docShell->GetContentViewer (getter_AddRefs(contentViewer));
+ if (!contentViewer) continue;
+
+ nsCOMPtr<nsIDOMDocument> domDoc;
+ contentViewer->GetDOMDocument (getter_AddRefs (domDoc));
+
+ nsresult result;
+ result = GetDocumentHasModifiedForms (domDoc, &numTextFields, &hasTextArea);
+ if (NS_SUCCEEDED (result) &&
+ (numTextFields >= NUM_MODIFIED_TEXTFIELDS_REQUIRED || hasTextArea))
+ {
+ *modified = PR_TRUE;
+ break;
+ }
+ }
+
+ return NS_OK;
+}