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.

dateplugin.cpp 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /***************************************************************************
  2. dateplugin.cpp - description
  3. -------------------
  4. begin : Mon Feb 02 2004
  5. copyright : (C) 2004 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 "dateplugin.h"
  17. // TQt includes
  18. #include <tqdatetime.h>
  19. // KDE includes
  20. #include <tdefileitem.h>
  21. #include <tdeio/netaccess.h>
  22. #include <tdelocale.h>
  23. DatePlugin::DatePlugin()
  24. : FilePlugin(0)
  25. {
  26. keys.append("date");
  27. keys.append("date;.*");
  28. keys.append("year");
  29. keys.append("month");
  30. keys.append("day");
  31. keys.append("time");
  32. keys.append("hour");
  33. keys.append("minute");
  34. keys.append("second");
  35. keys.append("user");
  36. keys.append("group");
  37. keys.append("creationdate");
  38. keys.append("creationdate;.*");
  39. keys.append("modificationdate");
  40. keys.append("modificationdate;.*");
  41. keys.append("accessdate");
  42. keys.append("accessdate;.*");
  43. setupKeys();
  44. m_icon = "clock";
  45. }
  46. TQString DatePlugin::processFile(BatchRenamer* b, int i, TQString token, int )
  47. {
  48. if( !supports( token ) )
  49. return TQString();
  50. if( token.lower().startsWith( getPattern() ) )
  51. token = token.mid( getPattern().length(), token.length() - getPattern().length() );
  52. TQDate d = TQDate::currentDate();
  53. TQTime t = TQTime::currentTime();
  54. TQString tmp, text;
  55. TQString format = "dd-MM-yyyy";
  56. if( token.contains( ";" ) )
  57. {
  58. format = token.section( ';', 1, 1 );
  59. token = token.section( ';', 0, 0 ).lower();
  60. } else
  61. token = token.lower();
  62. if( token == "date" ) {
  63. return TQDateTime::currentDateTime().toString( format );
  64. } else if( token == "year" )
  65. return TQString( "%1" ).arg( d.year() );
  66. else if( token == "month" )
  67. return tmp.sprintf("%0*i", 2, d.month() );
  68. else if( token == "day" )
  69. return tmp.sprintf("%0*i", 2, d.day() );
  70. else if( token == "time" )
  71. return TQString( "%1-%2-%3" ).arg( t.hour() ).arg( TQString().sprintf("%0*i", 2, t.minute() ) ).arg( TQString().sprintf("%0*i", 2, t.second() ) );
  72. else if( token == "hour" )
  73. return tmp.sprintf("%0*i", 2, t.hour() );
  74. else if( token == "minute" )
  75. return tmp.sprintf("%0*i", 2, t.minute() );
  76. else if( token == "second" )
  77. return tmp.sprintf("%0*i", 2, t.second() );
  78. else {
  79. TDEIO::UDSEntry entry;
  80. TDEIO::NetAccess::stat(b->files()[i].src.url, entry, 0);
  81. KFileItem item( entry, b->files()[i].src.url );
  82. if( token == "user" )
  83. return item.user();
  84. else if( token == "group" )
  85. return item.group();
  86. else if( token == "creationdate" )
  87. return time( item.time( TDEIO::UDS_CREATION_TIME ), format );
  88. else if( token == "modificationdate" )
  89. return time( item.time( TDEIO::UDS_MODIFICATION_TIME ), format );
  90. else if( token == "accessdate" )
  91. return time( item.time( TDEIO::UDS_ACCESS_TIME ), format );
  92. }
  93. return TQString();
  94. }
  95. const TQString DatePlugin::getAccelName() const
  96. {
  97. return i18n("&System Functions");
  98. }
  99. const TQString DatePlugin::getName() const
  100. {
  101. return i18n("System Functions");
  102. }
  103. const TQString DatePlugin::getPattern() const
  104. {
  105. return TQString();
  106. }
  107. const TQString DatePlugin::time( time_t time, const TQString & format )
  108. {
  109. TQDateTime dt;
  110. dt.setTime_t( time );
  111. return dt.toString( format );
  112. }
  113. void DatePlugin::addHelp( HelpDialogData* data )
  114. {
  115. TQStringList list;
  116. list.append( "[date];;" + i18n("Insert the current date") );
  117. list.append( "[date;yyyy-MM-dd];;" + i18n("Insert the current date using the formatting string yyyy-MM-dd") );
  118. list.append( "[year];;" + i18n("Insert the current year") );
  119. list.append( "[month];;" + i18n("Insert the current month as number") );
  120. list.append( "[day];;" + i18n("Insert the current day as number") );
  121. list.append( "[time];;" + i18n("Insert the current time") );
  122. list.append( "[hour];;" + i18n("Insert the current hour as number") );
  123. list.append( "[minute];;" + i18n("Insert the current minute as number") );
  124. list.append( "[second];;" + i18n("Insert the current second as number") );
  125. list.append( "[user];;" + i18n("Owner of the file") );
  126. list.append( "[group];;" + i18n("Owning group of the file") );
  127. list.append( "[creationdate];;" + i18n("Insert the files creation date"));
  128. list.append( "[creationdate;yyyy-MM-dd];;" + i18n("Insert the formatted file creation date") );
  129. list.append( "[modificationdate];;" + i18n("Insert the files modification date"));
  130. list.append( "[modificationdate;yyyy-MM-dd];;" + i18n("Insert the formatted modification date") );
  131. list.append( "[accessdate];;" + i18n("Insert the date of the last file access") );
  132. list.append( "[accessdate;yyyy-MM-dd];;" + i18n("Insert the formatted date of the last file access") );
  133. data->add( getName(), &list, getIcon() );
  134. }
  135. #include "dateplugin.moc"