File indexing completed on 2024-04-14 15:51:31

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