KDEPrint
cupsinfos.cpp
Go to the documentation of this file.00001
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
00025 #include <kio/authinfo.h>
00026 #include <klocale.h>
00027 #include <kconfig.h>
00028 #include <kapplication.h>
00029 #include <kdebug.h>
00030 #include <kstringhandler.h>
00031
00032 #include <cups/cups.h>
00033 #include <cups/ipp.h>
00034
00035 const char* cupsGetPasswordCB(const char*)
00036 {
00037 return CupsInfos::self()->getPasswordCB();
00038 }
00039
00040 CupsInfos* CupsInfos::unique_ = 0;
00041
00042 CupsInfos* CupsInfos::self()
00043 {
00044 if (!unique_) {
00045 unique_ = new CupsInfos();
00046 }
00047 return unique_;
00048 }
00049
00050 CupsInfos::CupsInfos()
00051 : KPReloadObject(true)
00052 {
00053 count_ = 0;
00054
00055 load();
00056
00057
00058
00059
00060
00061
00062 cupsSetPasswordCB(cupsGetPasswordCB);
00063 }
00064
00065 CupsInfos::~CupsInfos()
00066 {
00067 }
00068
00069 void CupsInfos::setHost(const QString& s)
00070 {
00071 host_ = s;
00072 cupsSetServer(qPrintable(s));
00073 }
00074
00075 void CupsInfos::setPort(int p)
00076 {
00077 port_ = p;
00078 ippSetPort(p);
00079 }
00080
00081 void CupsInfos::setLogin(const QString& s)
00082 {
00083 login_ = s;
00084 cupsSetUser(qPrintable(s));
00085 }
00086
00087 void CupsInfos::setPassword(const QString& s)
00088 {
00089 password_ = s;
00090 }
00091
00092 void CupsInfos::setSavePassword(bool on)
00093 {
00094 savepwd_ = on;
00095 }
00096
00097 const char* CupsInfos::getPasswordCB()
00098 {
00099 QPair<QString, QString> pwd = KMFactory::self()->requestPassword(count_, login_, host_, port_);
00100
00101 if (pwd.first.isEmpty() && pwd.second.isEmpty())
00102 return NULL;
00103 setLogin(pwd.first);
00104 setPassword(pwd.second);
00105 return qPrintable(pwd.second);
00106 }
00107
00108 void CupsInfos::load()
00109 {
00110 KConfig *cf = KMFactory::self()->printConfig();
00111
00112 KConfigGroup conf_ = cf->group("CUPS");
00113 host_ = conf_.readEntry("Host", cupsServer());
00114 port_ = conf_.readEntry("Port", ippPort());
00115 login_ = conf_.readEntry("Login", cupsUser());
00116 savepwd_ = conf_.readEntry("SavePassword", false);
00117 if (savepwd_) {
00118 password_ = KStringHandler::obscure(conf_.readEntry("Password"));
00119 KMFactory::self()->initPassword(login_, password_, host_, port_);
00120 } else
00121 password_.clear();
00122 if (login_.isEmpty()) login_.clear();
00123 reallogin_ = cupsUser();
00124
00125
00126 cupsSetServer(qPrintable(host_));
00127 cupsSetUser(qPrintable(login_));
00128 ippSetPort(port_);
00129 }
00130
00131 void CupsInfos::save()
00132 {
00133 KConfig *cf = KMFactory::self()->printConfig();
00134 KConfigGroup conf_ = cf->group("CUPS");
00135 conf_.writeEntry("Host", host_);
00136 conf_.writeEntry("Port", port_);
00137 conf_.writeEntry("Login", login_);
00138 conf_.writeEntry("SavePassword", savepwd_);
00139 if (savepwd_)
00140 conf_.writeEntry("Password", KStringHandler::obscure(password_));
00141 else
00142 conf_.deleteEntry("Password");
00143 conf_.sync();
00144 }
00145
00146 void CupsInfos::reload()
00147 {
00148
00149 }
00150
00151 void CupsInfos::configChanged()
00152 {
00153
00154 load();
00155 }
00156
00157 const QString CupsInfos::hostaddr() const
00158 {
00159 if (host_[0] != '/')
00160 return QString("%1:%2")
00161 .arg(host_)
00162 .arg(port_);
00163 else
00164 return QString("%1")
00165 .arg(host_);
00166 }
00167
00168 const QString CupsInfos::ippaddr() const
00169 {
00170 if (host_[0] != '/')
00171 return QString("%1:%2")
00172 .arg(host_)
00173 .arg(port_);
00174 else
00175 return QString("localhost");
00176 }