Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
Merge pull request #104 from jacobmarble/job-title
Browse files Browse the repository at this point in the history
Make GCP job id prefix to job title optional.
  • Loading branch information
vitalybuka committed Oct 5, 2015
2 parents 149c605 + 627c534 commit 29cea39
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 16 deletions.
25 changes: 15 additions & 10 deletions cups/cups.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,16 @@ var (

// Interface between Go and the CUPS API.
type CUPS struct {
cc *cupsCore
pc *ppdCache
infoToDisplayName bool
displayNamePrefix string
printerAttributes []string
systemTags map[string]string
cc *cupsCore
pc *ppdCache
infoToDisplayName bool
prefixJobIDToJobTitle bool
displayNamePrefix string
printerAttributes []string
systemTags map[string]string
}

func NewCUPS(infoToDisplayName bool, displayNamePrefix string, printerAttributes []string, maxConnections uint, connectTimeout time.Duration) (*CUPS, error) {
func NewCUPS(infoToDisplayName, prefixJobIDToJobTitle bool, displayNamePrefix string, printerAttributes []string, maxConnections uint, connectTimeout time.Duration) (*CUPS, error) {
if err := checkPrinterAttributes(printerAttributes); err != nil {
return nil, err
}
Expand Down Expand Up @@ -406,12 +407,16 @@ func convertJobState(cupsState int32) cdd.PrintJobStateDiff {

// Print sends a new print job to the specified printer. The job ID
// is returned.
func (c *CUPS) Print(printername, filename, title, user string, ticket *cdd.CloudJobTicket) (uint32, error) {
func (c *CUPS) Print(printername, filename, title, user, gcpJobID string, ticket *cdd.CloudJobTicket) (uint32, error) {
pn := C.CString(printername)
defer C.free(unsafe.Pointer(pn))
fn := C.CString(filename)
defer C.free(unsafe.Pointer(fn))
var t *C.char

if c.prefixJobIDToJobTitle {
title = fmt.Sprintf("gcp:%s %s", gcpJobID, title)
}
if len(title) > 255 {
t = C.CString(title[:255])
} else {
Expand All @@ -433,12 +438,12 @@ func (c *CUPS) Print(printername, filename, title, user string, ticket *cdd.Clou
u := C.CString(user)
defer C.free(unsafe.Pointer(u))

jobID, err := c.cc.printFile(u, pn, fn, t, numOptions, o)
cupsJobID, err := c.cc.printFile(u, pn, fn, t, numOptions, o)
if err != nil {
return 0, err
}

return uint32(jobID), nil
return uint32(cupsJobID), nil
}

// convertIPPDateToTime converts an RFC 2579 date to a time.Time object.
Expand Down
4 changes: 4 additions & 0 deletions gcp-cups-connector-util/gcp-cups-connector-util.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ func updateConfigFile() {
if err != nil {
panic(err)
}
if configFilename == "" {
fmt.Println("Could not find a config file to update")
return
}

// Same config in []byte format.
configRaw, err := ioutil.ReadFile(configFilename)
Expand Down
4 changes: 4 additions & 0 deletions gcp-cups-connector-util/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ var (
copyPrinterInfoToDisplayNameFlag = flag.String(
"copy-printer-info-to-display-name", "",
"Whether to copy the CUPS printer's printer-info attribute to the GCP printer's defaultDisplayName")
prefixJobIDToJobTitleFlag = flag.String(
"prefix-job-id-to-job-title", "",
"Whether to add the job ID to the beginning of the job title")
displayNamePrefixFlag = flag.String(
"display-name-prefix", "",
"Prefix to add to GCP printer's defaultDisplayName")
Expand Down Expand Up @@ -390,6 +393,7 @@ func createConfigFile(xmppJID, robotRefreshToken, userRefreshToken, shareScope,
flagToBool(cupsJobFullUsernameFlag, lib.DefaultConfig.CUPSJobFullUsername),
flagToBool(cupsIgnoreRawPrintersFlag, lib.DefaultConfig.CUPSIgnoreRawPrinters),
flagToBool(copyPrinterInfoToDisplayNameFlag, lib.DefaultConfig.CopyPrinterInfoToDisplayName),
flagToBool(prefixJobIDToJobTitleFlag, lib.DefaultConfig.PrefixJobIDToJobTitle),
flagToString(displayNamePrefixFlag, lib.DefaultConfig.DisplayNamePrefix),
flagToString(monitorSocketFilenameFlag, lib.DefaultConfig.MonitorSocketFilename),
flagToString(gcpBaseURLFlag, lib.DefaultConfig.GCPBaseURL),
Expand Down
5 changes: 3 additions & 2 deletions gcp-cups-connector/gcp-cups-connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ func main() {
defer x.Quit()
}

c, err := cups.NewCUPS(config.CopyPrinterInfoToDisplayName, config.DisplayNamePrefix,
config.CUPSPrinterAttributes, config.CUPSMaxConnections, cupsConnectTimeout)
c, err := cups.NewCUPS(config.CopyPrinterInfoToDisplayName, config.PrefixJobIDToJobTitle,
config.DisplayNamePrefix, config.CUPSPrinterAttributes, config.CUPSMaxConnections,
cupsConnectTimeout)
if err != nil {
glog.Fatal(err)
}
Expand Down
4 changes: 1 addition & 3 deletions gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,12 +630,10 @@ func (gcp *GoogleCloudPrint) processJob(job *Job, printer *lib.Printer, reportJo
return
}

jobTitle := fmt.Sprintf("gcp:%s %s", job.GCPJobID, job.Title)

gcp.jobs <- &lib.Job{
CUPSPrinterName: printer.Name,
Filename: filename,
Title: jobTitle,
Title: job.Title,
User: job.OwnerID,
JobID: job.GCPJobID,
Ticket: ticket,
Expand Down
4 changes: 4 additions & 0 deletions lib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ type Config struct {
// Whether to copy the CUPS printer's printer-info attribute to the GCP printer's defaultDisplayName.
CopyPrinterInfoToDisplayName bool `json:"copy_printer_info_to_display_name"`

// Whether to add the job ID to the beginning of the job title. Useful for debugging.
PrefixJobIDToJobTitle bool `json:"prefix_job_id_to_job_title"`

// Prefix for all GCP printers hosted by this connector.
DisplayNamePrefix string `json:"display_name_prefix"`

Expand Down Expand Up @@ -172,6 +175,7 @@ var DefaultConfig = Config{
CUPSJobFullUsername: false,
CUPSIgnoreRawPrinters: true,
CopyPrinterInfoToDisplayName: true,
PrefixJobIDToJobTitle: false,
DisplayNamePrefix: "",
MonitorSocketFilename: "/tmp/cups-connector-monitor.sock",
GCPBaseURL: "https://www.google.com/cloudprint/",
Expand Down
2 changes: 1 addition & 1 deletion manager/printermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ func (pm *PrinterManager) printJob(cupsPrinterName, filename, title, user, jobID
printer.CUPSJobSemaphore.Acquire()
defer printer.CUPSJobSemaphore.Release()

cupsJobID, err := pm.cups.Print(printer.Name, filename, title, user, ticket)
cupsJobID, err := pm.cups.Print(printer.Name, filename, title, user, jobID, ticket)
if err != nil {
pm.incrementJobsProcessed(false)
glog.Errorf("Failed to send job %s to CUPS: %s", jobID, err)
Expand Down

0 comments on commit 29cea39

Please sign in to comment.