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 "konq_profiledlg.h"
21
#include "konq_viewmgr.h"
22
#include "konq_settingsxt.h"
24
#include <qcheckbox.h>
29
#include <qlineedit.h>
31
#include <klistview.h>
33
#include <kstdguiitem.h>
34
#include <kio/global.h>
35
#include <kstandarddirs.h>
37
#include <ksimpleconfig.h>
38
#include <kseparator.h>
39
#include <kpushbutton.h>
41
KonqProfileMap KonqProfileDlg::readAllProfiles()
43
KonqProfileMap mapProfiles;
45
QStringList profiles = KGlobal::dirs()->findAllResources( "data", "konqueror/profiles/*", false, true );
46
QStringList::ConstIterator pIt = profiles.begin();
47
QStringList::ConstIterator pEnd = profiles.end();
48
for (; pIt != pEnd; ++pIt )
50
QFileInfo info( *pIt );
51
QString profileName = KIO::decodeFileName( info.baseName() );
52
KSimpleConfig cfg( *pIt, true );
53
if ( cfg.hasGroup( "Profile" ) )
55
cfg.setGroup( "Profile" );
56
if ( cfg.hasKey( "Name" ) )
57
profileName = cfg.readEntry( "Name" );
59
mapProfiles.insert( profileName, *pIt );
66
KonqProfileItem::KonqProfileItem( KListView *parent, const QString & text )
67
: QListViewItem( parent, text ), m_profileName( text )
71
#define BTN_RENAME KDialogBase::User1
72
#define BTN_DELETE KDialogBase::User2
73
#define BTN_SAVE KDialogBase::User3
75
KonqProfileDlg::KonqProfileDlg( KonqViewManager *manager, const QString & preselectProfile, QWidget *parent )
76
: KDialogBase( parent, "konq_profile_dialog", true, i18n( "Profile Management" ),
77
KDialogBase::Close | BTN_RENAME | BTN_DELETE | BTN_SAVE, BTN_SAVE, true,
78
KGuiItem( i18n( "&Rename Profile" ) ),
79
KGuiItem( i18n( "&Delete Profile" ), "editdelete"),
82
m_pViewManager = manager;
84
QVBox* box = new QVBox( this );
85
box->setSpacing( KDialog::spacingHint() );
88
QLabel *lblName = new QLabel( i18n( "&Profile name:" ), box );
90
m_pProfileNameLineEdit = new QLineEdit( box );
91
m_pProfileNameLineEdit->setFocus();
93
lblName->setBuddy( m_pProfileNameLineEdit );
95
m_pListView = new KListView( box );
96
m_pListView->setAllColumnsShowFocus(true);
97
m_pListView->header()->hide();
98
m_pListView->addColumn("");
99
m_pListView->setRenameable( 0 );
101
box->setStretchFactor( m_pListView, 1 );
103
connect( m_pListView, SIGNAL( itemRenamed( QListViewItem * ) ),
104
SLOT( slotItemRenamed( QListViewItem * ) ) );
106
loadAllProfiles( preselectProfile );
107
m_pListView->setMinimumSize( m_pListView->sizeHint() );
109
m_cbSaveURLs = new QCheckBox( i18n("Save &URLs in profile"), box );
110
m_cbSaveURLs->setChecked( KonqSettings::saveURLInProfile() );
112
m_cbSaveSize = new QCheckBox( i18n("Save &window size in profile"), box );
113
m_cbSaveSize->setChecked( KonqSettings::saveWindowSizeInProfile() );
115
connect( m_pListView, SIGNAL( selectionChanged( QListViewItem * ) ),
116
this, SLOT( slotSelectionChanged( QListViewItem * ) ) );
118
connect( m_pProfileNameLineEdit, SIGNAL( textChanged( const QString & ) ),
119
this, SLOT( slotTextChanged( const QString & ) ) );
121
enableButton( BTN_RENAME, m_pListView->selectedItem ()!=0 );
122
enableButton( BTN_DELETE, m_pListView->selectedItem ()!=0 );
124
resize( sizeHint() );
127
KonqProfileDlg::~KonqProfileDlg()
129
KonqSettings::setSaveURLInProfile( m_cbSaveURLs->isChecked() );
130
KonqSettings::setSaveWindowSizeInProfile( m_cbSaveSize->isChecked() );
133
void KonqProfileDlg::loadAllProfiles(const QString & preselectProfile)
135
bool profileFound = false;
136
m_mapEntries.clear();
137
m_pListView->clear();
138
m_mapEntries = readAllProfiles();
139
KonqProfileMap::ConstIterator eIt = m_mapEntries.begin();
140
KonqProfileMap::ConstIterator eEnd = m_mapEntries.end();
141
for (; eIt != eEnd; ++eIt )
143
QListViewItem *item = new KonqProfileItem( m_pListView, eIt.key() );
144
QString filename = eIt.data().mid( eIt.data().findRev( '/' ) + 1 );
145
kdDebug(1202) << filename << endl;
146
if ( filename == preselectProfile )
149
m_pProfileNameLineEdit->setText( eIt.key() );
150
m_pListView->setSelected( item, true );
154
m_pProfileNameLineEdit->setText( preselectProfile);
157
void KonqProfileDlg::slotUser3() // Save button
159
QString name = KIO::encodeFileName( m_pProfileNameLineEdit->text() ); // in case of '/'
161
// Reuse filename of existing item, if any
162
if ( m_pListView->selectedItem() )
164
KonqProfileMap::Iterator it = m_mapEntries.find( m_pListView->selectedItem()->text(0) );
165
if ( it != m_mapEntries.end() )
167
QFileInfo info( it.data() );
168
name = info.baseName();
172
kdDebug(1202) << "Saving as " << name << endl;
173
m_pViewManager->saveViewProfile( name, m_pProfileNameLineEdit->text(),
174
m_cbSaveURLs->isChecked(), m_cbSaveSize->isChecked() );
179
void KonqProfileDlg::slotUser2() // Delete button
181
if(!m_pListView->selectedItem())
183
KonqProfileMap::Iterator it = m_mapEntries.find( m_pListView->selectedItem()->text(0) );
185
if ( it != m_mapEntries.end() && QFile::remove( it.data() ) )
188
enableButton( BTN_RENAME, m_pListView->selectedItem() != 0 );
189
enableButton( BTN_DELETE, m_pListView->selectedItem() != 0 );
192
void KonqProfileDlg::slotUser1() // Rename button
194
QListViewItem *item = m_pListView->selectedItem();
197
m_pListView->rename( item, 0 );
200
void KonqProfileDlg::slotItemRenamed( QListViewItem * item )
202
KonqProfileItem * profileItem = static_cast<KonqProfileItem *>( item );
204
QString newName = profileItem->text(0);
205
QString oldName = profileItem->m_profileName;
207
if (!newName.isEmpty())
209
KonqProfileMap::ConstIterator it = m_mapEntries.find( oldName );
211
if ( it != m_mapEntries.end() )
213
QString fileName = it.data();
214
KSimpleConfig cfg( fileName );
215
cfg.setGroup( "Profile" );
216
cfg.writeEntry( "Name", newName );
218
// Didn't find how to change a key...
219
m_mapEntries.remove( oldName );
220
m_mapEntries.insert( newName, fileName );
221
m_pProfileNameLineEdit->setText( newName );
222
profileItem->m_profileName = newName;
227
void KonqProfileDlg::slotSelectionChanged( QListViewItem * item )
229
m_pProfileNameLineEdit->setText( item ? item->text(0) : QString::null );
232
void KonqProfileDlg::slotTextChanged( const QString & text )
234
enableButton( KDialogBase::User3, !text.isEmpty() );
236
// If we type the name of a profile, select it in the list
238
bool itemSelected = false;
239
QListViewItem * item;
241
for ( item = m_pListView->firstChild() ; item ; item = item->nextSibling() )
242
if ( item->text(0) == text /*only full text, not partial*/ )
245
m_pListView->setSelected( item, true );
249
if ( !itemSelected ) // otherwise, clear selection
250
m_pListView->clearSelection();
254
QFileInfo fi( m_mapEntries[ item->text( 0 ) ] );
255
itemSelected = itemSelected && fi.isWritable();
258
enableButton( BTN_RENAME, itemSelected );
259
enableButton( BTN_DELETE, itemSelected );
266
#include "konq_profiledlg.moc"