| 1 |
/* AbiSource Application Framework |
| 2 |
* Copyright (C) 1998 AbiSource, Inc. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU General Public License |
| 6 |
* as published by the Free Software Foundation; either version 2 |
| 7 |
* of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 17 |
* 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#ifdef HAVE_CONFIG_H |
| 21 |
#include "config.h" |
| 22 |
#endif |
| 23 |
|
| 24 |
#include <stdlib.h> |
| 25 |
#include <stdio.h> |
| 26 |
#include <string.h> |
| 27 |
#include "ut_string.h" |
| 28 |
#include "ut_string_class.h" |
| 29 |
#include "ut_assert.h" |
| 30 |
#include "ut_debugmsg.h" |
| 31 |
#include "ut_Language.h" |
| 32 |
#include "xap_Dlg_Language.h" |
| 33 |
#include "xap_Strings.h" |
| 34 |
#include "xap_App.h" |
| 35 |
|
| 36 |
static bool is_utf8_encoding; |
| 37 |
|
| 38 |
static int s_compareQ(const void * a, const void * b) |
| 39 |
{ |
| 40 |
const gchar ** A = (const gchar **)(a); |
| 41 |
const gchar ** B = (const gchar **)(b); |
| 42 |
|
| 43 |
if (is_utf8_encoding) |
| 44 |
return g_utf8_collate(*A,*B); |
| 45 |
else |
| 46 |
return g_ascii_strcasecmp(*A,*B); |
| 47 |
} |
| 48 |
|
| 49 |
/*****************************************************************/ |
| 50 |
|
| 51 |
XAP_Dialog_Language::XAP_Dialog_Language(XAP_DialogFactory * pDlgFactory, XAP_Dialog_Id id) |
| 52 |
: XAP_Dialog_NonPersistent(pDlgFactory,id, "interface/dialoglanguage"), |
| 53 |
m_bDocDefault(false) |
| 54 |
{ |
| 55 |
UT_uint32 nDontSort = 0, nSort = 0; |
| 56 |
UT_uint32 i; |
| 57 |
|
| 58 |
m_answer = a_CANCEL; |
| 59 |
m_pLanguage = NULL; |
| 60 |
m_pLangProperty = NULL; |
| 61 |
m_bChangedLanguage = false; |
| 62 |
m_pLangTable = new UT_Language; |
| 63 |
const gchar ** ppLanguagesTemp = new const gchar * [m_pLangTable->getCount()]; |
| 64 |
|
| 65 |
UT_ASSERT(m_pLangTable); |
| 66 |
m_iLangCount = m_pLangTable->getCount(); |
| 67 |
m_ppLanguages = new const gchar * [m_iLangCount]; |
| 68 |
m_ppLanguagesCode = new const gchar * [m_iLangCount]; |
| 69 |
|
| 70 |
is_utf8_encoding = g_ascii_strcasecmp (XAP_App::getApp()->getDefaultEncoding(), "UTF-8") == 0; |
| 71 |
|
| 72 |
for(i=0; i<m_iLangCount; i++) |
| 73 |
{ |
| 74 |
if (m_pLangTable->getNthId(i)==XAP_STRING_ID_LANG_0) // Unsorted languages |
| 75 |
{ |
| 76 |
m_ppLanguages[nDontSort]=m_pLangTable->getNthLangName(i); |
| 77 |
nDontSort++; |
| 78 |
} |
| 79 |
else |
| 80 |
{ |
| 81 |
ppLanguagesTemp[nSort] = m_pLangTable->getNthLangName(i); |
| 82 |
nSort++; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// sort the temporary array |
| 87 |
qsort(ppLanguagesTemp, m_iLangCount-nDontSort, sizeof(gchar *), s_compareQ); |
| 88 |
|
| 89 |
|
| 90 |
// Copy the sorted codes and a ssign each language its code |
| 91 |
for(UT_uint32 nLang = 0; nLang < m_iLangCount; nLang++) |
| 92 |
{ |
| 93 |
if (nLang>=nDontSort) |
| 94 |
m_ppLanguages[nLang]=ppLanguagesTemp[nLang-nDontSort]; |
| 95 |
|
| 96 |
for(i = 0; i < m_iLangCount; i++) |
| 97 |
{ |
| 98 |
if (strcmp (m_ppLanguages[nLang], m_pLangTable->getNthLangName(i))==0) |
| 99 |
{ |
| 100 |
m_ppLanguagesCode[nLang] = m_pLangTable->getNthLangCode(i); |
| 101 |
break; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
delete [] ppLanguagesTemp; |
| 106 |
|
| 107 |
// TODO: move spell-checking into XAP land and make this dialog |
| 108 |
// TODO: more like the MSWord one (i.e. add): |
| 109 |
// [] do not check spelling or grammar |
| 110 |
m_bSpellCheck = true; |
| 111 |
} |
| 112 |
|
| 113 |
XAP_Dialog_Language::~XAP_Dialog_Language(void) |
| 114 |
{ |
| 115 |
DELETEP(m_pLangTable); |
| 116 |
DELETEPV(m_ppLanguages); |
| 117 |
DELETEPV(m_ppLanguagesCode); |
| 118 |
} |
| 119 |
|
| 120 |
// we will not use the value passed to us, but rather will reference |
| 121 |
// ourselves into m_pLangTable; that way we do not have to worry about |
| 122 |
// the string disappearing on us, nor do we need to clone it |
| 123 |
void XAP_Dialog_Language::setLanguageProperty(const gchar * pLangProp) |
| 124 |
{ |
| 125 |
UT_ASSERT(m_pLangTable); |
| 126 |
UT_uint32 indx = m_pLangTable->getIndxFromCode( |
| 127 |
pLangProp ? pLangProp :"-none-"); |
| 128 |
m_pLanguage = m_pLangTable->getNthLangName(indx); |
| 129 |
m_pLangProperty = m_pLangTable->getNthLangCode(indx); |
| 130 |
} |
| 131 |
|
| 132 |
// in this case we do not need to worry about the lifespan of pLang |
| 133 |
// since we call it only internally, always referring back to m_pLangTable |
| 134 |
void XAP_Dialog_Language::_setLanguage(const gchar * pLang) |
| 135 |
{ |
| 136 |
UT_ASSERT(m_pLangTable); |
| 137 |
m_pLanguage = pLang; |
| 138 |
m_pLangProperty = m_pLangTable->getCodeFromName(pLang); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
XAP_Dialog_Language::tAnswer XAP_Dialog_Language::getAnswer(void) const |
| 143 |
{ |
| 144 |
return m_answer; |
| 145 |
} |
| 146 |
|
| 147 |
bool XAP_Dialog_Language::getChangedLangProperty(const gchar ** pszLangProp) const |
| 148 |
{ |
| 149 |
UT_return_val_if_fail(pszLangProp,false); |
| 150 |
*pszLangProp = m_pLangProperty; |
| 151 |
return m_bChangedLanguage; |
| 152 |
} |
| 153 |
|
| 154 |
/* |
| 155 |
Creates a vector with a list of support languages for spell checking |
| 156 |
|
| 157 |
You must to g_free the allocated memory |
| 158 |
*/ |
| 159 |
UT_Vector* XAP_Dialog_Language::getAvailableDictionaries() |
| 160 |
{ |
| 161 |
#ifdef ENABLE_SPELL |
| 162 |
SpellChecker * checker = SpellManager::instance().getInstance(); |
| 163 |
UT_Vector& vec= checker->getMapping(); |
| 164 |
DictionaryMapping * mapping; |
| 165 |
UT_Vector* vecRslt = new UT_Vector(); |
| 166 |
|
| 167 |
const UT_uint32 nItems = vec.getItemCount(); |
| 168 |
|
| 169 |
for (UT_uint32 iItem = nItems; iItem; --iItem) |
| 170 |
{ |
| 171 |
mapping = static_cast<DictionaryMapping*>(const_cast<void*>(vec.getNthItem(iItem - 1))); |
| 172 |
|
| 173 |
if (checker->doesDictionaryExist(mapping->lang.c_str())) |
| 174 |
vecRslt->addItem( g_strdup(mapping->lang.c_str())); |
| 175 |
} |
| 176 |
|
| 177 |
return vecRslt; |
| 178 |
#else |
| 179 |
return NULL; |
| 180 |
#endif |
| 181 |
} |
| 182 |
|
| 183 |
/*! |
| 184 |
Fills s with the string to be used as a label for the the default language checkbox |
| 185 |
*/ |
| 186 |
void XAP_Dialog_Language::getDocDefaultLangCheckboxLabel(UT_UTF8String &s) |
| 187 |
{ |
| 188 |
const XAP_StringSet * pSS = XAP_App::getApp()->getStringSet(); |
| 189 |
UT_return_if_fail(pSS); |
| 190 |
|
| 191 |
std::string str; |
| 192 |
pSS->getValueUTF8(XAP_STRING_ID_DLG_ULANG_DefaultLangChkbox, str); |
| 193 |
s = str; |
| 194 |
} |
| 195 |
|
| 196 |
void XAP_Dialog_Language::getDocDefaultLangCheckboxLabel(std::string &s) |
| 197 |
{ |
| 198 |
const XAP_StringSet * pSS = XAP_App::getApp()->getStringSet(); |
| 199 |
UT_return_if_fail(pSS); |
| 200 |
|
| 201 |
pSS->getValueUTF8(XAP_STRING_ID_DLG_ULANG_DefaultLangChkbox, s); |
| 202 |
} |
| 203 |
|
| 204 |
/*! |
| 205 |
Fills s with the string to be displayed in the default language static control |
| 206 |
*/ |
| 207 |
void XAP_Dialog_Language::getDocDefaultLangDescription(UT_UTF8String & s) |
| 208 |
{ |
| 209 |
const XAP_StringSet * pSS = XAP_App::getApp()->getStringSet(); |
| 210 |
UT_return_if_fail(pSS); |
| 211 |
|
| 212 |
std::string str; |
| 213 |
pSS->getValueUTF8(XAP_STRING_ID_DLG_ULANG_DefaultLangLabel, str); |
| 214 |
s = str; |
| 215 |
|
| 216 |
s += m_docLang; |
| 217 |
} |
| 218 |
|
| 219 |
void XAP_Dialog_Language::getDocDefaultLangDescription(std::string & s) |
| 220 |
{ |
| 221 |
const XAP_StringSet * pSS = XAP_App::getApp()->getStringSet(); |
| 222 |
UT_return_if_fail(pSS); |
| 223 |
|
| 224 |
pSS->getValueUTF8(XAP_STRING_ID_DLG_ULANG_DefaultLangLabel, s); |
| 225 |
|
| 226 |
s += m_docLang; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
/*! |
| 231 |
Initialises the dialogue to the current default lanaguage |
| 232 |
pLang is a standard lang property (e.g., en-GB) |
| 233 |
*/ |
| 234 |
void XAP_Dialog_Language::setDocumentLanguage(const gchar * pLang) |
| 235 |
{ |
| 236 |
UT_return_if_fail( pLang ); |
| 237 |
UT_return_if_fail(m_pLangTable); |
| 238 |
|
| 239 |
UT_uint32 indx = m_pLangTable->getIdFromCode(pLang); |
| 240 |
|
| 241 |
// NB: m_docLang holds the translated language name, not the tag |
| 242 |
XAP_App::getApp()->getStringSet()->getValueUTF8(indx, m_docLang); |
| 243 |
} |
| 244 |
|
| 245 |
|