aboutsummaryrefslogtreecommitdiff
path: root/main/src/ui/chat_input/spell_checker.vala
blob: 8359b1b403b34b4842395694d8e3099f7ffed545 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using Gdk;
using Gee;
using Gspell;

using Dino.Entities;

namespace Dino.Ui {

public class SpellChecker {

    private Conversation? conversation;
    private TextView text_view;
    private TextBuffer text_buffer;

    public SpellChecker(Gtk.TextView text_input) {
        this.text_view = TextView.get_from_gtk_text_view(text_input);
        this.text_buffer = TextBuffer.get_from_gtk_text_buffer(text_view.view.buffer);

        text_view.basic_setup();
        text_buffer.spell_checker.notify["language"].connect(lang_changed);

        // Enable/Disable spell checking live
        Dino.Application.get_default().settings.notify["check-spelling"].connect((obj, _) => {
            if (((Dino.Entities.Settings) obj).check_spelling) {
                initialize_for_conversation(this.conversation);
            } else {
                text_buffer.set_spell_checker(null);
            }
        });
    }

    public void initialize_for_conversation(Conversation conversation) {
        this.conversation = conversation;

        if (!Dino.Application.get_default().settings.check_spelling) {
            text_buffer.set_spell_checker(null);
            return;
        }
        if (text_buffer.spell_checker == null) text_buffer.spell_checker = new Checker(null);

        // Set the conversation language (from cache or db)
        text_buffer.spell_checker.notify["language"].disconnect(lang_changed);

        var db = Dino.Application.get_default().db;
        Qlite.RowOption row_option = db.conversation_settings.select()
                .with(db.conversation_settings.conversation_id, "=", conversation.id)
                .with(db.conversation_settings.key, "=", "lang")
                .single().row();
        if (row_option.is_present()) {
            string lang_code = row_option.inner[db.conversation_settings.value];
            Language? lang = Language.lookup(lang_code);
            text_buffer.spell_checker.language = lang;
        } else {
            text_buffer.spell_checker.language = null;
        }

        text_buffer.spell_checker.notify["language"].connect(lang_changed);
    }

    private void lang_changed() {
        var db = Dino.Application.get_default().db;

        Checker spell_checker = text_buffer.spell_checker;
        if (spell_checker.language.get_code() == null) return;
        db.conversation_settings.upsert()
                .value(db.conversation_settings.conversation_id, conversation.id, true)
                .value(db.conversation_settings.key, "lang", true)
                .value(db.conversation_settings.value, spell_checker.language.get_code())
                .perform();
    }
}

}