Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / konqueror / src / konqprofiledlg.cpp
blobc3f5106fd9fa704be84c9a16321d869a37347670
1 /* This file is part of the KDE project
2 Copyright (C) 1999 Simon Hausmann <hausmann@kde.org>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "konqprofiledlg.h"
21 #include "konqviewmanager.h"
22 #include "konqsettingsxt.h"
23 #include "ui_konqprofiledlg_base.h"
25 #include <QtGui/QCheckBox>
26 #include <QtCore/QDir>
27 #include <kvbox.h>
28 #include <QtGui/QLabel>
29 #include <QtGui/QLineEdit>
30 #include <QtGui/QListWidgetItem>
32 #include <kdebug.h>
33 #include <kstandardguiitem.h>
34 #include <kio/global.h>
35 #include <kstandarddirs.h>
36 #include <klocale.h>
37 #include <kconfig.h>
38 #include <kseparator.h>
39 #include <klistwidget.h>
40 #include <kpushbutton.h>
42 class KonqProfileDlg::KonqProfileItem : public QListWidgetItem
44 public:
45 KonqProfileItem( KListWidget *, const QString & );
46 ~KonqProfileItem() {}
48 QString m_profileName;
52 KonqProfileMap KonqProfileDlg::readAllProfiles()
54 KonqProfileMap mapProfiles;
56 const QStringList profiles = KGlobal::dirs()->findAllResources( "data", "konqueror/profiles/*", KStandardDirs::NoDuplicates );
57 QStringList::ConstIterator pIt = profiles.constBegin();
58 QStringList::ConstIterator pEnd = profiles.constEnd();
59 for (; pIt != pEnd; ++pIt )
61 QFileInfo info( *pIt );
62 QString profileName = KIO::decodeFileName( info.baseName() );
63 KConfig cfg( *pIt, KConfig::SimpleConfig);
64 if ( cfg.hasGroup( "Profile" ) )
66 KConfigGroup profileGroup( &cfg, "Profile" );
67 if ( profileGroup.hasKey( "Name" ) )
68 profileName = profileGroup.readEntry( "Name" );
70 mapProfiles.insert( profileName, *pIt );
74 return mapProfiles;
77 KonqProfileDlg::KonqProfileItem::KonqProfileItem( KListWidget *parent, const QString & text )
78 : QListWidgetItem( text, parent ), m_profileName( text )
80 setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
83 class KonqProfileDlg::KonqProfileDlgPrivate : public QWidget, public Ui::KonqProfileDlgBase
85 public:
86 KonqProfileDlgPrivate( KonqViewManager *manager, QWidget *parent = 0 )
87 : QWidget( parent )
88 , m_pViewManager( manager )
90 setupUi( this );
93 KonqViewManager * const m_pViewManager;
95 KonqProfileMap m_mapEntries;
98 #define BTN_RENAME KDialog::User1
99 #define BTN_DELETE KDialog::User2
100 #define BTN_SAVE KDialog::User3
102 KonqProfileDlg::KonqProfileDlg( KonqViewManager *manager, const QString & preselectProfile, QWidget *parent )
103 : KDialog( parent )
104 , d( new KonqProfileDlgPrivate( manager, this ) )
106 d->layout()->setMargin( 0 );
107 setMainWidget( d );
109 setObjectName( "konq_profile_dialog" );
110 setModal( true );
111 setCaption( i18n( "Profile Management" ) );
112 setButtons( Close | BTN_RENAME | BTN_DELETE | BTN_SAVE );
113 setDefaultButton( BTN_SAVE );
114 showButtonSeparator( true );
115 setButtonGuiItem( BTN_RENAME, KGuiItem( i18n( "&Rename Profile" ) ) );
116 setButtonGuiItem( BTN_DELETE, KGuiItem( i18n( "&Delete Profile" ), "edit-delete" ) );
117 setButtonGuiItem( BTN_SAVE, KStandardGuiItem::save() );
119 d->m_pProfileNameLineEdit->setFocus();
121 connect( d->m_pListView, SIGNAL( itemChanged( QListWidgetItem * ) ),
122 SLOT( slotItemRenamed( QListWidgetItem * ) ) );
124 loadAllProfiles( preselectProfile );
125 d->m_pListView->setMinimumSize( d->m_pListView->sizeHint() );
127 d->m_cbSaveURLs->setChecked( KonqSettings::saveURLInProfile() );
129 connect( d->m_pListView, SIGNAL( itemSelectionChanged() ),
130 this, SLOT( slotSelectionChanged() ) );
132 connect( d->m_pProfileNameLineEdit, SIGNAL( textChanged( const QString & ) ),
133 this, SLOT( slotTextChanged( const QString & ) ) );
135 enableButton( BTN_RENAME, d->m_pListView->currentItem() != 0 );
136 enableButton( BTN_DELETE, d->m_pListView->currentItem() != 0 );
138 connect( this,SIGNAL(user1Clicked()),SLOT(slotRenameProfile()));
139 connect( this,SIGNAL(user2Clicked()),SLOT(slotDeleteProfile()));
140 connect( this,SIGNAL(user3Clicked()),SLOT(slotSave()));
142 resize( sizeHint() );
145 KonqProfileDlg::~KonqProfileDlg()
147 KonqSettings::setSaveURLInProfile( d->m_cbSaveURLs->isChecked() );
150 void KonqProfileDlg::loadAllProfiles(const QString & preselectProfile)
152 bool profileFound = false;
153 d->m_mapEntries.clear();
154 d->m_pListView->clear();
155 d->m_mapEntries = readAllProfiles();
156 KonqProfileMap::ConstIterator eIt = d->m_mapEntries.constBegin();
157 KonqProfileMap::ConstIterator eEnd = d->m_mapEntries.constEnd();
158 for (; eIt != eEnd; ++eIt )
160 QListWidgetItem *item = new KonqProfileItem( d->m_pListView, eIt.key() );
161 QString filename = eIt.value().mid( eIt.value().lastIndexOf( '/' ) + 1 );
162 kDebug(1202) << filename;
163 if ( filename == preselectProfile )
165 profileFound = true;
166 d->m_pProfileNameLineEdit->setText( eIt.key() );
167 d->m_pListView->setCurrentItem( item );
170 if (!profileFound)
171 d->m_pProfileNameLineEdit->setText( preselectProfile);
173 slotTextChanged( d->m_pProfileNameLineEdit->text() ); // really disable save button if text empty
176 void KonqProfileDlg::slotSave()
178 QString name = KIO::encodeFileName( d->m_pProfileNameLineEdit->text() ); // in case of '/'
180 // Reuse filename of existing item, if any
181 if ( d->m_pListView->currentItem() )
183 KonqProfileMap::Iterator it = d->m_mapEntries.find( d->m_pListView->currentItem()->text() );
184 if ( it != d->m_mapEntries.end() )
186 QFileInfo info( it.value() );
187 name = info.baseName();
191 kDebug(1202) << "Saving as " << name;
192 d->m_pViewManager->saveViewProfileToFile( name, d->m_pProfileNameLineEdit->text(),
193 d->m_cbSaveURLs->isChecked() ? KonqFrameBase::saveURLs : KonqFrameBase::None);
195 accept();
198 void KonqProfileDlg::slotDeleteProfile()
200 if(!d->m_pListView->currentItem())
201 return;
202 KonqProfileMap::Iterator it = d->m_mapEntries.find( d->m_pListView->currentItem()->text() );
204 if ( it != d->m_mapEntries.end() && QFile::remove( it.value() ) )
205 loadAllProfiles();
207 enableButton( BTN_RENAME, d->m_pListView->currentItem() != 0 );
208 enableButton( BTN_DELETE, d->m_pListView->currentItem() != 0 );
211 void KonqProfileDlg::slotRenameProfile()
213 QListWidgetItem *item = d->m_pListView->currentItem();
215 if ( item )
216 d->m_pListView->editItem( item );
219 void KonqProfileDlg::slotItemRenamed( QListWidgetItem * item )
221 if (!item) {
222 return;
225 KonqProfileItem * profileItem = static_cast<KonqProfileItem *>( item );
227 QString newName = profileItem->text();
228 QString oldName = profileItem->m_profileName;
230 if ( newName == oldName )
231 return;
233 if (!newName.isEmpty())
235 KonqProfileMap::ConstIterator it = d->m_mapEntries.constFind( oldName );
237 if ( it != d->m_mapEntries.constEnd() )
239 QString fileName = it.value();
240 KConfig _cfg( fileName, KConfig::SimpleConfig );
241 KConfigGroup cfg(&_cfg, "Profile" );
242 cfg.writeEntry( "Name", newName );
243 cfg.sync();
244 // Didn't find how to change a key...
245 d->m_mapEntries.remove( oldName );
246 d->m_mapEntries.insert( newName, fileName );
247 d->m_pProfileNameLineEdit->setText( newName );
248 profileItem->m_profileName = newName;
253 void KonqProfileDlg::slotSelectionChanged()
255 if ( d->m_pListView->currentItem() )
256 d->m_pProfileNameLineEdit->setText( d->m_pListView->currentItem()->text() );
259 void KonqProfileDlg::slotTextChanged( const QString & text )
261 enableButton( BTN_SAVE, !text.isEmpty() );
263 // If we type the name of a profile, select it in the list
265 QList<QListWidgetItem*> items = d->m_pListView->findItems(text, Qt::MatchCaseSensitive);
266 QListWidgetItem * item = !items.isEmpty() ? items.first() : 0;
267 d->m_pListView->setCurrentItem(item);
269 bool itemSelected = item;
270 if ( itemSelected )
272 KConfig config( d->m_mapEntries[text], KConfig::SimpleConfig );
273 KConfigGroup profile( &config, "Profile" );
275 QFileInfo fi( d->m_mapEntries[ item->text() ] );
276 itemSelected = itemSelected && fi.isWritable();
277 if ( itemSelected )
278 item->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable );
281 enableButton( BTN_RENAME, itemSelected );
282 enableButton( BTN_DELETE, itemSelected );
285 #undef BTN_RENAME
286 #undef BTN_DELETE
287 #undef BTN_SAVE
289 #include "konqprofiledlg.moc"