From 2ec46186d21b0e9620e56da2430c6eadf170bf3b Mon Sep 17 00:00:00 2001
From: Tobias Markmann <tm@ayena.de>
Date: Wed, 2 Mar 2016 09:09:18 +0100
Subject: Fix crashes in spellchecking code in case of broken backend

If Swift uses Hunspell as spellchecking backend its
configuration can fail, because a dictionary is missing
or a wrong path was specified.
Instead of crashing, spellchecking will be disabled and a
warning is printed to the terminal.

Test-Information:

Tested on Debian 8 with Qt 5.3.2.
Tested enabling/disabling of spellchecking in the UI,
selecting different dictionaries and broken dictionaries.

Change-Id: Ib6d73ed3d7a6a3701410b0f6dc983d41e807df82

diff --git a/Swift/QtUI/QtTextEdit.cpp b/Swift/QtUI/QtTextEdit.cpp
index a0188d9..adf3878 100644
--- a/Swift/QtUI/QtTextEdit.cpp
+++ b/Swift/QtUI/QtTextEdit.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010-2015 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
  * All rights reserved.
  * See the COPYING file for more information.
  */
@@ -13,9 +13,9 @@
 #include <QApplication>
 #include <QKeyEvent>
 #include <QMenu>
-#include <QTime>
 #include <QTextDocument>
 
+#include <Swiften/Base/Log.h>
 #include <Swiften/Base/foreach.h>
 
 #include <Swift/Controllers/SettingConstants.h>
@@ -181,15 +181,23 @@ void QtTextEdit::addSuggestions(QMenu* menu, QContextMenuEvent* event)
 #ifdef HAVE_SPELLCHECKER
 void QtTextEdit::setUpSpellChecker()
 {
+	delete highlighter_;
+	highlighter_ = NULL;
 	delete checker_;
 	checker_ = NULL;
 	if (settings_->getSetting(SettingConstants::SPELL_CHECKER)) {
 		std::string dictPath = settings_->getSetting(SettingConstants::DICT_PATH);
 		std::string dictFile = settings_->getSetting(SettingConstants::DICT_FILE);
 		checker_ = SpellCheckerFactory().createSpellChecker(dictPath + dictFile);
-		delete highlighter_;
-		highlighter_ = NULL;
-		highlighter_ = new QtSpellCheckHighlighter(document(), checker_);
+		if (checker_) {
+			highlighter_ = new QtSpellCheckHighlighter(document(), checker_);
+		}
+		else {
+			// Spellchecking is not working, as we did not get a valid checker from the factory. Disable spellchecking.
+			SWIFT_LOG(warning) << "Spellchecking is currently misconfigured in Swift (e.g. missing dictionary or broken dictionary file). Disable spellchecking." << std::endl;
+			settings_->storeSetting(SettingConstants::SPELL_CHECKER, false);
+		}
+
 	}
 }
 #endif
@@ -213,7 +221,9 @@ void QtTextEdit::handleSettingChanged(const std::string& settings) {
 		|| settings == SettingConstants::DICT_FILE.getKey()) {
 #ifdef HAVE_SPELLCHECKER
 		setUpSpellChecker();
-		highlighter_->rehighlight();
+		if (highlighter_) {
+			highlighter_->rehighlight();
+		}
 #endif
 	}
 }
-- 
cgit v0.10.2-6-g49f6