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 QString CupsInfos::hostaddr() const 00073 { 00074 if (host_[0] != '/') 00075 return host_ + ":" + QString::number(port_); 00076 return "localhost"; 00077 } 00078 00079 void CupsInfos::setHost(const QString& s) 00080 { 00081 host_ = s; 00082 cupsSetServer(s.latin1()); 00083 } 00084 00085 void CupsInfos::setPort(int p) 00086 { 00087 port_ = p; 00088 ippSetPort(p); 00089 } 00090 00091 void CupsInfos::setLogin(const QString& s) 00092 { 00093 login_ = s; 00094 cupsSetUser(s.latin1()); 00095 } 00096 00097 void CupsInfos::setPassword(const QString& s) 00098 { 00099 password_ = s; 00100 } 00101 00102 void CupsInfos::setSavePassword( bool on ) 00103 { 00104 savepwd_ = on; 00105 } 00106 00107 const char* CupsInfos::getPasswordCB() 00108 { 00109 QPair<QString,QString> pwd = KMFactory::self()->requestPassword( count_, login_, host_, port_ ); 00110 00111 if ( pwd.first.isEmpty() && pwd.second.isEmpty() ) 00112 return NULL; 00113 setLogin( pwd.first ); 00114 setPassword( pwd.second ); 00115 return pwd.second.latin1(); 00116 } 00117 00118 void CupsInfos::load() 00119 { 00120 KConfig *conf_ = KMFactory::self()->printConfig(); 00121 conf_->setGroup("CUPS"); 00122 host_ = conf_->readEntry("Host",QString::fromLatin1(cupsServer())); 00123 port_ = conf_->readNumEntry("Port",ippPort()); 00124 login_ = conf_->readEntry("Login",QString::fromLatin1(cupsUser())); 00125 savepwd_ = conf_->readBoolEntry( "SavePassword", false ); 00126 if ( savepwd_ ) 00127 { 00128 password_ = KStringHandler::obscure( conf_->readEntry( "Password" ) ); 00129 KMFactory::self()->initPassword( login_, password_, host_, port_ ); 00130 } 00131 else 00132 password_ = QString::null; 00133 if (login_.isEmpty()) login_ = QString::null; 00134 reallogin_ = cupsUser(); 00135 00136 // synchronize with CUPS 00137 cupsSetServer(host_.latin1()); 00138 cupsSetUser(login_.latin1()); 00139 ippSetPort(port_); 00140 } 00141 00142 void CupsInfos::save() 00143 { 00144 KConfig *conf_ = KMFactory::self()->printConfig(); 00145 conf_->setGroup("CUPS"); 00146 conf_->writeEntry("Host",host_); 00147 conf_->writeEntry("Port",port_); 00148 conf_->writeEntry("Login",login_); 00149 conf_->writeEntry( "SavePassword", savepwd_ ); 00150 if ( savepwd_ ) 00151 conf_->writeEntry( "Password", KStringHandler::obscure( password_ ) ); 00152 else 00153 conf_->deleteEntry( "Password" ); 00154 conf_->sync(); 00155 } 00156 00157 void CupsInfos::reload() 00158 { 00159 // do nothing, but needs to be implemented 00160 } 00161 00162 void CupsInfos::configChanged() 00163 { 00164 // we need to reload settings 00165 load(); 00166 }