File indexing completed on 2024-04-28 15:29:12

0001 /*
0002     SPDX-FileCopyrightText: 2005-2006 Olivier Goffart <ogoffart at kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "notifybylogfile.h"
0008 
0009 #include <QDateTime>
0010 #include <QFile>
0011 #include <QString>
0012 #include <QTextStream>
0013 #include <QUrl>
0014 
0015 #include "knotification.h"
0016 #include "knotifyconfig.h"
0017 
0018 NotifyByLogfile::NotifyByLogfile(QObject *parent)
0019     : KNotificationPlugin(parent)
0020 {
0021 }
0022 
0023 NotifyByLogfile::~NotifyByLogfile()
0024 {
0025 }
0026 
0027 void NotifyByLogfile::notify(KNotification *notification, KNotifyConfig *config)
0028 {
0029     QString file = config->readEntry(QStringLiteral("Logfile"));
0030 
0031     if (file.isEmpty()) {
0032         finish(notification);
0033         return;
0034     }
0035 
0036     // open file in append mode
0037     QFile logFile(QUrl(file).path());
0038 
0039     if (!logFile.open(QIODevice::WriteOnly | QIODevice::Append)) {
0040         finish(notification);
0041         return;
0042     }
0043 
0044     QString text = notification->text();
0045     if (text.isEmpty()) {
0046         text = config->readEntry(QStringLiteral("Name"));
0047     }
0048     // append msg
0049     QTextStream strm(&logFile);
0050     strm << "- KNotify " << QDateTime::currentDateTime().toString() << ": ";
0051     strm << text << QLatin1Char('\n');
0052 
0053     // close file
0054     logFile.close();
0055 
0056     finish(notification);
0057 }
0058 
0059 #include "moc_notifybylogfile.cpp"