KRename – powerful batch renamer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

datetime.cpp 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /***************************************************************************
  2. datetime.cpp - description
  3. -------------------
  4. begin : Mon Jan 7 2002
  5. copyright : (C) 2002 by Dominik Seichter
  6. email : domseichter@web.de
  7. ***************************************************************************/
  8. /***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************/
  16. #include "datetime.h"
  17. // QT includes
  18. #include <tqcheckbox.h>
  19. #include <tqfileinfo.h>
  20. #include <tqlabel.h>
  21. #include <tqlayout.h>
  22. #include <tqpushbutton.h>
  23. // KDE includes
  24. #include <kapplication.h>
  25. #include <kiconloader.h>
  26. #include <kdatepik.h>
  27. #include <knuminput.h>
  28. #include <klocale.h>
  29. // OS includes
  30. #include <stdio.h>
  31. #include <time.h>
  32. #include <utime.h>
  33. #include <unistd.h>
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. const TQString MyDatePlugin::getName() const
  37. {
  38. return i18n("Date & Time");
  39. }
  40. const TQString MyDatePlugin::getAccelName() const
  41. {
  42. return i18n("Date && &Time");
  43. }
  44. const int MyDatePlugin::type() const
  45. {
  46. return TYPE_FINAL_FILE;
  47. }
  48. bool MyDatePlugin::checkError()
  49. {
  50. return true;
  51. }
  52. const TQPixmap MyDatePlugin::getIcon() const
  53. {
  54. return kapp->iconLoader()->loadIcon( "kalarm", KIcon::Small );
  55. }
  56. void MyDatePlugin::drawInterface( TQWidget* w, TQVBoxLayout* l )
  57. {
  58. m_widget = w;
  59. #ifdef BENNY
  60. l->addWidget( new TQLabel("Setze Modifiaction Time um eine Stunde zurück", w) );
  61. #else
  62. Layout0 = new TQHBoxLayout( 0, 0, 6, "Layout0");
  63. Layout1 = new TQVBoxLayout( 0, 0, 6, "Layout1");
  64. Layout2 = new TQHBoxLayout( 0, 0, 6, "Layout2");
  65. kDate = new KDatePicker( w );
  66. checkAccess = new TQCheckBox( w, "checkAccess" );
  67. checkAccess->setText( i18n( "Change &access date && time" ) );
  68. checkModification = new TQCheckBox( w, "checkModification" );
  69. checkModification->setText( i18n( "Change &modification date && time" ) );
  70. labelTime = new TQLabel( w, "labelTime" );
  71. labelTime->setText( i18n( "Time:" ) );
  72. spinHour = new KIntSpinBox( w, "spinHour" );
  73. spinHour->setSuffix( i18n( "h" ) );
  74. spinHour->setMaxValue( 23 );
  75. spinMinute = new KIntSpinBox( w, "spinMinute" );
  76. spinMinute->setSuffix( i18n( "min" ) );
  77. spinMinute->setMaxValue( 59 );
  78. spinSecond = new KIntSpinBox( w, "spinSecond" );
  79. spinSecond->setSuffix( i18n( "s" ) );
  80. spinSecond->setMaxValue( 59 );
  81. buttonCurrentDT = new TQPushButton( w, "buttonCurrentDT" );
  82. buttonCurrentDT->setText( i18n( "&Get Current Date && Time" ) );
  83. Layout2->addWidget( labelTime );
  84. Layout2->addWidget( spinHour );
  85. Layout2->addWidget( spinMinute );
  86. Layout2->addWidget( spinSecond );
  87. Layout1->addWidget( kDate );
  88. Layout1->addWidget( checkAccess );
  89. Layout1->addWidget( checkModification );
  90. Layout1->addLayout( Layout2 );
  91. Layout1->addWidget( buttonCurrentDT );
  92. Layout0->addLayout( Layout1 );
  93. TQSpacerItem* spacer = new TQSpacerItem( 20, 20, TQSizePolicy::Minimum, TQSizePolicy::Expanding );
  94. Layout0->addItem( spacer );
  95. l->addLayout( Layout0 );
  96. connect( buttonCurrentDT, TQT_SIGNAL(clicked()), this, TQT_SLOT(changeDT()));
  97. #endif
  98. }
  99. void MyDatePlugin::fillStructure()
  100. {
  101. #ifndef BENNY
  102. dvals.date = kDate->getDate();
  103. dvals.changeModification = checkModification->isChecked();
  104. dvals.changeAccess = checkAccess->isChecked();
  105. dvals.hour = spinHour->value();
  106. dvals.minute = spinMinute->value();
  107. dvals.second = spinSecond->value();
  108. #endif
  109. }
  110. TQString MyDatePlugin::processFile( BatchRenamer* b, int i, TQString, int )
  111. {
  112. #ifdef BENNY
  113. TQString filename = b->files()[i].dst.name;
  114. FILE * f;
  115. struct utimbuf * t = new utimbuf();
  116. struct tm tmp;
  117. struct stat st;
  118. time_t ti;
  119. f = fopen((const char *)filename, "r");
  120. if( f == NULL )
  121. return TQString( i18n("Can't change date of file %1.") ).tqarg(filename);
  122. fclose( f );
  123. TQFileInfo info( filename );
  124. tmp.tm_mday = info.lastModified().date().day();
  125. tmp.tm_mon = info.lastModified().date().month() - 1;
  126. tmp.tm_year = info.lastModified().date().year() - 1900;
  127. tmp.tm_hour = info.lastModified().time().hour() - 1;
  128. tmp.tm_min = info.lastModified().time().minute();
  129. tmp.tm_sec = info.lastModified().time().second();
  130. tmp.tm_isdst = -1;
  131. ti = mktime( &tmp );
  132. if( ti == -1 )
  133. return TQString( i18n("Can't change date of file %1.") ).tqarg(filename);
  134. if( stat( (const char *)filename, &st ) == -1 )
  135. return TQString( i18n("Can't change date of file %1.") ).tqarg(filename);
  136. t->actime = st.st_atime;
  137. t->modtime = ti;
  138. if(utime( (const char *)filename, t ) != 0)
  139. return TQString( i18n("Can't change date of file %1.") ).tqarg(filename);
  140. return TQString();
  141. #else
  142. TQString filename = b->files()[i].dst.name;
  143. FILE * f;
  144. struct utimbuf * t = new utimbuf();
  145. struct tm tmp;
  146. struct stat st;
  147. time_t ti;
  148. f = fopen((const char *)filename, "r");
  149. if( f == NULL )
  150. return TQString( i18n("Can't change date of file %1.") ).tqarg(filename);
  151. fclose( f );
  152. tmp.tm_mday = dvals.date.day();
  153. tmp.tm_mon = dvals.date.month() - 1;
  154. tmp.tm_year = dvals.date.year() - 1900;
  155. tmp.tm_hour = dvals.hour;
  156. tmp.tm_min = dvals.minute;
  157. tmp.tm_sec = dvals.second;
  158. tmp.tm_isdst = -1;
  159. ti = mktime( &tmp );
  160. if( ti == -1 )
  161. return TQString( i18n("Can't change date of file %1.") ).tqarg(filename);
  162. if( stat( (const char *)filename, &st ) == -1 )
  163. return TQString( i18n("Can't change date of file %1.") ).tqarg(filename);
  164. if(dvals.changeAccess)
  165. t->actime = ti;
  166. else
  167. t->actime = st.st_atime;
  168. if(dvals.changeModification)
  169. t->modtime = ti;
  170. else
  171. t->modtime = st.st_mtime;
  172. if(utime( (const char *)filename, t ) != 0)
  173. return TQString( i18n("Can't change date of file %1.") ).tqarg(filename);
  174. return TQString();
  175. #endif
  176. }
  177. void MyDatePlugin::changeDT()
  178. {
  179. spinHour->setValue( TQTime::currentTime().hour());
  180. spinMinute->setValue( TQTime::currentTime().minute());
  181. spinSecond->setValue( TQTime::currentTime().second());
  182. kDate->setDate( TQDate::tqcurrentDate() );
  183. }