File indexing completed on 2024-12-15 04:51:47
0001 /* 0002 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 #include "noteutils.h" 0007 #include "attributes/notedisplayattribute.h" 0008 #include "network/notehostdialog.h" 0009 #include "network/notesnetworksender.h" 0010 #include "notesharedglobalconfig.h" 0011 #include <KLocalizedString> 0012 #include <KMessageBox> 0013 #include <KProcess> 0014 0015 #include <KMime/KMimeMessage> 0016 0017 #include <QApplication> 0018 #include <QPointer> 0019 #include <QRegularExpression> 0020 #include <QSslSocket> 0021 0022 using namespace NoteShared; 0023 0024 NoteUtils::NoteUtils() = default; 0025 0026 bool NoteUtils::sendToMail(QWidget *parent, const QString &title, const QString &message) 0027 { 0028 // get the mail action command 0029 const QList<QStringView> cmd_list = QStringView(NoteShared::NoteSharedGlobalConfig::mailAction()).split(QLatin1Char(' '), Qt::SkipEmptyParts); 0030 if (cmd_list.isEmpty()) { 0031 KMessageBox::error(parent, i18n("Please configure send mail action.")); 0032 return false; 0033 } 0034 KProcess mail; 0035 for (const QStringView &cmd : cmd_list) { 0036 if (cmd == QLatin1StringView("%f")) { 0037 mail << message; 0038 } else if (cmd == QLatin1StringView("%t")) { 0039 mail << i18n("Note: \"%1\"", title); 0040 } else { 0041 mail << cmd.toString(); 0042 } 0043 } 0044 0045 if (!mail.startDetached()) { 0046 KMessageBox::error(parent, i18n("Unable to start the mail process.")); 0047 return false; 0048 } 0049 return true; 0050 } 0051 0052 void NoteUtils::sendToNetwork(QWidget *parent, const QString &title, const QString &message) 0053 { 0054 // pop up dialog to get the IP 0055 QPointer<NoteShared::NoteHostDialog> hostDlg = new NoteShared::NoteHostDialog(i18n("Send \"%1\"", title), parent); 0056 if (hostDlg->exec()) { 0057 const QString host = hostDlg->host(); 0058 if (host.isEmpty()) { 0059 KMessageBox::error(parent, i18n("The host cannot be empty.")); 0060 delete hostDlg; 0061 return; 0062 } 0063 quint16 port = hostDlg->port(); 0064 0065 if (!port) { // not specified, use default 0066 port = NoteShared::NoteSharedGlobalConfig::port(); 0067 } 0068 0069 // Send the note 0070 auto socket = new QSslSocket; 0071 socket->connectToHost(host, port); 0072 auto sender = new NoteShared::NotesNetworkSender(socket); 0073 sender->setSenderId(NoteShared::NoteSharedGlobalConfig::senderID()); 0074 sender->setNote(title, message); // FIXME: plainText ?? 0075 } 0076 delete hostDlg; 0077 } 0078 0079 QString NoteUtils::createToolTip(const Akonadi::Item &item) 0080 { 0081 const auto noteMessage = item.payload<KMime::Message::Ptr>(); 0082 if (!noteMessage) { 0083 return {}; 0084 } 0085 const QString description = QString::fromUtf8(noteMessage->mainBodyPart()->decodedContent()); 0086 const KMime::Headers::Subject *const subject = noteMessage->subject(false); 0087 0088 const QString realName = subject ? subject->asUnicodeString() : QString(); 0089 const bool isRichText = noteMessage->contentType()->isHTMLText(); 0090 0091 QString tip; 0092 if (item.hasAttribute<NoteDisplayAttribute>()) { 0093 const auto attr = item.attribute<NoteDisplayAttribute>(); 0094 if (attr) { 0095 const QString bckColorName = attr->backgroundColor().name(); 0096 const QString txtColorName = attr->foregroundColor().name(); 0097 const bool textIsLeftToRight = (QApplication::layoutDirection() == Qt::LeftToRight); 0098 const QString textDirection = textIsLeftToRight ? QStringLiteral("left") : QStringLiteral("right"); 0099 0100 tip = QStringLiteral("<table width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\">"); 0101 tip += QStringLiteral( 0102 "<tr>" 0103 "<td bgcolor=\"%1\" align=\"%4\" valign=\"middle\">" 0104 "<div style=\"color: %2; font-weight: bold;\">" 0105 "%3" 0106 "</div>" 0107 "</td>" 0108 "</tr>") 0109 .arg(bckColorName, txtColorName, realName.toHtmlEscaped(), textDirection); 0110 const QString htmlCodeForStandardRow = QStringLiteral( 0111 "<tr>" 0112 "<td bgcolor=\"%1\" align=\"left\" valign=\"top\">" 0113 "<div style=\"color: %2;\">" 0114 "%3" 0115 "</div>" 0116 "</td>" 0117 "</tr>"); 0118 0119 QString content = description; 0120 if (!content.trimmed().isEmpty()) { 0121 tip += 0122 htmlCodeForStandardRow.arg(bckColorName, txtColorName, isRichText ? content : content.replace(QLatin1Char('\n'), QStringLiteral("<br>"))); 0123 } 0124 tip += QLatin1StringView( 0125 "</table" 0126 "</td>" 0127 "</tr>"); 0128 } 0129 } 0130 return tip; 0131 } 0132 0133 NoteUtils::NoteText NoteUtils::extractNoteText(QString noteText, const QString &titleAddon) 0134 { 0135 const int pos = noteText.indexOf(QRegularExpression(QStringLiteral("[\r\n]"))); 0136 const QString noteTitle = noteText.left(pos).trimmed() + titleAddon; 0137 0138 noteText = noteText.mid(pos).trimmed(); 0139 NoteUtils::NoteText noteTextResult; 0140 noteTextResult.noteText = noteText; 0141 noteTextResult.noteTitle = noteTitle; 0142 return noteTextResult; 0143 }