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.

profiledlg.cpp 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /***************************************************************************
  2. profiledlg.cpp - description
  3. -------------------
  4. begin : Sat Nov 20 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 "profiledlg.h"
  17. #include "krenameimpl.h"
  18. #include "kmyhistorycombo.h"
  19. #include "pluginloader.h"
  20. #include "kmylistbox.h"
  21. #include <tdeapplication.h>
  22. #include <kcombobox.h>
  23. #include <tdeconfig.h>
  24. #include <kiconloader.h>
  25. #include <kinputdialog.h>
  26. #include <tdelistbox.h>
  27. #include <tdelocale.h>
  28. #include <tdemessagebox.h>
  29. #include <kpushbutton.h>
  30. #include <kstandarddirs.h>
  31. #include <kurlrequester.h>
  32. #include <tqcheckbox.h>
  33. #include <tqdom.h>
  34. #include <tqfile.h>
  35. #include <tqlayout.h>
  36. #include <tqpainter.h>
  37. #include <tqradiobutton.h>
  38. #include <tqtooltip.h>
  39. #ifdef HAVE_CONFIG_H
  40. #include <config.h>
  41. #endif
  42. #ifndef VERSION
  43. #define VERSION "unknown"
  44. #endif
  45. #define PROFILE_WIZARD_INDEX 0
  46. #define PROFILE_TABBED_INDEX 1
  47. #define PROFILE_WIZARD_NAME i18n("KRename: Wizard default profile")
  48. #define PROFILE_TABBED_NAME i18n("KRename: Tabbed default profile")
  49. class ProfileListBoxText : public TQListBoxText {
  50. public:
  51. ProfileListBoxText( const TQString & text = TQString() )
  52. : TQListBoxText( text )
  53. {
  54. m_default = false;
  55. }
  56. inline bool isDefault() const {
  57. return m_default;
  58. }
  59. inline void setDefault( const bool b ) {
  60. m_default = b;
  61. }
  62. protected:
  63. void paint( TQPainter* painter )
  64. {
  65. TQFont f = painter->font();
  66. f.setBold( m_default );
  67. painter->setFont( f );
  68. TQListBoxText::paint( painter );
  69. }
  70. private:
  71. bool m_default;
  72. };
  73. ProfileManager::ProfileManager( KRenameImpl* krename )
  74. : m_krename( krename )
  75. {
  76. }
  77. const TQString ProfileManager::readProfilePath( const TQString & name )
  78. {
  79. TQString path;
  80. TDEConfig* conf = kapp->config();
  81. conf->setGroup( "Profiles" );
  82. path = conf->readEntry( name, TQString() );
  83. return path;
  84. }
  85. const TQString ProfileManager::getProfilePath( const TQString & name )
  86. {
  87. TQStringList list;
  88. if( name == PROFILE_WIZARD_NAME )
  89. return locate( "data", "krename/krename_system_default_wizard.xml" );
  90. else if( name == PROFILE_TABBED_NAME )
  91. return locate( "data", "krename/krename_system_default_tabbed.xml" );
  92. TQString path = locateLocal( "data", TQString( "krename/%1.xml" ).arg( name ) );
  93. TDEConfig* conf = kapp->config();
  94. conf->setGroup( "ProfilesHeader" );
  95. list = conf->readListEntry( "list" );
  96. if( !list.contains( name ) )
  97. list.append( name );
  98. conf->writeEntry( "list", list );
  99. conf->setGroup( "Profiles" );
  100. conf->writeEntry( name, path );
  101. conf->sync();
  102. return path;
  103. }
  104. void ProfileManager::writeXML( const TQString & name )
  105. {
  106. TQString path = getProfilePath( name );
  107. TQFile file( path );
  108. if( !file.open( IO_WriteOnly ) )
  109. {
  110. tqDebug("Cannot write to: %s", path.latin1() );
  111. return;
  112. }
  113. TQDomDocument doc( "KRenameProfile" );
  114. TQDomElement root = doc.createElement( "krename" );
  115. doc.appendChild( root );
  116. TQDomElement v = doc.createElement( "version" );
  117. v.setAttribute( "version", VERSION );
  118. root.appendChild( v );
  119. // General settings of Krename
  120. TQDomElement settings = doc.createElement( "settings" );
  121. settings.setAttribute( "wizard", m_krename->m_wizard );
  122. settings.setAttribute( "fileplugins", m_krename->plugin->filePluginsLoaded() );
  123. root.appendChild( settings );
  124. // Filename settings
  125. TQDomElement filename = doc.createElement( "filename" );
  126. filename.setAttribute( "filename", m_krename->filename->text() );
  127. filename.setAttribute( "extension", m_krename->extemplate->text() );
  128. filename.setAttribute( "extension_enabled", m_krename->checkExtension->isChecked() );
  129. filename.setAttribute( "extension_start", m_krename->comboExtension->currentItem() );
  130. TQDomElement numbering = doc.createElement( "numbering" );
  131. numbering.setAttribute( "start", m_krename->m_index );
  132. numbering.setAttribute( "step", m_krename->m_step );
  133. numbering.setAttribute( "skip", listToString( m_krename->skip ) );
  134. numbering.setAttribute( "reset", m_krename->m_reset );
  135. TQDomElement replace = doc.createElement( "replace" );
  136. for( unsigned int i=0;i<m_krename->rep.count();i++)
  137. {
  138. TQDomElement r = doc.createElement( "item" );
  139. r.setAttribute( "find", m_krename->rep[i].find );
  140. r.setAttribute( "replace", m_krename->rep[i].replace );
  141. r.setAttribute( "reg", m_krename->rep[i].reg );
  142. replace.appendChild( r );
  143. }
  144. // destination settings
  145. TQDomElement dest = doc.createElement( "destination" );
  146. dest.setAttribute( "mode", m_krename->currentRenameMode() );
  147. dest.setAttribute( "overwrite", TQString::number( m_krename->checkOverwrite->isChecked() ) );
  148. dest.setAttribute( "directory", m_krename->dirname->text() );
  149. dest.setAttribute( "undo", TQString::number( m_krename->checkUndoScript->isChecked() ) );
  150. dest.setAttribute( "undoscript", m_krename->undorequester->url() );
  151. // file adding settings
  152. TQDomElement files = doc.createElement( "files" );
  153. files.setAttribute( "sorting", TQString::number( m_krename->comboSort->currentItem() ) );
  154. files.setAttribute( "preview", TQString::number( m_krename->checkPreview->isChecked() ) );
  155. files.setAttribute( "names", TQString::number( m_krename->checkName->isChecked() ) );
  156. filename.appendChild( replace );
  157. filename.appendChild( numbering );
  158. root.appendChild( settings );
  159. root.appendChild( filename );
  160. root.appendChild( dest );
  161. root.appendChild( files );
  162. TQCString xml = doc.toCString();
  163. file.writeBlock( xml, xml.length() );
  164. file.close();
  165. }
  166. bool ProfileManager::loadXML( const TQString & path )
  167. {
  168. TQFile file( path );
  169. if( !file.open( IO_ReadOnly ) )
  170. {
  171. tqDebug("Cannot read from: %s", path.latin1() );
  172. return false;
  173. }
  174. TQDomDocument doc( "KRenameProfile" );
  175. if ( !doc.setContent( &file ) )
  176. {
  177. file.close();
  178. return false;
  179. }
  180. TQDomNode n = doc.documentElement().firstChild();
  181. while( !n.isNull() )
  182. {
  183. TQDomElement e = n.toElement(); // try to convert the node to an element.
  184. if( !e.isNull() )
  185. {
  186. if( e.tagName() == "settings" )
  187. {
  188. bool wiz, plug;
  189. wiz = (bool)e.attribute( "wizard",
  190. TQString( "%1" ).arg( m_krename->m_wizard ) ).toInt();
  191. m_krename->setWizardMode( wiz );
  192. // if( wiz != m_krename->m_wizard )
  193. {
  194. m_krename->m_wizard = wiz;
  195. m_krename->changeGUIMode();
  196. }
  197. plug = (bool)e.attribute( "fileplugins",
  198. TQString( "%1" ).arg( m_krename->plugin->filePluginsLoaded() ) ).toInt();
  199. if( plug && !m_krename->plugin->filePluginsLoaded() )
  200. m_krename->plugin->loadPlugins( true );
  201. }
  202. else if( e.tagName() == "filename" )
  203. {
  204. m_krename->filename->setText( e.attribute("filename", m_krename->filename->text() ) );
  205. m_krename->extemplate->setText( e.attribute("extension", m_krename->extemplate->text() ) );
  206. m_krename->checkExtension->setChecked(
  207. (bool)e.attribute("extension_enabled",
  208. TQString("%1").arg(m_krename->checkExtension->isChecked() ) ).toInt() );
  209. m_krename->comboExtension->setCurrentItem(
  210. e.attribute("extension_start",
  211. TQString::number( m_krename->comboExtension->currentItem() ) ).toInt() );
  212. TQDomNode n = e.firstChild();
  213. while( !n.isNull() )
  214. {
  215. TQDomElement e = n.toElement(); // try to convert the node to an element.
  216. if( !e.isNull() )
  217. {
  218. if( e.tagName() == "numbering" )
  219. {
  220. m_krename->m_index = e.attribute( "start", TQString::number( m_krename->m_index ) ).toInt();
  221. m_krename->m_step = e.attribute( "step", TQString::number( m_krename->m_step ) ).toInt();
  222. m_krename->skip = stringToList( e.attribute("skip", listToString( m_krename->skip ) ) );
  223. m_krename->m_reset = (bool)e.attribute( "reset", TQString::number( (int)m_krename->m_reset ) ).toInt();
  224. }
  225. else if( e.tagName() == "replace" )
  226. {
  227. m_krename->rep.clear();
  228. TQDomNode n = e.firstChild();
  229. while( !n.isNull() )
  230. {
  231. TQDomElement e = n.toElement(); // try to convert the node to an element.
  232. if( !e.isNull() && e.tagName() == "item" )
  233. {
  234. replacestrings r;
  235. r.find = e.attribute( "find", TQString() );
  236. r.replace = e.attribute( "replace", TQString() );
  237. r.reg = (bool)e.attribute( "reg", "0" ).toInt();
  238. m_krename->rep.append( r );
  239. }
  240. n = n.nextSibling();
  241. }
  242. }
  243. }
  244. n = n.nextSibling();
  245. }
  246. }
  247. else if( e.tagName() == "destination" )
  248. {
  249. int mode = e.attribute( "mode", TQString::number( m_krename->currentRenameMode() ) ).toInt();
  250. m_krename->optionRename->setChecked( false );
  251. m_krename->optionCopy->setChecked( false );
  252. m_krename->optionMove->setChecked( false );
  253. m_krename->optionLink->setChecked( false );
  254. switch( mode )
  255. {
  256. default:
  257. case RENAME:
  258. m_krename->optionRename->setChecked( true ); break;
  259. case COPY:
  260. m_krename->optionCopy->setChecked( true ); break;
  261. case MOVE:
  262. m_krename->optionMove->setChecked( true ); break;
  263. case LINK:
  264. m_krename->optionLink->setChecked( true ); break;
  265. }
  266. m_krename->checkOverwrite->setChecked( e.attribute( "overwrite",
  267. TQString::number( m_krename->checkOverwrite->isChecked() ) ).toInt() );
  268. m_krename->dirname->setText( e.attribute( "directory", m_krename->dirname->text() ) );
  269. m_krename->checkUndoScript->setChecked( e.attribute( "undo",
  270. TQString::number( m_krename->checkUndoScript->isChecked() ) ).toInt() );
  271. m_krename->undorequester->setURL( e.attribute( "undoscript", m_krename->undorequester->url() ) );
  272. }
  273. else if( e.tagName() == "files" )
  274. {
  275. m_krename->comboSort->setCurrentItem( e.attribute( "sorting",
  276. TQString::number( m_krename->comboSort->currentItem() ) ).toInt() );
  277. m_krename->checkPreview->setChecked( e.attribute( "preview",
  278. TQString::number( m_krename->checkPreview->isChecked() ) ).toInt() );
  279. m_krename->checkName->setChecked( e.attribute( "names",
  280. TQString::number( m_krename->checkName->isChecked() ) ).toInt() );
  281. }
  282. }
  283. n = n.nextSibling();
  284. }
  285. if( m_krename->m_wizard )
  286. m_krename->parseWizardMode();
  287. file.close();
  288. return true;
  289. }
  290. const TQString ProfileManager::listToString( TQValueList<int> & list )
  291. {
  292. TQString data;
  293. for( unsigned int i = 0; i < list.count(); i++ )
  294. data += TQString( "%1;" ).arg( list[i] );
  295. return data;
  296. }
  297. const TQValueList<int> ProfileManager::stringToList( const TQString & data )
  298. {
  299. TQValueList<int> list;
  300. int c = data.contains( ";" );
  301. if( c > 0 )
  302. {
  303. for( int i = 0;i<c;i++)
  304. list.append( data.section( ';', i, i ).toInt() );
  305. }
  306. return list;
  307. }
  308. bool ProfileManager::hasDefaultProfile()
  309. {
  310. TDEConfig* conf = kapp->config();
  311. conf->setGroup( "ProfilesHeader" );
  312. TQString def = conf->readEntry( "defprofile", TQString() );
  313. return (!def.isEmpty());
  314. }
  315. void ProfileManager::loadDefaultProfile( KRenameImpl* krename )
  316. {
  317. TDEConfig* conf = kapp->config();
  318. conf->setGroup( "ProfilesHeader" );
  319. TQString def = conf->readEntry( "defprofile", TQString() );
  320. if( !def.isEmpty() )
  321. ProfileManager::loadProfile( def, krename );
  322. }
  323. void ProfileManager::loadProfile( const TQString & name, KRenameImpl* krename )
  324. {
  325. ProfileManager manager( krename );
  326. manager.loadXML( manager.getProfilePath( name ) );
  327. }
  328. ///////////////////////////////////////////////////////////////////////////////
  329. ProfileDlg::ProfileDlg(KRenameImpl* krename, TQWidget *parent, const char *name)
  330. : KDialogBase( KDialogBase::Plain, i18n("Profiles"),
  331. KDialogBase::Close, KDialogBase::Close, parent, name, true, true ), ProfileManager( krename )
  332. {
  333. int i;
  334. TQHBoxLayout* layout = new TQHBoxLayout( plainPage(), 6, 6 );
  335. TQVBoxLayout* button = new TQVBoxLayout( 0, 6, 6 );
  336. profiles = new TDEListBox( plainPage() );
  337. profiles->insertItem( new ProfileListBoxText( PROFILE_WIZARD_NAME ), PROFILE_WIZARD_INDEX );
  338. profiles->insertItem( new ProfileListBoxText( PROFILE_TABBED_NAME ), PROFILE_TABBED_INDEX );
  339. createProfile = new KPushButton( i18n("&Save As Profile..."), plainPage() );
  340. loadProfile = new KPushButton( i18n("&Load Profile"), plainPage() );
  341. deleteProfile = new KPushButton( i18n("&Delete Profile"), plainPage() );
  342. checkDefault = new TQCheckBox( i18n("&Use as default profile on startup"), plainPage() );
  343. createProfile->setIconSet( SmallIconSet( "document-save-as") );
  344. loadProfile->setIconSet( SmallIconSet( "document-open" ) );
  345. deleteProfile->setIconSet( SmallIconSet( "edittrash" ) );
  346. TQSpacerItem* spacer = new TQSpacerItem( 20, 20, TQSizePolicy::Expanding, TQSizePolicy::Expanding );
  347. button->addWidget( createProfile );
  348. button->addWidget( loadProfile );
  349. button->addWidget( deleteProfile );
  350. button->addItem( spacer );
  351. button->addWidget( checkDefault );
  352. layout->addWidget( profiles );
  353. layout->addLayout( button );
  354. layout->setStretchFactor( profiles, 2 );
  355. TQToolTip::add( createProfile, i18n("<qt>Save KRename's current settings as a new profile. "
  356. "The settings are saved and can be restored with Load Profile later.</qt>" ) );
  357. TQToolTip::add( loadProfile, i18n("<qt>Load all settings stored in this profile.</qt>") );
  358. enableControls();
  359. connect( createProfile, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotCreateProfile() ) );
  360. connect( loadProfile, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotLoadProfile() ) );
  361. connect( deleteProfile, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotDeleteProfile() ) );
  362. connect( profiles, TQT_SIGNAL( selectionChanged () ), this, TQT_SLOT( enableControls() ) );
  363. connect( checkDefault, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotSetDefault() ) );
  364. connect( this, TQT_SIGNAL( hidden() ), this, TQT_SLOT( slotHidden() ) );
  365. TDEConfig* conf = kapp->config();
  366. conf->setGroup( "ProfilesHeader" );
  367. ProfileListBoxText* item;
  368. TQStringList list = conf->readListEntry( "list" );
  369. TQString def = conf->readEntry( "defprofile", TQString() );
  370. for( i=0;i<(int)list.count();i++ )
  371. profiles->insertItem( new ProfileListBoxText( list[i] ) );
  372. for( i=0;i<(int)profiles->count();i++ )
  373. {
  374. item = static_cast<ProfileListBoxText*>(profiles->item(i));
  375. if( item->text() == def )
  376. {
  377. item->setDefault( true);
  378. break;
  379. }
  380. }
  381. profiles->repaintContents();
  382. }
  383. ProfileDlg::~ProfileDlg()
  384. {
  385. }
  386. void ProfileDlg::enableControls()
  387. {
  388. ProfileListBoxText* item;
  389. loadProfile->setEnabled( profiles->selectedItem() );
  390. // Do not allow to delete the two default profiles
  391. deleteProfile->setEnabled( profiles->selectedItem() &&
  392. profiles->currentItem() != PROFILE_TABBED_INDEX && profiles->currentItem() != PROFILE_WIZARD_INDEX );
  393. checkDefault->setEnabled( profiles->selectedItem() );
  394. item = static_cast<ProfileListBoxText*>(profiles->selectedItem());
  395. checkDefault->setChecked( item && item->isDefault() );
  396. }
  397. void ProfileDlg::slotSetDefault()
  398. {
  399. int i;
  400. ProfileListBoxText* item;
  401. for( i=0;i<(int)profiles->count();i++ )
  402. {
  403. item = static_cast<ProfileListBoxText*>(profiles->item( i ));
  404. item->setDefault( false );
  405. }
  406. item = static_cast<ProfileListBoxText*>(profiles->selectedItem());
  407. if( item )
  408. item->setDefault( checkDefault->isChecked() );
  409. profiles->repaintContents();
  410. }
  411. void ProfileDlg::slotLoadProfile()
  412. {
  413. TQString profile = profiles->currentText();
  414. TQString msg = i18n("Do you really want to load the profile and overwrite the current settings: %1").arg( profile );
  415. TQString path = getProfilePath( profile );
  416. if( path.isEmpty() )
  417. {
  418. KMessageBox::error( this, i18n("The profile \"%1\" could not be found.").arg( profile ) );
  419. return;
  420. }
  421. if( KMessageBox::questionYesNo( this, msg ) == KMessageBox::Yes )
  422. {
  423. if( loadXML( path ) )
  424. this->close();
  425. enableControls();
  426. m_krename->enableControls();
  427. m_krename->updatePre();
  428. }
  429. }
  430. void ProfileDlg::slotCreateProfile()
  431. {
  432. bool ok = false;
  433. const char* mask = "xXXXXXXXXXXXXXXXXXXX"; // allows for 20 characters
  434. TQString name = KInputDialog::getText( i18n("Profile Name"), i18n("Please enter a name for the new profile:"),
  435. "KRename", &ok, this, 0, 0, mask );
  436. if( !ok )
  437. return;
  438. if( profiles->findItem( name, TQt::ExactMatch ) )
  439. {
  440. KMessageBox::error( this, i18n("This profile does already exist. Please choose another name.") );
  441. slotCreateProfile();
  442. return;
  443. }
  444. profiles->insertItem( new ProfileListBoxText( name ) );
  445. writeXML( name );
  446. enableControls();
  447. }
  448. void ProfileDlg::slotDeleteProfile()
  449. {
  450. if( profiles->currentItem() == PROFILE_WIZARD_INDEX || profiles->currentItem() == PROFILE_TABBED_INDEX )
  451. {
  452. KMessageBox::error( this, i18n("You cannot delete default profiles!") );
  453. return;
  454. }
  455. TQString profile = profiles->currentText();
  456. TQString msg = i18n("Do you really want to delete the profile: %1").arg( profile );
  457. if( KMessageBox::questionYesNo( this, msg ) == KMessageBox::Yes )
  458. {
  459. TQString path = readProfilePath( profile );
  460. TQFile::remove( path );
  461. TQListBoxItem* item = profiles->findItem( profile, TQt::ExactMatch );
  462. if( item )
  463. delete item;
  464. TDEConfig* conf = kapp->config();
  465. conf->setGroup( "ProfilesHeader" );
  466. TQStringList list = conf->readListEntry( "list" );
  467. list.remove( profile );
  468. conf->writeEntry( "list", list );
  469. conf->sync();
  470. enableControls();
  471. }
  472. }
  473. void ProfileDlg::slotHidden()
  474. {
  475. int i;
  476. TDEConfig* conf = kapp->config();
  477. TQString def = TQString();
  478. ProfileListBoxText* item;
  479. for( i=0;i<(int)profiles->count();i++ )
  480. {
  481. item = static_cast<ProfileListBoxText*>(profiles->item( i ));
  482. if( item->isDefault() )
  483. {
  484. def = profiles->text( i );
  485. break;
  486. }
  487. }
  488. conf->setGroup( "ProfilesHeader" );
  489. conf->writeEntry( "defprofile", def );
  490. conf->sync();
  491. }
  492. #include "profiledlg.moc"