1 /***************************************************************************
2 datetimeplugin.cpp - description
3 -------------------
4 begin : Sun Mar 9 2008
5 copyright : (C) 2002 by Dominik Seichter
6 email : domseichter@web.de
7 ***************************************************************************/
8
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18 #include "datetimeplugin.h"
19
20 #ifndef Q_OS_WIN
21 #include <kiconloader.h>
22
23 #include "ui_datetimepluginwidget.h"
24
25 #include <QDate>
26 #include <QTime>
27 #include <QUrl>
28 #include <qplatformdefs.h>
29
30 // OS includes
31 #include <stdio.h>
32 #include <time.h>
33 #include <utime.h>
34 #include <unistd.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37
DateTimePlugin(PluginLoader * loader)38 DateTimePlugin::DateTimePlugin(PluginLoader *loader)
39 : QObject(nullptr), Plugin(loader)
40 {
41 m_widget = new Ui::DateTimePluginWidget();
42 }
43
~DateTimePlugin()44 DateTimePlugin::~DateTimePlugin()
45 {
46 delete m_widget;
47 }
48
name() const49 const QString DateTimePlugin::name() const
50 {
51 return i18n("Date & Time Plugin");
52 }
53
icon() const54 const QPixmap DateTimePlugin::icon() const
55 {
56 return KIconLoader::global()->loadIcon("chronometer", KIconLoader::NoGroup, KIconLoader::SizeSmall);
57 }
58
processFile(BatchRenamer *,int,const QString & filenameOrToken,EPluginType)59 QString DateTimePlugin::processFile(BatchRenamer *, int, const QString &filenameOrToken, EPluginType)
60 {
61 const QString &filename = filenameOrToken;
62 bool bModification = m_widget->checkModification->isChecked();
63 bool bAccess = m_widget->checkAccess->isChecked();
64
65 QDate date = m_widget->datepicker->date();
66 QTime time(m_widget->spinHour->value(),
67 m_widget->spinMinute->value(),
68 m_widget->spinSecond->value());
69
70 if (!QUrl(filename).isLocalFile()) {
71 return i18n("DateTimePlugin works only with local files. %1 is a remote file.", filename);
72 }
73
74 if (bModification || bAccess) {
75 return changeDateTime(filename, bModification, bAccess, date, time);
76 }
77
78 return QString();
79 }
80
createUI(QWidget * parent) const81 void DateTimePlugin::createUI(QWidget *parent) const
82 {
83 m_widget->setupUi(parent);
84
85 connect(m_widget->buttonCurrent, &QPushButton::clicked,
86 this, &DateTimePlugin::slotGetCurrentTime);
87 }
88
slotGetCurrentTime()89 void DateTimePlugin::slotGetCurrentTime()
90 {
91 m_widget->spinHour->setValue(QTime::currentTime().hour());
92 m_widget->spinMinute->setValue(QTime::currentTime().minute());
93 m_widget->spinSecond->setValue(QTime::currentTime().second());
94 m_widget->datepicker->setDate(QDate::currentDate());
95 }
96
changeDateTime(const QString & filename,bool bModification,bool bAccess,const QDate & date,const QTime & time)97 QString DateTimePlugin::changeDateTime(const QString &filename, bool bModification, bool bAccess,
98 const QDate &date, const QTime &time)
99 {
100 // Initialze fields
101 struct tm tmp;
102 tmp.tm_mday = date.day();
103 tmp.tm_mon = date.month() - 1;
104 tmp.tm_year = date.year() - 1900;
105
106 tmp.tm_hour = time.hour();
107 tmp.tm_min = time.minute();
108 tmp.tm_sec = time.second();
109 tmp.tm_isdst = -1;
110
111 // Create time
112 time_t ti;
113 ti = mktime(&tmp);
114
115 if (ti == -1) {
116 return i18n("Cannot change date of file %1. (Cannot mktime)", filename);
117 }
118
119 // Get current values
120 QT_STATBUF st;
121 if (QT_STAT(filename.toUtf8().data(), &st) == -1) {
122 return i18n("Cannot change date of file %1. (Cannot stat the file)", filename);
123 }
124
125 // Fill structure;
126 struct utimbuf buf;
127
128 buf.actime = (bAccess ? ti : st.st_atime);
129 buf.modtime = (bModification ? ti : st.st_mtime);
130
131 if (utime(filename.toUtf8().data(), &buf) != 0) {
132 return i18n("Cannot change date of file %1. (utime failed)", filename);
133 }
134
135 return QString();
136 }
137
138 #endif // Q_OS_WIN
139