Akonadi Contacts

contactgroupviewer.cpp
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 "contactgroupviewer.h"
10
11#include "job/contactgroupexpandjob.h"
12#include "standardcontactgroupformatter.h"
13#include "textbrowser_p.h"
14
15#include <Akonadi/CollectionFetchJob>
16#include <Akonadi/EntityDisplayAttribute>
17#include <Akonadi/Item>
18#include <Akonadi/ItemFetchScope>
19#include <KContacts/Addressee>
20#include <KContacts/ContactGroup>
21
22#include <KLocalizedString>
23
24#include <QIcon>
25#include <QVBoxLayout>
26
27using namespace Akonadi;
28using namespace Akonadi;
29
30class Akonadi::ContactGroupViewerPrivate
31{
32public:
33 explicit ContactGroupViewerPrivate(ContactGroupViewer *parent)
34 : mParent(parent)
35 {
36 mBrowser = new Akonadi::TextBrowser;
37
38 static QPixmap groupPixmap = QIcon::fromTheme(QStringLiteral("x-mail-distribution-list")).pixmap(QSize(100, 100));
39 mBrowser->document()->addResource(QTextDocument::ImageResource, QUrl(QStringLiteral("group_photo")), groupPixmap);
40
41 mStandardContactGroupFormatter = new StandardContactGroupFormatter;
42 mContactGroupFormatter = mStandardContactGroupFormatter;
43 }
44
45 ~ContactGroupViewerPrivate()
46 {
47 delete mStandardContactGroupFormatter;
48 }
49
50 void updateView()
51 {
52 mParent->setWindowTitle(i18nc("@title:window", "Contact Group %1", mCurrentGroupName));
53
55 group.setName(mCurrentGroupName);
56 for (const KContacts::Addressee &contact : std::as_const(mCurrentContacts)) {
57 group.append(KContacts::ContactGroup::Data(contact.realName(), contact.preferredEmail()));
58 }
59
60 mContactGroupFormatter->setContactGroup(group);
61
62 QList<QVariantMap> additionalFields;
63
64 if (!mCurrentAddressBookName.isEmpty()) {
65 QVariantMap addressBookName;
66 addressBookName.insert(QStringLiteral("title"), i18n("Address Book"));
67 addressBookName.insert(QStringLiteral("value"), mCurrentAddressBookName);
68
69 additionalFields << addressBookName;
70 }
71
72 mContactGroupFormatter->setAdditionalFields(additionalFields);
73
74 mBrowser->setHtml(mContactGroupFormatter->toHtml());
75 }
76
77 void _k_expandResult(KJob *job)
78 {
79 mExpandJob = nullptr;
80
81 if (!job->error()) {
82 auto expandJob = qobject_cast<ContactGroupExpandJob *>(job);
83 mCurrentContacts = expandJob->contacts();
84 }
85
86 // stop any running fetch job
87 if (mParentCollectionFetchJob) {
88 mParent->disconnect(mCollectionFetchJobConnection);
89 delete mParentCollectionFetchJob;
90 mParentCollectionFetchJob = nullptr;
91 }
92
93 mParentCollectionFetchJob = new CollectionFetchJob(mCurrentItem.parentCollection(), CollectionFetchJob::Base, mParent);
94 mCollectionFetchJobConnection = mParent->connect(mParentCollectionFetchJob, &CollectionFetchJob::result, mParent, [this](KJob *job) {
95 slotParentCollectionFetched(job);
96 });
97 }
98
99 void slotParentCollectionFetched(KJob *job)
100 {
101 mParentCollectionFetchJob = nullptr;
102 mCurrentAddressBookName.clear();
103
104 if (!job->error()) {
105 auto fetchJob = qobject_cast<CollectionFetchJob *>(job);
106 if (!fetchJob->collections().isEmpty()) {
107 const Collection collection = fetchJob->collections().at(0);
108 mCurrentAddressBookName = collection.displayName();
109 }
110 }
111
112 updateView();
113 }
114
115 QMetaObject::Connection mCollectionFetchJobConnection;
116 QMetaObject::Connection mJobConnection;
117 ContactGroupViewer *const mParent;
118 Akonadi::TextBrowser *mBrowser = nullptr;
119 QString mCurrentGroupName;
120 KContacts::Addressee::List mCurrentContacts;
121 QString mCurrentAddressBookName;
122 Item mCurrentItem;
123 ContactGroupExpandJob *mExpandJob = nullptr;
124 CollectionFetchJob *mParentCollectionFetchJob = nullptr;
125 AbstractContactGroupFormatter *mStandardContactGroupFormatter = nullptr;
126 AbstractContactGroupFormatter *mContactGroupFormatter = nullptr;
127};
128
130 : QWidget(parent)
131 , d(new ContactGroupViewerPrivate(this))
132{
133 auto layout = new QVBoxLayout(this);
135
136 connect(d->mBrowser, &Akonadi::TextBrowser::anchorClicked, this, &ContactGroupViewer::urlClicked);
137
138 layout->addWidget(d->mBrowser);
139
140 // always fetch full payload for contact groups
143}
144
146
151
156
158{
159 if (formatter == nullptr) {
160 d->mContactGroupFormatter = d->mStandardContactGroupFormatter;
161 } else {
162 d->mContactGroupFormatter = formatter;
163 }
164}
165
167{
168 itemChanged(d->mCurrentItem);
169}
170
171void ContactGroupViewer::itemChanged(const Item &item)
172{
174 return;
175 }
176
177 const auto group = item.payload<KContacts::ContactGroup>();
178 d->mCurrentGroupName = group.name();
179 d->mCurrentItem = item;
180
181 if (d->mExpandJob) {
182 disconnect(d->mJobConnection);
183 d->mExpandJob->kill();
184 }
185
186 d->mExpandJob = new ContactGroupExpandJob(group);
187 d->mJobConnection = connect(d->mExpandJob, &ContactGroupExpandJob::result, this, [this](KJob *job) {
188 d->_k_expandResult(job);
189 });
190 d->mExpandJob->start();
191}
192
193void ContactGroupViewer::itemRemoved()
194{
195 d->mBrowser->clear();
196}
197
198#include "moc_contactgroupviewer.cpp"
The interface for all contact group formatters.
virtual QString toHtml(HtmlForm form=SelfcontainedForm) const =0
This method must be reimplemented to return the contact group formatted as HTML according to the requ...
void setContactGroup(const KContacts::ContactGroup &group)
Sets the contact group that will be formatted.
void setAdditionalFields(const QList< QVariantMap > &fields)
Sets the additional fields that will be shown.
QString displayName() const
Job that expands a ContactGroup to a list of contacts.
A viewer component for contact groups in Akonadi.
void setContactGroup(const Akonadi::Item &group)
Sets the contact group that shall be displayed in the viewer.
void setContactGroupFormatter(Akonadi::AbstractContactGroupFormatter *formatter)
Sets the contact group formatter that should be used for formatting the contact group.
void urlClicked(const QUrl &url)
This signal is emitted whenever the user has clicked on a URL in the viewer (e.g.
~ContactGroupViewer() override
Destroys the contact group viewer.
ContactGroupViewer(QWidget *parent=nullptr)
Creates a new contact group viewer.
Akonadi::Item contactGroup() const
Returns the contact group that is currently displayed.
void setAncestorRetrieval(AncestorRetrieval ancestorDepth)
void fetchFullPayload(bool fetch=true)
void setItem(const Item &item)
ItemFetchScope & fetchScope()
T payload() const
Collection & parentCollection()
bool hasPayload() const
A class that formats a contact group as HTML code.
AddresseeList List
void setName(const QString &name)
void append(const ContactGroupReference &reference)
QString name() const
int error() const
void result(KJob *job)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
A widget for editing the display name of a contact.
QPixmap pixmap(QWindow *window, const QSize &size, Mode mode, State state) const const
QIcon fromTheme(const QString &name)
void addWidget(QWidget *w)
void setContentsMargins(const QMargins &margins)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
void clear()
bool isEmpty() const const
QLayout * layout() const const
void setWindowTitle(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:20 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.