summaryrefslogtreecommitdiffstats
blob: 9232b19c178c93f2523e8da971780042f591a90d (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * Copyright (c) 2011 Vlad Voicu
 * Licensed under the Simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

/*
 * Copyright (c) 2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swift/QtUI/QtSpellCheckerWindow.h>

#include <QCoreApplication>
#include <QDir>
#include <QFileDialog>
#include <QStringList>
#include <QTimer>

#include <Swift/Controllers/SettingConstants.h>
#include <Swift/Controllers/Settings/SettingsProvider.h>

#include <Swift/QtUI/QtSwiftUtil.h>
#include <Swift/QtUI/QtUISettingConstants.h>

namespace Swift {

QtSpellCheckerWindow::QtSpellCheckerWindow(SettingsProvider* settings, QWidget* parent) : QDialog(parent) {
    settings_ = settings;
    ui_.setupUi(this);
#ifdef HAVE_HUNSPELL
    ui_.hunspellOptions->show();
#else
    ui_.hunspellOptions->hide();
    QTimer::singleShot(0, this, SLOT(shrinkWindow()));
#endif
    connect(ui_.spellChecker, SIGNAL(toggled(bool)), this, SLOT(handleChecker(bool)));
    connect(ui_.cancel, SIGNAL(clicked()), this, SLOT(handleCancel()));
    connect(ui_.apply, SIGNAL(clicked()), this, SLOT(handleApply()));
    connect(ui_.pathButton, SIGNAL(clicked()), this, SLOT(handlePathButton()));
    setFromSettings();
}

void QtSpellCheckerWindow::shrinkWindow() {
    resize(0,0);
}

void QtSpellCheckerWindow::setFromSettings() {
    ui_.spellChecker->setChecked(settings_->getSetting(SettingConstants::SPELL_CHECKER));
    ui_.pathContent->setText(P2QSTRING(settings_->getSetting(SettingConstants::DICT_PATH)));
    ui_.currentLanguageValue->setText(P2QSTRING(settings_->getSetting(SettingConstants::DICT_FILE)));
    std::string currentPath = settings_->getSetting(SettingConstants::DICT_PATH);
    QString filename = "*.dic";
    QDir dictDirectory = QDir(P2QSTRING(currentPath));
    QStringList files = dictDirectory.entryList(QStringList(filename), QDir::Files);
    showFiles(files);
    setEnabled(settings_->getSetting(SettingConstants::SPELL_CHECKER));
}

void QtSpellCheckerWindow::handleChecker(bool state) {
    setEnabled(state);
}

void QtSpellCheckerWindow::setEnabled(bool state) {
    ui_.pathContent->setEnabled(state);
    ui_.languageView->setEnabled(state);
    ui_.pathButton->setEnabled(state);
    ui_.pathLabel->setEnabled(state);
    ui_.currentLanguage->setEnabled(state);
    ui_.currentLanguageValue->setEnabled(state);
    ui_.language->setEnabled(state);
}

void QtSpellCheckerWindow::handleApply() {
    settings_->storeSetting(SettingConstants::SPELL_CHECKER, ui_.spellChecker->isChecked());
    QList<QListWidgetItem* > selectedLanguage = ui_.languageView->selectedItems();
    if (!selectedLanguage.empty()) {
        settings_->storeSetting(SettingConstants::DICT_FILE, Q2PSTRING((selectedLanguage.first())->text()));
    }
    this->done(0);
}

void QtSpellCheckerWindow::handleCancel() {
    this->done(0);
}

void QtSpellCheckerWindow::handlePathButton() {
    std::string currentPath = settings_->getSetting(SettingConstants::DICT_PATH);
    QString dirpath = QFileDialog::getExistingDirectory(this, tr("Dictionary Path"), P2QSTRING(currentPath));
    if (dirpath != P2QSTRING(currentPath)) {
        ui_.languageView->clear();
        settings_->storeSetting(SettingConstants::DICT_FILE, "");
        ui_.currentLanguageValue->setText(" ");
    }
    if (!dirpath.isEmpty()) {
        if (!dirpath.endsWith("/")) {
            dirpath.append("/");
        }
        settings_->storeSetting(SettingConstants::DICT_PATH, Q2PSTRING(dirpath));
        QDir dictDirectory = QDir(dirpath);
        ui_.pathContent->setText(dirpath);
        QString filename = "*.dic";
        QStringList files = dictDirectory.entryList(QStringList(filename), QDir::Files);
        showFiles(files);
    }
}

void QtSpellCheckerWindow::handlePersonalPathButton() {
    std::string currentPath = settings_->getSetting(SettingConstants::PERSONAL_DICT_PATH);
    QString filename = QFileDialog::getOpenFileName(this, tr("Select Personal Dictionary"), P2QSTRING(currentPath), tr("(*.dic"));
    settings_->storeSetting(SettingConstants::PERSONAL_DICT_PATH, Q2PSTRING(filename));
}

void QtSpellCheckerWindow::showFiles(const QStringList& files) {
    ui_.languageView->clear();
    for (int i = 0; i < files.size(); ++i) {
        ui_.languageView->insertItem(i, files[i]);
    }
}

}