cupsinfos.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "cupsinfos.h"
00021 #include "kmfactory.h"
00022 #include "kmtimer.h"
00023 #include "messagewindow.h"
00024 #include "ipprequest.h"
00025
00026 #include <kio/passdlg.h>
00027 #include <kio/authinfo.h>
00028 #include <klocale.h>
00029 #include <kconfig.h>
00030 #include <kapplication.h>
00031 #include <dcopclient.h>
00032 #include <kdebug.h>
00033 #include <kstringhandler.h>
00034
00035 #include <cups/cups.h>
00036 #include <cups/ipp.h>
00037
00038 const char* cupsGetPasswordCB(const char*)
00039 {
00040 return CupsInfos::self()->getPasswordCB();
00041 }
00042
00043 CupsInfos* CupsInfos::unique_ = 0;
00044
00045 CupsInfos* CupsInfos::self()
00046 {
00047 if (!unique_)
00048 {
00049 unique_ = new CupsInfos();
00050 }
00051 return unique_;
00052 }
00053
00054 CupsInfos::CupsInfos()
00055 : KPReloadObject(true)
00056 {
00057 count_ = 0;
00058
00059 load();
00060
00061
00062
00063
00064
00065
00066 cupsSetPasswordCB(cupsGetPasswordCB);
00067 }
00068
00069 CupsInfos::~CupsInfos()
00070 {
00071 }
00072
00073 QString CupsInfos::hostaddr() const
00074 {
00075 if (host_[0] != '/')
00076 return host_ + ":" + QString::number(port_);
00077 return host_;
00078 }
00079
00080 QString CupsInfos::ippAssembleURI(const QString& resource) const
00081 {
00082 return IppRequest::assembleURI(host(), port(), resource);
00083 }
00084
00085 void CupsInfos::setHost(const QString& s)
00086 {
00087 host_ = s;
00088 cupsSetServer(s.latin1());
00089 }
00090
00091 void CupsInfos::setPort(int p)
00092 {
00093 port_ = p;
00094 ippSetPort(p);
00095 }
00096
00097 void CupsInfos::setLogin(const QString& s)
00098 {
00099 login_ = s;
00100 cupsSetUser(s.latin1());
00101 }
00102
00103 void CupsInfos::setPassword(const QString& s)
00104 {
00105 password_ = s;
00106 }
00107
00108 void CupsInfos::setSavePassword( bool on )
00109 {
00110 savepwd_ = on;
00111 }
00112
00113 const char* CupsInfos::getPasswordCB()
00114 {
00115 QPair<QString,QString> pwd = KMFactory::self()->requestPassword( count_, login_, host_, port_ );
00116
00117 if ( pwd.first.isEmpty() && pwd.second.isEmpty() )
00118 return NULL;
00119 setLogin( pwd.first );
00120 setPassword( pwd.second );
00121 return pwd.second.latin1();
00122 }
00123
00124 void CupsInfos::load()
00125 {
00126 KConfig *conf_ = KMFactory::self()->printConfig();
00127 conf_->setGroup("CUPS");
00128 host_ = conf_->readEntry("Host",QString::fromLatin1(cupsServer()));
00129 port_ = conf_->readNumEntry("Port",ippPort());
00130 login_ = conf_->readEntry("Login",QString::fromLatin1(cupsUser()));
00131 savepwd_ = conf_->readBoolEntry( "SavePassword", false );
00132 if ( savepwd_ )
00133 {
00134 password_ = KStringHandler::obscure( conf_->readEntry( "Password" ) );
00135 KMFactory::self()->initPassword( login_, password_, host_, port_ );
00136 }
00137 else
00138 password_ = QString::null;
00139 if (login_.isEmpty()) login_ = QString::null;
00140 reallogin_ = cupsUser();
00141
00142
00143 cupsSetServer(host_.latin1());
00144 cupsSetUser(login_.latin1());
00145 ippSetPort(port_);
00146 }
00147
00148 void CupsInfos::save()
00149 {
00150 KConfig *conf_ = KMFactory::self()->printConfig();
00151 conf_->setGroup("CUPS");
00152 conf_->writeEntry("Host",host_);
00153 conf_->writeEntry("Port",port_);
00154 conf_->writeEntry("Login",login_);
00155 conf_->writeEntry( "SavePassword", savepwd_ );
00156 if ( savepwd_ )
00157 conf_->writeEntry( "Password", KStringHandler::obscure( password_ ) );
00158 else
00159 conf_->deleteEntry( "Password" );
00160 conf_->sync();
00161 }
00162
00163 void CupsInfos::reload()
00164 {
00165
00166 }
00167
00168 void CupsInfos::configChanged()
00169 {
00170
00171 load();
00172 }
|