recipientseditorsidewidget.cpp 5.59 KB
Newer Older
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
/*
    Copyright (C) 2010 Casey Link <unnamedrambler@gmail.com>
    Copyright (C) 2009-2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>

    Refactored from earlier code by:
    Copyright (c) 2010 Volker Krause <vkrause@kde.org>
    Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org>

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

    This library 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 Library General Public
    License for more details.

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

#include "recipientseditorsidewidget.h"

#include "recipientspicker.h"
#include "kwindowpositioner.h"

#include <QDialog>
#include <KLocalizedString>
#include <QPushButton>

#include <QLabel>
#include <QBoxLayout>

using namespace MessageComposer;

RecipientsEditorSideWidget::RecipientsEditorSideWidget(RecipientsEditor *view, QWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
40
    : QWidget(parent), mEditor(view), mRecipientPicker(nullptr), mPickerPositioner(nullptr)
41 42 43 44 45 46 47 48
{
    QBoxLayout *topLayout = new QVBoxLayout(this);

    topLayout->setMargin(0);
    topLayout->addStretch(1);

    mTotalLabel = new QLabel(this);
    mTotalLabel->setAlignment(Qt::AlignCenter);
Laurent Montel's avatar
Laurent Montel committed
49
    mTotalLabel->setTextFormat(Qt::PlainText);
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
    topLayout->addWidget(mTotalLabel);
    mTotalLabel->hide();

    topLayout->addStretch(1);

    mDistributionListButton = new QPushButton(
        i18nc("@action:button", "Save List..."), this);
    topLayout->addWidget(mDistributionListButton);
    mDistributionListButton->hide();
    connect(mDistributionListButton, &QAbstractButton::clicked,
            this, &RecipientsEditorSideWidget::saveDistributionList);
    mDistributionListButton->setToolTip(
        i18nc("@info:tooltip", "Save recipients as distribution list"));

    mSelectButton = new QPushButton(
        i18nc("@action:button Open recipient selection dialog.", "Se&lect..."), this);
    topLayout->addWidget(mSelectButton);
    connect(mSelectButton, &QPushButton::clicked, this, &RecipientsEditorSideWidget::pickRecipient);
    mSelectButton->setToolTip(i18nc("@info:tooltip", "Select recipients from address book"));
    updateTotalToolTip();
}

RecipientsEditorSideWidget::~RecipientsEditorSideWidget()
{
}

RecipientsPicker *RecipientsEditorSideWidget::picker() const
{
    if (!mRecipientPicker) {
        // hacks to allow picker() to be const in the presence of lazy loading
        RecipientsEditorSideWidget *non_const_this = const_cast<RecipientsEditorSideWidget *>(this);
        mRecipientPicker = new RecipientsPicker(non_const_this);
        connect(mRecipientPicker, &RecipientsPicker::pickedRecipient,
                non_const_this, &RecipientsEditorSideWidget::pickedRecipient);
        mPickerPositioner = new KWindowPositioner(mSelectButton, mRecipientPicker);
    }
    return mRecipientPicker;
}

void RecipientsEditorSideWidget::setFocus()
{
    mSelectButton->setFocus();
}

void RecipientsEditorSideWidget::setTotal(int recipients, int lines)
{
    QString labelText;
    if (recipients == 0) labelText = i18nc("@info:status No recipients selected"
                                               , "No recipients");
    else labelText = i18ncp("@info:status Number of recipients selected"
                                , "1 recipient", "%1 recipients", recipients);

    if (lines > 3) {
Laurent Montel's avatar
Laurent Montel committed
103
        mTotalLabel->setText(labelText);
104
        mTotalLabel->show();
Laurent Montel's avatar
Laurent Montel committed
105
        updateTotalToolTip();
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    } else {
        mTotalLabel->hide();
    }

    if (lines > 2) {
        mDistributionListButton->show();
    } else {
        mDistributionListButton->hide();
    }

}

void RecipientsEditorSideWidget::updateTotalToolTip()
{
    QString text = QStringLiteral("<qt>");

    QString to;
    QString cc;
    QString bcc;

    Recipient::List recipients = mEditor->recipients();
    Recipient::List::ConstIterator it;
    Recipient::List::ConstIterator end(recipients.constEnd());
Laurent Montel's avatar
Laurent Montel committed
129
    for (it = recipients.constBegin(); it != end; ++it) {
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
        QString emailLine = QLatin1String("&nbsp;&nbsp;") + (*it)->email().toHtmlEscaped() + QLatin1String("<br/>");
        switch ((*it)->type()) {
        case Recipient::To:
            to += emailLine;
            break;
        case Recipient::Cc:
            cc += emailLine;
            break;
        case Recipient::Bcc:
            bcc += emailLine;
            break;
        default:
            break;
        }
    }

    text += xi18nc("@info:tooltip %1 list of emails", "<interface>To:</interface><nl/>%1", to);
    if (!cc.isEmpty()) {
        text += xi18nc("@info:tooltip %1 list of emails", "<interface>CC:</interface><nl/>%1", cc);
    }
    if (!bcc.isEmpty()) {
        text += xi18nc("@info:tooltip %1 list of emails", "<interface>BCC:</interface><nl/>%1", bcc);
    }

    text.append(QLatin1String("</qt>"));
    mTotalLabel->setToolTip(text);
}

void RecipientsEditorSideWidget::pickRecipient()
{
    MessageComposer::RecipientsPicker *p = picker();
    Recipient::Ptr rec = mEditor->activeRecipient();
    if (rec) {
        p->setDefaultType(rec->type());
        p->setRecipients(mEditor->recipients());
        mPickerPositioner->reposition();
        p->show();
    }
}