File indexing completed on 2025-02-02 04:57:03

0001 /***************************************************************************
0002  * SPDX-FileCopyrightText: 2022 S. MANKOWSKI stephane@mankowski.fr
0003  * SPDX-FileCopyrightText: 2022 G. DE BURE support@mankowski.fr
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  ***************************************************************************/
0006 #include "skgaddoperation.h"
0007 
0008 #include <qdir.h>
0009 #include <qicon.h>
0010 #include <qsavefile.h>
0011 #include <qstringbuilder.h>
0012 #include <qtextstream.h>
0013 #include <quuid.h>
0014 #include <qstandardpaths.h>
0015 
0016 #include <klocalizedstring.h>
0017 #include <kmessagebox.h>
0018 
0019 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
0020 #define SKGENDL endl
0021 #else
0022 #define SKGENDL Qt::endl
0023 #endif
0024 
0025 K_EXPORT_PLASMA_RUNNER_WITH_JSON(SKGAddOperation, "metadata.json")
0026 
0027 SKGAddOperation::SKGAddOperation(QObject* iParent, const QVariantList& args)
0028     : AbstractRunner(iParent, args)
0029 {
0030     setPriority(HighPriority);
0031 }
0032 
0033 void SKGAddOperation::reloadConfiguration()
0034 {
0035     KConfigGroup c = config();
0036     m_triggerWord = c.readEntry("buy", i18nc("default keyword for krunner plugin", "buy"));
0037     if (!m_triggerWord.isEmpty()) {
0038         m_triggerWord.append(' ');
0039     }
0040 
0041     QList<Plasma::RunnerSyntax> listofSyntaxes;
0042     Plasma::RunnerSyntax syntax(QStringLiteral("%1:q:").arg(m_triggerWord), i18n("Add a new transaction in skrooge"));
0043     syntax.addExampleQuery(i18nc("Example of krunner command", "%1 10 computer", m_triggerWord));
0044     listofSyntaxes.append(syntax);
0045     setSyntaxes(listofSyntaxes);
0046 }
0047 
0048 void SKGAddOperation::match(Plasma::RunnerContext& iContext)
0049 {
0050     QString query = iContext.query();
0051     if (!query.startsWith(m_triggerWord)) {
0052         return;
0053     }
0054     query = query.remove(0, m_triggerWord.length());
0055 
0056     Plasma::QueryMatch m(this);
0057     m.setText(i18n("Add transaction %1", query));
0058     m.setData(query);
0059     m.setIcon(QIcon::fromTheme(QStringLiteral("skrooge")));
0060     m.setId(query);
0061 
0062     iContext.addMatch(m);
0063 }
0064 
0065 void SKGAddOperation::run(const Plasma::RunnerContext& iContext, const Plasma::QueryMatch& iMatch)
0066 {
0067     Q_UNUSED(iContext)
0068 
0069     QString dirName = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
0070     QDir().mkpath(dirName);
0071     QString fileName = dirName % "add_operation_" % QUuid::createUuid().toString() % ".txt";
0072     QSaveFile file(fileName);
0073     if (file.open(QIODevice::WriteOnly)) {
0074         QTextStream stream(&file);
0075         stream << "buy" << SKGENDL;
0076         stream << QDate::currentDate().toString(QStringLiteral("yyyy-MM-dd")) << SKGENDL;
0077         QString s = iMatch.id().remove(0, QStringLiteral("skroogeaddoperation_").length());
0078         int pos = s.indexOf(QStringLiteral(" "));
0079         if (pos == -1) {
0080             stream << s << SKGENDL;
0081             stream << "" << SKGENDL;
0082         } else {
0083             stream << s.left(pos).trimmed() << SKGENDL;
0084             stream << s.right(s.length() - pos - 1).trimmed() << SKGENDL;
0085         }
0086 
0087         // Close file
0088         file.commit();
0089 
0090         KMessageBox::information(nullptr, i18nc("Information message", "Transaction created"));
0091     } else {
0092         KMessageBox::error(nullptr, i18nc("Error message: Could not create a file", "Creation of file %1 failed", fileName));
0093     }
0094 }
0095 
0096 #include "skgaddoperation.moc"