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.

216 lines
6.0KB

  1. /***************************************************************************
  2. fileplugin.cpp - description
  3. -------------------
  4. begin : Mon Jul 1 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 "fileplugin.h"
  17. // TQt includes
  18. #include <tqcheckbox.h>
  19. #include <tqlabel.h>
  20. #include <tqlayout.h>
  21. #include <tqregexp.h>
  22. // KDE includes
  23. #include <tdeapplication.h>
  24. #include <tdefilemetainfo.h>
  25. #include <klineedit.h>
  26. #include <tdelistbox.h>
  27. #include <tdelocale.h>
  28. #include <kiconloader.h>
  29. #include <kpushbutton.h>
  30. FilePlugin::FilePlugin( KService* service )
  31. {
  32. if(!service) {
  33. setupKeys();
  34. return;
  35. }
  36. KFileMetaInfoProvider* mip = KFileMetaInfoProvider::self();
  37. m_name = service->name();
  38. m_comment = service->comment();
  39. TQStringList options = service->serviceTypes();
  40. for( unsigned int i = 0; i < options.count(); i++ ) {
  41. if( options[i] != "KFilePlugin" ) {
  42. m_mimetype = options[i];
  43. const KFileMimeTypeInfo* info = mip->mimeTypeInfo( m_mimetype );
  44. if( info )
  45. keys = info->supportedKeys();
  46. fileplugin = mip->plugin( m_mimetype );
  47. KMimeType::Ptr mime = KMimeType::mimeType( m_mimetype );
  48. m_icon = mime->icon( TQString(), true ); // arguments are unused
  49. setPattern( mime );
  50. }
  51. }
  52. }
  53. FilePlugin::~FilePlugin()
  54. { }
  55. void FilePlugin::setPattern( KMimeType::Ptr mime )
  56. {
  57. TQStringList pattern = mime->patterns();
  58. if( pattern.count() ) {
  59. m_pattern = pattern[0];
  60. if( m_pattern.startsWith( "*." ) )
  61. m_pattern = m_pattern.right( m_pattern.length() - 2 );
  62. }
  63. // TODO: REFACTOR
  64. // We need a pattern
  65. if( m_pattern.isEmpty() ) {
  66. int a = 0;
  67. a = m_name.find( "-" );
  68. if( a > -1 )
  69. m_pattern = m_name.left( a ).lower();
  70. else {
  71. a = m_pattern.find( " " );
  72. if( a > -1 )
  73. m_pattern = m_name.left( a ).lower();
  74. else
  75. m_pattern = m_name;
  76. }
  77. }
  78. setupKeys();
  79. }
  80. void FilePlugin::setupKeys()
  81. {
  82. for( unsigned int i = 0; i < keys.count(); i++ )
  83. keys[i] = getPattern() + keys[i];
  84. }
  85. const TQString FilePlugin::getName() const
  86. {
  87. return m_name;
  88. }
  89. const TQString FilePlugin::getAccelName() const
  90. {
  91. return "&" + getName();
  92. }
  93. const TQString FilePlugin::getPattern() const
  94. {
  95. return m_pattern;
  96. }
  97. const int FilePlugin::type() const
  98. {
  99. return TYPE_BRACKET;
  100. }
  101. bool FilePlugin::checkError()
  102. {
  103. return true;
  104. }
  105. void FilePlugin::drawInterface( TQWidget* w, TQVBoxLayout* l )
  106. {
  107. TQSpacerItem* spacer = new TQSpacerItem( 20, 20, TQSizePolicy::Expanding, TQSizePolicy::Expanding );
  108. TQHBoxLayout* hbox = new TQHBoxLayout( 0, 6, 6 );
  109. TQLabel* pix = new TQLabel( w );
  110. pix->setPixmap( kapp->iconLoader()->loadIcon( m_icon, TDEIcon::Desktop ) );
  111. hbox->addWidget( pix );
  112. hbox->addWidget( new TQLabel( "<qt><b>"+getName()+"</b></qt>", w ) );
  113. hbox->addItem( spacer );
  114. l->addLayout( hbox );
  115. l->addWidget( new TQLabel( m_comment, w ) );
  116. l->addWidget( new TQLabel( i18n("Supported tokens:"), w ) );
  117. TDEListBox* list = new TDEListBox( w );
  118. list->setColumnMode( TDEListBox::FitToWidth );
  119. for( unsigned int i = 0; i < keys.count(); i++ )
  120. list->insertItem( "[" + keys[i] + "]" );
  121. l->addWidget( list );
  122. l->setStretchFactor( list, 2 );
  123. }
  124. TQString FilePlugin::processFile( BatchRenamer* b, int i, TQString token, int )
  125. {
  126. TQString filename = BatchRenamer::buildFilename( &b->files()[i].src );
  127. token = token.lower();
  128. /*
  129. * Check if we have something cached for this file
  130. */
  131. if( cache.contains( filename + "::" + token ) )
  132. return cache[filename + "::" + token ];
  133. for( unsigned int i = 0; i < keys.count(); i++ ) {
  134. if( token.lower() == keys[i].lower() ) {
  135. KFileMetaInfo meta( filename );
  136. if( meta.isValid() ) {
  137. TQString k = keys[i];
  138. if( k.startsWith( getPattern() ) )
  139. k = k.mid( getPattern().length(), k.length() - getPattern().length() );
  140. TQString ret = meta.item( k ).string( true ).stripWhiteSpace();
  141. if( cache.count() >= CACHE_MAX )
  142. cache.remove( cache.begin() );
  143. cache.insert( filename + "::" + token, ret );
  144. kapp->processEvents();
  145. return ret;
  146. }
  147. }
  148. }
  149. return TQString();
  150. }
  151. void FilePlugin::addHelp( HelpDialogData* data )
  152. {
  153. TQStringList list;
  154. for( unsigned int i = 0; i < keys.count(); i++ )
  155. list.append( "[" + keys[i] + "]" + ";;" + keys[i] );
  156. data->add( getName(), &list, getIcon() );
  157. }
  158. const TQPixmap FilePlugin::getIcon() const
  159. {
  160. return kapp->iconLoader()->loadIcon( m_icon, TDEIcon::Small );
  161. }
  162. bool FilePlugin::supports( const TQString & token )
  163. {
  164. for( unsigned int i = 0; i < keys.count(); i++ )
  165. if( TQRegExp( keys[i].lower() ).exactMatch( token.lower() ) )
  166. return true;
  167. return false;
  168. }
  169. void FilePlugin::clearCache()
  170. {
  171. cache.clear();
  172. }
  173. #include "fileplugin.moc"