File indexing completed on 2025-01-19 04:51:20

0001 /*
0002   This file is part of Kontact
0003   SPDX-FileCopyrightText: 2002 Daniel Molkentin <molkentin@kde.org>
0004 
0005   SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "knotes_plugin.h"
0009 #include "apps/knotes_options.h"
0010 #include "knotes_part.h"
0011 #include "knotesglobalconfig.h"
0012 #include "summarywidget.h"
0013 #include <KCalUtils/ICalDrag>
0014 #include <KCalUtils/VCalDrag>
0015 #include <KCalendarCore/FileStorage>
0016 #include <KCalendarCore/MemoryCalendar>
0017 
0018 #include "knotes-version.h"
0019 
0020 using namespace KCalUtils;
0021 using namespace KCalendarCore;
0022 
0023 #include <KContacts/VCardDrag>
0024 
0025 #include <KontactInterface/Core>
0026 
0027 #include "knotes_kontact_plugin_debug.h"
0028 #include <KActionCollection>
0029 #include <KLocalizedString>
0030 #include <QAction>
0031 #include <QIcon>
0032 #include <QTimeZone>
0033 
0034 #include <QDropEvent>
0035 #include <QStandardPaths>
0036 
0037 EXPORT_KONTACT_PLUGIN_WITH_JSON(KNotesPlugin, "knotesplugin.json")
0038 
0039 KNotesPlugin::KNotesPlugin(KontactInterface::Core *core, const KPluginMetaData &data, const QVariantList &)
0040     : KontactInterface::Plugin(core, core, data, "knotes")
0041 {
0042     setComponentName(QStringLiteral("knotes"), i18n("KNotes"));
0043 
0044     auto action = new QAction(QIcon::fromTheme(QStringLiteral("knotes")), i18nc("@action:inmenu", "New Popup Note..."), this);
0045     actionCollection()->addAction(QStringLiteral("new_note"), action);
0046     connect(action, &QAction::triggered, this, &KNotesPlugin::slotNewNote);
0047     actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_N));
0048     // action->setHelpText(
0049     //            i18nc( "@info:status", "Create new popup note" ) );
0050     action->setWhatsThis(i18nc("@info:whatsthis", "You will be presented with a dialog where you can create a new popup note."));
0051     insertNewAction(action);
0052 
0053     mUniqueAppWatcher = new KontactInterface::UniqueAppWatcher(new KontactInterface::UniqueAppHandlerFactory<KNotesUniqueAppHandler>(), this);
0054 }
0055 
0056 KNotesPlugin::~KNotesPlugin() = default;
0057 
0058 bool KNotesPlugin::isRunningStandalone() const
0059 {
0060     return mUniqueAppWatcher->isRunningStandalone();
0061 }
0062 
0063 KParts::Part *KNotesPlugin::createPart()
0064 {
0065     return new KNotesPart(this);
0066 }
0067 
0068 KontactInterface::Summary *KNotesPlugin::createSummaryWidget(QWidget *parentWidget)
0069 {
0070     return new KNotesSummaryWidget(this, parentWidget);
0071 }
0072 
0073 const KAboutData KNotesPlugin::aboutData()
0074 {
0075     KAboutData aboutData = KAboutData(QStringLiteral("knotes"),
0076                                       xi18nc("@title", "KNotes"),
0077                                       QStringLiteral(KNOTES_VERSION),
0078                                       xi18nc("@title", "Popup Notes"),
0079                                       KAboutLicense::GPL_V2,
0080                                       xi18nc("@info:credit", "Copyright © 2003–%1 Kontact authors", QStringLiteral("2023")));
0081 
0082     aboutData.addAuthor(xi18nc("@info:credit", "Laurent Montel"), xi18nc("@info:credit", "Current Maintainer"), QStringLiteral("montel@kde.org"));
0083 
0084     aboutData.addAuthor(xi18nc("@info:credit", "Michael Brade"), xi18nc("@info:credit", "Previous Maintainer"), QStringLiteral("brade@kde.org"));
0085     aboutData.addAuthor(xi18nc("@info:credit", "Tobias Koenig"), xi18nc("@info:credit", "Developer"), QStringLiteral("tokoe@kde.org"));
0086 
0087     return aboutData;
0088 }
0089 
0090 bool KNotesPlugin::canDecodeMimeData(const QMimeData *mimeData) const
0091 {
0092     return mimeData->hasText() || KContacts::VCardDrag::canDecode(mimeData) || ICalDrag::canDecode(mimeData);
0093 }
0094 
0095 void KNotesPlugin::processDropEvent(QDropEvent *event)
0096 {
0097     const QMimeData *md = event->mimeData();
0098 
0099     if (KContacts::VCardDrag::canDecode(md)) {
0100         KContacts::Addressee::List contacts;
0101 
0102         KContacts::VCardDrag::fromMimeData(md, contacts);
0103 
0104         KContacts::Addressee::List::ConstIterator it;
0105 
0106         QStringList attendees;
0107         for (const auto &contact : std::as_const(contacts)) {
0108             const QString email = contact.fullEmail();
0109             if (email.isEmpty()) {
0110                 attendees.append(contact.realName() + QLatin1StringView("<>"));
0111             } else {
0112                 attendees.append(email);
0113             }
0114         }
0115         event->accept();
0116         static_cast<KNotesPart *>(part())->newNote(i18nc("@item", "Meeting"), attendees.join(QLatin1StringView(", ")));
0117         return;
0118     }
0119 
0120     if (KCalUtils::ICalDrag::canDecode(md)) {
0121         KCalendarCore::MemoryCalendar::Ptr cal(new KCalendarCore::MemoryCalendar(QTimeZone::systemTimeZone()));
0122         if (KCalUtils::ICalDrag::fromMimeData(md, cal)) {
0123             KCalendarCore::Incidence::List incidences = cal->incidences();
0124             Q_ASSERT(incidences.count());
0125             if (!incidences.isEmpty()) {
0126                 event->accept();
0127                 KCalendarCore::Incidence::Ptr i = incidences.first();
0128                 QString summary;
0129                 if (i->type() == KCalendarCore::Incidence::TypeJournal) {
0130                     summary = i18nc("@item", "Note: %1", i->summary());
0131                 } else {
0132                     summary = i->summary();
0133                 }
0134                 static_cast<KNotesPart *>(part())->newNote(i18nc("@item", "Note: %1", summary), i->description());
0135                 return;
0136             }
0137         }
0138     }
0139     if (md->hasText()) {
0140         static_cast<KNotesPart *>(part())->newNote(i18nc("@item", "New Note"), md->text());
0141         return;
0142     }
0143 
0144     qCWarning(KNOTES_KONTACT_PLUGIN_LOG) << QStringLiteral("Cannot handle drop events of type '%1'.").arg(event->mimeData()->formats().join(QLatin1Char(';')));
0145 }
0146 
0147 void KNotesPlugin::shortcutChanged()
0148 {
0149     if (part()) {
0150         static_cast<KNotesPart *>(part())->updateClickMessage();
0151     }
0152 }
0153 
0154 // private slots
0155 
0156 void KNotesPlugin::slotNewNote()
0157 {
0158     if (part()) {
0159         static_cast<KNotesPart *>(part())->newNote();
0160         core()->selectPlugin(this);
0161     }
0162 }
0163 
0164 void KNotesUniqueAppHandler::loadCommandLineOptions(QCommandLineParser *parser)
0165 {
0166     knotesOptions(parser);
0167 }
0168 
0169 int KNotesUniqueAppHandler::activate(const QStringList &args, const QString &workingDir)
0170 {
0171     qCDebug(KNOTES_KONTACT_PLUGIN_LOG);
0172     // Ensure part is loaded
0173     (void)plugin()->part();
0174 
0175     return KontactInterface::UniqueAppHandler::activate(args, workingDir);
0176 }
0177 
0178 #include "knotes_plugin.moc"
0179 
0180 #include "moc_knotes_plugin.cpp"