~ubuntu-branches/ubuntu/lucid/kdebase/lucid

« back to all changes in this revision

Viewing changes to konqueror/konq_profiledlg.cc

  • Committer: Bazaar Package Importer
  • Author(s): Ana Beatriz Guerrero Lopez
  • Date: 2009-04-05 05:22:13 UTC
  • mfrom: (0.4.2 experimental) (0.2.2 upstream)
  • mto: This revision was merged to the branch mainline in revision 235.
  • Revision ID: james.westby@ubuntu.com-20090405052213-39thr4l6p2ss07uj
Tags: 4:4.2.2-1
* New upstream release:
  - khtml fixes. (Closes: #290285, #359680)
  - Default konsole sessions can be deleted. (Closes: #286342)
  - Tag widget uses standard application palette. (Closes: #444800)
  - ... and surely many more but we have lost track...

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  This file is part of the KDE project
2
 
    Copyright (C) 1999 Simon Hausmann <hausmann@kde.org>
3
 
 
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.
8
 
 
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.
13
 
 
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.
17
 
 
18
 
*/
19
 
 
20
 
#include "konq_profiledlg.h"
21
 
#include "konq_viewmgr.h"
22
 
#include "konq_settingsxt.h"
23
 
 
24
 
#include <qcheckbox.h>
25
 
#include <qdir.h>
26
 
#include <qvbox.h>
27
 
#include <qlabel.h>
28
 
#include <qheader.h>
29
 
#include <qlineedit.h>
30
 
 
31
 
#include <klistview.h>
32
 
#include <kdebug.h>
33
 
#include <kstdguiitem.h>
34
 
#include <kio/global.h>
35
 
#include <kstandarddirs.h>
36
 
#include <klocale.h>
37
 
#include <ksimpleconfig.h>
38
 
#include <kseparator.h>
39
 
#include <kpushbutton.h>
40
 
 
41
 
KonqProfileMap KonqProfileDlg::readAllProfiles()
42
 
{
43
 
  KonqProfileMap mapProfiles;
44
 
 
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 )
49
 
  {
50
 
    QFileInfo info( *pIt );
51
 
    QString profileName = KIO::decodeFileName( info.baseName() );
52
 
    KSimpleConfig cfg( *pIt, true );
53
 
    if ( cfg.hasGroup( "Profile" ) )
54
 
    {
55
 
      cfg.setGroup( "Profile" );
56
 
      if ( cfg.hasKey( "Name" ) )
57
 
        profileName = cfg.readEntry( "Name" );
58
 
 
59
 
      mapProfiles.insert( profileName, *pIt );
60
 
    }
61
 
  }
62
 
 
63
 
  return mapProfiles;
64
 
}
65
 
 
66
 
KonqProfileItem::KonqProfileItem( KListView *parent, const QString & text )
67
 
    : QListViewItem( parent, text ), m_profileName( text )
68
 
{
69
 
}
70
 
 
71
 
#define BTN_RENAME KDialogBase::User1
72
 
#define BTN_DELETE KDialogBase::User2
73
 
#define BTN_SAVE   KDialogBase::User3
74
 
 
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"),
80
 
    KStdGuiItem::save() )
81
 
{
82
 
  m_pViewManager = manager;
83
 
 
84
 
  QVBox* box = new QVBox( this );
85
 
  box->setSpacing( KDialog::spacingHint() );
86
 
  setMainWidget( box );
87
 
 
88
 
  QLabel *lblName = new QLabel( i18n(  "&Profile name:" ), box );
89
 
 
90
 
  m_pProfileNameLineEdit = new QLineEdit( box );
91
 
  m_pProfileNameLineEdit->setFocus();
92
 
 
93
 
  lblName->setBuddy( m_pProfileNameLineEdit );
94
 
 
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 );
100
 
 
101
 
  box->setStretchFactor( m_pListView, 1 );
102
 
 
103
 
  connect( m_pListView, SIGNAL( itemRenamed( QListViewItem * ) ),
104
 
            SLOT( slotItemRenamed( QListViewItem * ) ) );
105
 
 
106
 
  loadAllProfiles( preselectProfile );
107
 
  m_pListView->setMinimumSize( m_pListView->sizeHint() );
108
 
 
109
 
  m_cbSaveURLs = new QCheckBox( i18n("Save &URLs in profile"), box );
110
 
  m_cbSaveURLs->setChecked( KonqSettings::saveURLInProfile() );
111
 
 
112
 
  m_cbSaveSize = new QCheckBox( i18n("Save &window size in profile"), box );
113
 
  m_cbSaveSize->setChecked( KonqSettings::saveWindowSizeInProfile() );
114
 
 
115
 
  connect( m_pListView, SIGNAL( selectionChanged( QListViewItem * ) ),
116
 
           this, SLOT( slotSelectionChanged( QListViewItem * ) ) );
117
 
 
118
 
  connect( m_pProfileNameLineEdit, SIGNAL( textChanged( const QString & ) ),
119
 
           this, SLOT( slotTextChanged( const QString & ) ) );
120
 
 
121
 
  enableButton( BTN_RENAME, m_pListView->selectedItem ()!=0 );
122
 
  enableButton( BTN_DELETE, m_pListView->selectedItem ()!=0 );
123
 
 
124
 
  resize( sizeHint() );
125
 
}
126
 
 
127
 
KonqProfileDlg::~KonqProfileDlg()
128
 
{
129
 
  KonqSettings::setSaveURLInProfile( m_cbSaveURLs->isChecked() );
130
 
  KonqSettings::setSaveWindowSizeInProfile( m_cbSaveSize->isChecked() );
131
 
}
132
 
 
133
 
void KonqProfileDlg::loadAllProfiles(const QString & preselectProfile)
134
 
{
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 )
142
 
    {
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 )
147
 
        {
148
 
            profileFound = true;
149
 
            m_pProfileNameLineEdit->setText( eIt.key() );
150
 
            m_pListView->setSelected( item, true );
151
 
        }
152
 
    }
153
 
    if (!profileFound)
154
 
        m_pProfileNameLineEdit->setText( preselectProfile);
155
 
}
156
 
 
157
 
void KonqProfileDlg::slotUser3() // Save button
158
 
{
159
 
  QString name = KIO::encodeFileName( m_pProfileNameLineEdit->text() ); // in case of '/'
160
 
 
161
 
  // Reuse filename of existing item, if any
162
 
  if ( m_pListView->selectedItem() )
163
 
  {
164
 
    KonqProfileMap::Iterator it = m_mapEntries.find( m_pListView->selectedItem()->text(0) );
165
 
    if ( it != m_mapEntries.end() )
166
 
    {
167
 
      QFileInfo info( it.data() );
168
 
      name = info.baseName();
169
 
    }
170
 
  }
171
 
 
172
 
  kdDebug(1202) << "Saving as " << name << endl;
173
 
  m_pViewManager->saveViewProfile( name, m_pProfileNameLineEdit->text(),
174
 
            m_cbSaveURLs->isChecked(), m_cbSaveSize->isChecked() );
175
 
 
176
 
  accept();
177
 
}
178
 
 
179
 
void KonqProfileDlg::slotUser2() // Delete button
180
 
{
181
 
    if(!m_pListView->selectedItem())
182
 
        return;
183
 
  KonqProfileMap::Iterator it = m_mapEntries.find( m_pListView->selectedItem()->text(0) );
184
 
 
185
 
  if ( it != m_mapEntries.end() && QFile::remove( it.data() ) )
186
 
      loadAllProfiles();
187
 
 
188
 
  enableButton( BTN_RENAME, m_pListView->selectedItem() != 0 );
189
 
  enableButton( BTN_DELETE, m_pListView->selectedItem() != 0 );
190
 
}
191
 
 
192
 
void KonqProfileDlg::slotUser1() // Rename button
193
 
{
194
 
  QListViewItem *item = m_pListView->selectedItem();
195
 
 
196
 
  if ( item )
197
 
    m_pListView->rename( item, 0 );
198
 
}
199
 
 
200
 
void KonqProfileDlg::slotItemRenamed( QListViewItem * item )
201
 
{
202
 
  KonqProfileItem * profileItem = static_cast<KonqProfileItem *>( item );
203
 
 
204
 
  QString newName = profileItem->text(0);
205
 
  QString oldName = profileItem->m_profileName;
206
 
 
207
 
  if (!newName.isEmpty())
208
 
  {
209
 
    KonqProfileMap::ConstIterator it = m_mapEntries.find( oldName );
210
 
 
211
 
    if ( it != m_mapEntries.end() )
212
 
    {
213
 
      QString fileName = it.data();
214
 
      KSimpleConfig cfg( fileName );
215
 
      cfg.setGroup( "Profile" );
216
 
      cfg.writeEntry( "Name", newName );
217
 
      cfg.sync();
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;
223
 
    }
224
 
  }
225
 
}
226
 
 
227
 
void KonqProfileDlg::slotSelectionChanged( QListViewItem * item )
228
 
{
229
 
    m_pProfileNameLineEdit->setText( item ? item->text(0) : QString::null );
230
 
}
231
 
 
232
 
void KonqProfileDlg::slotTextChanged( const QString & text )
233
 
{
234
 
  enableButton( KDialogBase::User3, !text.isEmpty() );
235
 
 
236
 
  // If we type the name of a profile, select it in the list
237
 
 
238
 
  bool itemSelected = false;
239
 
  QListViewItem * item;
240
 
 
241
 
  for ( item = m_pListView->firstChild() ; item ; item = item->nextSibling() )
242
 
      if ( item->text(0) == text /*only full text, not partial*/ )
243
 
      {
244
 
          itemSelected = true;
245
 
          m_pListView->setSelected( item, true );
246
 
          break;
247
 
      }
248
 
 
249
 
  if ( !itemSelected ) // otherwise, clear selection
250
 
    m_pListView->clearSelection();
251
 
 
252
 
  if ( itemSelected )
253
 
  {
254
 
    QFileInfo fi( m_mapEntries[ item->text( 0 ) ] );
255
 
    itemSelected = itemSelected && fi.isWritable();
256
 
  }
257
 
 
258
 
  enableButton( BTN_RENAME, itemSelected );
259
 
  enableButton( BTN_DELETE, itemSelected );
260
 
}
261
 
 
262
 
#undef BTN_RENAME
263
 
#undef BTN_DELETE
264
 
#undef BTN_SAVE
265
 
 
266
 
#include "konq_profiledlg.moc"