Mailcommon

collectiongeneralpage.cpp
1/*
2 SPDX-FileCopyrightText: 2009-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "collectiongeneralpage.h"
8#include "collectiongeneralwidget.h"
9#include <KStatefulBrush>
10#include <PimCommonAkonadi/CollectionTypeUtil>
11
12#include "folder/foldersettings.h"
13#include "kernel/mailkernel.h"
14#include <Akonadi/CollectionAnnotationsAttribute>
15#include <PimCommonAkonadi/ContentTypeWidget>
16#include <PimCommonAkonadi/IncidencesForWidget>
17
18#include <Akonadi/AgentManager>
19#include <Akonadi/AttributeFactory>
20#include <Akonadi/Collection>
21#include <Akonadi/CollectionModifyJob>
22#include <Akonadi/EntityDisplayAttribute>
23#include <PimCommon/PimUtil>
24
25#include <KLineEditEventHandler>
26#include <PimCommonAkonadi/ImapResourceCapabilitiesManager>
27
28#include <KColorScheme>
29#include <KLocalizedString>
30#include <KMessageBox>
31
32#include <QCheckBox>
33#include <QFormLayout>
34#include <QLineEdit>
35#include <QVBoxLayout>
36
37using namespace Akonadi;
38using namespace MailCommon;
39
40CollectionGeneralPage::CollectionGeneralPage(QWidget *parent)
42{
43 setObjectName(QLatin1StringView("MailCommon::CollectionGeneralPage"));
44 setPageTitle(i18nc("@title:tab General settings for a folder.", "General"));
45}
46
47CollectionGeneralPage::~CollectionGeneralPage() = default;
48
49void CollectionGeneralPage::init(const Akonadi::Collection &collection)
50{
51 mIsLocalSystemFolder = CommonKernel->isSystemFolderCollection(collection) || Kernel::folderIsInbox(collection);
52
53 mIsResourceFolder = (collection.parentCollection() == Akonadi::Collection::root());
54 auto topLayout = new QVBoxLayout(this);
55
56 mCollectionGeneralWidget = new CollectionGeneralWidget(this);
57 topLayout->addWidget(mCollectionGeneralWidget);
58 auto innerLayout = qobject_cast<QFormLayout *>(mCollectionGeneralWidget->layout());
59
60 // Mustn't be able to edit details for a non-resource, system folder.
61 if ((!mIsLocalSystemFolder || mIsResourceFolder) && !mFolderCollection->isReadOnly()) {
62 mNameEdit = new QLineEdit(this);
64 connect(mNameEdit, &QLineEdit::textChanged, this, &CollectionGeneralPage::slotNameChanged);
65 innerLayout->insertRow(0, i18nc("@label:textbox Name of the folder.", "Folder &Name:"), mNameEdit);
66 }
67
68 // Only do make this settable, if the IMAP resource is enabled
69 // and it's not the personal folders (those must not be changed)
70 const QString collectionResource = collection.resource();
71 if (CommonKernel->imapResourceManager()->hasAnnotationSupport(collectionResource)) {
72 PimCommon::CollectionTypeUtil::FolderContentsType contentsType = PimCommon::CollectionTypeUtil::ContentsTypeMail;
73
74 const auto *annotationAttribute = collection.attribute<Akonadi::CollectionAnnotationsAttribute>();
75
76 const QMap<QByteArray, QByteArray> annotations = (annotationAttribute ? annotationAttribute->annotations() : QMap<QByteArray, QByteArray>());
77
78 const bool sharedSeen = (annotations.value(PimCommon::CollectionTypeUtil::kolabSharedSeen()) == "true");
79
80 PimCommon::CollectionTypeUtil collectionUtil;
81 const PimCommon::CollectionTypeUtil::IncidencesFor incidencesFor =
82 collectionUtil.incidencesForFromString(QLatin1StringView(annotations.value(PimCommon::CollectionTypeUtil::kolabIncidencesFor())));
83
84 const PimCommon::CollectionTypeUtil::FolderContentsType folderType =
85 collectionUtil.typeFromKolabName(annotations.value(PimCommon::CollectionTypeUtil::kolabFolderType()));
86
87 mContentsComboBox = new PimCommon::ContentTypeWidget(this);
88 innerLayout->addRow(PimCommon::ContentTypeWidget::labelName(), mContentsComboBox);
89 mContentsComboBox->setCurrentIndex(contentsType);
90
91 connect(mContentsComboBox, &PimCommon::ContentTypeWidget::activated, this, &CollectionGeneralPage::slotFolderContentsSelectionChanged);
92
93 if (mFolderCollection->isReadOnly() || mIsResourceFolder) {
94 mContentsComboBox->setEnabled(false);
95 }
96
97 // Kolab incidences-for annotation.
98 // Show incidences-for combobox if the contents type can be changed (new folder),
99 // or if it's set to calendar or task (existing folder)
100 const bool folderTypeComboboxEnabled =
101 (folderType == PimCommon::CollectionTypeUtil::ContentsTypeCalendar || folderType == PimCommon::CollectionTypeUtil::ContentsTypeTask);
102 if (folderTypeComboboxEnabled) {
103 mIncidencesForComboBox = new PimCommon::IncidencesForWidget(this);
104 innerLayout->addRow(PimCommon::IncidencesForWidget::labelName(), mIncidencesForComboBox);
105
106 mIncidencesForComboBox->setCurrentIndex(incidencesFor);
107 }
108
109 mSharedSeenFlagsCheckBox = new QCheckBox(this);
110 mSharedSeenFlagsCheckBox->setText(i18n("Share unread state with all users"));
111 mSharedSeenFlagsCheckBox->setChecked(sharedSeen);
112 mSharedSeenFlagsCheckBox->setWhatsThis(
113 i18n("If enabled, the unread state of messages in this folder will be "
114 "the same for all users having access to this folder. If disabled "
115 "(the default), every user with access to this folder has their "
116 "own unread state."));
117 innerLayout->addRow(QString(), mSharedSeenFlagsCheckBox);
118 }
119
120 topLayout->addStretch(100); // eat all superfluous space
121}
122
123void CollectionGeneralPage::load(const Akonadi::Collection &collection)
124{
125 mFolderCollection = FolderSettings::forCollection(collection);
126 init(collection);
127
128 mCollectionGeneralWidget->load(collection);
129 if (mNameEdit) {
130 const QString displayName = collection.displayName();
131
132 if (!mIsLocalSystemFolder || mIsResourceFolder) {
133 mNameEdit->setText(displayName);
134 }
135 }
136
137 if (mContentsComboBox) {
138 const auto *annotationsAttribute = collection.attribute<Akonadi::CollectionAnnotationsAttribute>();
139
140 if (annotationsAttribute) {
141 const QMap<QByteArray, QByteArray> annotations = annotationsAttribute->annotations();
142 if (annotations.contains(PimCommon::CollectionTypeUtil::kolabFolderType())) {
143 PimCommon::CollectionTypeUtil collectionUtil;
144 mContentsComboBox->setCurrentItem(collectionUtil.typeNameFromKolabType(annotations[PimCommon::CollectionTypeUtil::kolabFolderType()]));
145 }
146 }
147 }
148}
149
150void CollectionGeneralPage::save(Collection &collection)
151{
152 if (mNameEdit) {
153 if (!mIsLocalSystemFolder) {
154 const QString nameFolder(mNameEdit->text().trimmed());
155 bool canRenameFolder = !(nameFolder.startsWith(QLatin1Char('.')) || nameFolder.endsWith(QLatin1Char('.')) || nameFolder.contains(QLatin1Char('/'))
156 || nameFolder.isEmpty());
157
158 if (mIsResourceFolder && (PimCommon::Util::isImapResource(collection.resource()))) {
159 collection.setName(nameFolder);
161 instance.setName(nameFolder);
162 } else if (canRenameFolder) {
165 collection.attribute<Akonadi::EntityDisplayAttribute>()->setDisplayName(nameFolder);
166 } else if (!nameFolder.isEmpty()) {
167 collection.setName(nameFolder);
168 }
169 }
170 }
171 }
172 mCollectionGeneralWidget->save(collection);
173
174 auto *annotationsAttribute = collection.attribute<Akonadi::CollectionAnnotationsAttribute>(Collection::AddIfMissing);
175
176 QMap<QByteArray, QByteArray> annotations = annotationsAttribute->annotations();
177 if (mSharedSeenFlagsCheckBox && mSharedSeenFlagsCheckBox->isEnabled()) {
178 annotations[PimCommon::CollectionTypeUtil::kolabSharedSeen()] = mSharedSeenFlagsCheckBox->isChecked() ? "true" : "false";
179 }
180
181 PimCommon::CollectionTypeUtil collectionUtil;
182 if (mIncidencesForComboBox && mIncidencesForComboBox->isEnabled()) {
183 annotations[PimCommon::CollectionTypeUtil::kolabIncidencesFor()] =
184 collectionUtil.incidencesForToString(static_cast<PimCommon::CollectionTypeUtil::IncidencesFor>(mIncidencesForComboBox->currentIndex())).toLatin1();
185 }
186
187 if (mContentsComboBox) {
188 const PimCommon::CollectionTypeUtil::FolderContentsType type = collectionUtil.contentsTypeFromString(mContentsComboBox->currentText());
189
190 const QByteArray kolabName = collectionUtil.kolabNameFromType(type);
191 if (!kolabName.isEmpty()) {
192 const QString iconName = collectionUtil.iconNameFromContentsType(type);
194 attribute->setIconName(iconName);
195 new Akonadi::CollectionModifyJob(collection);
196 annotations[PimCommon::CollectionTypeUtil::kolabFolderType()] = kolabName;
197 }
198 }
199 if (annotations.isEmpty()) {
201 } else {
202 annotationsAttribute->setAnnotations(annotations);
203 }
204}
205
206void CollectionGeneralPage::slotFolderContentsSelectionChanged(int)
207{
208 PimCommon::CollectionTypeUtil collectionUtil;
209 const PimCommon::CollectionTypeUtil::FolderContentsType type = collectionUtil.contentsTypeFromString(mContentsComboBox->currentText());
210
211 if (type != PimCommon::CollectionTypeUtil::ContentsTypeMail) {
212 const QString message = i18n(
213 "You have configured this folder to contain groupware information. "
214 "That means that this folder will disappear once the configuration "
215 "dialog is closed.");
216
217 KMessageBox::information(this, message);
218 }
219
220 const bool enable = (type == PimCommon::CollectionTypeUtil::ContentsTypeCalendar || type == PimCommon::CollectionTypeUtil::ContentsTypeTask);
221
222 if (mIncidencesForComboBox) {
223 mIncidencesForComboBox->setEnabled(enable);
224 }
225}
226
227void CollectionGeneralPage::slotNameChanged(const QString &name)
228{
229#ifndef QT_NO_STYLE_STYLESHEET
232 if (mColorName.isEmpty()) {
234 KStatefulBrush bgBrush(KColorScheme::View, bgColorScheme);
235 mColorName = bgBrush.brush(palette()).color().name();
236 }
237 styleSheet = QStringLiteral("QLineEdit{ background-color:%1 }").arg(mColorName);
238 }
240#endif
241}
242
243#include "moc_collectiongeneralpage.cpp"
void setName(const QString &name)
static AgentManager * self()
AgentInstance instance(const QString &identifier) const
QString resource() const
QString displayName() const
static Collection root()
void setName(const QString &name)
const T * attribute() const
Collection & parentCollection()
bool hasAttribute() const
void setIconName(const QString &name)
static QString labelName()
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
Type type(const QSqlDatabase &db)
void catchReturnKey(QObject *lineEdit)
void information(QWidget *parent, const QString &text, const QString &title=QString(), const QString &dontShowAgainName=QString(), Options options=Notify)
QString name(StandardAction id)
The filter dialog.
void setChecked(bool)
void setText(const QString &text)
bool isEmpty() const const
void activated(int index)
void setCurrentIndex(int index)
void setText(const QString &)
void textChanged(const QString &text)
bool contains(const Key &key) const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T qobject_cast(QObject *object)
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QString trimmed() const const
void setEnabled(bool)
QLayout * layout() const const
void setWhatsThis(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.