#include "cgi-private.h"
static void do_job_op(http_t *http, int job_id, ipp_op_t op);
int
main(int argc,
char *argv[])
{
http_t *http;
const char *op;
const char *job_id_var;
int job_id;
cgiInitialize();
cgiSetVariable("SECTION", "jobs");
cgiSetVariable("REFRESH_PAGE", "");
http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
if ((job_id_var = cgiGetVariable("JOB_ID")) != NULL)
job_id = atoi(job_id_var);
else
job_id = 0;
if ((op = cgiGetVariable("OP")) != NULL && job_id > 0 && cgiIsPOST())
{
if (!strcmp(op, "cancel-job"))
do_job_op(http, job_id, IPP_CANCEL_JOB);
else if (!strcmp(op, "hold-job"))
do_job_op(http, job_id, IPP_HOLD_JOB);
else if (!strcmp(op, "move-job"))
cgiMoveJobs(http, NULL, job_id);
else if (!strcmp(op, "release-job"))
do_job_op(http, job_id, IPP_RELEASE_JOB);
else if (!strcmp(op, "restart-job"))
do_job_op(http, job_id, IPP_RESTART_JOB);
else
{
cgiStartHTML(cgiText(_("Jobs")));
cgiCopyTemplateLang("error-op.tmpl");
cgiEndHTML();
}
}
else
{
cgiStartHTML(cgiText(_("Jobs")));
cgiShowJobs(http, NULL);
cgiEndHTML();
}
httpClose(http);
return (0);
}
static void
do_job_op(http_t *http,
int job_id,
ipp_op_t op)
{
ipp_t *request;
char uri[HTTP_MAX_URI];
const char *user;
request = ippNewRequest(op);
snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", job_id);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri",
NULL, uri);
if ((user = getenv("REMOTE_USER")) == NULL)
user = "guest";
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
"requesting-user-name", NULL, user);
ippDelete(cupsDoRequest(http, request, "/jobs"));
if (cupsLastError() <= IPP_OK_CONFLICT && getenv("HTTP_REFERER"))
{
char url[1024];
strcpy(url, "5;URL=");
cgiFormEncode(url + 6, getenv("HTTP_REFERER"), sizeof(url) - 6);
cgiSetVariable("refresh_page", url);
}
else if (cupsLastError() == IPP_NOT_AUTHORIZED)
{
puts("Status: 401\n");
exit(0);
}
cgiStartHTML(cgiText(_("Jobs")));
if (cupsLastError() > IPP_OK_CONFLICT)
cgiShowIPPError(_("Job operation failed:"));
else if (op == IPP_CANCEL_JOB)
cgiCopyTemplateLang("job-cancel.tmpl");
else if (op == IPP_HOLD_JOB)
cgiCopyTemplateLang("job-hold.tmpl");
else if (op == IPP_RELEASE_JOB)
cgiCopyTemplateLang("job-release.tmpl");
else if (op == IPP_RESTART_JOB)
cgiCopyTemplateLang("job-restart.tmpl");
cgiEndHTML();
}