From 5ca749621b991e6a1e10d7a809d0fe1f0680ca74 Mon Sep 17 00:00:00 2001
From: Vlad Voicu <vladv@rosedu.org>
Date: Mon, 5 Mar 2012 20:28:47 +0200
Subject: Improved settings for Spell Checker


diff --git a/SwifTools/SpellCheckerFactory.cpp b/SwifTools/SpellCheckerFactory.cpp
index 9ec1151..9ac33cd 100644
--- a/SwifTools/SpellCheckerFactory.cpp
+++ b/SwifTools/SpellCheckerFactory.cpp
@@ -10,8 +10,6 @@
 
 #ifdef HAVE_HUNSPELL
 #include <hunspell/hunspell.hxx>
-#else
-#define HAVE_NO_SPELL_CHECK
 #endif
 
 namespace Swift {
@@ -19,14 +17,12 @@ namespace Swift {
 SpellCheckerFactory::SpellCheckerFactory() {
 }
 
-SpellChecker* SpellCheckerFactory::createSpellChecker() {
+SpellChecker* SpellCheckerFactory::createSpellChecker(const char* affixPath, const char* dictPath) {
 #ifdef HAVE_HUNSPELL
-	//TODO(vladv): Do something smart about finding available dictionaries,
-	// maybe a UI button to select language from available dictionaries?
-	const char* dictPath = "/usr/share/hunspell/en_US.dic";
-	const char* affixPath = "/usr/share/hunspell/en_US.aff";
+	std::cout << affixPath << std::endl << dictPath << std::endl;
 	return new HunspellChecker(affixPath, dictPath);
 #endif
+	return NULL;
 }
 
 }
diff --git a/SwifTools/SpellCheckerFactory.h b/SwifTools/SpellCheckerFactory.h
index 23f89b8..85aa8b6 100644
--- a/SwifTools/SpellCheckerFactory.h
+++ b/SwifTools/SpellCheckerFactory.h
@@ -13,6 +13,6 @@ namespace Swift {
 	class SpellCheckerFactory {
 		public:
 			SpellCheckerFactory();
-			SpellChecker* createSpellChecker();
+			SpellChecker* createSpellChecker(const char* affixPath, const char* dictPath);
 	};
 }
diff --git a/Swift/Controllers/SettingConstants.cpp b/Swift/Controllers/SettingConstants.cpp
index 6f147a7..678babc 100644
--- a/Swift/Controllers/SettingConstants.cpp
+++ b/Swift/Controllers/SettingConstants.cpp
@@ -21,6 +21,6 @@ const SettingsProvider::Setting<std::string> SettingConstants::EXPANDED_ROSTER_G
 const SettingsProvider::Setting<bool> SettingConstants::PLAY_SOUNDS("playSounds", true);
 const SettingsProvider::Setting<bool> SettingConstants::SPELL_CHECKER("spellChecker", false);
 const SettingsProvider::Setting<std::string> SettingConstants::DICT_PATH("dictPath", "/usr/share/myspell/dicts/");
-const SettingsProvider::Setting<std::string> SettingConstants::PERSONAL_DICT_PATH("personaldictPath", "/home");
+const SettingsProvider::Setting<std::string> SettingConstants::PERSONAL_DICT_PATH("personaldictPath", "/home/");
 const SettingsProvider::Setting<std::string> SettingConstants::DICT_FILE("dictFile", "en_US.dic");
 }
diff --git a/Swift/QtUI/QtChatWindow.cpp b/Swift/QtUI/QtChatWindow.cpp
index 314e36c..3a20713 100644
--- a/Swift/QtUI/QtChatWindow.cpp
+++ b/Swift/QtUI/QtChatWindow.cpp
@@ -150,7 +150,7 @@ QtChatWindow::QtChatWindow(const QString &contact, QtChatTheme* theme, UIEventSt
 	QHBoxLayout* inputBarLayout = new QHBoxLayout();
 	inputBarLayout->setContentsMargins(0,0,0,0);
 	inputBarLayout->setSpacing(2);
-	input_ = new QtTextEdit(this);
+	input_ = new QtTextEdit(settings_, this);
 	input_->setAcceptRichText(false);
 	inputBarLayout->addWidget(midBar_);
 	inputBarLayout->addWidget(input_);
diff --git a/Swift/QtUI/QtSpellCheckerWindow.cpp b/Swift/QtUI/QtSpellCheckerWindow.cpp
index c6fcc2c..ef1985e 100644
--- a/Swift/QtUI/QtSpellCheckerWindow.cpp
+++ b/Swift/QtUI/QtSpellCheckerWindow.cpp
@@ -27,13 +27,19 @@ QtSpellCheckerWindow::QtSpellCheckerWindow(SettingsProvider* settings, QWidget*
 	connect(ui_.pathButton, SIGNAL(clicked()), this, SLOT(handlePathButton()));
 	connect(ui_.personalPathButton, SIGNAL(clicked()), this, SLOT(handlePersonalPathButton()));
 	setFromSettings();
-	setEnabled(settings_->getSetting(SettingConstants::SPELL_CHECKER));
 }
 
 void QtSpellCheckerWindow::setFromSettings() {
 	ui_.spellChecker->setChecked(settings_->getSetting(SettingConstants::SPELL_CHECKER));
 	ui_.personalPathContent->insert(P2QSTRING(settings_->getSetting(SettingConstants::PERSONAL_DICT_PATH)));
 	ui_.pathContent->insert(P2QSTRING(settings_->getSetting(SettingConstants::DICT_PATH)));
+	ui_.currentLanguageValue->setText(P2QSTRING(settings_->getSetting(SettingConstants::DICT_FILE)));
+	std::string currentPath = settings_->getSetting(SettingConstants::DICT_PATH);
+	QString filename = "*.dic";
+	QDir dictDirectory = QDir(P2QSTRING(currentPath));
+	QStringList files = dictDirectory.entryList(QStringList(filename), QDir::Files);
+	showFiles(files);
+	setEnabled(settings_->getSetting(SettingConstants::SPELL_CHECKER));
 }
 
 void QtSpellCheckerWindow::handleChecker(bool state) {
@@ -48,6 +54,8 @@ void QtSpellCheckerWindow::setEnabled(bool state) {
 	ui_.personalPathButton->setEnabled(state);
 	ui_.pathLabel->setEnabled(state);
 	ui_.personalDictionaryLabel->setEnabled(state);
+	ui_.currentLanguage->setEnabled(state);
+	ui_.currentLanguageValue->setEnabled(state);
 	ui_.language->setEnabled(state);
 }
 
@@ -56,6 +64,8 @@ void QtSpellCheckerWindow::handleApply() {
 	QList<QListWidgetItem* > selectedLanguage = ui_.languageView->selectedItems();
 	if (!selectedLanguage.empty()) {
 		settings_->storeSetting(SettingConstants::DICT_FILE, Q2PSTRING(selectedLanguage[0]->text()));
+	} else {
+		settings_->storeSetting(SettingConstants::SPELL_CHECKER, false);
 	}
 	this->done(0);
 }
@@ -67,10 +77,13 @@ void QtSpellCheckerWindow::handleCancel() {
 void QtSpellCheckerWindow::handlePathButton() {
 	std::string currentPath = settings_->getSetting(SettingConstants::DICT_PATH);
 	QString dirpath = QFileDialog::getExistingDirectory(this, tr("Dictionary Path"), P2QSTRING(currentPath));
-	if (dirpath != NULL) {
-		QDir dictDirectory = QDir(dirpath);
+	if (!dirpath.isEmpty()) {
+		if (!dirpath.endsWith("/")) {
+			dirpath.append("/");
+		}
 		settings_->storeSetting(SettingConstants::DICT_PATH, Q2PSTRING(dirpath));
-		ui_.personalPathContent->insert(dirpath);
+		QDir dictDirectory = QDir(dirpath);
+		ui_.pathContent->insert(dirpath);
 		QString filename = "*.dic";
 		QStringList files = dictDirectory.entryList(QStringList(filename), QDir::Files);
 		showFiles(files);
diff --git a/Swift/QtUI/QtSpellCheckerWindow.ui b/Swift/QtUI/QtSpellCheckerWindow.ui
index 619ed88..63f3ed4 100644
--- a/Swift/QtUI/QtSpellCheckerWindow.ui
+++ b/Swift/QtUI/QtSpellCheckerWindow.ui
@@ -6,8 +6,8 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>431</width>
-    <height>214</height>
+    <width>502</width>
+    <height>303</height>
    </rect>
   </property>
   <property name="windowTitle">
@@ -31,7 +31,7 @@
    <item row="1" column="1">
     <widget class="QLineEdit" name="pathContent"/>
    </item>
-   <item row="1" column="2">
+   <item row="1" column="3">
     <widget class="QPushButton" name="pathButton">
      <property name="text">
       <string>Change</string>
@@ -41,14 +41,14 @@
    <item row="2" column="0">
     <widget class="QLabel" name="personalDictionaryLabel">
      <property name="text">
-      <string>Personal Dictionary</string>
+      <string>Personal Dictionary:</string>
      </property>
     </widget>
    </item>
    <item row="2" column="1">
     <widget class="QLineEdit" name="personalPathContent"/>
    </item>
-   <item row="2" column="2">
+   <item row="2" column="3">
     <widget class="QPushButton" name="personalPathButton">
      <property name="text">
       <string>Change</string>
@@ -56,13 +56,30 @@
     </widget>
    </item>
    <item row="3" column="0">
+    <widget class="QLabel" name="currentLanguage">
+     <property name="text">
+      <string>Current Language:</string>
+     </property>
+    </widget>
+   </item>
+   <item row="3" column="1">
+    <widget class="QLabel" name="currentLanguageValue">
+     <property name="text">
+      <string/>
+     </property>
+    </widget>
+   </item>
+   <item row="4" column="0">
     <widget class="QLabel" name="language">
      <property name="text">
-      <string>Language</string>
+      <string>Language:</string>
      </property>
     </widget>
    </item>
-   <item row="4" column="0" colspan="3">
+   <item row="4" column="1" colspan="2">
+    <widget class="QListWidget" name="languageView"/>
+   </item>
+   <item row="5" column="2" colspan="2">
     <layout class="QHBoxLayout" name="horizontalLayout">
      <item>
       <spacer name="horizontalSpacer">
@@ -93,9 +110,6 @@
      </item>
     </layout>
    </item>
-   <item row="3" column="1">
-    <widget class="QListWidget" name="languageView"/>
-   </item>
   </layout>
  </widget>
  <resources/>
diff --git a/Swift/QtUI/QtTextEdit.cpp b/Swift/QtUI/QtTextEdit.cpp
index 60f0939..e523dca 100644
--- a/Swift/QtUI/QtTextEdit.cpp
+++ b/Swift/QtUI/QtTextEdit.cpp
@@ -5,12 +5,14 @@
  */
 
 #include <boost/tuple/tuple.hpp>
+#include <boost/algorithm/string.hpp>
 
 #include <SwifTools/SpellCheckerFactory.h>
 #include <SwifTools/SpellChecker.h>
 
 #include <Swift/QtUI/QtTextEdit.h>
 #include <Swift/QtUI/QtSwiftUtil.h>
+#include <Swift/QtUI/QtSpellCheckerWindow.h>
 
 #include <QApplication>
 #include <QFontMetrics>
@@ -20,12 +22,11 @@
 
 namespace Swift {
 
-QtTextEdit::QtTextEdit(QWidget* parent) : QTextEdit(parent) {
+QtTextEdit::QtTextEdit(SettingsProvider* settings, QWidget* parent) : QTextEdit(parent) {
 	connect(this, SIGNAL(textChanged()), this, SLOT(handleTextChanged()));
+	settings_ = settings;
+	setUpSpellChecker();
 	handleTextChanged();
-	SpellCheckerFactory *checkerFactory = new SpellCheckerFactory();
-	checker_ = checkerFactory->createSpellChecker();
-	delete checkerFactory;
 };
 
 QtTextEdit::~QtTextEdit() {
@@ -57,11 +58,21 @@ void QtTextEdit::keyPressEvent(QKeyEvent* event) {
 	}
 	else {
 		QTextEdit::keyPressEvent(event);
-		underlineMisspells();
+		if(settings_->getSetting(SettingConstants::SPELL_CHECKER)) {
+			underlineMisspells();
+		}
 	}
 }
 
 void QtTextEdit::underlineMisspells() {
+	if (checker_ == NULL) {
+		// Try to setUp the checker again in case that
+		// settings changed.
+		setUpSpellChecker();
+		if (checker_ == NULL) {
+			return;
+		}
+	}
 	misspelledPositions_.clear();
 	QTextCharFormat spellingErrorFormat;
 	QTextCharFormat normalFormat;
@@ -160,4 +171,26 @@ void QtTextEdit::contextMenuEvent(QContextMenuEvent* event) {
 	delete menu;
 }
 
+void QtTextEdit::setUpSpellChecker()
+{
+	SpellCheckerFactory *checkerFactory = new SpellCheckerFactory();
+	std::string dictFile = settings_->getSetting(SettingConstants::DICT_FILE);
+	if (dictFile.empty()) {
+		// Disable the dictionary to force the user to select a dictionary
+		settings_->storeSetting(SettingConstants::SPELL_CHECKER, false);
+	}
+	if (settings_->getSetting(SettingConstants::SPELL_CHECKER)) {
+		std::string dictPath = settings_->getSetting(SettingConstants::DICT_PATH);
+		std::string affixFile(dictFile);
+		boost::replace_all(affixFile, ".dic", ".aff");
+		if (checker_ != NULL) {
+			delete checker_;
+		}
+		checker_ = checkerFactory->createSpellChecker((dictPath + affixFile).c_str(), (dictPath + dictFile).c_str());
+		delete checkerFactory;
+	} else {
+		checker_ = NULL;
+	}
+}
+
 }
diff --git a/Swift/QtUI/QtTextEdit.h b/Swift/QtUI/QtTextEdit.h
index 297312d..38d5d0a 100644
--- a/Swift/QtUI/QtTextEdit.h
+++ b/Swift/QtUI/QtTextEdit.h
@@ -6,15 +6,19 @@
 
 #pragma once
 
-#include <QTextEdit>
 #include <SwifTools/SpellParser.h>
 
+#include <Swift/Controllers/Settings/SettingsProvider.h>
+#include <Swift/Controllers/SettingConstants.h>
+
+#include <QTextEdit>
+
 namespace Swift {
 	class SpellChecker;
 	class QtTextEdit : public QTextEdit {
 		Q_OBJECT
 	public:
-		QtTextEdit(QWidget* parent = 0);
+		QtTextEdit(SettingsProvider* settings, QWidget* parent = 0);
 		virtual ~QtTextEdit();
 		virtual QSize sizeHint() const;
 	signals:
@@ -31,6 +35,8 @@ namespace Swift {
 	private:
 		SpellChecker *checker_;
 		PositionPairList misspelledPositions_;
+		SettingsProvider *settings_;
+		void setUpSpellChecker();
 		void underlineMisspells();
 		boost::tuple<int,int> getWordFromCursor(int cursorPosition);
 	};
-- 
cgit v0.10.2-6-g49f6