cupsinfos.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 "cupsinfos.h"
00021 #include "kmfactory.h"
00022 #include "kmtimer.h"
00023 #include "messagewindow.h"
00024 
00025 #include <kio/passdlg.h>
00026 #include <kio/authinfo.h>
00027 #include <klocale.h>
00028 #include <kconfig.h>
00029 #include <kapplication.h>
00030 #include <dcopclient.h>
00031 #include <kdebug.h>
00032 #include <kstringhandler.h>
00033 
00034 #include <cups/cups.h>
00035 #include <cups/ipp.h>
00036 
00037 const char* cupsGetPasswordCB(const char*)
00038 {
00039     return CupsInfos::self()->getPasswordCB();
00040 }
00041 
00042 CupsInfos* CupsInfos::unique_ = 0;
00043 
00044 CupsInfos* CupsInfos::self()
00045 {
00046     if (!unique_)
00047     {
00048         unique_ = new CupsInfos();
00049     }
00050     return unique_;
00051 }
00052 
00053 CupsInfos::CupsInfos()
00054 : KPReloadObject(true)
00055 {
00056     count_ = 0;
00057 
00058     load();
00059 /*  host_ = cupsServer();
00060     login_ = cupsUser();
00061     if (login_.isEmpty()) login_ = QString::null;
00062     port_ = ippPort();
00063     password_ = QString::null;*/
00064 
00065     cupsSetPasswordCB(cupsGetPasswordCB);
00066 }
00067 
00068 CupsInfos::~CupsInfos()
00069 {
00070 }
00071 
00072 void CupsInfos::setHost(const QString& s)
00073 {
00074     host_ = s;
00075     cupsSetServer(s.latin1());
00076 }
00077 
00078 void CupsInfos::setPort(int p)
00079 {
00080     port_ = p;
00081     ippSetPort(p);
00082 }
00083 
00084 void CupsInfos::setLogin(const QString& s)
00085 {
00086     login_ = s;
00087     cupsSetUser(s.latin1());
00088 }
00089 
00090 void CupsInfos::setPassword(const QString& s)
00091 {
00092     password_ = s;
00093 }
00094 
00095 void CupsInfos::setSavePassword( bool on )
00096 {
00097     savepwd_ = on;
00098 }
00099 
00100 const char* CupsInfos::getPasswordCB()
00101 {
00102     QPair<QString,QString> pwd = KMFactory::self()->requestPassword( count_, login_, host_, port_ );
00103 
00104     if ( pwd.first.isEmpty() && pwd.second.isEmpty() )
00105         return NULL;
00106     setLogin( pwd.first );
00107     setPassword( pwd.second );
00108     return pwd.second.latin1();
00109 }
00110 
00111 void CupsInfos::load()
00112 {
00113     KConfig *conf_ = KMFactory::self()->printConfig();
00114     conf_->setGroup("CUPS");
00115     host_ = conf_->readEntry("Host",QString::fromLatin1(cupsServer()));
00116      // Don't try to connect to unix domain sockets (currently not supported by kdeprint), try localhost instead. <jwustrack@mandriva.com>
00117     if (host_.at(0) == '/')
00118         host_ = QString("localhost");
00119     port_ = conf_->readNumEntry("Port",ippPort());
00120     login_ = conf_->readEntry("Login",QString::fromLatin1(cupsUser()));
00121     savepwd_ = conf_->readBoolEntry( "SavePassword", false );
00122     if ( savepwd_ )
00123     {
00124         password_ = KStringHandler::obscure( conf_->readEntry( "Password" ) );
00125         KMFactory::self()->initPassword( login_, password_, host_, port_ );
00126     }
00127     else
00128         password_ = QString::null;
00129     if (login_.isEmpty()) login_ = QString::null;
00130     reallogin_ = cupsUser();
00131 
00132     // synchronize with CUPS
00133     cupsSetServer(host_.latin1());
00134     cupsSetUser(login_.latin1());
00135     ippSetPort(port_);
00136 }
00137 
00138 void CupsInfos::save()
00139 {
00140     KConfig *conf_ = KMFactory::self()->printConfig();
00141     conf_->setGroup("CUPS");
00142     conf_->writeEntry("Host",host_);
00143     conf_->writeEntry("Port",port_);
00144     conf_->writeEntry("Login",login_);
00145     conf_->writeEntry( "SavePassword", savepwd_ );
00146     if ( savepwd_ )
00147         conf_->writeEntry( "Password", KStringHandler::obscure( password_ ) );
00148     else
00149         conf_->deleteEntry( "Password" );
00150     conf_->sync();
00151 }
00152 
00153 void CupsInfos::reload()
00154 {
00155     // do nothing, but needs to be implemented
00156 }
00157 
00158 void CupsInfos::configChanged()
00159 {
00160     // we need to reload settings
00161     load();
00162 }
KDE Home | KDE Accessibility Home | Description of Access Keys