Messagelib

vcardviewer.cpp
1 /* This file is part of the KDE project
2  SPDX-FileCopyrightText: 2002 Daniel Molkentin <[email protected]>
3  SPDX-FileCopyrightText: 2013-2023 Laurent Montel <[email protected]>
4 
5  SPDX-License-Identifier: GPL-2.0-or-later
6  */
7 
8 #include "vcardviewer.h"
9 #include "settings/messageviewersettings.h"
10 #include <Akonadi/GrantleeContactViewer>
11 
12 #include <KContacts/VCardConverter>
15 
16 #include <Akonadi/ContactDefaultActions>
17 #include <KLocalizedString>
18 
19 #include <Akonadi/AddContactJob>
20 #include <KConfigGroup>
21 #include <KGuiItem>
22 #include <KWindowConfig>
23 #include <QDialogButtonBox>
24 #include <QPushButton>
25 #include <QVBoxLayout>
26 #include <QWindow>
27 
28 using namespace MessageViewer;
29 namespace
30 {
31 static const char myVCardViewerConfigGroupName[] = "VCardViewer";
32 }
33 VCardViewer::VCardViewer(QWidget *parent, const QByteArray &vCard)
34  : QDialog(parent)
35  , mContactViewer(new KAddressBookGrantlee::GrantleeContactViewer(this))
36  , mUser2Button(new QPushButton)
37  , mUser3Button(new QPushButton)
38 {
39  setWindowTitle(i18nc("@title:window", "vCard Viewer"));
40  auto mainLayout = new QVBoxLayout(this);
41  auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
42  auto user1Button = new QPushButton;
43  buttonBox->addButton(user1Button, QDialogButtonBox::ActionRole);
44  buttonBox->addButton(mUser3Button, QDialogButtonBox::ActionRole);
45  buttonBox->addButton(mUser2Button, QDialogButtonBox::ActionRole);
46  connect(buttonBox, &QDialogButtonBox::rejected, this, &VCardViewer::reject);
47  setModal(false);
48  buttonBox->button(QDialogButtonBox::Close)->setDefault(true);
49 
50  KGuiItem::assign(user1Button, KGuiItem(i18n("&Import")));
51  KGuiItem::assign(mUser2Button, KGuiItem(i18n("&Next Card")));
52  KGuiItem::assign(mUser3Button, KGuiItem(i18n("&Previous Card")));
53 
54  auto actions = new Akonadi::ContactDefaultActions(this);
55  actions->connectToView(mContactViewer);
56 
57  mContactViewer->setForceDisableQRCode(true);
58  mainLayout->addWidget(mContactViewer);
59  mainLayout->addWidget(buttonBox);
60 
61  VCardConverter vcc;
62  mAddresseeList = vcc.parseVCards(vCard);
63  if (!mAddresseeList.empty()) {
64  mContactViewer->setRawContact(mAddresseeList.at(0));
65  if (mAddresseeList.size() <= 1) {
66  mUser2Button->setVisible(false);
67  mUser3Button->setVisible(false);
68  } else {
69  mUser3Button->setEnabled(false);
70  }
71  connect(user1Button, &QPushButton::clicked, this, &VCardViewer::slotUser1);
72  connect(mUser2Button, &QPushButton::clicked, this, &VCardViewer::slotUser2);
73  connect(mUser3Button, &QPushButton::clicked, this, &VCardViewer::slotUser3);
74  } else {
75  mContactViewer->setRawContact(KContacts::Addressee());
76  user1Button->setEnabled(false);
77  mUser2Button->setVisible(false);
78  mUser3Button->setVisible(false);
79  }
80  readConfig();
81 }
82 
83 VCardViewer::~VCardViewer()
84 {
85  writeConfig();
86 }
87 
88 void VCardViewer::readConfig()
89 {
90  create(); // ensure a window is created
91  windowHandle()->resize(QSize(300, 400));
92  KConfigGroup group(KSharedConfig::openStateConfig(), myVCardViewerConfigGroupName);
93  KWindowConfig::restoreWindowSize(windowHandle(), group);
94  resize(windowHandle()->size()); // workaround for QTBUG-40584
95 }
96 
97 void VCardViewer::writeConfig()
98 {
99  KConfigGroup group(KSharedConfig::openStateConfig(), myVCardViewerConfigGroupName);
100  KWindowConfig::saveWindowSize(windowHandle(), group);
101 }
102 
103 void VCardViewer::slotUser1()
104 {
105  const KContacts::Addressee contact = mAddresseeList.at(mAddresseeListIndex);
106 
107  auto job = new Akonadi::AddContactJob(contact, this, this);
108  job->start();
109 }
110 
111 void VCardViewer::slotUser2()
112 {
113  // next vcard
114  mContactViewer->setRawContact(mAddresseeList.at(++mAddresseeListIndex));
115  if ((mAddresseeListIndex + 1) == (mAddresseeList.count())) {
116  mUser2Button->setEnabled(false);
117  }
118  mUser3Button->setEnabled(true);
119 }
120 
121 void VCardViewer::slotUser3()
122 {
123  // previous vcard
124  mContactViewer->setRawContact(mAddresseeList.at(--mAddresseeListIndex));
125  if (mAddresseeListIndex == 0) {
126  mUser3Button->setEnabled(false);
127  }
128  mUser2Button->setEnabled(true);
129 }
void clicked(bool checked)
void readConfig()
Addressee::List parseVCards(const QByteArray &vcard) const
static void assign(QPushButton *button, const KGuiItem &item)
QString i18n(const char *text, const TYPE &arg...)
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
QAction * create(StandardGameAction id, const QObject *recvr, const char *slot, QObject *parent)
KCONFIGGUI_EXPORT void restoreWindowSize(QWindow *window, const KConfigGroup &config)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
KCONFIGGUI_EXPORT void saveWindowSize(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options=KConfigGroup::Normal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Mar 26 2023 04:08:12 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.