File indexing completed on 2024-04-14 05:44:34

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2007 Dominik Seichter <domseichter@web.de>
0003 
0004 #include "systemplugin.h"
0005 
0006 #include "batchrenamer.h"
0007 
0008 #include <QDate>
0009 #include <QTime>
0010 
0011 #include <kfileitem.h>
0012 #include <KLocalizedString>
0013 #include <KIO/StatJob>
0014 #include <kio_version.h>
0015 
0016 SystemPlugin::SystemPlugin(PluginLoader *loader)
0017     : FilePlugin(loader)
0018 {
0019     this->addSupportedToken("date");
0020     this->addSupportedToken("date;.*");
0021     this->addSupportedToken("year");
0022     this->addSupportedToken("month");
0023     this->addSupportedToken("day");
0024     this->addSupportedToken("time");
0025     this->addSupportedToken("hour");
0026     this->addSupportedToken("minute");
0027     this->addSupportedToken("second");
0028     this->addSupportedToken("user");
0029     this->addSupportedToken("group");
0030     this->addSupportedToken("creationdate");
0031     this->addSupportedToken("creationdate;.*");
0032     this->addSupportedToken("modificationdate");
0033     this->addSupportedToken("modificationdate;.*");
0034     this->addSupportedToken("accessdate");
0035     this->addSupportedToken("accessdate;.*");
0036     this->addSupportedToken("filesize");
0037 
0038     m_help.append(Plugin::createHelpEntry("date", i18n("Insert the current date")));
0039     m_help.append(Plugin::createHelpEntry("date;yyyy-MM-dd", i18n("Insert the current date using the formatting string yyyy-MM-dd")));
0040     m_help.append(Plugin::createHelpEntry("year", i18n("Insert the current year")));
0041     m_help.append(Plugin::createHelpEntry("month", i18n("Insert the current month as number")));
0042     m_help.append(Plugin::createHelpEntry("day", i18n("Insert the current day as number")));
0043     m_help.append(Plugin::createHelpEntry("time", i18n("Insert the current time")));
0044     m_help.append(Plugin::createHelpEntry("hour", i18n("Insert the current hour as number")));
0045     m_help.append(Plugin::createHelpEntry("minute", i18n("Insert the current minute as number")));
0046     m_help.append(Plugin::createHelpEntry("second", i18n("Insert the current second as number")));
0047     m_help.append(Plugin::createHelpEntry("user", i18n("Owner of the file")));
0048     m_help.append(Plugin::createHelpEntry("group", i18n("Owning group of the file")));
0049     m_help.append(Plugin::createHelpEntry("creationdate", i18n("Insert the files creation date")));
0050     m_help.append(Plugin::createHelpEntry("creationdate;yyyy-MM-dd", i18n("Insert the formatted file creation date")));
0051     m_help.append(Plugin::createHelpEntry("modificationdate", i18n("Insert the files modification date")));
0052     m_help.append(Plugin::createHelpEntry("modificationdate;yyyy-MM-dd", i18n("Insert the formatted modification date")));
0053     m_help.append(Plugin::createHelpEntry("accessdate", i18n("Insert the date of the last file access")));
0054     m_help.append(Plugin::createHelpEntry("accessdate;yyyy-MM-dd", i18n("Insert the formatted date of the last file access")));
0055     m_help.append(Plugin::createHelpEntry("filesize", i18n("Insert the file size in bytes")));
0056 
0057     m_name = i18n("Date and system functions");
0058     m_icon = "system-run";
0059     m_comment = i18n("<qt>This plugin contains tokens to get "
0060                      "the creation, modification and last access "
0061                      "time of files and the current system time and date.</qt>");
0062 }
0063 
0064 SystemPlugin::~SystemPlugin()
0065 {
0066 
0067 }
0068 
0069 QString SystemPlugin::processFile(BatchRenamer *b, int index, const QString &filenameOrToken, EPluginType)
0070 {
0071     /*
0072     if( token.lower().startsWith( getPattern() ) )
0073         token = token.mid( getPattern().length(), token.length() - getPattern().length() );
0074     */
0075 
0076     QString token(filenameOrToken);
0077     QDate d = QDate::currentDate();
0078     QTime t = QTime::currentTime();
0079     QString tmp;
0080     QString format = "dd-MM-yyyy";
0081     if (token.contains(";")) {
0082         format = token.section(';', 1, 1);
0083         token = token.section(';', 0, 0).toLower();
0084     } else {
0085         token = token.toLower();
0086     }
0087 
0088     if (token == "date") {
0089         return QDateTime::currentDateTime().toString(format);
0090     } else if (token == "year") {
0091         return QString("%1").arg(d.year());
0092     } else if (token == "month") {
0093         return tmp.sprintf("%0*i", 2, d.month());
0094     } else if (token == "day") {
0095         return tmp.sprintf("%0*i", 2, d.day());
0096     } else if (token == "time") {
0097         QString hour   = QString().sprintf("%0*i", 2, t.hour());
0098         QString minute = QString().sprintf("%0*i", 2, t.minute());
0099         QString second = QString().sprintf("%0*i", 2, t.second());
0100         return QString("%1-%2-%3").arg(hour, minute, second);
0101     } else if (token == "hour") {
0102         return tmp.sprintf("%0*i", 2, t.hour());
0103     } else if (token == "minute") {
0104         return tmp.sprintf("%0*i", 2, t.minute());
0105     } else if (token == "second") {
0106         return tmp.sprintf("%0*i", 2, t.second());
0107     } else {
0108         const QUrl &url = b->files()->at(index).srcUrl();
0109 #if KIO_VERSION >= QT_VERSION_CHECK(5, 69, 0)
0110         KIO::StatJob *statJob = KIO::statDetails(url, KIO::StatJob::DestinationSide, KIO::StatDefaultDetails);
0111 #else
0112         KIO::StatJob *statJob = KIO::stat(url, KIO::StatJob::SourceSide, 2);
0113 #endif
0114         statJob->exec();
0115         if (statJob->error()) {
0116             return QString();
0117         }
0118         KFileItem item(statJob->statResult(), url);
0119         if (token == "user") {
0120             return item.user();
0121         } else if (token == "group") {
0122             return item.group();
0123         } else if (token == "creationdate")
0124             // TODO: Use toDateTime()
0125         {
0126             return time(item.time(KFileItem::ModificationTime).toSecsSinceEpoch(), format);
0127         } else if (token == "modificationdate") {
0128             return time(item.time(KFileItem::ModificationTime).toSecsSinceEpoch(), format);
0129         } else if (token == "accessdate") {
0130             return time(item.time(KFileItem::AccessTime).toSecsSinceEpoch(), format);
0131         } else if (token == "filesize") {
0132             return QString::number(item.size());
0133         }
0134     }
0135 
0136     return QString();
0137 }
0138 
0139 const QString SystemPlugin::time(time_t time, const QString &format)
0140 {
0141     QDateTime dt;
0142     dt.setSecsSinceEpoch(time);
0143     return dt.toString(format);
0144 }