tdebase
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.

konq_profiledlg.cc 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* This file is part of the KDE project
  2. Copyright (C) 1999 Simon Hausmann <hausmann@kde.org>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. */
  15. #include "konq_profiledlg.h"
  16. #include "konq_viewmgr.h"
  17. #include "konq_settingsxt.h"
  18. #include <tqcheckbox.h>
  19. #include <tqdir.h>
  20. #include <tqvbox.h>
  21. #include <tqlabel.h>
  22. #include <tqheader.h>
  23. #include <tqlineedit.h>
  24. #include <tdelistview.h>
  25. #include <kdebug.h>
  26. #include <kstdguiitem.h>
  27. #include <tdeio/global.h>
  28. #include <kstandarddirs.h>
  29. #include <tdelocale.h>
  30. #include <ksimpleconfig.h>
  31. #include <kseparator.h>
  32. #include <kpushbutton.h>
  33. KonqProfileMap KonqProfileDlg::readAllProfiles()
  34. {
  35. KonqProfileMap mapProfiles;
  36. TQStringList profiles = TDEGlobal::dirs()->findAllResources( "data", "konqueror/profiles/*", false, true );
  37. TQStringList::ConstIterator pIt = profiles.begin();
  38. TQStringList::ConstIterator pEnd = profiles.end();
  39. for (; pIt != pEnd; ++pIt )
  40. {
  41. TQFileInfo info( *pIt );
  42. TQString profileName = TDEIO::decodeFileName( info.baseName() );
  43. KSimpleConfig cfg( *pIt, true );
  44. if ( cfg.hasGroup( "Profile" ) )
  45. {
  46. cfg.setGroup( "Profile" );
  47. if ( cfg.hasKey( "Name" ) )
  48. profileName = cfg.readEntry( "Name" );
  49. mapProfiles.insert( profileName, *pIt );
  50. }
  51. }
  52. return mapProfiles;
  53. }
  54. KonqProfileItem::KonqProfileItem( TDEListView *parent, const TQString & text )
  55. : TQListViewItem( parent, text ), m_profileName( text )
  56. {
  57. }
  58. #define BTN_RENAME KDialogBase::User1
  59. #define BTN_DELETE KDialogBase::User2
  60. #define BTN_SAVE KDialogBase::User3
  61. KonqProfileDlg::KonqProfileDlg( KonqViewManager *manager, const TQString & preselectProfile, TQWidget *parent )
  62. : KDialogBase( parent, "konq_profile_dialog", true, i18n( "Profile Management" ),
  63. KDialogBase::Close | BTN_RENAME | BTN_DELETE | BTN_SAVE, BTN_SAVE, true,
  64. KGuiItem( i18n( "&Rename Profile" ) ),
  65. KGuiItem( i18n( "&Delete Profile" ), "edit-delete"),
  66. KStdGuiItem::save() )
  67. {
  68. m_pViewManager = manager;
  69. TQVBox* box = new TQVBox( this );
  70. box->setSpacing( KDialog::spacingHint() );
  71. setMainWidget( box );
  72. TQLabel *lblName = new TQLabel( i18n( "&Profile name:" ), box );
  73. m_pProfileNameLineEdit = new TQLineEdit( box );
  74. m_pProfileNameLineEdit->setFocus();
  75. lblName->setBuddy( m_pProfileNameLineEdit );
  76. m_pListView = new TDEListView( box );
  77. m_pListView->setAllColumnsShowFocus(true);
  78. m_pListView->header()->hide();
  79. m_pListView->addColumn("");
  80. m_pListView->setRenameable( 0 );
  81. box->setStretchFactor( m_pListView, 1 );
  82. connect( m_pListView, TQT_SIGNAL( itemRenamed( TQListViewItem * ) ),
  83. TQT_SLOT( slotItemRenamed( TQListViewItem * ) ) );
  84. loadAllProfiles( preselectProfile );
  85. m_pListView->setMinimumSize( m_pListView->sizeHint() );
  86. m_cbSaveURLs = new TQCheckBox( i18n("Save &URLs in profile"), box );
  87. m_cbSaveURLs->setChecked( KonqSettings::saveURLInProfile() );
  88. m_cbSaveSize = new TQCheckBox( i18n("Save &window size in profile"), box );
  89. m_cbSaveSize->setChecked( KonqSettings::saveWindowSizeInProfile() );
  90. connect( m_pListView, TQT_SIGNAL( selectionChanged( TQListViewItem * ) ),
  91. this, TQT_SLOT( slotSelectionChanged( TQListViewItem * ) ) );
  92. connect( m_pProfileNameLineEdit, TQT_SIGNAL( textChanged( const TQString & ) ),
  93. this, TQT_SLOT( slotTextChanged( const TQString & ) ) );
  94. enableButton( BTN_RENAME, m_pListView->selectedItem ()!=0 );
  95. enableButton( BTN_DELETE, m_pListView->selectedItem ()!=0 );
  96. resize( sizeHint() );
  97. }
  98. KonqProfileDlg::~KonqProfileDlg()
  99. {
  100. KonqSettings::setSaveURLInProfile( m_cbSaveURLs->isChecked() );
  101. KonqSettings::setSaveWindowSizeInProfile( m_cbSaveSize->isChecked() );
  102. }
  103. void KonqProfileDlg::loadAllProfiles(const TQString & preselectProfile)
  104. {
  105. bool profileFound = false;
  106. m_mapEntries.clear();
  107. m_pListView->clear();
  108. m_mapEntries = readAllProfiles();
  109. KonqProfileMap::ConstIterator eIt = m_mapEntries.begin();
  110. KonqProfileMap::ConstIterator eEnd = m_mapEntries.end();
  111. for (; eIt != eEnd; ++eIt )
  112. {
  113. TQListViewItem *item = new KonqProfileItem( m_pListView, eIt.key() );
  114. TQString filename = eIt.data().mid( eIt.data().findRev( '/' ) + 1 );
  115. kdDebug(1202) << filename << endl;
  116. if ( filename == preselectProfile )
  117. {
  118. profileFound = true;
  119. m_pProfileNameLineEdit->setText( eIt.key() );
  120. m_pListView->setSelected( item, true );
  121. }
  122. }
  123. if (!profileFound)
  124. m_pProfileNameLineEdit->setText( preselectProfile);
  125. }
  126. void KonqProfileDlg::slotUser3() // Save button
  127. {
  128. TQString name = TDEIO::encodeFileName( m_pProfileNameLineEdit->text() ); // in case of '/'
  129. // Reuse filename of existing item, if any
  130. if ( m_pListView->selectedItem() )
  131. {
  132. KonqProfileMap::Iterator it = m_mapEntries.find( m_pListView->selectedItem()->text(0) );
  133. if ( it != m_mapEntries.end() )
  134. {
  135. TQFileInfo info( it.data() );
  136. name = info.baseName();
  137. }
  138. }
  139. kdDebug(1202) << "Saving as " << name << endl;
  140. m_pViewManager->saveViewProfile( name, m_pProfileNameLineEdit->text(),
  141. m_cbSaveURLs->isChecked(), m_cbSaveSize->isChecked() );
  142. accept();
  143. }
  144. void KonqProfileDlg::slotUser2() // Delete button
  145. {
  146. if(!m_pListView->selectedItem())
  147. return;
  148. KonqProfileMap::Iterator it = m_mapEntries.find( m_pListView->selectedItem()->text(0) );
  149. if ( it != m_mapEntries.end() && TQFile::remove( it.data() ) )
  150. loadAllProfiles();
  151. enableButton( BTN_RENAME, m_pListView->selectedItem() != 0 );
  152. enableButton( BTN_DELETE, m_pListView->selectedItem() != 0 );
  153. }
  154. void KonqProfileDlg::slotUser1() // Rename button
  155. {
  156. TQListViewItem *item = m_pListView->selectedItem();
  157. if ( item )
  158. m_pListView->rename( item, 0 );
  159. }
  160. void KonqProfileDlg::slotItemRenamed( TQListViewItem * item )
  161. {
  162. KonqProfileItem * profileItem = static_cast<KonqProfileItem *>( item );
  163. TQString newName = profileItem->text(0);
  164. TQString oldName = profileItem->m_profileName;
  165. if (!newName.isEmpty())
  166. {
  167. KonqProfileMap::ConstIterator it = m_mapEntries.find( oldName );
  168. if ( it != m_mapEntries.end() )
  169. {
  170. TQString fileName = it.data();
  171. KSimpleConfig cfg( fileName );
  172. cfg.setGroup( "Profile" );
  173. cfg.writeEntry( "Name", newName );
  174. cfg.sync();
  175. // Didn't find how to change a key...
  176. m_mapEntries.remove( oldName );
  177. m_mapEntries.insert( newName, fileName );
  178. m_pProfileNameLineEdit->setText( newName );
  179. profileItem->m_profileName = newName;
  180. }
  181. }
  182. }
  183. void KonqProfileDlg::slotSelectionChanged( TQListViewItem * item )
  184. {
  185. m_pProfileNameLineEdit->setText( item ? item->text(0) : TQString::null );
  186. }
  187. void KonqProfileDlg::slotTextChanged( const TQString & text )
  188. {
  189. enableButton( KDialogBase::User3, !text.isEmpty() );
  190. // If we type the name of a profile, select it in the list
  191. bool itemSelected = false;
  192. TQListViewItem * item;
  193. for ( item = m_pListView->firstChild() ; item ; item = item->nextSibling() )
  194. if ( item->text(0) == text /*only full text, not partial*/ )
  195. {
  196. itemSelected = true;
  197. m_pListView->setSelected( item, true );
  198. break;
  199. }
  200. if ( !itemSelected ) // otherwise, clear selection
  201. m_pListView->clearSelection();
  202. if ( itemSelected )
  203. {
  204. TQFileInfo fi( m_mapEntries[ item->text( 0 ) ] );
  205. itemSelected = itemSelected && fi.isWritable();
  206. }
  207. enableButton( BTN_RENAME, itemSelected );
  208. enableButton( BTN_DELETE, itemSelected );
  209. }
  210. #undef BTN_RENAME
  211. #undef BTN_DELETE
  212. #undef BTN_SAVE
  213. #include "konq_profiledlg.moc"