Changeset 124763 in webkit


Ignore:
Timestamp:
Aug 6, 2012 6:43:07 AM (12 years ago)
Author:
mario@webkit.org
Message:

[WK2][GTK] Improvements for the new spell-checking API
https://bugs.webkit.org/show_bug.cgi?id=93262

Reviewed by Carlos Garcia Campos.

Source/WebCore:

Provide a way to ask TextCheckerEnchant helper class for the list
of languages currently available for the spell checking feature.

  • platform/text/gtk/TextCheckerEnchant.cpp:

(enchantDictDescribeCallback): Renamed from
getAvailableDictionariesCallback, for consistency.
(TextCheckerEnchant::updateSpellCheckingLanguages): Updated usage
of getAvailableDictionariesCallback to enchantDictDescribeCallback.
(TextCheckerEnchant::getSpellCheckingLanguages): New. Will build
and return a String with a comma-separated list of languages
currently in use by the spell checking feature.

  • platform/text/gtk/TextCheckerEnchant.h:

(TextCheckerEnchant): Added new function getSpellCheckingLanguages.

Source/WebKit2:

Improve the way the new spell-checking API is implemented, by
using better internal representations for data, documenting better
the new functions and using better unit tests.

  • UIProcess/API/gtk/WebKitTextChecker.cpp:

(WebKitTextChecker::getSpellCheckingLanguages): Just return the
value previously cached when calling to the setter function.
(WebKitTextChecker::setSpellCheckingLanguages): Update the text
checker in WebCore and cache the value returned from it.

  • UIProcess/API/gtk/WebKitTextChecker.h:

(WebKitTextChecker): Use a CString instead of an String to cache
the list of spell checking languages. Update getter and setter.

  • UIProcess/API/gtk/WebKitWebContext.cpp:

(_WebKitWebContextPrivate): No need to cache the spell checking
languages here anymore.
(webkit_web_context_get_spell_checking_languages): Improve
both implementation and documentation to be more consistent.
(webkit_web_context_set_spell_checking_languages): Make
'languages' a mandatory (non-NULL) parameter. Update documentation.

  • UIProcess/API/gtk/tests/TestWebKitWebContext.cpp:

(testWebContextSpellChecker): Test even more situations.

Location:
trunk/Source
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r124760 r124763  
     12012-08-06  Mario Sanchez Prada  <msanchez@igalia.com>
     2
     3        [WK2][GTK] Improvements for the new spell-checking API
     4        https://bugs.webkit.org/show_bug.cgi?id=93262
     5
     6        Reviewed by Carlos Garcia Campos.
     7
     8        Provide a way to ask TextCheckerEnchant helper class for the list
     9        of languages currently available for the spell checking feature.
     10
     11        * platform/text/gtk/TextCheckerEnchant.cpp:
     12        (enchantDictDescribeCallback): Renamed from
     13        getAvailableDictionariesCallback, for consistency.
     14        (TextCheckerEnchant::updateSpellCheckingLanguages): Updated usage
     15        of getAvailableDictionariesCallback to enchantDictDescribeCallback.
     16        (TextCheckerEnchant::getSpellCheckingLanguages): New. Will build
     17        and return a String with a comma-separated list of languages
     18        currently in use by the spell checking feature.
     19        * platform/text/gtk/TextCheckerEnchant.h:
     20        (TextCheckerEnchant): Added new function getSpellCheckingLanguages.
     21
    1222012-08-06  Andreas Kling  <kling@webkit.org>
    223
  • trunk/Source/WebCore/platform/text/gtk/TextCheckerEnchant.cpp

    r124578 r124763  
    2525#include <wtf/gobject/GOwnPtr.h>
    2626#include <wtf/text/CString.h>
     27#include <wtf/text/StringBuilder.h>
    2728
    2829using namespace WebCore;
     
    3031static const size_t maximumNumberOfSuggestions = 10;
    3132
    32 static void getAvailableDictionariesCallback(const char* const languageTag, const char* const, const char* const, const char* const, void* data)
     33static void enchantDictDescribeCallback(const char* const languageTag, const char* const, const char* const, const char* const, void* data)
    3334{
    3435    Vector<CString>* dictionaries = static_cast<Vector<CString>*>(data);
     
    177178            // No dictionaries selected, we get one from the list.
    178179            Vector<CString> allDictionaries;
    179             enchant_broker_list_dicts(m_broker, getAvailableDictionariesCallback, &allDictionaries);
     180            enchant_broker_list_dicts(m_broker, enchantDictDescribeCallback, &allDictionaries);
    180181            if (!allDictionaries.isEmpty()) {
    181182                EnchantDict* dict = enchant_broker_request_dict(m_broker, allDictionaries[0].data());
     
    188189}
    189190
     191String TextCheckerEnchant::getSpellCheckingLanguages()
     192{
     193    if (m_enchantDictionaries.isEmpty())
     194        return String();
     195
     196    // Get a Vector<CString> with the list of languages in use.
     197    Vector<CString> currentDictionaries;
     198    for (Vector<EnchantDict*>::const_iterator iter = m_enchantDictionaries.begin(); iter != m_enchantDictionaries.end(); ++iter)
     199        enchant_dict_describe(*iter, enchantDictDescribeCallback, &currentDictionaries);
     200
     201    // Build the result String;
     202    StringBuilder builder;
     203    for (Vector<CString>::const_iterator iter = currentDictionaries.begin(); iter != currentDictionaries.end(); ++iter) {
     204        if (iter != currentDictionaries.begin())
     205            builder.append(",");
     206        builder.append(String::fromUTF8(iter->data()));
     207    }
     208    return builder.toString();
     209}
     210
    190211void TextCheckerEnchant::freeEnchantBrokerDictionaries()
    191212{
  • trunk/Source/WebCore/platform/text/gtk/TextCheckerEnchant.h

    r124578 r124763  
    4343    Vector<String> getGuessesForWord(const String&);
    4444    void updateSpellCheckingLanguages(const String& languages);
     45    String getSpellCheckingLanguages();
    4546
    4647private:
  • trunk/Source/WebKit2/ChangeLog

    r124758 r124763  
     12012-08-06  Mario Sanchez Prada  <msanchez@igalia.com>
     2
     3        [WK2][GTK] Improvements for the new spell-checking API
     4        https://bugs.webkit.org/show_bug.cgi?id=93262
     5
     6        Reviewed by Carlos Garcia Campos.
     7
     8        Improve the way the new spell-checking API is implemented, by
     9        using better internal representations for data, documenting better
     10        the new functions and using better unit tests.
     11
     12        * UIProcess/API/gtk/WebKitTextChecker.cpp:
     13        (WebKitTextChecker::getSpellCheckingLanguages): Just return the
     14        value previously cached when calling to the setter function.
     15        (WebKitTextChecker::setSpellCheckingLanguages): Update the text
     16        checker in WebCore and cache the value returned from it.
     17        * UIProcess/API/gtk/WebKitTextChecker.h:
     18        (WebKitTextChecker): Use a CString instead of an String to cache
     19        the list of spell checking languages. Update getter and setter.
     20        * UIProcess/API/gtk/WebKitWebContext.cpp:
     21        (_WebKitWebContextPrivate): No need to cache the spell checking
     22        languages here anymore.
     23        (webkit_web_context_get_spell_checking_languages): Improve
     24        both implementation and documentation to be more consistent.
     25        (webkit_web_context_set_spell_checking_languages): Make
     26        'languages' a mandatory (non-NULL) parameter. Update documentation.
     27        * UIProcess/API/gtk/tests/TestWebKitWebContext.cpp:
     28        (testWebContextSpellChecker): Test even more situations.
     29
    1302012-08-06  Andras Becsi  <andras.becsi@nokia.com>
    231
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitTextChecker.cpp

    r124741 r124763  
    141141}
    142142
    143 void WebKitTextChecker::setSpellCheckingLanguages(const String& languages)
     143const CString& WebKitTextChecker::getSpellCheckingLanguages()
    144144{
    145     if (m_spellCheckingLanguages == languages)
    146         return;
    147     m_spellCheckingLanguages = languages;
     145    String spellCheckingLanguages = m_textChecker->getSpellCheckingLanguages();
     146    m_spellCheckingLanguages = spellCheckingLanguages.isEmpty() ? CString() : spellCheckingLanguages.utf8();
     147    return m_spellCheckingLanguages;
     148}
    148149
    149     // We need to update the languages in the enchant-based checker too.
    150     m_textChecker->updateSpellCheckingLanguages(languages);
     150void WebKitTextChecker::setSpellCheckingLanguages(const CString& languages)
     151{
     152    m_textChecker->updateSpellCheckingLanguages(String::fromUTF8(languages.data()));
    151153}
    152154#endif // ENABLE(SPELLCHECK)
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitTextChecker.h

    r124741 r124763  
    2727#include <wtf/PassOwnPtr.h>
    2828#include <wtf/Vector.h>
     29#include <wtf/text/CString.h>
    2930
    3031class WebKitTextChecker {
     
    4445
    4546    // To be called from WebKitWebContext only.
    46     const String getSpellCheckingLanguages() { return m_spellCheckingLanguages; }
    47     void setSpellCheckingLanguages(const String& spellCheckingLanguages);
     47    const CString& getSpellCheckingLanguages();
     48    void setSpellCheckingLanguages(const CString& spellCheckingLanguages);
    4849
    4950private:
     
    5152
    5253    OwnPtr<WebCore::TextCheckerEnchant> m_textChecker;
    53     String m_spellCheckingLanguages;
     54    CString m_spellCheckingLanguages;
    5455    bool m_spellCheckingEnabled;
    5556};
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitWebContext.cpp

    r124741 r124763  
    7878#if ENABLE(SPELLCHECK)
    7979    OwnPtr<WebKitTextChecker> textChecker;
    80     GOwnPtr<gchar> spellCheckingLanguages;
    8180#endif
    8281};
     
    433432 * @context: a #WebKitWebContext
    434433 *
    435  * Get the current status of the spell checking feature.
     434 * Get whether spell checking feature is currently enabled.
    436435 *
    437436 * Returns: %TRUE If spell checking is enabled, or %FALSE otherwise.
     
    469468 *
    470469 * Get the the list of spell checking languages associated with
    471  * @context, separated by commas. See
    472  * webkit_web_context_set_spell_checking_languages() for more details
    473  * on the format of the languages in the list.
    474  *
    475  * Returns: (transfer none): A comma separated list of languages.
     470 * @context separated by commas, or %NULL if no languages have been
     471 * previously set.
     472
     473 * See webkit_web_context_set_spell_checking_languages() for more
     474 * details on the format of the languages in the list.
     475 *
     476 * Returns: (transfer none): A comma separated list of languages if
     477 * available, or %NULL otherwise.
    476478 */
    477479const gchar* webkit_web_context_get_spell_checking_languages(WebKitWebContext* context)
     
    480482
    481483#if ENABLE(SPELLCHECK)
    482     return context->priv->spellCheckingLanguages.get();
     484    CString spellCheckingLanguages = context->priv->textChecker->getSpellCheckingLanguages();
     485    if (spellCheckingLanguages.isNull())
     486        return 0;
     487    return spellCheckingLanguages.data();
    483488#else
    484489    return 0;
     
    489494 * webkit_web_context_set_spell_checking_languages:
    490495 * @context: a #WebKitWebContext
    491  * @languages: (allow-none): new list of spell checking
    492  * languages separated by commas, or %NULL
     496 * @languages: new list of spell checking languages separated by
     497 * commas
    493498 *
    494499 * Set the list of spell checking languages to be used for spell
    495  * checking, separated by commas. In case %NULL is passed, the default
    496  * value as returned by gtk_get_default_language() will be used.
     500 * checking, separated by commas.
    497501 *
    498502 * The locale string typically is in the form lang_COUNTRY, where lang
     
    500504 * For instance, sv_FI for Swedish as written in Finland or pt_BR
    501505 * for Portuguese as written in Brazil.
     506 *
     507 * You need to call this function with a valid list of languages at
     508 * least once in order to properly enable the spell checking feature
     509 * in WebKit.
    502510 */
    503511void webkit_web_context_set_spell_checking_languages(WebKitWebContext* context, const gchar* languages)
    504512{
    505513    g_return_if_fail(WEBKIT_IS_WEB_CONTEXT(context));
     514    g_return_if_fail(languages);
    506515
    507516#if ENABLE(SPELLCHECK)
    508     context->priv->textChecker->setSpellCheckingLanguages(String(languages));
    509     context->priv->spellCheckingLanguages.set(g_strdup(languages));
     517    context->priv->textChecker->setSpellCheckingLanguages(languages);
    510518#endif
    511519}
  • trunk/Source/WebKit2/UIProcess/API/gtk/tests/TestWebKitWebContext.cpp

    r124741 r124763  
    234234static void testWebContextSpellChecker(Test* test, gconstpointer)
    235235{
    236     GRefPtr<WebKitWebContext> webContext(webkit_web_context_get_default());
    237 
    238     // Set the language to a specific one, an empty one and a list of them.
    239     webkit_web_context_set_spell_checking_languages(webContext.get(), "en_US");
    240     const gchar* currentLanguage(webkit_web_context_get_spell_checking_languages(webContext.get()));
     236    WebKitWebContext* webContext = webkit_web_context_get_default();
     237
     238    // Check what happens if no spell checking language has been set.
     239    const gchar* currentLanguage = webkit_web_context_get_spell_checking_languages(webContext);
     240    g_assert(!currentLanguage);
     241
     242    // Set the language to a specific one.
     243    webkit_web_context_set_spell_checking_languages(webContext, "en_US");
     244    currentLanguage = webkit_web_context_get_spell_checking_languages(webContext);
    241245    g_assert_cmpstr(currentLanguage, ==, "en_US");
    242246
    243     webkit_web_context_set_spell_checking_languages(webContext.get(), 0);
    244     currentLanguage = webkit_web_context_get_spell_checking_languages(webContext.get());
    245     g_assert_cmpstr(currentLanguage, ==, 0);
    246 
    247     webkit_web_context_set_spell_checking_languages(webContext.get(), "es_ES,en_US");
    248     currentLanguage = webkit_web_context_get_spell_checking_languages(webContext.get());
    249     g_assert_cmpstr(currentLanguage, ==, "es_ES,en_US");
     247    // Set the language string to list of valid languages.
     248    webkit_web_context_set_spell_checking_languages(webContext, "en_GB,en_US");
     249    currentLanguage = webkit_web_context_get_spell_checking_languages(webContext);
     250    g_assert_cmpstr(currentLanguage, ==, "en_GB,en_US");
     251
     252    // Try passing a wrong language along with good ones.
     253    webkit_web_context_set_spell_checking_languages(webContext, "bd_WR,en_US,en_GB");
     254    currentLanguage = webkit_web_context_get_spell_checking_languages(webContext);
     255    g_assert_cmpstr(currentLanguage, ==, "en_US,en_GB");
     256
     257    // Try passing a list with only wrong languages.
     258    webkit_web_context_set_spell_checking_languages(webContext, "bd_WR,wr_BD");
     259    currentLanguage = webkit_web_context_get_spell_checking_languages(webContext);
     260    g_assert(!currentLanguage);
    250261
    251262    // Check disabling and re-enabling spell checking.
    252     webkit_web_context_set_spell_checking_enabled(webContext.get(), FALSE);
    253     gboolean isSpellCheckingEnabled = webkit_web_context_get_spell_checking_enabled(webContext.get());
    254     g_assert(!isSpellCheckingEnabled);
    255     webkit_web_context_set_spell_checking_enabled(webContext.get(), TRUE);
    256     isSpellCheckingEnabled = webkit_web_context_get_spell_checking_enabled(webContext.get());
    257     g_assert(isSpellCheckingEnabled);
     263    webkit_web_context_set_spell_checking_enabled(webContext, FALSE);
     264    g_assert(!webkit_web_context_get_spell_checking_enabled(webContext));
     265    webkit_web_context_set_spell_checking_enabled(webContext, TRUE);
     266    g_assert(webkit_web_context_get_spell_checking_enabled(webContext));
    258267}
    259268
Note: See TracChangeset for help on using the changeset viewer.