contactgroupeditordialog.cpp 5.5 KB
Newer Older
1 2 3 4 5
/*
    This file is part of Akonadi Contact.

    Copyright (c) 2007-2009 Tobias Koenig <tokoe@kde.org>

Tobias Koenig's avatar
Tobias Koenig committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19
    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.
20 21 22 23
*/

#include "contactgroupeditordialog.h"

Tobias Koenig's avatar
Tobias Koenig committed
24
#include "contactgroupeditor.h"
Tobias Koenig's avatar
Tobias Koenig committed
25
#include "contactgroupeditor_p.h"
Tobias Koenig's avatar
Tobias Koenig committed
26

27 28
#include <collectioncombobox.h>
#include <item.h>
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
29
#include <kcontacts/contactgroup.h>
Laurent Montel's avatar
Laurent Montel committed
30
#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
31

Laurent Montel's avatar
Laurent Montel committed
32
#include <KConfig>
33

Laurent Montel's avatar
Laurent Montel committed
34
#include <QPushButton>
35 36
#include <QGridLayout>
#include <QLabel>
Laurent Montel's avatar
Laurent Montel committed
37 38 39
#include <QDialogButtonBox>
#include <KConfigGroup>
#include <QVBoxLayout>
40 41 42

using namespace Akonadi;

43
class Q_DECL_HIDDEN ContactGroupEditorDialog::Private
Tobias Koenig's avatar
Tobias Koenig committed
44
{
Guy Maurel's avatar
Guy Maurel committed
45 46 47
public:
    Private(ContactGroupEditorDialog *qq, ContactGroupEditorDialog::Mode mode)
        : q(qq)
Laurent Montel's avatar
Laurent Montel committed
48 49
        , mAddressBookBox(nullptr)
        , mEditor(nullptr)
Guy Maurel's avatar
Guy Maurel committed
50
        , mMode(mode)
Laurent Montel's avatar
Laurent Montel committed
51
        , okButton(nullptr)
52 53 54
    {
    }

Guy Maurel's avatar
Guy Maurel committed
55
    void slotGroupNameChanged(const QString &name)
Tobias Koenig's avatar
Tobias Koenig committed
56
    {
Guy Maurel's avatar
Guy Maurel committed
57
        bool isValid = !(name.contains(QLatin1Char('@')) || name.contains(QLatin1Char('.')));
58
        okButton->setEnabled(!name.trimmed().isEmpty() && isValid);
Guy Maurel's avatar
Guy Maurel committed
59
        mEditor->groupNameIsValid(isValid);
Tobias Koenig's avatar
Tobias Koenig committed
60 61
    }

Laurent Montel's avatar
Laurent Montel committed
62 63
    void readConfig()
    {
Laurent Montel's avatar
Laurent Montel committed
64 65
        KConfig config(QStringLiteral("akonadi_contactrc"));
        KConfigGroup group(&config, QStringLiteral("ContactGroupEditorDialog"));
Guy Maurel's avatar
Guy Maurel committed
66 67 68 69
        const QSize size = group.readEntry("Size", QSize(470, 400));
        if (size.isValid()) {
            q->resize(size);
        }
Laurent Montel's avatar
Laurent Montel committed
70 71 72
    }
    void writeConfig()
    {
Laurent Montel's avatar
Laurent Montel committed
73 74
        KConfig config(QStringLiteral("akonadi_contactrc"));
        KConfigGroup group(&config, QStringLiteral("ContactGroupEditorDialog"));
Guy Maurel's avatar
Guy Maurel committed
75 76
        group.writeEntry("Size", q->size());
        group.sync();
Laurent Montel's avatar
Laurent Montel committed
77
    }
Tobias Koenig's avatar
Tobias Koenig committed
78
    ContactGroupEditorDialog *q;
79
    CollectionComboBox *mAddressBookBox;
Tobias Koenig's avatar
Tobias Koenig committed
80
    ContactGroupEditor *mEditor;
81
    ContactGroupEditorDialog::Mode mMode;
Laurent Montel's avatar
Laurent Montel committed
82
    QPushButton *okButton;
Tobias Koenig's avatar
Tobias Koenig committed
83 84
};

Guy Maurel's avatar
Guy Maurel committed
85
ContactGroupEditorDialog::ContactGroupEditorDialog(Mode mode, QWidget *parent)
Guy Maurel's avatar
Guy Maurel committed
86 87
    : QDialog(parent)
    , d(new Private(this, mode))
88
{
Guy Maurel's avatar
Guy Maurel committed
89
    setWindowTitle(mode == CreateMode ? i18n("New Contact Group") : i18n("Edit Contact Group"));
Laurent Montel's avatar
Laurent Montel committed
90 91
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

Laurent Montel's avatar
Laurent Montel committed
92
    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
Guy Maurel's avatar
Guy Maurel committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106
    d->okButton = buttonBox->button(QDialogButtonBox::Ok);
    d->okButton->setDefault(true);
    d->okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
    connect(buttonBox, &QDialogButtonBox::accepted, this, &ContactGroupEditorDialog::slotAccepted);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &ContactGroupEditorDialog::reject);

    // Disable default button, so that finish editing of
    // a member with the Enter key does not close the dialog
    d->okButton->setAutoDefault(false);
    buttonBox->button(QDialogButtonBox::Cancel)->setAutoDefault(false);

    QWidget *mainWidget = new QWidget(this);

    QGridLayout *layout = new QGridLayout(mainWidget);
Laurent Montel's avatar
Laurent Montel committed
107
    layout->setMargin(0);
Guy Maurel's avatar
Guy Maurel committed
108 109

    d->mEditor = new Akonadi::ContactGroupEditor(mode == CreateMode ?
Laurent Montel's avatar
Laurent Montel committed
110 111
            Akonadi::ContactGroupEditor::CreateMode : Akonadi::ContactGroupEditor::EditMode,
            this);
Guy Maurel's avatar
Guy Maurel committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

    if (mode == CreateMode) {
        QLabel *label = new QLabel(i18n("Add to:"), mainWidget);

        d->mAddressBookBox = new CollectionComboBox(mainWidget);
        d->mAddressBookBox->setMimeTypeFilter(QStringList() << KContacts::ContactGroup::mimeType());
        d->mAddressBookBox->setAccessRightsFilter(Collection::CanCreateItem);

        layout->addWidget(label, 0, 0);
        layout->addWidget(d->mAddressBookBox, 0, 1);
    }

    layout->addWidget(d->mEditor, 1, 0, 1, 2);
    layout->setColumnStretch(1, 1);

    connect(d->mEditor, &ContactGroupEditor::contactGroupStored, this, &ContactGroupEditorDialog::contactGroupStored);
    connect(d->mEditor->d->mGui.groupName, SIGNAL(textChanged(QString)),
            this, SLOT(slotGroupNameChanged(QString)));

131
    d->okButton->setEnabled(!d->mEditor->d->mGui.groupName->text().trimmed().isEmpty());
Guy Maurel's avatar
Guy Maurel committed
132 133 134
    mainLayout->addWidget(mainWidget);
    mainLayout->addWidget(buttonBox);
    d->readConfig();
135 136 137 138
}

ContactGroupEditorDialog::~ContactGroupEditorDialog()
{
Guy Maurel's avatar
Guy Maurel committed
139 140
    d->writeConfig();
    delete d;
141 142
}

Guy Maurel's avatar
Guy Maurel committed
143
void ContactGroupEditorDialog::setContactGroup(const Akonadi::Item &group)
144
{
Guy Maurel's avatar
Guy Maurel committed
145
    d->mEditor->loadContactGroup(group);
146 147
}

Guy Maurel's avatar
Guy Maurel committed
148
void ContactGroupEditorDialog::setDefaultAddressBook(const Akonadi::Collection &addressbook)
149
{
Guy Maurel's avatar
Guy Maurel committed
150 151 152
    if (d->mMode == EditMode) {
        return;
    }
153

Guy Maurel's avatar
Guy Maurel committed
154
    d->mAddressBookBox->setDefaultCollection(addressbook);
155 156
}

Guy Maurel's avatar
Guy Maurel committed
157
ContactGroupEditor *ContactGroupEditorDialog::editor() const
158
{
Guy Maurel's avatar
Guy Maurel committed
159
    return d->mEditor;
160 161
}

Laurent Montel's avatar
Laurent Montel committed
162
void ContactGroupEditorDialog::slotAccepted()
163
{
Guy Maurel's avatar
Guy Maurel committed
164 165
    if (d->mAddressBookBox) {
        d->mEditor->setDefaultAddressBook(d->mAddressBookBox->currentCollection());
166
    }
167

Guy Maurel's avatar
Guy Maurel committed
168 169
    if (d->mEditor->saveContactGroup()) {
        accept();
170
    }
171 172
}

173
#include "moc_contactgroupeditordialog.cpp"