Mailcommon

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

KDE's Doxygen guidelines are available online.