From 3b2a5153a79ec627113223222ed61190bcb3a7e3 Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Tue, 4 Dec 2012 10:11:35 +0000
Subject: Add validation for BOSH URLs in the connection dialog.

Change-Id: I967565abb867279238919f0e5eae6ebe0641d195
Resolves: #1186

diff --git a/Swift/QtUI/QtConnectionSettings.ui b/Swift/QtUI/QtConnectionSettings.ui
index 7283184..6d59fb8 100644
--- a/Swift/QtUI/QtConnectionSettings.ui
+++ b/Swift/QtUI/QtConnectionSettings.ui
@@ -73,7 +73,7 @@
    <item>
     <widget class="QStackedWidget" name="stackedWidget">
      <property name="currentIndex">
-      <number>0</number>
+      <number>2</number>
      </property>
      <widget class="QWidget" name="page_3"/>
      <widget class="QWidget" name="page_2">
@@ -511,22 +511,6 @@
  <connections>
   <connection>
    <sender>buttonBox</sender>
-   <signal>accepted()</signal>
-   <receiver>QtConnectionSettings</receiver>
-   <slot>accept()</slot>
-   <hints>
-    <hint type="sourcelabel">
-     <x>248</x>
-     <y>254</y>
-    </hint>
-    <hint type="destinationlabel">
-     <x>157</x>
-     <y>274</y>
-    </hint>
-   </hints>
-  </connection>
-  <connection>
-   <sender>buttonBox</sender>
    <signal>rejected()</signal>
    <receiver>QtConnectionSettings</receiver>
    <slot>reject()</slot>
diff --git a/Swift/QtUI/QtConnectionSettingsWindow.cpp b/Swift/QtUI/QtConnectionSettingsWindow.cpp
index ea693cf..2da527a 100644
--- a/Swift/QtUI/QtConnectionSettingsWindow.cpp
+++ b/Swift/QtUI/QtConnectionSettingsWindow.cpp
@@ -17,7 +17,10 @@
 #include <QTextEdit>
 #include <QFile>
 #include <QTextStream>
+#include <QMessageBox>
+
 #include <Swift/QtUI/QtSwiftUtil.h>
+#include <Swift/QtUI/QtURLValidator.h>
 
 namespace Swift {
 
@@ -43,6 +46,11 @@ QtConnectionSettingsWindow::QtConnectionSettingsWindow(const ClientOptions& opti
 
 	connect(ui.manual_proxyType, SIGNAL(currentIndexChanged(int)), SLOT(handleProxyTypeChanged(int)));
 
+	connect(ui.buttonBox, SIGNAL(accepted()), this, SLOT(handleAcceptRequested()));
+
+	QtURLValidator* urlValidator = new QtURLValidator(this);
+	ui.bosh_uri->setValidator(urlValidator);
+
 	ui.manual_useTLS->setCurrentIndex(2);
 
 	ui.manual_proxyType->setCurrentIndex(0);
@@ -101,6 +109,15 @@ void QtConnectionSettingsWindow::handleProxyTypeChanged(int index) {
 	ui.manual_manualProxyPort->setVisible(proxySettingsVisible);
 }
 
+void QtConnectionSettingsWindow::handleAcceptRequested() {
+	if (ui.connectionMethod->currentIndex() != 2 || ui.bosh_uri->hasAcceptableInput()) {
+		accept();
+	}
+	else {
+		QMessageBox::critical(this, tr("Configuration invalid"), tr("The provided BOSH URL is not valid."));
+	}
+}
+
 ClientOptions QtConnectionSettingsWindow::getOptions() {
 	ClientOptions options;
 	if (ui.connectionMethod->currentIndex() > 0) {
diff --git a/Swift/QtUI/QtConnectionSettingsWindow.h b/Swift/QtUI/QtConnectionSettingsWindow.h
index 2aed5d1..3b017ab 100644
--- a/Swift/QtUI/QtConnectionSettingsWindow.h
+++ b/Swift/QtUI/QtConnectionSettingsWindow.h
@@ -23,6 +23,7 @@ namespace Swift {
 
 		private slots:
 			void handleProxyTypeChanged(int);
+			void handleAcceptRequested();
 
 		private:
 			enum {
diff --git a/Swift/QtUI/QtURLValidator.cpp b/Swift/QtUI/QtURLValidator.cpp
new file mode 100644
index 0000000..2df59c4
--- /dev/null
+++ b/Swift/QtUI/QtURLValidator.cpp
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2012 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <Swift/QtUI/QtURLValidator.h>
+
+#include <Swiften/Base/URL.h>
+#include <Swift/QtUI/QtSwiftUtil.h>
+
+namespace Swift {
+QtURLValidator::QtURLValidator(QObject* parent) {
+
+}
+
+QValidator::State QtURLValidator::validate(QString& input, int& pos) const {
+	URL url = URL::fromString(Q2PSTRING(input));
+	bool valid = !url.isEmpty();
+	valid &= (url.getScheme() == "http" || url.getScheme() == "https");
+	return valid ? Acceptable : Intermediate;
+}
+
+}
+
diff --git a/Swift/QtUI/QtURLValidator.h b/Swift/QtUI/QtURLValidator.h
new file mode 100644
index 0000000..fa17253
--- /dev/null
+++ b/Swift/QtUI/QtURLValidator.h
@@ -0,0 +1,17 @@
+/*
+ * Copyright (c) 2012 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <QValidator>
+
+namespace Swift {
+	class QtURLValidator : public QValidator {
+		Q_OBJECT
+		public:
+			QtURLValidator(QObject* parent);
+			virtual QValidator::State validate(QString& input, int& pos) const;
+	};
+}
+
diff --git a/Swift/QtUI/SConscript b/Swift/QtUI/SConscript
index c40ba4b..64c3b15 100644
--- a/Swift/QtUI/SConscript
+++ b/Swift/QtUI/SConscript
@@ -161,7 +161,8 @@ sources = [
     "QtChatWindowJSBridge.cpp",
     "QtMUCConfigurationWindow.cpp",
     "QtAffiliationEditor.cpp",
-    "QtUISettingConstants.cpp"
+    "QtUISettingConstants.cpp",
+    "QtURLValidator.cpp"
   ]
 
 # Determine the version
-- 
cgit v0.10.2-6-g49f6