summaryrefslogtreecommitdiffstats
path: root/kuser/selectconn.cpp
blob: 9ab2911d0b0b50e442579793008f8412cc2c2709 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 *  Copyright (c) 2004 Szombathelyi György <gyurco@freemail.hu>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public
 *  License version 2 as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 **/

#include <tqpixmap.h>
#include <tqlayout.h>

#include <tqlabel.h>
#include <tqgrid.h>
#include <tqregexp.h>

#include <kdebug.h>
#include <tdeapplication.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kinputdialog.h>

#include "kglobal_.h"
#include "selectconn.h"
#include "editDefaults.h"

SelectConn::SelectConn(const TQString &selected, TQWidget* parent, const char * name) :
  KDialogBase( Plain, WStyle_DialogBorder, parent, name, true,
  i18n("Connection Selection"), Ok | Apply | Cancel | User1 | User2 | User3 )
{
  TQStringList conns;

  setButtonText( User3, i18n("&New...") );
  setButtonText( User2, i18n("&Edit") );
  setButtonText( User1, i18n("&Delete") );

  TQFrame *page = plainPage();
  TQVBoxLayout *topLayout = new TQVBoxLayout( page, 0, KDialog::spacingHint() );
  TQLabel *label = new TQLabel( i18n("Defined connections:"), page );
  mCombo = new KComboBox( page );
  mSelected = selected;
  kdDebug() << "selected item: " << mSelected << endl;

  conns = kapp->sharedConfig()->groupList();
  TQStringList::iterator it = conns.begin();
  int i = 0, sel = 0;
  while ( it != conns.end() ) {
    if ( (*it).startsWith( "connection-" ) ) {
      (*it).remove( TQRegExp("^connection-") );
      if ( (*it) == mSelected ) sel = i;
      i++;
      it++;
    } else
      it = conns.remove( it );
  }
  mCombo->insertStringList( conns );
  if ( mCombo->count() == 0 ) mCombo->insertItem( "default" );
  mCombo->setCurrentItem( sel );
  mSelected = connSelected();
  topLayout->addWidget( label );
  topLayout->addWidget( mCombo );
}

TQString SelectConn::connSelected()
{
  return mCombo->currentText();
}

void SelectConn::slotUser3()
{
  newconn = KInputDialog::getText( TQString(),
    i18n("Please type the name of the new connection:") );
  if ( newconn.isEmpty() ) return;
  if ( kapp->sharedConfig()->groupList().contains( "connection-" + newconn ) ) {
    KMessageBox::sorry( 0, i18n("A connection with this name already exists.") );
    return;
  }

  KUserPrefsBase kcfg( kapp->sharedConfig(), newconn );

  editDefaults eddlg( &kcfg, this );
  connect(&eddlg, TQ_SIGNAL(settingsChanged()), this, TQ_SLOT(slotNewApplySettings()));
  eddlg.exec();

  if ( newconn.isEmpty() )
    emit( applyClicked() );
}

void SelectConn::slotNewApplySettings()
{
  if ( !newconn.isEmpty() ) {
    mCombo->insertItem( newconn );
    mCombo->setCurrentItem( mCombo->count()-1 );
    mSelected = newconn;
  }
}

void SelectConn::slotUser2()
{
  kdDebug() << "slotUser2: " << connSelected() << endl;
  KUserPrefsBase kcfg( kapp->sharedConfig(), connSelected() );
  kcfg.readConfig();

  editDefaults eddlg( &kcfg, this );
  connect( &eddlg, TQ_SIGNAL(settingsChanged()), this, TQ_SLOT(slotApplySettings()) );

  eddlg.exec();
}

void SelectConn::slotUser1()
{
  TQString conn = connSelected();
  if ( KMessageBox::warningContinueCancel( 0, i18n("Do you really want to delete the connection '%1'?").
    arg( conn ),i18n("Delete Connection"),KStdGuiItem::del() ) == KMessageBox::Cancel ) return;

  kapp->sharedConfig()->deleteGroup( "connection-" + conn, true );
  kapp->sharedConfig()->sync();
  mCombo->removeItem( mCombo->currentItem() );
  if ( mCombo->count() == 0 ) {
    mCombo->insertItem( "default" );
    mCombo->setCurrentItem( 0 );
  }
  kdDebug() << "slotUser1: " << conn << " " << mSelected << endl;
  if ( mSelected == conn )
    emit( applyClicked() );
}

void SelectConn::slotApply()
{
  kdDebug() << "slotApply()" << endl;
  if ( connSelected() != mSelected ) {
    mSelected = connSelected();
    emit( applyClicked() );
  }
}

void SelectConn::slotApplySettings()
{
  kdDebug() << "slotApplySettings()" << endl;
  if ( connSelected() == mSelected )
    emit( applyClicked() );
}

#include "selectconn.moc"