File indexing completed on 2024-04-21 05:51:42

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2008 Dominik Seichter <domseichter@web.de>
0003 
0004 #include "datetimeplugin.h"
0005 
0006 #ifndef Q_OS_WIN
0007 #include "ui_datetimepluginwidget.h"
0008 
0009 #include <QDate>
0010 #include <QTime>
0011 #include <QUrl>
0012 #include <qplatformdefs.h>
0013 
0014 // OS includes
0015 #include <cstdio>
0016 #include <ctime>
0017 #include <sys/stat.h>
0018 #include <sys/types.h>
0019 #include <unistd.h>
0020 #include <utime.h>
0021 
0022 DateTimePlugin::DateTimePlugin(PluginLoader *loader)
0023     : QObject(nullptr), Plugin(loader)
0024 {
0025     m_widget = new Ui::DateTimePluginWidget();
0026 }
0027 
0028 DateTimePlugin::~DateTimePlugin()
0029 {
0030     delete m_widget;
0031 }
0032 
0033 const QString DateTimePlugin::name() const
0034 {
0035     return i18n("Date & Time Plugin");
0036 }
0037 
0038 const QIcon DateTimePlugin::icon() const
0039 {
0040     return QIcon::fromTheme("chronometer");
0041 }
0042 
0043 QString DateTimePlugin::processFile(BatchRenamer *, int, const QString &filenameOrToken, EPluginType)
0044 {
0045     const QString &filename = filenameOrToken;
0046     bool  bModification = m_widget->checkModification->isChecked();
0047     bool  bAccess       = m_widget->checkAccess->isChecked();
0048 
0049     QDate date          = m_widget->datepicker->date();
0050     QTime time(m_widget->spinHour->value(),
0051                m_widget->spinMinute->value(),
0052                m_widget->spinSecond->value());
0053 
0054     if (!QUrl(filename).isLocalFile()) {
0055         return i18n("DateTimePlugin works only with local files. %1 is a remote file.", filename);
0056     }
0057 
0058     if (bModification || bAccess) {
0059         return changeDateTime(filename, bModification, bAccess, date, time);
0060     }
0061 
0062     return QString();
0063 }
0064 
0065 void DateTimePlugin::createUI(QWidget *parent) const
0066 {
0067     m_widget->setupUi(parent);
0068 
0069     connect(m_widget->buttonCurrent, &QPushButton::clicked,
0070             this, &DateTimePlugin::slotGetCurrentTime);
0071 }
0072 
0073 void DateTimePlugin::slotGetCurrentTime()
0074 {
0075     m_widget->spinHour->setValue(QTime::currentTime().hour());
0076     m_widget->spinMinute->setValue(QTime::currentTime().minute());
0077     m_widget->spinSecond->setValue(QTime::currentTime().second());
0078     m_widget->datepicker->setDate(QDate::currentDate());
0079 }
0080 
0081 QString DateTimePlugin::changeDateTime(const QString &filename, bool bModification, bool bAccess,
0082                                        const QDate &date, const QTime &time)
0083 {
0084     // Initialze fields
0085     struct tm tmp;
0086     tmp.tm_mday = date.day();
0087     tmp.tm_mon  = date.month() - 1;
0088     tmp.tm_year = date.year() - 1900;
0089 
0090     tmp.tm_hour = time.hour();
0091     tmp.tm_min  = time.minute();
0092     tmp.tm_sec  = time.second();
0093     tmp.tm_isdst = -1;
0094 
0095     // Create time
0096     time_t ti;
0097     ti = mktime(&tmp);
0098 
0099     if (ti == -1) {
0100         return i18n("Cannot change date of file %1. (Cannot mktime)", filename);
0101     }
0102 
0103     // Get current values
0104     QT_STATBUF st;
0105     if (QT_STAT(filename.toUtf8().data(), &st) == -1) {
0106         return i18n("Cannot change date of file %1. (Cannot stat the file)", filename);
0107     }
0108 
0109     // Fill structure;
0110     struct utimbuf buf;
0111 
0112     buf.actime  = (bAccess ? ti : st.st_atime);
0113     buf.modtime = (bModification ? ti : st.st_mtime);
0114 
0115     if (utime(filename.toUtf8().data(), &buf) != 0) {
0116         return i18n("Cannot change date of file %1. (utime failed)", filename);
0117     }
0118 
0119     return QString();
0120 }
0121 
0122 #include "moc_datetimeplugin.cpp"
0123 
0124 #endif // Q_OS_WIN