Akonadi Calendar

calendarutils.cpp
1 /*
2  SPDX-FileCopyrightText: 2009, 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
3  SPDX-FileContributor: Frank Osterfeld <[email protected]>
4  SPDX-FileContributor: Andras Mantia <[email protected]>
5  SPDX-FileCopyrightText: 2010-2012 SĂ©rgio Martins <[email protected]>
6  SPDX-FileCopyrightText: 2022 Volker Krause <[email protected]>
7 
8  SPDX-License-Identifier: LGPL-2.0-or-later
9 */
10 
11 #include "calendarutils.h"
12 #include "etmcalendar.h"
13 
14 #include <Akonadi/AgentManager>
15 #include <Akonadi/Collection>
16 #include <Akonadi/EntityTreeModel>
17 
18 #include <KCalUtils/ICalDrag>
19 
20 #include <KLocalizedString>
21 
22 #include <QMimeData>
23 
24 using namespace Akonadi;
25 
27 {
28  // With this try-catch block, we get a 2x performance improvement in retrieving the payload
29  // since we don't call hasPayload()
30  try {
32  } catch (const Akonadi::PayloadException &) {
33  return {};
34  }
35 }
36 
38 {
39  try {
42  return item.payload<KCalendarCore::Event::Ptr>();
43  }
44  } catch (const Akonadi::PayloadException &) {
45  return {};
46  }
47  return {};
48 }
49 
51 {
52  try {
55  return item.payload<KCalendarCore::Todo::Ptr>();
56  }
57  } catch (const Akonadi::PayloadException &) {
58  return {};
59  }
60  return {};
61 }
62 
64 {
65  try {
68  return item.payload<KCalendarCore::Journal::Ptr>();
69  }
70  } catch (const Akonadi::PayloadException &) {
71  return {};
72  }
73  return {};
74 }
75 
76 static QString displayNameImpl(const Akonadi::EntityTreeModel *model, const Akonadi::Collection &c)
77 {
78  Akonadi::Collection fullCollection = c;
79  if (model) {
80  if (const auto col = Akonadi::EntityTreeModel::updatedCollection(model, c); col.isValid()) {
81  fullCollection = col;
82  }
83  }
84 
85  QString cName = fullCollection.name();
86  const QString resourceName = fullCollection.resource();
87 
88  // Kolab Groupware
89  if (resourceName.contains(QLatin1String("kolab"))) {
90  QString typeStr = cName; // contents type: "Calendar", "Tasks", etc
91  QString ownerStr; // folder owner: "fred", "ethel", etc
92  QString nameStr; // folder name: "Public", "Test", etc
93  if (model) {
95  while (p != Akonadi::Collection::root()) {
97  const QString tName = tCol.name();
98  if (tName.startsWith(QLatin1String("shared.cal"), Qt::CaseInsensitive)) {
99  ownerStr = QStringLiteral("Shared");
100  nameStr = cName;
101  typeStr = i18n("Calendar");
102  break;
103  } else if (tName.startsWith(QLatin1String("shared.tasks"), Qt::CaseInsensitive)
104  || tName.startsWith(QLatin1String("shared.todo"), Qt::CaseInsensitive)) {
105  ownerStr = QStringLiteral("Shared");
106  nameStr = cName;
107  typeStr = i18n("Tasks");
108  break;
109  } else if (tName.startsWith(QLatin1String("shared.journal"), Qt::CaseInsensitive)) {
110  ownerStr = QStringLiteral("Shared");
111  nameStr = cName;
112  typeStr = i18n("Journal");
113  break;
114  } else if (tName.startsWith(QLatin1String("shared.notes"), Qt::CaseInsensitive)) {
115  ownerStr = QStringLiteral("Shared");
116  nameStr = cName;
117  typeStr = i18n("Notes");
118  break;
119  } else if (tName != i18n("Calendar") && tName != i18n("Tasks") && tName != i18n("Journal") && tName != i18n("Notes")) {
120  ownerStr = tName;
121  break;
122  } else {
123  nameStr = typeStr;
124  typeStr = tName;
125  }
126  p = p.parentCollection();
127  }
128  }
129 
130  if (!ownerStr.isEmpty()) {
131  if (!ownerStr.compare(QLatin1String("INBOX"), Qt::CaseInsensitive)) {
132  return i18nc("%1 is folder contents", "My Kolab %1", typeStr);
133  } else if (!ownerStr.compare(QLatin1String("SHARED"), Qt::CaseInsensitive) || !ownerStr.compare(QLatin1String("CALENDAR"), Qt::CaseInsensitive)
134  || !ownerStr.compare(QLatin1String("RESOURCES"), Qt::CaseInsensitive)) {
135  return i18nc("%1 is folder name, %2 is folder contents", "Shared Kolab %1 %2", nameStr, typeStr);
136  } else {
137  if (nameStr.isEmpty()) {
138  return i18nc("%1 is folder owner name, %2 is folder contents", "%1's Kolab %2", ownerStr, typeStr);
139  } else {
140  return i18nc("%1 is folder owner name, %2 is folder name, %3 is folder contents", "%1's %2 Kolab %3", ownerStr, nameStr, typeStr);
141  }
142  }
143  } else {
144  return i18nc("%1 is folder contents", "Kolab %1", typeStr);
145  }
146  } // end kolab section
147 
148  // Dav Groupware
149  if (resourceName.contains(QLatin1String("davgroupware"))) {
150  const QString resourceDisplayName = Akonadi::AgentManager::self()->instance(resourceName).name();
151  return i18nc("%1 is the folder name", "%1 in %2", fullCollection.displayName(), resourceDisplayName);
152  } // end caldav section
153 
154  // Google
155  if (resourceName.contains(QLatin1String("google"))) {
156  QString ownerStr; // folder owner: "[email protected]"
157  if (model) {
160  }
161 
162  const QString nameStr = c.displayName(); // folder name: can be anything
163 
164  QString typeStr;
165  const QString mimeStr = c.contentMimeTypes().join(QLatin1Char(','));
166  if (mimeStr.contains(QLatin1String(".event"))) {
167  typeStr = i18n("Calendar");
168  } else if (mimeStr.contains(QLatin1String(".todo"))) {
169  typeStr = i18n("Tasks");
170  } else if (mimeStr.contains(QLatin1String(".journal"))) {
171  typeStr = i18n("Journal");
172  } else if (mimeStr.contains(QLatin1String(".note"))) {
173  typeStr = i18n("Notes");
174  } else {
175  typeStr = mimeStr;
176  }
177 
178  if (!ownerStr.isEmpty()) {
179  const int atChar = ownerStr.lastIndexOf(QLatin1Char('@'));
180  if (atChar >= 0) {
181  ownerStr.truncate(atChar);
182  }
183  if (nameStr.isEmpty()) {
184  return i18nc("%1 is folder owner name, %2 is folder contents", "%1's Google %2", ownerStr, typeStr);
185  } else {
186  return i18nc("%1 is folder owner name, %2 is folder name", "%1's %2", ownerStr, nameStr);
187  }
188  } else {
189  return i18nc("%1 is folder contents", "Google %1", typeStr);
190  }
191  } // end google section
192 
193  // Not groupware so the collection is "mine"
194  const QString dName = fullCollection.displayName();
195 
196  if (!dName.isEmpty()) {
197  return fullCollection.name().startsWith(QLatin1String("akonadi_")) ? i18n("My %1", dName) : dName;
198  } else if (!fullCollection.name().isEmpty()) {
199  return fullCollection.name();
200  } else {
201  return i18nc("unknown resource", "Unknown");
202  }
203 }
204 
206 {
207  return displayNameImpl(calendar ? calendar->entityTreeModel() : nullptr, c);
208 }
209 
211 {
212  return displayNameImpl(model, c);
213 }
214 
216 {
217  return displayNameImpl(nullptr, c);
218 }
219 
221 {
222  if (items.isEmpty()) {
223  return nullptr;
224  }
225 
227 
228  QList<QUrl> urls;
229  int incidencesFound = 0;
230  for (const Akonadi::Item &item : items) {
232  if (!incidence) {
233  continue;
234  }
235  ++incidencesFound;
236  urls.push_back(item.url());
238  cal->addIncidence(i);
239  }
240 
241  if (incidencesFound == 0) {
242  return nullptr;
243  }
244 
245  auto mimeData = std::make_unique<QMimeData>();
246  mimeData->setUrls(urls);
247 
248  if (KCalUtils::ICalDrag::populateMimeData(mimeData.get(), cal)) {
249  return mimeData.release();
250  } else {
251  return nullptr;
252  }
253 }
static Collection updatedCollection(const QAbstractItemModel *model, qint64 collectionId)
void truncate(int position)
KCALUTILS_EXPORT bool populateMimeData(QMimeData *e, const KCalendarCore::Calendar::Ptr &cal)
CaseInsensitive
QString displayName() const
void push_back(const T &value)
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
AKONADI_CALENDAR_EXPORT KCalendarCore::Incidence::Ptr incidence(const Akonadi::Item &item)
Returns the incidence from an Akonadi item, or a null pointer if the item has no such payload.
AgentInstance instance(const QString &identifier) const
QString i18n(const char *text, const TYPE &arg...)
bool isEmpty() const const
AKONADI_CALENDAR_EXPORT QMimeData * createMimeData(const Akonadi::Item::List &items)
Creates a MIME data object for dragging Akonadi items containing calendar incidences.
A KCalendarCore::Calendar that uses an EntityTreeModel to populate itself.
Definition: etmcalendar.h:40
Collection & parentCollection()
bool isEmpty() const const
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
Returns the event from an Akonadi item, or a null pointer if the item has no such payload.
QString join(const QString &separator) const const
QStringList contentMimeTypes() const
static Collection root()
QString name() const
AKONADI_CALENDAR_EXPORT QString displayName(Akonadi::ETMCalendar *calendar, const Akonadi::Collection &collection)
Returns a suitable display name for the calendar (or calendar folder) collection.
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
AKONADI_CALENDAR_EXPORT KCalendarCore::Journal::Ptr journal(const Akonadi::Item &item)
Returns the journal from an Akonadi item, or a null pointer if the item has no such payload.
Akonadi::EntityTreeModel * entityTreeModel() const
Returns the underlying EntityTreeModel.
QTimeZone systemTimeZone()
QString resource() const
QString i18nc(const char *context, const char *text, const TYPE &arg...)
static AgentManager * self()
bool isValid() const
int compare(const QString &other, Qt::CaseSensitivity cs) const const
T payload() const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
AKONADI_CALENDAR_EXPORT KCalendarCore::Todo::Ptr todo(const Akonadi::Item &item)
Returns the todo from an Akonadi item, or a null pointer if the item has no such payload.
FreeBusyManager::Singleton.
QString name() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Sep 26 2023 03:49:30 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.