File indexing completed on 2024-04-21 16:32:36

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