1 /* This file is part of the KDE project
2    SPDX-FileCopyrightText: 2002 Daniel Molkentin <molkentin@kde.org>
3    SPDX-FileCopyrightText: 2013-2021 Laurent Montel <montel@kde.org>
4 
5    SPDX-License-Identifier: GPL-2.0-or-later
6  */
7 
8 #include "vcardviewer.h"
9 #include "settings/messageviewersettings.h"
10 #include <Akonadi/Contact/GrantleeContactViewer>
11 
12 #include <KContacts/VCardConverter>
13 using KContacts::Addressee;
14 using KContacts::VCardConverter;
15 
16 #include <Akonadi/Contact/ContactDefaultActions>
17 #include <KLocalizedString>
18 
19 #include <Akonadi/Contact/AddContactJob>
20 #include <KConfigGroup>
21 #include <KGuiItem>
22 #include <QDialogButtonBox>
23 #include <QPushButton>
24 #include <QVBoxLayout>
25 
26 using namespace MessageViewer;
27 namespace
28 {
29 static const char myVCardViewerConfigGroupName[] = "VCardViewer";
30 }
VCardViewer(QWidget * parent,const QByteArray & vCard)31 VCardViewer::VCardViewer(QWidget *parent, const QByteArray &vCard)
32     : QDialog(parent)
33     , mContactViewer(new KAddressBookGrantlee::GrantleeContactViewer(this))
34     , mUser2Button(new QPushButton)
35     , mUser3Button(new QPushButton)
36 {
37     setWindowTitle(i18nc("@title:window", "vCard Viewer"));
38     auto mainLayout = new QVBoxLayout(this);
39     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
40     auto user1Button = new QPushButton;
41     buttonBox->addButton(user1Button, QDialogButtonBox::ActionRole);
42     buttonBox->addButton(mUser3Button, QDialogButtonBox::ActionRole);
43     buttonBox->addButton(mUser2Button, QDialogButtonBox::ActionRole);
44     connect(buttonBox, &QDialogButtonBox::rejected, this, &VCardViewer::reject);
45     setModal(false);
46     buttonBox->button(QDialogButtonBox::Close)->setDefault(true);
47 
48     KGuiItem::assign(user1Button, KGuiItem(i18n("&Import")));
49     KGuiItem::assign(mUser2Button, KGuiItem(i18n("&Next Card")));
50     KGuiItem::assign(mUser3Button, KGuiItem(i18n("&Previous Card")));
51 
52     auto actions = new Akonadi::ContactDefaultActions(this);
53     actions->connectToView(mContactViewer);
54 
55     mContactViewer->setForceDisableQRCode(true);
56     mainLayout->addWidget(mContactViewer);
57     mainLayout->addWidget(buttonBox);
58 
59     VCardConverter vcc;
60     mAddresseeList = vcc.parseVCards(vCard);
61     if (!mAddresseeList.empty()) {
62         mContactViewer->setRawContact(mAddresseeList.at(0));
63         if (mAddresseeList.size() <= 1) {
64             mUser2Button->setVisible(false);
65             mUser3Button->setVisible(false);
66         } else {
67             mUser3Button->setEnabled(false);
68         }
69         connect(user1Button, &QPushButton::clicked, this, &VCardViewer::slotUser1);
70         connect(mUser2Button, &QPushButton::clicked, this, &VCardViewer::slotUser2);
71         connect(mUser3Button, &QPushButton::clicked, this, &VCardViewer::slotUser3);
72     } else {
73         mContactViewer->setRawContact(KContacts::Addressee());
74         user1Button->setEnabled(false);
75         mUser2Button->setVisible(false);
76         mUser3Button->setVisible(false);
77     }
78     readConfig();
79 }
80 
~VCardViewer()81 VCardViewer::~VCardViewer()
82 {
83     writeConfig();
84 }
85 
readConfig()86 void VCardViewer::readConfig()
87 {
88     KConfigGroup group(KSharedConfig::openStateConfig(), myVCardViewerConfigGroupName);
89     const QSize size = group.readEntry("Size", QSize(300, 400));
90     if (size.isValid()) {
91         resize(size);
92     }
93 }
94 
writeConfig()95 void VCardViewer::writeConfig()
96 {
97     KConfigGroup group(KSharedConfig::openStateConfig(), myVCardViewerConfigGroupName);
98     group.writeEntry("Size", size());
99     group.sync();
100 }
101 
slotUser1()102 void VCardViewer::slotUser1()
103 {
104     const KContacts::Addressee contact = mAddresseeList.at(mAddresseeListIndex);
105 
106     auto job = new Akonadi::AddContactJob(contact, this, this);
107     job->start();
108 }
109 
slotUser2()110 void VCardViewer::slotUser2()
111 {
112     // next vcard
113     mContactViewer->setRawContact(mAddresseeList.at(++mAddresseeListIndex));
114     if ((mAddresseeListIndex + 1) == (mAddresseeList.count())) {
115         mUser2Button->setEnabled(false);
116     }
117     mUser3Button->setEnabled(true);
118 }
119 
slotUser3()120 void VCardViewer::slotUser3()
121 {
122     // previous vcard
123     mContactViewer->setRawContact(mAddresseeList.at(--mAddresseeListIndex));
124     if (mAddresseeListIndex == 0) {
125         mUser3Button->setEnabled(false);
126     }
127     mUser2Button->setEnabled(true);
128 }
129