20 #include "kmcupsconfigwidget.h"
21 #include "cupsinfos.h"
24 #include <tqgroupbox.h>
25 #include <tqlineedit.h>
26 #include <tqcheckbox.h>
28 #include <tqpushbutton.h>
29 #include <tqvalidator.h>
31 #include <tdelocale.h>
33 #include <tdeconfig.h>
34 #include <kstringhandler.h>
36 class PortValidator :
public TQIntValidator
39 PortValidator(TQWidget *parent,
const char *name = 0);
40 virtual TQValidator::State validate(TQString&,
int&)
const;
43 PortValidator::PortValidator(TQWidget *parent,
const char *name)
44 : TQIntValidator(1, 65535, parent, name)
48 TQValidator::State PortValidator::validate(TQString& txt,
int&)
const
51 int p = txt.toInt(&ok);
53 return TQValidator::Intermediate;
54 else if (ok && p >= bottom() && p <= top())
55 return TQValidator::Acceptable;
56 return TQValidator::Invalid;
61 KMCupsConfigWidget::KMCupsConfigWidget(TQWidget *parent,
const char *name)
62 : TQWidget(parent,name)
65 TQGroupBox *m_hostbox =
new TQGroupBox(0, TQt::Vertical, i18n(
"Server Information"),
this);
66 TQGroupBox *m_loginbox =
new TQGroupBox(0, TQt::Vertical, i18n(
"Account Information"),
this);
67 TQLabel *m_hostlabel =
new TQLabel(i18n(
"&Host:"), m_hostbox);
68 TQLabel *m_portlabel =
new TQLabel(i18n(
"&Port:"), m_hostbox);
69 m_host =
new TQLineEdit(m_hostbox);
70 m_port =
new TQLineEdit(m_hostbox);
71 m_hostlabel->setBuddy(m_host);
72 m_portlabel->setBuddy(m_port);
73 m_port->setValidator(
new PortValidator(m_port));
74 m_login =
new TQLineEdit(m_loginbox);
75 TQLabel *m_loginlabel =
new TQLabel(i18n(
"&User:"), m_loginbox);
76 TQLabel *m_passwordlabel =
new TQLabel(i18n(
"Pass&word:"), m_loginbox);
77 m_password =
new TQLineEdit(m_loginbox);
78 m_password->setEchoMode(TQLineEdit::Password);
79 m_savepwd =
new TQCheckBox( i18n(
"&Store password in configuration file" ), m_loginbox );
80 m_savepwd->setCursor( KCursor::handCursor() );
81 m_anonymous =
new TQCheckBox(i18n(
"Use &anonymous access"), m_loginbox);
82 m_anonymous->setCursor(KCursor::handCursor());
83 m_loginlabel->setBuddy(m_login);
84 m_passwordlabel->setBuddy(m_password);
87 TQVBoxLayout *lay0 =
new TQVBoxLayout(
this, 0, 10);
88 lay0->addWidget(m_hostbox,1);
89 lay0->addWidget(m_loginbox,1);
90 TQGridLayout *lay2 =
new TQGridLayout(m_hostbox->layout(), 2, 2, 10);
91 lay2->setColStretch(1,1);
92 lay2->addWidget(m_hostlabel,0,0);
93 lay2->addWidget(m_portlabel,1,0);
94 lay2->addWidget(m_host,0,1);
95 lay2->addWidget(m_port,1,1);
96 TQGridLayout *lay3 =
new TQGridLayout(m_loginbox->layout(), 4, 2, 10);
97 lay3->setColStretch(1,1);
98 lay3->addWidget(m_loginlabel,0,0);
99 lay3->addWidget(m_passwordlabel,1,0);
100 lay3->addWidget(m_login,0,1);
101 lay3->addWidget(m_password,1,1);
102 lay3->addMultiCellWidget(m_savepwd,2,2,0,1);
103 lay3->addMultiCellWidget(m_anonymous,3,3,0,1);
106 connect(m_anonymous,TQ_SIGNAL(toggled(
bool)),m_login,TQ_SLOT(setDisabled(
bool)));
107 connect(m_anonymous,TQ_SIGNAL(toggled(
bool)),m_password,TQ_SLOT(setDisabled(
bool)));
108 connect(m_anonymous,TQ_SIGNAL(toggled(
bool)),m_savepwd,TQ_SLOT(setDisabled(
bool)));
111 void KMCupsConfigWidget::load()
113 CupsInfos *inf = CupsInfos::self();
114 m_host->setText(inf->host());
115 m_port->setText(TQString::number(inf->port()));
116 if (inf->login().isEmpty())
117 m_anonymous->setChecked(
true);
120 m_login->setText(inf->login());
121 m_password->setText(inf->password());
122 m_savepwd->setChecked( inf->savePassword() );
126 void KMCupsConfigWidget::save(
bool sync)
128 CupsInfos *inf = CupsInfos::self();
129 inf->setHost(m_host->text());
130 inf->setPort(m_port->text().toInt());
131 if (m_anonymous->isChecked())
133 inf->setLogin(TQString::null);
134 inf->setPassword(TQString::null);
135 inf->setSavePassword(
false );
139 inf->setLogin(m_login->text());
140 inf->setPassword(m_password->text());
141 inf->setSavePassword( m_savepwd->isChecked() );
143 if (sync) inf->save();
146 void KMCupsConfigWidget::saveConfig(TDEConfig *conf)
148 conf->setGroup(
"CUPS");
149 conf->writeEntry(
"Host",m_host->text());
150 conf->writeEntry(
"Port",m_port->text().toInt());
151 conf->writeEntry(
"Login",(m_anonymous->isChecked() ? TQString::null : m_login->text()));
152 conf->writeEntry(
"SavePassword", ( m_anonymous->isChecked() ?
false : m_savepwd->isChecked() ) );
153 if ( m_savepwd->isChecked() && !m_anonymous->isChecked() )
154 conf->writeEntry(
"Password", ( m_anonymous->isChecked() ? TQString::null : KStringHandler::obscure( m_password->text() ) ) );
156 conf->deleteEntry(
"Password" );