File indexing completed on 2024-04-28 15:34:19

0001 /*
0002  * SPDX-FileCopyrightText: 2007 Zack Rusin <zack@kde.org>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-or-later
0005  */
0006 #ifndef SONNET_SPELLER_H
0007 #define SONNET_SPELLER_H
0008 
0009 #include <QMap>
0010 #include <QString>
0011 #include <QStringList>
0012 
0013 #include "sonnetcore_export.h"
0014 
0015 namespace Sonnet
0016 {
0017 class SpellerPrivate;
0018 /**
0019  * @class Sonnet::Speller speller.h <Sonnet/Speller>
0020  *
0021  * Spell checker object.
0022  *
0023  * @short class used for actual spell checking
0024  */
0025 class SONNETCORE_EXPORT Speller
0026 {
0027 public:
0028     explicit Speller(const QString &lang = QString());
0029     ~Speller();
0030 
0031     Speller(const Speller &speller);
0032     Speller &operator=(const Speller &speller);
0033 
0034     /**
0035      * @return @c true if the speller supports currently selected
0036      * language.
0037      */
0038     bool isValid() const;
0039 
0040     /**
0041      * Sets the language supported by this speller.
0042      */
0043     void setLanguage(const QString &lang);
0044 
0045     /**
0046      * @return language supported by this speller.
0047      */
0048     QString language() const;
0049 
0050     /**
0051      * Checks the given word.
0052      * @return false if the word is misspelled. true otherwise
0053      */
0054     bool isCorrect(const QString &word) const;
0055 
0056     /**
0057      * Checks the given word.
0058      * @return true if the word is misspelled. false otherwise
0059      */
0060     bool isMisspelled(const QString &word) const;
0061 
0062     /**
0063      * Fetches suggestions for the word.
0064      *
0065      * @return list of all suggestions for the word
0066      */
0067     QStringList suggest(const QString &word) const;
0068 
0069     /**
0070      * Convenience method calling isCorrect() and suggest()
0071      * if the word isn't correct.
0072      */
0073     bool checkAndSuggest(const QString &word, QStringList &suggestions) const;
0074 
0075     /**
0076      * Stores user defined good replacement for the bad word.
0077      * @return @c true on success
0078      */
0079     bool storeReplacement(const QString &bad, const QString &good);
0080 
0081     /**
0082      * Adds word to the list of of personal words.
0083      * @return true on success
0084      */
0085     bool addToPersonal(const QString &word);
0086 
0087     /**
0088      * Adds word to the words recognizable in the current session.
0089      * @return true on success
0090      */
0091     bool addToSession(const QString &word);
0092 
0093 public: // Configuration API
0094     enum Attribute {
0095         CheckUppercase,
0096         SkipRunTogether,
0097         AutoDetectLanguage,
0098     };
0099     void save();
0100     void restore();
0101 
0102     /**
0103      * @return names of all supported backends (e.g. ISpell, ASpell)
0104      */
0105     QStringList availableBackends() const;
0106 
0107     /**
0108      * @return a list of supported languages.
0109      *
0110      * Note: use availableDictionaries
0111      */
0112     QStringList availableLanguages() const;
0113 
0114     /**
0115      * @return a localized list of names of supported languages.
0116      *
0117      * Note: use availableDictionaries
0118      */
0119     QStringList availableLanguageNames() const;
0120 
0121     /**
0122      * @return a map of all available dictionaries with language descriptions and
0123      * their codes. The key is the description, the code the value.
0124      */
0125     QMap<QString, QString> availableDictionaries() const;
0126 
0127     /**
0128      * @return a map of user preferred dictionaries with language descriptions and
0129      * their codes. The key is the description, the code the value.
0130      * @since 5.54
0131      */
0132     QMap<QString, QString> preferredDictionaries() const;
0133 
0134     void setDefaultLanguage(const QString &lang);
0135     QString defaultLanguage() const;
0136 
0137     void setDefaultClient(const QString &client);
0138     QString defaultClient() const;
0139 
0140     void setAttribute(Attribute attr, bool b = true);
0141     bool testAttribute(Attribute attr) const;
0142 
0143 private:
0144     SpellerPrivate *const d;
0145 };
0146 }
0147 #endif