From 0ccfe17b41250d1c5f21f46309b60bab31ab0d7e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Dan=20Hor=C3=A1k?= <dan@danny.cz>
Date: Tue, 5 Oct 2010 16:45:49 +0200
Subject: [PATCH] support arbitrary seperately packaged hyphenators

---
 Scribus/scribus/hyphenator.cpp |   62 +++++++++++++++++++++++----------------
 Scribus/scribus/hysettings.cpp |   12 +++++++-
 Scribus/scribus/langmgr.cpp    |   26 ++++++++++++++++-
 Scribus/scribus/scribus.cpp    |    2 +-
 4 files changed, 73 insertions(+), 29 deletions(-)

diff --git a/Scribus/scribus/hyphenator.cpp b/Scribus/scribus/hyphenator.cpp
index 3e0d3d8..38cf5d2 100644
--- a/Scribus/scribus/hyphenator.cpp
+++ b/Scribus/scribus/hyphenator.cpp
@@ -61,23 +61,26 @@ Hyphenator::Hyphenator(QWidget* parent, ScribusDoc *dok) : QObject( parent ),
 		Language = PrefsManager::instance()->appPrefs.Language;
 		doc->Language = Language;
 	}
+
 // 	pfad += ScCore->primaryMainWindow()->Sprachen[Language];
-	pfad += lmgr->getHyphFilename( doc->Language, false );
-	QFile f(pfad);
-	if (f.open(QIODevice::ReadOnly))
+	QString fileName = lmgr->getHyphFilename( doc->Language, false );
+ 	if (!fileName.isEmpty())
 	{
-		QTextStream st(&f);
-    	QString line;
-    	line = st.readLine();
-		codec = QTextCodec::codecForName(line.toUtf8());
-		f.close();
+		pfad += fileName;
+		QFile f(pfad);
+		if (f.open(QIODevice::ReadOnly))
+		{
+			QTextStream st(&f);
+			QString line;
+			line = st.readLine();
+			f.close();
+			useAble = true;
+		}
 	}
-	else
-	{
-		useAble = false;
-		hdict = NULL;
+
+	if (!useAble)
 		return;
-	}
+
 	QByteArray fn = pfad.toLocal8Bit();
 	const char * filename = fn.data();
 	hdict = hnj_hyphen_load(filename);
@@ -111,22 +114,29 @@ void Hyphenator::NewDict(const QString& name)
 		if (hdict != NULL)
 			hnj_hyphen_free(hdict);
 
-		pfad +=  LanguageManager::instance()->getHyphFilename(Language, false) ;
-		QFile f(pfad);
-		if (f.open(QIODevice::ReadOnly))
+		useAble = false;
+		hdict = NULL;
+
+		QString fileName = LanguageManager::instance()->getHyphFilename( doc->Language, false );
+		if (!fileName.isEmpty())
 		{
-			QTextStream st(&f);
-			QString line;
-			line = st.readLine();
-			codec = QTextCodec::codecForName(line.toUtf8());
-			f.close();
+			pfad += fileName;
+			QFile f(pfad);
+			if (f.open(QIODevice::ReadOnly))
+			{
+				QTextStream st(&f);
+				QString line;
+				line = st.readLine();
+				codec = QTextCodec::codecForName(line.toUtf8());
+				f.close();
+				useAble = true;
+			}
 		}
-		else
-		{
-			useAble = false;
-			hdict = NULL;
+
+
+		if (!useAble)
 			return;
-		}
+
 		QByteArray fn = pfad.toLocal8Bit();
 		filename = fn.data();
 		hdict = hnj_hyphen_load(filename);
diff --git a/Scribus/scribus/hysettings.cpp b/Scribus/scribus/hysettings.cpp
index 27dc7b0..ebd0407 100644
--- a/Scribus/scribus/hysettings.cpp
+++ b/Scribus/scribus/hysettings.cpp
@@ -27,7 +27,17 @@ HySettings::HySettings( QWidget* parent/*, QMap<QString,QString>* langs*/ ) : QW
 	language->setInsertPolicy(QComboBox::InsertAlphabetically);
 	foreach(QString hlang, lmg->hyphLangs())
 	{
-		language->addItem( lmg->getLangFromAbbrev(hlang), lmg->getLangFromAbbrev(hlang,false) );
+		QString uiname = lmg->getLangFromAbbrev(hlang);
+		QString lang = lmg->getLangFromAbbrev(hlang,false);
+
+		//Fallback to use lang code as name if otherwise unknown
+		if (uiname.isEmpty())
+			uiname = hlang;
+
+		if (lang.isEmpty())
+			lang = hlang;
+
+		language->addItem( uiname, lang );
 	}
 	
 	buttonExceptAdd->setIcon(QIcon(loadIcon("16/list-add.png")));
diff --git a/Scribus/scribus/langmgr.cpp b/Scribus/scribus/langmgr.cpp
index 6167c50..cd510b1 100644
--- a/Scribus/scribus/langmgr.cpp
+++ b/Scribus/scribus/langmgr.cpp
@@ -315,7 +315,31 @@ const QString LanguageManager::getHyphFilename(const QString & lang, bool langIs
 {
 	if(langIsAbbreviated)
 		return hyphLangList.value(lang);
-	return hyphLangList.value(getAbbrevFromLang(lang, false));
+
+	QString abbrev = getAbbrevFromLang(lang, false);
+
+	//If we don't have the language in our hard-coded table, 
+	//then try it directly
+	if (abbrev.isEmpty())
+		abbrev = lang;
+	QString ret = hyphLangList.value(abbrev);
+	if (ret.isEmpty())
+	{
+		//As a hack, if the language was munged into an abbreviated
+		//generic version through our table, e.g. German to "de"
+		//then hope for the best and pick the first of e.g.
+		//"de_AT", "de_DE" if they exist
+        	foreach(QString hlang, hyphLangs())
+		{
+			QStringList items = hlang.split("_");
+			if (!items.empty() && items[0] == abbrev)
+			{
+				ret = hyphLangList.value(hlang);
+				break;
+			}
+		}
+	}
+	return ret;
 }
 
 const QStringList LanguageManager::hyphLangs()
diff --git a/Scribus/scribus/scribus.cpp b/Scribus/scribus/scribus.cpp
index 6b8fb9c..5f3aef2 100644
--- a/Scribus/scribus/scribus.cpp
+++ b/Scribus/scribus/scribus.cpp
@@ -8899,7 +8899,7 @@ void ScribusMainWindow::initHyphenator()
 	//Grab the language abbreviation from it, get the full language text
 	//Insert the name as key and a new string list into the map
 	QString hyphDirName = QDir::convertSeparators(ScPaths::instance().dictDir());
-	QDir hyphDir(hyphDirName, "*.dic", QDir::Name, QDir::Files | QDir::NoSymLinks);
+	QDir hyphDir(hyphDirName, "*.dic", QDir::Name, QDir::Files);
 	if ((hyphDir.exists()) && (hyphDir.count() != 0))
 	{
 // 		LanguageManager langmgr;
-- 
1.7.2.3

