]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cgi-bin/ipp-var.c
Load cups into easysw/current.
[thirdparty/cups.git] / cgi-bin / ipp-var.c
index 292cfea24664ba3de4ce104892b68889d5c50f48..274c8e9b8d8e026e8a2c14f2126d32795923d27e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * "$Id: ipp-var.c 4921 2006-01-12 21:26:26Z mike $"
+ * "$Id: ipp-var.c 5235 2006-03-06 13:02:23Z mike $"
  *
  *   CGI <-> IPP variable routines for the Common UNIX Printing System (CUPS).
  *
  *
  *   cgiGetAttributes()    - Get the list of attributes that are needed
  *                           by the template file.
- *   cupsGetIPPObjects()   - Get the objects in an IPP response.
+ *   cgiGetIPPObjects()    - Get the objects in an IPP response.
+ *   cgiMoveJobs()         - Move one or more jobs.
+ *   cgiPrintTestPage()    - Print a test page.
  *   cgiRewriteURL()       - Rewrite a printer URI into a web browser URL...
  *   cgiSetIPPObjectVars() - Set CGI variables from an IPP object.
  *   cgiSetIPPVars()       - Set CGI variables from an IPP response.
+ *   cgiShowIPPError()     - Show the last IPP error message.
+ *   cgiShowJobs()         - Show print jobs.
+ *   cgiText()             - Return localized text.
  */
 
 /*
@@ -165,7 +170,7 @@ cgiGetAttributes(ipp_t      *request,       /* I - IPP request */
 
 
 /*
- * 'cupsGetIPPObjects()' - Get the objects in an IPP response.
+ * 'cgiGetIPPObjects()' - Get the objects in an IPP response.
  */
 
 cups_array_t *                         /* O - Array of objects */
@@ -263,6 +268,355 @@ cgiGetIPPObjects(ipp_t *response, /* I - IPP response */
 }
 
 
+/*
+ * 'cgiMoveJobs()' - Move one or more jobs.
+ *
+ * At least one of dest or job_id must be non-zero/NULL.
+ */
+
+void
+cgiMoveJobs(http_t     *http,          /* I - Connection to server */
+            const char *dest,          /* I - Destination or NULL */
+            int        job_id)         /* I - Job ID or 0 for all */
+{
+  int          i;                      /* Looping var */
+  const char   *user;                  /* Username */
+  ipp_t                *request,               /* IPP request */
+               *response;              /* IPP response */
+  ipp_attribute_t *attr;               /* Current attribute */
+  const char   *name;                  /* Destination name */
+  const char   *job_printer_uri;       /* JOB_PRINTER_URI form variable */
+  char         current_dest[1024];     /* Current destination */
+
+
+ /*
+  * See who is logged in...
+  */
+
+  if ((user = getenv("REMOTE_USER")) == NULL)
+    user = "guest";
+
+ /*
+  * See if the user has already selected a new destination...
+  */
+
+  if ((job_printer_uri = cgiGetVariable("JOB_PRINTER_URI")) == NULL)
+  {
+   /*
+    * Make sure necessary form variables are set...
+    */
+
+    if (job_id)
+    {
+      char     temp[255];              /* Temporary string */
+
+
+      sprintf(temp, "%d", job_id);
+      cgiSetVariable("JOB_ID", temp);
+    }
+
+    if (dest)
+      cgiSetVariable("PRINTER_NAME", dest);
+
+   /*
+    * No new destination specified, show the user what the available
+    * printers/classes are...
+    */
+
+    if (!dest)
+    {
+     /*
+      * Get the current destination for job N...
+      */
+
+      char     job_uri[1024];          /* Job URI */
+
+
+      request = ippNewRequest(IPP_GET_JOB_ATTRIBUTES);
+
+      snprintf(job_uri, sizeof(job_uri), "ipp://localhost/jobs/%d", job_id);
+      ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri",
+                   NULL, job_uri);
+      ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
+                   "requested-attributes", NULL, "job-printer-uri");
+      
+      if ((response = cupsDoRequest(http, request, "/")) != NULL)
+      {
+        if ((attr = ippFindAttribute(response, "job-printer-uri",
+                                    IPP_TAG_URI)) != NULL)
+       {
+        /*
+         * Pull the name from the URI...
+         */
+
+         strlcpy(current_dest, strrchr(attr->values[0].string.text, '/') + 1,
+                 sizeof(current_dest));
+          dest = current_dest;
+       }
+
+        ippDelete(response);
+      }
+
+      if (!dest)
+      {
+       /*
+        * Couldn't get the current destination...
+       */
+
+        cgiStartHTML(cgiText(_("Move Job")));
+       cgiShowIPPError(_("Unable to find destination for job!"));
+       cgiEndHTML();
+       return;
+      }
+    }
+
+   /*
+    * Get the list of available destinations...
+    */
+
+    request = ippNewRequest(CUPS_GET_PRINTERS);
+
+    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
+                 "requested-attributes", NULL, "printer-uri-supported");
+
+    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
+                 "requesting-user-name", NULL, user);
+
+    if ((response = cupsDoRequest(http, request, "/")) != NULL)
+    {
+      for (i = 0, attr = ippFindAttribute(response, "printer-uri-supported",
+                                          IPP_TAG_URI);
+           attr;
+          attr = ippFindNextAttribute(response, "printer-uri-supported",
+                                      IPP_TAG_URI))
+      {
+       /*
+       * Pull the name from the URI...
+       */
+
+       name = strrchr(attr->values[0].string.text, '/') + 1;
+
+       /*
+        * If the name is not the same as the current destination, add it!
+       */
+
+        if (strcasecmp(name, dest))
+       {
+         cgiSetArray("JOB_PRINTER_URI", i, attr->values[0].string.text);
+         cgiSetArray("JOB_PRINTER_NAME", i, name);
+         i ++;
+       }
+      }
+
+      ippDelete(response);
+    }
+
+   /*
+    * Show the form...
+    */
+
+    if (job_id)
+      cgiStartHTML(cgiText(_("Move Job")));
+    else
+      cgiStartHTML(cgiText(_("Move All Jobs")));
+
+    cgiCopyTemplateLang("job-move.tmpl");
+  }
+  else
+  {
+   /*
+    * Try moving the job or jobs...
+    */
+
+    char       uri[1024],              /* Job/printer URI */
+               resource[1024],         /* Post resource */
+               refresh[1024];          /* Refresh URL */
+    const char *job_printer_name;      /* New printer name */
+
+
+    request = ippNewRequest(CUPS_MOVE_JOB);
+
+    if (job_id)
+    {
+     /*
+      * Move 1 job...
+      */
+
+      snprintf(resource, sizeof(resource), "/jobs/%d", job_id);
+
+      snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", job_id);
+      ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri",
+                   NULL, uri);
+    }
+    else
+    {
+     /*
+      * Move all active jobs on a destination...
+      */
+
+      snprintf(resource, sizeof(resource), "/%s/%s",
+               cgiGetVariable("SECTION"), dest);
+
+      httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
+                       "localhost", ippPort(), "/%s/%s",
+                      cgiGetVariable("SECTION"), dest);
+      ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
+                   NULL, uri);
+    }
+
+    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-printer-uri",
+                 NULL, job_printer_uri);
+
+    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
+                 "requesting-user-name", NULL, user);
+
+    ippDelete(cupsDoRequest(http, request, resource));
+
+   /*
+    * Show the results...
+    */
+
+    job_printer_name = strrchr(job_printer_uri, '/') + 1;
+
+    if (cupsLastError() <= IPP_OK_CONFLICT)
+    {
+      cgiRewriteURL(job_printer_uri, resource, sizeof(resource), NULL);
+      cgiFormEncode(uri, resource, sizeof(uri));
+      snprintf(refresh, sizeof(refresh), "2;%s", uri);
+      cgiSetVariable("refresh_page", refresh);
+    }
+
+    if (job_id)
+      cgiStartHTML(cgiText(_("Move Job")));
+    else
+      cgiStartHTML(cgiText(_("Move All Jobs")));
+
+    if (cupsLastError() > IPP_OK_CONFLICT)
+    {
+      if (job_id)
+       cgiShowIPPError(_("Unable to move job"));
+      else
+        cgiShowIPPError(_("Unable to move jobs"));
+    }
+    else
+    {
+      cgiSetVariable("JOB_PRINTER_NAME", job_printer_name);
+      cgiCopyTemplateLang("job-moved.tmpl");
+    }
+  }
+
+  cgiEndHTML();
+}
+
+
+/*
+ * 'cgiPrintTestPage()' - Print a test page.
+ */
+
+void
+cgiPrintTestPage(http_t     *http,     /* I - Connection to server */
+                 const char *dest)     /* I - Destination printer/class */
+{
+  ipp_t                *request,               /* IPP request */
+               *response;              /* IPP response */
+  char         uri[HTTP_MAX_URI],      /* Printer URI */
+               resource[1024],         /* POST resource path */
+               refresh[1024],          /* Refresh URL */
+               filename[1024];         /* Test page filename */
+  const char   *datadir;               /* CUPS_DATADIR env var */
+  const char   *user;                  /* Username */
+
+
+ /*
+  * See who is logged in...
+  */
+
+  if ((user = getenv("REMOTE_USER")) == NULL)
+    user = "guest";
+
+ /*
+  * Locate the test page file...
+  */
+
+  if ((datadir = getenv("CUPS_DATADIR")) == NULL)
+    datadir = CUPS_DATADIR;
+
+  snprintf(filename, sizeof(filename), "%s/data/testprint.ps", datadir);
+
+ /*
+  * Point to the printer/class...
+  */
+
+  snprintf(resource, sizeof(resource), "/%s/%s", cgiGetVariable("SECTION"),
+           dest);
+
+  httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
+                   "localhost", ippPort(), "/%s/%s", cgiGetVariable("SECTION"),
+                  dest);
+
+ /*
+  * Build an IPP_PRINT_JOB request, which requires the following
+  * attributes:
+  *
+  *    attributes-charset
+  *    attributes-natural-language
+  *    printer-uri
+  *    requesting-user-name
+  *    document-format
+  */
+
+  request = ippNewRequest(IPP_PRINT_JOB);
+
+  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
+               NULL, uri);
+
+  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
+               "requesting-user-name", NULL, user);
+
+  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name",
+               NULL, "Test Page");
+
+  ippAddString(request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format",
+               NULL, "application/postscript");
+
+ /*
+  * Do the request and get back a response...
+  */
+
+  if ((response = cupsDoFileRequest(http, request, resource,
+                                    filename)) != NULL)
+  {
+    cgiSetIPPVars(response, NULL, NULL, NULL, 0);
+
+    ippDelete(response);
+  }
+
+  if (cupsLastError() <= IPP_OK_CONFLICT)
+  {
+   /*
+    * Automatically reload the printer status page...
+    */
+
+    cgiFormEncode(uri, resource, sizeof(uri));
+    snprintf(refresh, sizeof(refresh), "2;%s", uri);
+    cgiSetVariable("refresh_page", refresh);
+  }
+
+  cgiStartHTML(cgiText(_("Print Test Page")));
+
+  if (cupsLastError() > IPP_OK_CONFLICT)
+    cgiShowIPPError(_("Unable to print test page:"));
+  else
+  {
+    cgiSetVariable("PRINTER_NAME", dest);
+
+    cgiCopyTemplateLang("test-page.tmpl");
+  }
+
+  cgiEndHTML();
+}
+
+
 /*
  * 'cgiRewriteURL()' - Rewrite a printer URI into a web browser URL...
  */
@@ -307,7 +661,7 @@ cgiRewriteURL(const char *uri,              /* I - Current URI */
     if ((server = getenv("SERVER_NAME")) == NULL)
       server = "";
 
-    httpGetHostname(servername, sizeof(servername));
+    httpGetHostname(NULL, servername, sizeof(servername));
 
    /*
     * Then flag whether we are using SSL on this connection...
@@ -320,8 +674,8 @@ cgiRewriteURL(const char *uri,              /* I - Current URI */
   * Convert the URI to a URL...
   */
 
-  httpSeparateURI(uri, method, sizeof(method), userpass, sizeof(userpass),
-                  hostname, sizeof(hostname), &port,
+  httpSeparateURI(HTTP_URI_CODING_ALL, uri, method, sizeof(method), userpass,
+                  sizeof(userpass), hostname, sizeof(hostname), &port,
                  rawresource, sizeof(rawresource));
 
   if (!strcmp(method, "ipp") ||
@@ -542,7 +896,8 @@ cgiSetIPPObjectVars(
            break;
 
        case IPP_TAG_URI :
-           if (strchr(attr->values[i].string.text, ':') != NULL)
+           if (strchr(attr->values[i].string.text, ':') &&
+               strcmp(name, "device_uri"))
            {
             /*
              * Rewrite URIs...
@@ -686,6 +1041,21 @@ cgiSetIPPVars(ipp_t      *response,       /* I - Response data to be copied... */
 }
 
 
+/*
+ * 'cgiShowIPPError()' - Show the last IPP error message.
+ *
+ * The caller must still call cgiStartHTML() and cgiEndHTML().
+ */
+
+void
+cgiShowIPPError(const char *message)   /* I - Contextual message */
+{
+  cgiSetVariable("MESSAGE", cgiText(message));
+  cgiSetVariable("ERROR", cupsLastErrorString());
+  cgiCopyTemplateLang("error.tmpl");
+}
+
+
 /*
  * 'cgiShowJobs()' - Show print jobs.
  */
@@ -708,7 +1078,6 @@ cgiShowJobs(http_t     *http,              /* I - Connection to server */
   char                 url[1024],      /* URL for prev/next/this */
                        *urlptr,        /* Position in URL */
                        *urlend;        /* End of URL */
-  cups_lang_t          *language;      /* Language information */
 
 
  /*
@@ -720,23 +1089,12 @@ cgiShowJobs(http_t     *http,            /* I - Connection to server */
   *    printer-uri
   */
 
-  request = ippNew();
-
-  language = cupsLangDefault();
-
-  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
-               "attributes-charset", NULL, cupsLangEncoding(language));
-
-  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
-               "attributes-natural-language", NULL, language->language);
-
-  request->request.op.operation_id = IPP_GET_JOBS;
-  request->request.op.request_id   = 1;
+  request = ippNewRequest(IPP_GET_JOBS);
 
   if (dest)
   {
-    httpAssembleURIf(url, sizeof(url), "ipp", NULL, "localhost", ippPort(),
-                     "/printers/%s", dest);
+    httpAssembleURIf(HTTP_URI_CODING_ALL, url, sizeof(url), "ipp", NULL,
+                     "localhost", ippPort(), "/printers/%s", dest);
     ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
                  NULL, url);
   }
@@ -760,7 +1118,7 @@ cgiShowJobs(http_t     *http,              /* I - Connection to server */
     * Get a list of matching job objects.
     */
 
-    if (!dest && (var = cgiGetVariable("QUERY")) != NULL)
+    if ((var = cgiGetVariable("QUERY")) != NULL)
       search = cgiCompileSearch(var);
     else
       search = NULL;
@@ -794,7 +1152,10 @@ cgiShowJobs(http_t     *http,             /* I - Connection to server */
     if ((var = cgiGetVariable("ORDER")) != NULL)
       ascending = !strcasecmp(var, "asc");
     else
-      ascending = 1;
+    {
+      ascending = !which_jobs || !strcasecmp(which_jobs, "not-completed");
+      cgiSetVariable("ORDER", ascending ? "asc" : "dec");
+    }
 
     if (ascending)
     {
@@ -817,14 +1178,14 @@ cgiShowJobs(http_t     *http,            /* I - Connection to server */
 
     urlend = url + sizeof(url);
 
-    if (dest)
-    {
-      snprintf(url, sizeof(url), "/%s/%s?", cgiGetVariable("SECTION"), dest);
-      urlptr = url + strlen(url);
-    }
-    else if ((var = cgiGetVariable("QUERY")) != NULL)
+    if ((var = cgiGetVariable("QUERY")) != NULL)
     {
-      strlcpy(url, "/jobs/?QUERY=", sizeof(url));
+      if (dest)
+        snprintf(url, sizeof(url), "/%s/%s?QUERY=", cgiGetVariable("SECTION"),
+                dest);
+      else
+        strlcpy(url, "/jobs/?QUERY=", sizeof(url));
+
       urlptr = url + strlen(url);
 
       cgiFormEncode(urlptr, var, urlend - urlptr);
@@ -835,7 +1196,11 @@ cgiShowJobs(http_t     *http,             /* I - Connection to server */
     }
     else
     {
-      strlcpy(url, "/jobs/?", sizeof(url));
+      if (dest)
+        snprintf(url, sizeof(url), "/%s/%s?", cgiGetVariable("SECTION"), dest);
+      else
+        strlcpy(url, "/jobs/?", sizeof(url));
+
       urlptr = url + strlen(url);
     }
 
@@ -872,25 +1237,45 @@ cgiShowJobs(http_t     *http,            /* I - Connection to server */
     * Then show everything...
     */
 
-    if (!dest)
-      cgiCopyTemplateLang("search.tmpl");
+    if (dest)
+      cgiSetVariable("SEARCH_DEST", dest);
+
+    cgiCopyTemplateLang("search.tmpl");
 
     cgiCopyTemplateLang("jobs-header.tmpl");
 
-    if (count > CUPS_PAGE_MAX)
-      cgiCopyTemplateLang("page.tmpl");
+    if (count > 0)
+      cgiCopyTemplateLang("pager.tmpl");
 
     cgiCopyTemplateLang("jobs.tmpl");
 
-    if (count > CUPS_PAGE_MAX)
-      cgiCopyTemplateLang("page.tmpl");
+    if (count > 0)
+      cgiCopyTemplateLang("pager.tmpl");
 
+    cupsArrayDelete(jobs);
     ippDelete(response);
   }
 }
 
 
+/*
+ * 'cgiText()' - Return localized text.
+ */
+
+const char *                           /* O - Localized message */
+cgiText(const char *message)           /* I - Message */
+{
+  static cups_lang_t   *language = NULL;
+                                       /* Language */
+
+
+  if (!language)
+    language = cupsLangDefault();
+
+  return (_cupsLangString(language, message));
+}
+
 
 /*
- * End of "$Id: ipp-var.c 4921 2006-01-12 21:26:26Z mike $".
+ * End of "$Id: ipp-var.c 5235 2006-03-06 13:02:23Z mike $".
  */