kmcupsconfigwidget.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License version 2 as published by the Free Software Foundation.
00008  *
00009  *  This library is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *  Library General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Library General Public License
00015  *  along with this library; see the file COPYING.LIB.  If not, write to
00016  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  *  Boston, MA 02110-1301, USA.
00018  **/
00019 
00020 #include "kmcupsconfigwidget.h"
00021 #include "cupsinfos.h"
00022 
00023 #include <qlabel.h>
00024 #include <qgroupbox.h>
00025 #include <qlineedit.h>
00026 #include <qcheckbox.h>
00027 #include <qlayout.h>
00028 #include <qpushbutton.h>
00029 #include <qvalidator.h>
00030 
00031 #include <klocale.h>
00032 #include <kcursor.h>
00033 #include <kconfig.h>
00034 #include <kstringhandler.h>
00035 
00036 class PortValidator : public QIntValidator
00037 {
00038 public:
00039     PortValidator(QWidget *parent, const char *name = 0);
00040     virtual QValidator::State validate(QString&, int&) const;
00041 };
00042 
00043 PortValidator::PortValidator(QWidget *parent, const char *name)
00044 : QIntValidator(1, 65535, parent, name)
00045 {
00046 }
00047 
00048 QValidator::State PortValidator::validate(QString& txt, int&) const
00049 {
00050     bool    ok(false);
00051     int     p = txt.toInt(&ok);
00052     if (txt.isEmpty())
00053         return QValidator::Intermediate;
00054     else if (ok && p >= bottom() && p <= top())
00055         return QValidator::Acceptable;
00056     return QValidator::Invalid;
00057 }
00058 
00059 //******************************************************************************************
00060 
00061 KMCupsConfigWidget::KMCupsConfigWidget(QWidget *parent, const char *name)
00062 : QWidget(parent,name)
00063 {
00064     // widget creation
00065     QGroupBox   *m_hostbox = new QGroupBox(0, Qt::Vertical, i18n("Server Information"), this);
00066     QGroupBox   *m_loginbox = new QGroupBox(0, Qt::Vertical, i18n("Account Information"), this);
00067     QLabel  *m_hostlabel = new QLabel(i18n("&Host:"), m_hostbox);
00068     QLabel  *m_portlabel = new QLabel(i18n("&Port:"), m_hostbox);
00069     m_host = new QLineEdit(m_hostbox);
00070     m_port = new QLineEdit(m_hostbox);
00071     m_hostlabel->setBuddy(m_host);
00072     m_portlabel->setBuddy(m_port);
00073     m_port->setValidator(new PortValidator(m_port));
00074     m_login = new QLineEdit(m_loginbox);
00075     QLabel  *m_loginlabel = new QLabel(i18n("&User:"), m_loginbox);
00076     QLabel  *m_passwordlabel = new QLabel(i18n("Pass&word:"), m_loginbox);
00077     m_password = new QLineEdit(m_loginbox);
00078     m_password->setEchoMode(QLineEdit::Password);
00079     m_savepwd = new QCheckBox( i18n( "&Store password in configuration file" ), m_loginbox );
00080     m_savepwd->setCursor( KCursor::handCursor() );
00081     m_anonymous = new QCheckBox(i18n("Use &anonymous access"), m_loginbox);
00082     m_anonymous->setCursor(KCursor::handCursor());
00083     m_loginlabel->setBuddy(m_login);
00084     m_passwordlabel->setBuddy(m_password);
00085 
00086     // layout creation
00087     QVBoxLayout *lay0 = new QVBoxLayout(this, 0, 10);
00088     lay0->addWidget(m_hostbox,1);
00089     lay0->addWidget(m_loginbox,1);
00090     QGridLayout *lay2 = new QGridLayout(m_hostbox->layout(), 2, 2, 10);
00091     lay2->setColStretch(1,1);
00092     lay2->addWidget(m_hostlabel,0,0);
00093     lay2->addWidget(m_portlabel,1,0);
00094     lay2->addWidget(m_host,0,1);
00095     lay2->addWidget(m_port,1,1);
00096     QGridLayout *lay3 = new QGridLayout(m_loginbox->layout(), 4, 2, 10);
00097     lay3->setColStretch(1,1);
00098     lay3->addWidget(m_loginlabel,0,0);
00099     lay3->addWidget(m_passwordlabel,1,0);
00100     lay3->addWidget(m_login,0,1);
00101     lay3->addWidget(m_password,1,1);
00102     lay3->addMultiCellWidget(m_savepwd,2,2,0,1);
00103     lay3->addMultiCellWidget(m_anonymous,3,3,0,1);
00104 
00105     // connections
00106     connect(m_anonymous,SIGNAL(toggled(bool)),m_login,SLOT(setDisabled(bool)));
00107     connect(m_anonymous,SIGNAL(toggled(bool)),m_password,SLOT(setDisabled(bool)));
00108     connect(m_anonymous,SIGNAL(toggled(bool)),m_savepwd,SLOT(setDisabled(bool)));
00109 }
00110 
00111 void KMCupsConfigWidget::load()
00112 {
00113     CupsInfos   *inf = CupsInfos::self();
00114     m_host->setText(inf->host());
00115     m_port->setText(QString::number(inf->port()));
00116     if (inf->login().isEmpty())
00117         m_anonymous->setChecked(true);
00118     else
00119     {
00120         m_login->setText(inf->login());
00121         m_password->setText(inf->password());
00122         m_savepwd->setChecked( inf->savePassword() );
00123     }
00124 }
00125 
00126 void KMCupsConfigWidget::save(bool sync)
00127 {
00128     CupsInfos   *inf = CupsInfos::self();
00129     inf->setHost(m_host->text());
00130     inf->setPort(m_port->text().toInt());
00131     if (m_anonymous->isChecked())
00132     {
00133         inf->setLogin(QString::null);
00134         inf->setPassword(QString::null);
00135         inf->setSavePassword( false );
00136     }
00137     else
00138     {
00139         inf->setLogin(m_login->text());
00140         inf->setPassword(m_password->text());
00141         inf->setSavePassword( m_savepwd->isChecked() );
00142     }
00143     if (sync) inf->save();
00144 }
00145 
00146 void KMCupsConfigWidget::saveConfig(KConfig *conf)
00147 {
00148     conf->setGroup("CUPS");
00149     conf->writeEntry("Host",m_host->text());
00150     conf->writeEntry("Port",m_port->text().toInt());
00151     conf->writeEntry("Login",(m_anonymous->isChecked() ? QString::null : m_login->text()));
00152     conf->writeEntry( "SavePassword", ( m_anonymous->isChecked() ? false : m_savepwd->isChecked() ) );
00153     if ( m_savepwd->isChecked() && !m_anonymous->isChecked() )
00154         conf->writeEntry( "Password", ( m_anonymous->isChecked() ? QString::null : KStringHandler::obscure( m_password->text() ) ) );
00155     else
00156         conf->deleteEntry( "Password" );
00157     // synchronize CupsInfos object
00158     save(false);
00159 }
KDE Home | KDE Accessibility Home | Description of Access Keys