1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
/*
* XPP - The X Printing Panel
* --------------------------
*
* cupsHelper - This class was originally written by Michael Goffioul
* (e-mail: gofioul@emic.ucl.ac.be) for QTCUPS, another excellent
* printing frontend for CUPS. This version has the same functions,
* but is more general and it is not dependent on QT.
*
* Thank you for your great work, Michael!
*
* See the header file (cupshelper.h) for an overview of the fields and
* methods.
*
* Copyright 2000 by Till Kamppeter and Michael Goffioul
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*
*/
#include "config.h"
#include "cupshelper.h"
#include "passworddialog.h"
#include <stdlib.h>
#include <stdio.h>
#include <cups/language.h>
//*******************************************************
//* CUPS library callback function for getting password *
//*******************************************************
const char* cupsGetPasswordCB(const char* prompt)
{
return CupsHelper::cupsGetPassword(prompt);
}
const char* cupsUseCachedPasswordCB(const char* prompt)
{
return CupsHelper::cupsUseCachedPassword(prompt);
}
//**************************************************************************
char CupsHelper::host_[256] = "";
int CupsHelper::port_ = -1;
char CupsHelper::login_[256] = "";
char CupsHelper::password_[256] = "";
CupsHelper::CupsHelper(){}
CupsHelper::~CupsHelper(){}
const char* CupsHelper::host(){
return host_;
}
int CupsHelper::port(){
return port_;
}
void CupsHelper::setHostInfo(const char *host, int port){
strcpy(host_,host);
cupsSetServer(host_);
port_ = port;
ippSetPort(port_);
}
void CupsHelper::setLoginInfo(const char *usr, const char *pwd){
strcpy(login_,usr);
cupsSetUser(login_);
strcpy(password_,pwd);
}
const char* CupsHelper::login(){
return login_;
}
const char* CupsHelper::password(){
return password_;
}
ipp_t* CupsHelper::newIppRequest(){
ipp_t *request = ippNew();
request->request.op.request_id = 1;
cups_lang_t *lang = cupsLangDefault();
ippAddString(request,IPP_TAG_OPERATION,IPP_TAG_CHARSET,"attributes-charset",NULL,cupsLangEncoding(lang));
ippAddString(request,IPP_TAG_OPERATION,IPP_TAG_LANGUAGE,"attributes-natural-language",NULL,lang->language);
return request;
}
ipp_t* CupsHelper::processRequest(ipp_t *req, const char *res){
http_t *HTTP = httpConnect(host_,port_);
if (!HTTP) {
ippDelete(req);
return 0;
}
ipp_t *answer = cupsDoRequest(HTTP,req,res);
httpClose(HTTP);
if (!answer)
return 0;
if (answer->state == IPP_ERROR || answer->state == IPP_IDLE) {
ippDelete(answer);
return 0;
}
return answer;
}
bool CupsHelper::checkHost(){
http_t *HTTP = httpConnect(host_,port_);
if (!HTTP) return false;
httpClose(HTTP);
return true;
}
const char* CupsHelper::cupsGetPassword(const char* prompt){
char login[256];
const char *passwd;
strcpy(login,login_);
// open a dialog to ask for password
passwd = passworddialog(prompt,login,password());
if (passwd) {
setLoginInfo(login,passwd);
return passwd;
} else {
setLoginInfo(login,"");
return NULL;
}
}
const char* CupsHelper::cupsUseCachedPassword(const char* prompt){
return password_;
}
void CupsHelper::askForPassword(){
cupsSetPasswordCB(cupsGetPasswordCB);
}
void CupsHelper::useCachedPassword(){
cupsSetPasswordCB(cupsUseCachedPasswordCB);
}
void CupsHelper::setup(){
if (strcmp(host_,"") == 0) strcpy(host_,cupsServer());
if (host_[0] == '/') strcpy(host_,"localhost");
if (strcmp(login_,"") == 0) strcpy(login_,cupsUser());
if (port_ < 0) port_ = ippPort();
askForPassword();
}
int CupsHelper::printerType(const char *name){
ipp_t *request = newIppRequest();
char str[256];
int value(-1);
request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
sprintf(str,"ipp://%s:%d/printers/%s",host(),port(),name);
ippAddString(request,IPP_TAG_OPERATION,IPP_TAG_URI,"printer-uri",NULL,str);
//sprintf(str,"/printers/%s",name);
sprintf(str,"/printers/");
request = processRequest(request,str);
if (request && request->curtag == IPP_TAG_PRINTER) {
ipp_attribute_t *attr = ippFindAttribute(request,"printer-type",IPP_TAG_ENUM);
if (attr) value = attr->values[0].integer;
}
ippDelete(request);
return value;
}
|