def openFile(fileName, window, cmd = None): """ Opens a file with command cmd (string, read from config) or with the KDE default application (via KRun). """ if cmd: cmd, err = KShell.splitArgs(cmd) if err == KShell.NoError: cmd.append(fileName) try: Popen(cmd) return except OSError: pass # let C++ own the KRun object, it will delete itself. sip.transferto(KRun(KUrl.fromPath(fileName), window), None)
def initializeProcess(self, p): cmd, err = KShell.splitArgs(config("commands").readEntry("timidity", default_timidity_command)) if err == KShell.NoError: p.setProgram(cmd) else: pass # TODO: warn user about incorrect command
def printFiles(fileNames, printer): """Prints a list of files (PS or PDF) via a printer command. The printer command is constructed by quering the QPrinter object. If there is more than one PDF file, print to file should have been disabled in the QPrintDialog that configured the printer. """ output = printer.outputFileName() if output: # Print to File, determine suffixes, assume one file fileName = fileNames[0] inext, outext = (os.path.splitext(name)[1].lower() for name in (fileName, output)) if inext == outext: # just copy shutil.copyfile(fileName, output) else: cmd = "pdf2ps" if outext == ".ps" else "ps2pdf" try: ret = subprocess.call([cmd, fileName, output]) if ret: raise CommandFailed(KShell.joinArgs([cmd, fileName, output]), ret) except OSError: raise CommandNotFound(cmd) return # print to a printer cmd = [] # Which exe? for exe in "lpr-cups", "lpr.cups", "lpr", "lp": if KStandardDirs.findExe(exe): break else: raise NoPrintCommandFound() cmd.append(exe) # Add the arguments. # printer name if exe == "lp": cmd.append("-d") else: cmd.append("-P") cmd.append(printer.printerName()) # helper for adding (Cups) options to the command line def option(s): cmd.append("-o") cmd.append(s) # copies try: copies = printer.actualNumCopies() except AttributeError: # only available in Qt >= 4.6 copies = printer.numCopies() if exe == "lp": cmd.append("-n") cmd.append(format(copies)) else: cmd.append("-#{0}".format(copies)) # job name if printer.docName(): if exe == "lp": cmd.append("-t") cmd.append(printer.docName()) elif exe.startswith("lpr"): cmd.append("-J") cmd.append(printer.docName()) # page range if printer.printRange() == QPrinter.PageRange: pageRange = "{0}-{1}".format(printer.fromPage(), printer.toPage()) if exe == "lp": cmd.append("-P") cmd.append(pageRange) else: option("page-ranges={0}".format(pageRange)) # CUPS-specific options; detect if CUPS is available. test = QPrinter() test.setNumCopies(2) cups = test.numCopies() == 1 if cups: # media, size etc. media = [] size = printer.paperSize() if size == QPrinter.Custom: media.append("Custom.{0}x{1}mm".format(printer.heightMM(), printer.widthMM())) elif size in PAGE_SIZES: media.append(PAGE_SIZES[size]) # media source source = printer.paperSource() if source in PAPER_SOURCES: media.append(PAPER_SOURCES[source]) if media: option("media={0}".format(",".join(media))) # orientation orientation = printer.orientation() if orientation in ORIENTATIONS: option(ORIENTATIONS[orientation]) # double sided duplex = printer.duplex() if duplex == QPrinter.DuplexNone: option("sides=one-sided") elif duplex == QPrinter.DuplexAuto: if orientation == QPrinter.Landscape: option("sides=two-sided-short-edge") else: option("sides=two-sided-long-edge") elif duplex == QPrinter.DuplexLongSide: option("sides=two-sided-long-edge") elif duplex == QPrinter.DuplexShortSide: option("sides=two-sided-short-edge") # page order if printer.pageOrder() == QPrinter.LastPageFirst: option("outputorder=reverse") else: option("outputorder=normal") # collate copies if printer.collateCopies(): option("Collate=True") else: option("Collate=False") # page margins if printer.printEngine().property(QPrintEngine.PPK_PageMargins): left, top, right, bottom = printer.getPageMargins(QPrinter.Point) option("page-left={0}".format(left)) option("page-top={0}".format(top)) option("page-right={0}".format(right)) option("page-bottom={0}".format(bottom)) # cups properties properties = printer.printEngine().property(QPrintEngine.PrintEnginePropertyKey(0xFE00)) for name, value in zip(*repeat(iter(properties), 2)): option("{0}={1}".format(name, value) if value else name) # file names cmd.extend(fileNames) try: ret = subprocess.call(cmd) if ret: raise CommandFailed(KShell.joinArgs(cmd), ret) except OSError: raise CommandNotFound(cmd[0])