emaillineedit.cpp 3.97 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
/*
Laurent Montel's avatar
Minor  
Laurent Montel committed
2
   Copyright (C) 2018 Montel Laurent <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

Laurent Montel's avatar
Minor  
Laurent Montel committed
20
#include "emaillineedit.h"
21
#include "sieveeditoremaillineditplugin_debug.h"
Laurent Montel's avatar
Laurent Montel committed
22
#include <kpluginfactory.h>
23
#include <Akonadi/Contact/EmailAddressSelectionDialog>
Laurent Montel's avatar
Laurent Montel committed
24 25 26 27 28 29

#include <QHBoxLayout>
#include <QLineEdit>
#include <QToolButton>
#include <KLocalizedString>
#include <QDialog>
30 31 32
#include <QTreeView>
#include <KColorScheme>
#include <memory>
Laurent Montel's avatar
Laurent Montel committed
33

Laurent Montel's avatar
Laurent Montel committed
34
K_PLUGIN_CLASS_WITH_JSON(EmailLineEdit, "emaillineedit.json")
Laurent Montel's avatar
Laurent Montel committed
35

Laurent Montel's avatar
Laurent Montel committed
36
EmailLineEdit::EmailLineEdit(QWidget *parent, const QList<QVariant> &)
Laurent Montel's avatar
Minor  
Laurent Montel committed
37
    : KSieveUi::AbstractSelectEmailLineEdit(parent)
Laurent Montel's avatar
Laurent Montel committed
38 39 40 41 42 43
{
    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->setObjectName(QStringLiteral("mainlayout"));
    mainLayout->setMargin(0);

    mLineEdit = new QLineEdit(this);
44
    connect(mLineEdit, &QLineEdit::textChanged, this, &EmailLineEdit::slotTextChanged);
Laurent Montel's avatar
Laurent Montel committed
45 46 47
    mLineEdit->setObjectName(QStringLiteral("lineedit"));
    mainLayout->addWidget(mLineEdit);

Laurent Montel's avatar
Laurent Montel committed
48 49 50 51 52
    mEmailButton = new QToolButton(this);
    mEmailButton->setText(i18n("..."));
    mEmailButton->setObjectName(QStringLiteral("emailbutton"));
    mEmailButton->setToolTip(i18n("Select Emails"));
    mainLayout->addWidget(mEmailButton);
53 54
    connect(mEmailButton, &QToolButton::clicked, this, &EmailLineEdit::slotSelectEmail);
    verifyAkonadiStatus();
55
    connect(Akonadi::ServerManager::self(), &Akonadi::ServerManager::stateChanged, this, &EmailLineEdit::akonadiStateChanged);
Laurent Montel's avatar
Laurent Montel committed
56 57
}

Laurent Montel's avatar
Laurent Montel committed
58
EmailLineEdit::~EmailLineEdit()
Laurent Montel's avatar
Laurent Montel committed
59 60 61
{
}

62 63 64 65 66
void EmailLineEdit::akonadiStateChanged(Akonadi::ServerManager::State state)
{
    mEmailButton->setVisible(state == Akonadi::ServerManager::Running);
}

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
void EmailLineEdit::verifyAkonadiStatus()
{
    Akonadi::ServerManager::State state = Akonadi::ServerManager::self()->state();
    mEmailButton->setVisible(state == Akonadi::ServerManager::Running);
}

void EmailLineEdit::slotSelectEmail()
{
    std::unique_ptr<Akonadi::EmailAddressSelectionDialog> dlg(
        new Akonadi::EmailAddressSelectionDialog(this));
    dlg->setWindowTitle(i18n("Select Emails"));
    dlg->view()->view()->setSelectionMode(QAbstractItemView::SingleSelection);

    if (dlg->exec()) {
        QStringList addresses;
        const Akonadi::EmailAddressSelection::List lstAddress = dlg->selectedAddresses();
        if (!lstAddress.isEmpty()) {
            mLineEdit->setText(lstAddress.constFirst().email());
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
89
void EmailLineEdit::setText(const QString &str)
Laurent Montel's avatar
Laurent Montel committed
90
{
Laurent Montel's avatar
Laurent Montel committed
91
    mLineEdit->setText(str);
Laurent Montel's avatar
Laurent Montel committed
92 93
}

Laurent Montel's avatar
Laurent Montel committed
94
QString EmailLineEdit::text() const
Laurent Montel's avatar
Laurent Montel committed
95
{
Laurent Montel's avatar
Laurent Montel committed
96
    return mLineEdit->text();
Laurent Montel's avatar
Laurent Montel committed
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 123
void EmailLineEdit::slotTextChanged()
{
    verifyAddress();
    Q_EMIT valueChanged();
}

void EmailLineEdit::verifyAddress()
{
#ifndef QT_NO_STYLE_STYLESHEET
    QString styleSheet;
    const QString lineEditText = text();
    if (!lineEditText.isEmpty()) {
        const bool incorrectEmail = !lineEditText.contains(QLatin1Char('@'));
        if (mNegativeBackground.isEmpty()) {
            KStatefulBrush bgBrush = KStatefulBrush(KColorScheme::View, KColorScheme::NegativeText);
            mNegativeBackground = QStringLiteral("QLineEdit{ color:%1 }").arg(bgBrush.brush(this).color().name());
        }
        if (incorrectEmail) {
            styleSheet = mNegativeBackground;
        }
    }
    mLineEdit->setStyleSheet(styleSheet);
#endif
}

Laurent Montel's avatar
Minor  
Laurent Montel committed
124
#include "emaillineedit.moc"