mailwidgetlister.cpp 3.9 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1 2 3 4
/*
    This file is part of Akonadi Contact.

    Copyright (C) 2016 eyeOS S.L.U., a Telefonica company, sales@eyeos.com
Laurent Montel's avatar
Laurent Montel committed
5
    Copyright (C) 2016-2017 Laurent Montel <laurent.montel@kdab.com>
Laurent Montel's avatar
Laurent Montel committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

    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 "mailwidgetlister.h"
#include "mailwidget.h"
Laurent Montel's avatar
Laurent Montel committed
25 26

#include <KContacts/Addressee>
Laurent Montel's avatar
Laurent Montel committed
27 28 29
using namespace Akonadi;

MailWidgetLister::MailWidgetLister(QWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
30
    : KWidgetLister(1, 8, parent)
Laurent Montel's avatar
Laurent Montel committed
31 32 33 34 35 36 37 38 39 40
{
    setNumberOfShownWidgetsTo(widgetsMinimum());
    updateAddRemoveButton();
}

MailWidgetLister::~MailWidgetLister()
{

}

41 42
void MailWidgetLister::loadContact(const KContacts::Addressee &contact)
{
Laurent Montel's avatar
Laurent Montel committed
43
    KContacts::Email::List mailList = contact.emailList();
Laurent Montel's avatar
Laurent Montel committed
44 45 46 47
    if (mailList.isEmpty()) {
        setNumberOfShownWidgetsTo(1);
    } else {
        setNumberOfShownWidgetsTo(mailList.count());
48 49 50
        const QList<QWidget *> widgetList = widgets();
        auto wIt = widgetList.constBegin();
        auto wEnd = widgetList.constEnd();
Laurent Montel's avatar
Laurent Montel committed
51 52 53 54
        for (int i = 0; wIt != wEnd; ++wIt, ++i) {
            MailWidget *w = qobject_cast<MailWidget *>(*wIt);
            w->setMail(mailList.at(i));
        }
Laurent Montel's avatar
Laurent Montel committed
55
    }
56 57 58 59
}

void MailWidgetLister::storeContact(KContacts::Addressee &contact) const
{
60
    const QList<QWidget *> widgetList = widgets();
Laurent Montel's avatar
Laurent Montel committed
61
    KContacts::Email::List emailList;
62 63
    for (QWidget *widget : widgetList) {
        MailWidget *w = qobject_cast<MailWidget *>(widget);
Laurent Montel's avatar
Laurent Montel committed
64 65 66 67 68 69
        KContacts::Email newEmail = w->email();
        if (newEmail.isValid()) {
            emailList << newEmail;
        }
    }
    contact.setEmailList(emailList);
70 71
}

Laurent Montel's avatar
Laurent Montel committed
72 73 74 75 76 77 78 79 80 81 82
QWidget *MailWidgetLister::createWidget(QWidget *parent)
{
    MailWidget *w = new MailWidget(parent);
    reconnectWidget(w);
    return w;
}

void MailWidgetLister::reconnectWidget(MailWidget *w)
{
    connect(w, &MailWidget::addWidget, this, &MailWidgetLister::slotAddWidget, Qt::UniqueConnection);
    connect(w, &MailWidget::removeWidget, this, &MailWidgetLister::slotRemoveWidget, Qt::UniqueConnection);
Laurent Montel's avatar
Laurent Montel committed
83
    connect(w, &MailWidget::preferredChanged, this, &MailWidgetLister::slotPreferredChanged, Qt::UniqueConnection);
Laurent Montel's avatar
Laurent Montel committed
84 85
}

Laurent Montel's avatar
Laurent Montel committed
86
void MailWidgetLister::slotAddWidget(MailWidget *w)
Laurent Montel's avatar
Laurent Montel committed
87 88 89 90 91
{
    addWidgetAfterThisWidget(w);
    updateAddRemoveButton();
}

Laurent Montel's avatar
Laurent Montel committed
92
void MailWidgetLister::slotRemoveWidget(MailWidget *w)
Laurent Montel's avatar
Laurent Montel committed
93
{
Laurent Montel's avatar
Laurent Montel committed
94 95 96 97 98 99
    if (widgets().count() == 1) {
        w->clearWidget();
    } else {
        removeWidget(w);
        updateAddRemoveButton();
    }
Laurent Montel's avatar
Laurent Montel committed
100 101
}

Laurent Montel's avatar
Laurent Montel committed
102
void MailWidgetLister::slotPreferredChanged(MailWidget *w)
103 104 105 106
{
    const QList<QWidget *> widgetList = widgets();
    for (QWidget *widget : widgetList) {
        if (widget != w) {
Laurent Montel's avatar
Laurent Montel committed
107
            (static_cast<MailWidget *>(widget))->setPreferred(false);
108 109 110 111
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
112 113
void MailWidgetLister::updateAddRemoveButton()
{
114
    const QList<QWidget *> widgetList = widgets();
Laurent Montel's avatar
Laurent Montel committed
115 116 117 118 119 120 121 122 123
    const int numberOfWidget(widgetList.count());
    bool addButtonEnabled = false;
    if (numberOfWidget <= widgetsMinimum()) {
        addButtonEnabled = true;
    } else if (numberOfWidget >= widgetsMaximum()) {
        addButtonEnabled = false;
    } else {
        addButtonEnabled = true;
    }
124 125 126

    for (QWidget *widget : widgetList) {
        MailWidget *w = qobject_cast<MailWidget *>(widget);
Laurent Montel's avatar
Laurent Montel committed
127
        w->updateAddRemoveButton(addButtonEnabled);
Laurent Montel's avatar
Laurent Montel committed
128 129
    }
}