1 /*
2 This file is part of Akonadi Contact.
3
4 SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8
9 #include "contactviewerdialog.h"
10 #include "contactdefaultactions.h"
11 #include "contactviewer.h"
12
13 #include <Akonadi/Item>
14 using namespace Akonadi;
15
16 #include <KConfig>
17 #include <KLocalizedString>
18
19 #include <KConfigGroup>
20 #include <QDialogButtonBox>
21 #include <QPushButton>
22 #include <QVBoxLayout>
23
24 class Akonadi::ContactViewerDialogPrivate
25 {
26 public:
ContactViewerDialogPrivate(ContactViewerDialog * parent)27 explicit ContactViewerDialogPrivate(ContactViewerDialog *parent)
28 : q(parent)
29 {
30 }
31
readConfig()32 void readConfig()
33 {
34 KConfig config(QStringLiteral("akonadi_contactrc"));
35 KConfigGroup group(&config, QStringLiteral("ContactViewer"));
36 const QSize size = group.readEntry("Size", QSize(500, 600));
37 if (size.isValid()) {
38 q->resize(size);
39 }
40 }
41
writeConfig()42 void writeConfig()
43 {
44 KConfig config(QStringLiteral("akonadi_contactrc"));
45 KConfigGroup group(&config, QStringLiteral("ContactViewer"));
46 group.writeEntry("Size", q->size());
47 group.sync();
48 }
49
50 ContactViewerDialog *const q;
51 ContactViewer *mViewer = nullptr;
52 };
53
ContactViewerDialog(QWidget * parent)54 ContactViewerDialog::ContactViewerDialog(QWidget *parent)
55 : QDialog(parent)
56 , d(new ContactViewerDialogPrivate(this))
57 {
58 setWindowTitle(i18nc("@title:window", "Show Contact"));
59 auto mainLayout = new QVBoxLayout(this);
60
61 auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok, this);
62 auto mainWidget = new QWidget(this);
63 mainLayout->addWidget(mainWidget);
64 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
65 okButton->setDefault(true);
66 okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
67 connect(buttonBox, &QDialogButtonBox::accepted, this, &ContactViewerDialog::accept);
68 connect(buttonBox, &QDialogButtonBox::rejected, this, &ContactViewerDialog::reject);
69
70 auto layout = new QVBoxLayout(mainWidget);
71
72 d->mViewer = new ContactViewer(this);
73 layout->addWidget(d->mViewer);
74
75 auto actions = new ContactDefaultActions(this);
76 actions->connectToView(d->mViewer);
77
78 mainLayout->addWidget(buttonBox);
79
80 d->readConfig();
81 }
82
~ContactViewerDialog()83 ContactViewerDialog::~ContactViewerDialog()
84 {
85 d->writeConfig();
86 }
87
contact() const88 Akonadi::Item ContactViewerDialog::contact() const
89 {
90 return d->mViewer->contact();
91 }
92
viewer() const93 ContactViewer *ContactViewerDialog::viewer() const
94 {
95 return d->mViewer;
96 }
97
setContact(const Akonadi::Item & contact)98 void ContactViewerDialog::setContact(const Akonadi::Item &contact)
99 {
100 d->mViewer->setContact(contact);
101 }
102