Esempio n. 1
0
void KGVDocument::print()
{
    if( !dsc() ) return;

    KPrinter printer;
    
    if( dsc()->isStructured() ) 
    {
	printer.setPageSelection( KPrinter::ApplicationSide );

	printer.setCurrentPage( _part->miniWidget()->displayOptions().page() + 1 );
	printer.setMinMax( 1, dsc()->page_count() );
	printer.setOption( "kde-range", 
	                    pageListToRange( _part->markList()->markList() ) );

	if( printer.setup( _part->widget(), i18n("Print %1").arg(_part->url().fileName()) ) ) 
	{
	    KTempFile tf( QString::null, ".ps" );
	    if( tf.status() == 0 ) 
	    {
		if ( printer.pageList().empty() ) { 
		    KMessageBox::sorry( 0,
			    i18n( "Printing failed because the list of "
				"pages to be printed was empty." ),
			    i18n( "Error Printing" ) );
		} else if ( savePages( tf.name(), printer.pageList() ) ) {
		    printer.printFiles( QStringList( tf.name() ), true );
		} else {
		    KMessageBox::error( 0, i18n( "<qt><strong>Printing failure:</strong><br>Could not convert to PostScript</qt>" ) );
		}
	    }
	    else 
	    {
	        // TODO: Proper error handling
	        ;
	    }
	}
    }
    else 
    {
	printer.setPageSelection( KPrinter::SystemSide );
	
	if( printer.setup( _part->widget(), i18n("Print %1").arg(_part->url().fileName()) ) )
	    printer.printFiles( _fileName );
    }
}
Esempio n. 2
0
void PrintWrapper::slotPrint()
{
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();

#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
    struct sigaction action;
#endif /* HAVE_SIGACTION && !HAVE_SIGSET*/

    // read variables from command line
    QString printer = args->getOption("d");
    QString title = args->getOption("t");
    int ncopies = QString(args->getOption("n")).toInt();
    QString job_mode = args->getOption("j");
    QString system = args->getOption("system");
    KStringList optlist = args->getOptionList("o");
    QMap< QString, QString > opts;
    KURL::List files;
    QStringList filestoprint;
    force_stdin = args->isSet("stdin");
    docopy = args->isSet("c");
    bool nodialog = !(args->isSet("dialog"));

    if(isatty(0))
    {
        kdDebug(500) << "stdin is a terminal, disabling it" << endl;
        check_stdin = false;
    }

    // parse options
    for(KStringList::ConstIterator it = optlist.begin(); it != optlist.end(); ++it)
    {
        QStringList l = QStringList::split('=', QString(*it), false);
        if(l.count() >= 1)
            opts[l[0]] = (l.count() == 2 ? l[1] : QString::null);
    }

    // read file list
    for(int i = 0; i < args->count(); i++)
        files.append(args->url(i));

    // some clean-up
    args->clear();

    // set default values if necessary
    if(job_mode == "console")
        job_output = 1;
    else if(job_mode == "none")
        job_output = 2;
    else
        job_output = 0;

    // some checking
    if(files.count() > 0)
    {
        check_stdin = false;

        if(force_stdin)
        {
            showmsg(i18n("A file has been specified on the command line. Printing from STDIN will be disabled."), 1);
            force_stdin = false;
        }
    }
    if(nodialog && files.count() == 0 && !force_stdin && !check_stdin)
    {
        errormsg(i18n("When using '--nodialog', you must at least specify one file to print or use the '--stdin' flag."));
    }

    if(check_stdin)
    { // check if there's any input on stdin
        fd_set in;
        struct timeval tm;
        tm.tv_sec = 0;
        tm.tv_usec = 0;
        FD_ZERO(&in);
        FD_SET(0, &in);
        if(select(1, &in, NULL, NULL, &tm))
        { // we have data on stdin
            if(read(0, &readchar, 1) > 0)
            {
                force_stdin = true;
                check_stdin = false;
                dataread = true;
                kdDebug(500) << "input detected on stdin" << endl;
            }
            else
            {
                force_stdin = check_stdin = false;
                kdDebug(500) << "stdin closed and empty" << endl;
            }
        }
        else
            kdDebug(500) << "no input on stdin at startup" << endl;
    }

    // force_stdin ? or also check_stdin ?
    KPrinter::ApplicationType dialog_mode = (force_stdin || nodialog ? KPrinter::StandAlone : KPrinter::StandAlonePersistent);
    KPrinter::setApplicationType(dialog_mode);
    if(!force_stdin)
        KPrinter::addStandardPage(KPrinter::FilesPage);

    KPrinter kprinter;
    if(nodialog)
    {
        KMPrinter *prt(0);
        KMManager *mgr = KMManager::self();

        mgr->printerList(false);
        if(!printer.isEmpty())
            prt = mgr->findPrinter(printer);
        else
            prt = mgr->defaultPrinter();

        if(prt == 0)
            errormsg(i18n("The specified printer or the default printer could not be found."));
        else if(!prt->autoConfigure(&kprinter))
            errormsg(i18n("Operation aborted."));
    }
    else if(!printer.isEmpty())
        kprinter.setSearchName(printer);
    kprinter.setDocName(title);
    kprinter.initOptions(opts);
    kprinter.setOption("kde-filelist", files.toStringList().join("@@"));
    kdDebug(500) << kprinter.option("kde-filelist") << endl;
    if(ncopies > 0)
        kprinter.setNumCopies(ncopies);

    if(nodialog)
        slotPrintRequested(&kprinter);
    else
    {
        dlg = KPrintDialog::printerDialog(&kprinter, 0);
        if(dlg)
        {
            connect(dlg, SIGNAL(printRequested(KPrinter *)), SLOT(slotPrintRequested(KPrinter *)));
            if(check_stdin)
            {
                notif = new QSocketNotifier(0, QSocketNotifier::Read, this);
                connect(notif, SIGNAL(activated(int)), this, SLOT(slotGotStdin()));
                kdDebug(500) << "waiting for input on stdin" << endl;
            }
            dlg->exec();
            delete dlg;
        }
        else
            errormsg(i18n("Unable to construct the print dialog."));
    }