00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "kprintdialog.h"
00026 #include "kprinter.h"
00027 #include "kprinterimpl.h"
00028 #include "kmfactory.h"
00029 #include "kmuimanager.h"
00030 #include "kmmanager.h"
00031 #include "kmprinter.h"
00032 #include "kmvirtualmanager.h"
00033 #include "kprintdialogpage.h"
00034 #include "kprinterpropertydialog.h"
00035 #include "plugincombobox.h"
00036 #include "kpcopiespage.h"
00037 #include "treecombobox.h"
00038 #include "messagewindow.h"
00039
00040 #include <qgroupbox.h>
00041 #include <qcheckbox.h>
00042 #include <kpushbutton.h>
00043 #include <qlabel.h>
00044 #include <qcombobox.h>
00045 #include <qtabwidget.h>
00046 #include <qvbox.h>
00047 #include <qlayout.h>
00048 #include <qregexp.h>
00049 #include <kmessagebox.h>
00050 #include <qdir.h>
00051 #include <qtooltip.h>
00052 #include <qwhatsthis.h>
00053
00054 #include <klocale.h>
00055 #include <kiconloader.h>
00056 #include <kfiledialog.h>
00057 #include <kurlrequester.h>
00058 #include <klineedit.h>
00059 #include <kdebug.h>
00060 #include <kglobal.h>
00061 #include <kconfig.h>
00062 #include <kguiitem.h>
00063 #include <kstdguiitem.h>
00064 #include <kapplication.h>
00065 #include <kio/renamedlg.h>
00066
00067 #include <time.h>
00068
00069 #define SHOWHIDE(widget,on) if (on) widget->show(); else widget->hide();
00070
00071 class KPrintDialog::KPrintDialogPrivate
00072 {
00073 public:
00074 QLabel *m_type, *m_state, *m_comment, *m_location, *m_cmdlabel, *m_filelabel;
00075 KPushButton *m_properties, *m_default, *m_options, *m_ok, *m_extbtn;
00076 QPushButton *m_wizard, *m_filter;
00077 QCheckBox *m_preview;
00078 QLineEdit *m_cmd;
00079 TreeComboBox *m_printers;
00080 QVBox *m_dummy;
00081 PluginComboBox *m_plugin;
00082 KURLRequester *m_file;
00083 QCheckBox *m_persistent;
00084 bool m_reduced;
00085
00086 QPtrList<KPrintDialogPage> m_pages;
00087 KPrinter *m_printer;
00088 };
00089
00090 KPrintDialog::KPrintDialog(QWidget *parent, const char *name)
00091 : KDialog(parent,name,true)
00092 {
00093
00094 QString whatsThisLocationLabel = i18n( " <qt> The <em>Location</em> may describe where the"
00095 " selected printer is located. The Location description is created"
00096 " by the administrator of the print system (or may be"
00097 " left empty)."
00098 " </qt>" );
00099 QString whatsThisPrinterType = i18n( " <qt> The <em>Type</em> indicates your printer type."
00100 " </qt>" );
00101 QString whatsThisPrinterState = i18n( " <qt> The <em>State</em> indicates the state of the"
00102 " print queue on the print server (which could be your localhost). The"
00103 " state may be 'Idle', 'Processing', 'Stopped', 'Paused' or similar."
00104 " </qt>" );
00105 QString whatsThisPrinterComment = i18n( " <qt> The <em>Comment</em> may describe the selected"
00106 " printer. This comment is created by the administrator"
00107 " of the print system (or may be left empty)."
00108 " </qt>" );
00109 QString whatsThisPrinterSelect = i18n( " <qt>Use this combo box to select the printer to which you want to print."
00110 " If you only find the <em>KDE special printers</em> -- which save"
00111 " jobs to disk (as PostScript- or PDF-files), or deliver jobs via"
00112 " email (as a PDF"
00113 " attachment) -- but are missing a real printer, you need to..."
00114 " <ul>"
00115 " <li>...either create a local printer with the help of the <em>KDE Add"
00116 " Printer Wizard</em> which is available for the CUPS and RLPR printing"
00117 " systems (click button to the left of the 'Properties' button),</li>"
00118 " <li>...or you can try to connect to a remote"
00119 " CUPS print server by clicking the <em>System Options...</em> button"
00120 " below. A new dialog opens: click on the <em>CUPS server</em>"
00121 " icon and fill in the information required to use the remote"
00122 " server.</li>"
00123 " </ul>"
00124 " </qt>" );
00125 QString whatsThisPrintJobProperties = i18n( " <qt> This button takes you to where you can make decisions"
00126 " regarding all supported print job options."
00127 " </qt>" );
00128 QString whatsThisPrinterFilter = i18n( " <qt><p> This button reduces the list of visible printers"
00129 " to a shorter, more convenient, pre-defined list.</p>"
00130 " <p>This is particularly useful in enterprise environments"
00131 " with lots of printers. The default is to show <b>all</b> printers.</p>"
00132 " <p>To create a personal filter list, click on the"
00133 " <em>\"System Options...</em>\" button at the bottom of this dialog."
00134 " Then, in the new dialog, select <em>\"Filter\"</em> (left column in the"
00135 " <em>KDE Print Configuration</em> dialog).</p>"
00136 " </qt>" );
00137 QString whatsThisAddPrinterWizard = i18n( "<qt> This button starts the <em>KDE Add Printer Wizard</em>."
00138 " (<b>NOT</b> if you use <em>\"Generic LPD</em>\""
00139 " or <em>\"LPRng\"</em>, or <em>\"Print Through an External Program</em>\".)"
00140 " Use the Wizard (with <em>\"CUPS\"</em> or <em>\"RLPR\"</em>) to add locally"
00141 " defined printers to your system."
00142 " </qt>" );
00143 QString whatsThisExternalPrintCommand = i18n( " <qt> You can enter any command that"
00144 " would also print for you in a <em>konsole</em> window. <br>"
00145 " <b>Example:</b> <pre>a2ps -P <printername> --medium=A3</pre>."
00146 " </qt>" );
00147 QString whatsThisExpandCollapse = i18n( " <qt>This button expands or reduces the KDE"
00148 " printing dialog."
00149 " KDEPrint will remember what you do here; next time, it will"
00150 " start this dialog 'expanded' or 'collapsed', depending"
00151 " on the mode you are using this time...."
00152 " </qt>" );
00153 QString whatsThisSystemOptions = i18n( " <qt> This button starts a new dialog where you can adjust various"
00154 " settings of your printing system. Amongst them:"
00155 " <ul><li> Should KDE"
00156 " applications embed all fonts into the PostScript they"
00157 " generate for printing?"
00158 " <li> Should KDE use an external PostScript viewer"
00159 " like <em>gv</em> for print page previews?"
00160 " <li> Should KDEPrint use a local or a remote CUPS server?,"
00161 " </ul> and many more.... "
00162 " </qt>" );
00163
00164 QString whatsThisHelpButton = i18n( " <qt> This button takes you to the complete <em>KDEPrint"
00165 " Manual</em>."
00166 " </qt>" );
00167
00168 QString whatsThisCancelButton = i18n( " <qt> This button cancels your print job and quits the"
00169 " kprinter dialog."
00170 " </qt>" );
00171
00172 QString whatsThisPrintButton = i18n( " <qt> This button sends the job to the printing process."
00173 " If you are sending non-PostScript files, you may be"
00174 " asked if you want KDE to convert the files into PostScript,"
00175 " or if you want your print subsystem (like CUPS) to do this."
00176 " </qt>" );
00177
00178 QString whatsThisKeepDialogOpenCheckbox = i18n( " <qt><p>If you enable this checkbox, the printing dialog"
00179 " stays open after you hit the <em>Print</em> button.</p>"
00180 " <p> This is"
00181 " especially useful, if you need to test various"
00182 " print settings (like color matching for an inkjet printer)"
00183 " or if you want to send your job to multiple printers (one after"
00184 " the other) to have it finished more quickly.</p>"
00185 " </qt>" );
00186
00187 QString whatsThisOutputFileLabel = i18n(" <qt> The \"Output file:\" shows you where your file will be"
00188 " saved if you decide to \"Print to File\" your job, using one of the"
00189 " KDE <em>Special Printers</em> named \"Print to File (PostScript)\""
00190 " or \"Print to File (PDF)\". Choose a name and location that suits"
00191 " you by using the button and/or editing the line on the right."
00192 " </qt>" );
00193
00194 QString whatsThisOutputFileLineedit = i18n(" <qt> Edit this line to create a path and filename that suits"
00195 " you. (Only available if you \"Print to File\")"
00196 " </qt>" );
00197
00198 QString whatsThisOutputFileButton = i18n(" <qt> This button calls the \"File Open\" dialog to let you"
00199 " choose a directory and file name where your \"Print-to-File\""
00200 " job should be saved."
00201 " </qt>" );
00202
00203 QString whatsThisAddFileButton = i18n( " <qt> This button calls the \"File Open\" dialog to allow you"
00204 " to select a file for printing. Note, that "
00205 " <ul><li>you can select ASCII or International Text, PDF,"
00206 " PostScript, JPEG, TIFF, PNG, GIF and many other graphical"
00207 " formats."
00208 " <li>you can select various files from different paths"
00209 " and send them as one \"multi-file job\" to the printing"
00210 " system."
00211 " </ul>"
00212 " </qt>" );
00213
00214 QString whatsThisRemoveFileButton = i18n(" <qt> This button removes the highlighted file from the"
00215 " list of files to be printed."
00216 " </qt>" );
00217
00218 QString whatsThisOpenFileButton = i18n( " <qt> This button tries to open the highlighted file, so"
00219 " you can view or edit it before you send it to the printing"
00220 " system."
00221 " </qt>" );
00222
00223 QString whatsThisMoveFileUpButton = i18n(" <qt> This button moves the highlighted file up in the list"
00224 " of files to be printed. (In effect, this changes the order"
00225 " of the files' printout.)"
00226 " </qt>" );
00227
00228 QString whatsThisMoveFileDownButton = i18n(" <qt> This button moves the highlighted file down in the list"
00229 " of files to be printed. (In effect, this changes the order"
00230 " of the files' printout.)"
00231 " </qt>" );
00232
00233 QString whatsThisPreviewCheckBox = i18n(" <qt> Enable this checkbox if you want to see a preview of"
00234 " your printout. A preview lets you check if, for instance,"
00235 " your intended \"poster\" or \"pamphlet\" layout"
00236 " looks like you intended, without wasting paper first. It"
00237 " also lets you cancel the job if something looks wrong. "
00238 " </qt>" );
00239
00240 QString whatsThisSetDefaultPrinter = i18n(" <qt> This button sets the current printer as the user's"
00241 " default. (Button is only visible if the <em>System Options...</em>"
00242 " --> <em>General</em> --> <em>Miscellaneous</em>: <em>\"Defaults"
00243 " to the last printer used in the application\"</em> is disabled.)"
00244 " </qt>" );
00245
00246 d = new KPrintDialogPrivate;
00247
00248 d->m_pages.setAutoDelete(false);
00249 d->m_printer = 0;
00250 setCaption(i18n("Print"));
00251
00252
00253 QGroupBox *m_pbox = new QGroupBox(0,Qt::Vertical,i18n("Printer"), this);
00254 d->m_type = new QLabel(m_pbox);
00255 QWhatsThis::add(d->m_type, whatsThisPrinterType);
00256 d->m_state = new QLabel(m_pbox);
00257 QWhatsThis::add(d->m_state, whatsThisPrinterState);
00258 d->m_comment = new QLabel(m_pbox);
00259 QWhatsThis::add(d->m_comment, whatsThisPrinterComment);
00260 d->m_location = new QLabel(m_pbox);
00261 QWhatsThis::add(d->m_location, whatsThisLocationLabel);
00262
00263 d->m_printers = new TreeComboBox(m_pbox);
00264 QWhatsThis::add(d->m_printers, whatsThisPrinterSelect);
00265 d->m_printers->setMinimumHeight(25);
00266 QLabel *m_printerlabel = new QLabel(i18n("&Name:"), m_pbox);
00267 QWhatsThis::add(m_printerlabel, whatsThisPrinterSelect);
00268 QLabel *m_statelabel = new QLabel(i18n("Status", "State:"), m_pbox);
00269 QWhatsThis::add(m_statelabel, whatsThisPrinterState);
00270 QLabel *m_typelabel = new QLabel(i18n("Type:"), m_pbox);
00271 QWhatsThis::add(m_typelabel, whatsThisPrinterType);
00272 QLabel *m_locationlabel = new QLabel(i18n("Location:"), m_pbox);
00273 QWhatsThis::add(m_locationlabel, whatsThisLocationLabel);
00274 QLabel *m_commentlabel = new QLabel(i18n("Comment:"), m_pbox);
00275 QWhatsThis::add(m_commentlabel, whatsThisPrinterComment);
00276 m_printerlabel->setBuddy(d->m_printers);
00277 d->m_properties = new KPushButton(KGuiItem(i18n("P&roperties"), "edit"), m_pbox);
00278 QWhatsThis::add( d->m_properties, whatsThisPrintJobProperties);
00279 d->m_options = new KPushButton(KGuiItem(i18n("System Op&tions..."), "kdeprint_configmgr"), this);
00280 QWhatsThis::add(d->m_options,whatsThisSystemOptions);
00281 d->m_default = new KPushButton(KGuiItem(i18n("Set as &Default"), "kdeprint_defaultsoft"), m_pbox);
00282 QWhatsThis::add(d->m_default,whatsThisSetDefaultPrinter);
00283 d->m_filter = new QPushButton(m_pbox);
00284 d->m_filter->setPixmap(SmallIcon("filter"));
00285 d->m_filter->setMinimumSize(QSize(d->m_printers->minimumHeight(),d->m_printers->minimumHeight()));
00286 d->m_filter->setToggleButton(true);
00287 d->m_filter->setOn(KMManager::self()->isFilterEnabled());
00288 QToolTip::add(d->m_filter, i18n("Toggle printer filtering"));
00289 QWhatsThis::add(d->m_filter, whatsThisPrinterFilter);
00290 d->m_wizard = new QPushButton(m_pbox);
00291 d->m_wizard->setPixmap(SmallIcon("wizard"));
00292 d->m_wizard->setMinimumSize(QSize(d->m_printers->minimumHeight(),d->m_printers->minimumHeight()));
00293 QToolTip::add(d->m_wizard, i18n("Add printer..."));
00294 QWhatsThis::add(d->m_wizard, whatsThisAddPrinterWizard);
00295 d->m_ok = new KPushButton(KGuiItem(i18n("&Print"), "fileprint"), this);
00296 QWhatsThis::add( d->m_ok, whatsThisPrintButton);
00297 d->m_ok->setDefault(true);
00298 d->m_ok->setEnabled( false );
00299 QPushButton *m_cancel = new KPushButton(KStdGuiItem::cancel(), this);
00300 QWhatsThis::add(m_cancel, whatsThisCancelButton);
00301 d->m_preview = new QCheckBox(i18n("Previe&w"), m_pbox);
00302 QWhatsThis::add(d->m_preview, whatsThisPreviewCheckBox);
00303 d->m_filelabel = new QLabel(i18n("O&utput file:"), m_pbox);
00304 QWhatsThis::add(d->m_filelabel,whatsThisOutputFileLabel);
00305 d->m_file = new KURLRequester(QDir::homeDirPath()+"/print.ps", m_pbox);
00306 QWhatsThis::add(d->m_file,whatsThisOutputFileLineedit);
00307 d->m_file->setEnabled(false);
00308 d->m_filelabel->setBuddy(d->m_file);
00309 d->m_cmdlabel = new QLabel(i18n("Print co&mmand:"), m_pbox);
00310 QWhatsThis::add( d->m_cmdlabel, whatsThisExternalPrintCommand);
00311
00312 d->m_cmd = new QLineEdit(m_pbox);
00313 QWhatsThis::add( d->m_cmd, whatsThisExternalPrintCommand);
00314 d->m_cmdlabel->setBuddy(d->m_cmd);
00315 d->m_dummy = new QVBox(this);
00316 d->m_plugin = new PluginComboBox(this);
00317 d->m_extbtn = new KPushButton(this);
00318 QToolTip::add(d->m_extbtn, i18n("Show/hide advanced options"));
00319 QWhatsThis::add(d->m_extbtn, whatsThisExpandCollapse);
00320 d->m_persistent = new QCheckBox(i18n("&Keep this dialog open after printing"), this);
00321 QWhatsThis::add( d->m_persistent, whatsThisKeepDialogOpenCheckbox);
00322 QPushButton *m_help = new KPushButton(KStdGuiItem::help(), this);
00323 QWhatsThis::add( m_help, whatsThisHelpButton);
00324
00325 QWidget::setTabOrder( d->m_printers, d->m_filter );
00326 QWidget::setTabOrder( d->m_filter, d->m_wizard );
00327 QWidget::setTabOrder( d->m_wizard, d->m_properties );
00328 QWidget::setTabOrder( d->m_properties, d->m_preview );
00329 QWidget::setTabOrder( d->m_preview, d->m_file );
00330 QWidget::setTabOrder( d->m_file, d->m_cmd );
00331 QWidget::setTabOrder( d->m_plugin, d->m_persistent );
00332 QWidget::setTabOrder( d->m_persistent, d->m_extbtn );
00333 QWidget::setTabOrder( d->m_extbtn, d->m_options );
00334 QWidget::setTabOrder( d->m_options, m_help );
00335 QWidget::setTabOrder( m_help, d->m_ok );
00336 QWidget::setTabOrder( d->m_ok, m_cancel );
00337
00338
00339 QVBoxLayout *l1 = new QVBoxLayout(this, 10, 10);
00340 l1->addWidget(m_pbox,0);
00341 l1->addWidget(d->m_dummy,1);
00342 l1->addWidget(d->m_plugin,0);
00343 l1->addWidget(d->m_persistent);
00344 QHBoxLayout *l2 = new QHBoxLayout(0, 0, 10);
00345 l1->addLayout(l2);
00346 l2->addWidget(d->m_extbtn,0);
00347 l2->addWidget(d->m_options,0);
00348 l2->addWidget(m_help,0);
00349 l2->addStretch(1);
00350 l2->addWidget(d->m_ok,0);
00351 l2->addWidget(m_cancel,0);
00352 QGridLayout *l3 = new QGridLayout(m_pbox->layout(),3,3,7);
00353 l3->setColStretch(1,1);
00354 l3->setRowStretch(0,1);
00355 QGridLayout *l4 = new QGridLayout(0, 5, 2, 0, 5);
00356 l3->addMultiCellLayout(l4,0,0,0,1);
00357 l4->addWidget(m_printerlabel,0,0);
00358 l4->addWidget(m_statelabel,1,0);
00359 l4->addWidget(m_typelabel,2,0);
00360 l4->addWidget(m_locationlabel,3,0);
00361 l4->addWidget(m_commentlabel,4,0);
00362 QHBoxLayout *ll4 = new QHBoxLayout(0, 0, 3);
00363 l4->addLayout(ll4,0,1);
00364 ll4->addWidget(d->m_printers,1);
00365 ll4->addWidget(d->m_filter,0);
00366 ll4->addWidget(d->m_wizard,0);
00367
00368 l4->addWidget(d->m_state,1,1);
00369 l4->addWidget(d->m_type,2,1);
00370 l4->addWidget(d->m_location,3,1);
00371 l4->addWidget(d->m_comment,4,1);
00372 l4->setColStretch(1,1);
00373 QVBoxLayout *l5 = new QVBoxLayout(0, 0, 10);
00374 l3->addLayout(l5,0,2);
00375 l5->addWidget(d->m_properties,0);
00376 l5->addWidget(d->m_default,0);
00377 l5->addWidget(d->m_preview,0);
00378 l5->addStretch(1);
00379
00380 l3->addWidget(d->m_filelabel,1,0);
00381 l3->addWidget(d->m_file,1,1);
00382
00383 l3->addWidget(d->m_cmdlabel,2,0);
00384 l3->addMultiCellWidget(d->m_cmd,2,2,1,2);
00385
00386
00387 connect(d->m_ok,SIGNAL(clicked()),SLOT(accept()));
00388 connect(m_cancel,SIGNAL(clicked()),SLOT(reject()));
00389 connect(d->m_properties,SIGNAL(clicked()),SLOT(slotProperties()));
00390 connect(d->m_default,SIGNAL(clicked()),SLOT(slotSetDefault()));
00391 connect(d->m_printers,SIGNAL(activated(int)),SLOT(slotPrinterSelected(int)));
00392 connect(d->m_options,SIGNAL(clicked()),SLOT(slotOptions()));
00393 connect(d->m_wizard,SIGNAL(clicked()),SLOT(slotWizard()));
00394 connect(d->m_extbtn, SIGNAL(clicked()), SLOT(slotExtensionClicked()));
00395 connect(d->m_filter, SIGNAL(toggled(bool)), SLOT(slotToggleFilter(bool)));
00396 connect(m_help, SIGNAL(clicked()), SLOT(slotHelp()));
00397 connect(d->m_file, SIGNAL(urlSelected(const QString&)), SLOT(slotOutputFileSelected(const QString&)));
00398 connect( d->m_file, SIGNAL( openFileDialog( KURLRequester* ) ), SLOT( slotOpenFileDialog() ) );
00399 connect( KMFactory::self()->manager(), SIGNAL( updatePossible( bool ) ), SLOT( slotUpdatePossible( bool ) ) );
00400
00401 if (!kapp->authorize("print/system"))
00402 {
00403 d->m_plugin->hide();
00404 }
00405
00406 if (!kapp->authorize("print/options"))
00407 {
00408 d->m_options->hide();
00409 }
00410
00411 if (!kapp->authorize("print/selection"))
00412 {
00413 d->m_plugin->hide();
00414 d->m_options->hide();
00415
00416 d->m_extbtn->hide();
00417 m_pbox->hide();
00418
00419 expandDialog(true);
00420 }
00421 else
00422 {
00423 KConfig *config = KGlobal::config();
00424 config->setGroup("KPrinter Settings");
00425 expandDialog(!config->readBoolEntry("DialogReduced", (KMFactory::self()->settings()->application != KPrinter::StandAlone)));
00426 }
00427 }
00428
00429 KPrintDialog::~KPrintDialog()
00430 {
00431 KConfig *config = KGlobal::config();
00432 config->setGroup("KPrinter Settings");
00433 config->writeEntry("DialogReduced", d->m_reduced);
00434
00435 delete d;
00436 }
00437
00438 void KPrintDialog::setFlags(int f)
00439 {
00440 SHOWHIDE(d->m_properties, (f & KMUiManager::Properties))
00441 d->m_default->hide();
00442 SHOWHIDE(d->m_default, ((f & KMUiManager::Default) && !KMFactory::self()->printConfig("General")->readBoolEntry("UseLast", true)))
00443 SHOWHIDE(d->m_preview, (f & KMUiManager::Preview))
00444 bool on = (f & KMUiManager::OutputToFile);
00445 SHOWHIDE(d->m_filelabel, on)
00446 SHOWHIDE(d->m_file, on)
00447 on = (f & KMUiManager::PrintCommand);
00448 SHOWHIDE(d->m_cmdlabel, on)
00449 SHOWHIDE(d->m_cmd, on)
00450 SHOWHIDE(d->m_persistent, (f & KMUiManager::Persistent))
00451
00452
00453 KMManager *mgr = KMFactory::self()->manager();
00454 d->m_wizard->setEnabled((mgr->hasManagement() && (mgr->printerOperationMask() & KMManager::PrinterCreation)));
00455 }
00456
00457 void KPrintDialog::setDialogPages(QPtrList<KPrintDialogPage> *pages)
00458 {
00459 if (!pages) return;
00460 if (pages->count() + d->m_pages.count() == 1)
00461 {
00462
00463
00464 if (pages->count() > 0)
00465 d->m_pages.append(pages->take(0));
00466 d->m_pages.first()->reparent(d->m_dummy, QPoint(0,0));
00467 d->m_pages.first()->show();
00468 delete d->m_dummy->child("TabWidget", "QTabWidget");
00469 }
00470 else
00471 {
00472
00473 QTabWidget *tabs = static_cast<QTabWidget*>(d->m_dummy->child("TabWidget", "QTabWidget"));
00474 if (!tabs)
00475 {
00476
00477
00478 tabs = new QTabWidget(d->m_dummy, "TabWidget");
00479 tabs->setMargin(10);
00480 for (d->m_pages.first(); d->m_pages.current(); d->m_pages.next())
00481 {
00482 tabs->addTab(d->m_pages.current(), d->m_pages.current()->title());
00483 }
00484 }
00485 while (pages->count() > 0)
00486 {
00487 KPrintDialogPage *page = pages->take(0);
00488 d->m_pages.append(page);
00489 tabs->addTab(page, page->title());
00490 }
00491 tabs->show();
00492 }
00493 d->m_extbtn->setEnabled(d->m_pages.count() > 0);
00494 }
00495
00496 KPrintDialog* KPrintDialog::printerDialog(KPrinter *printer, QWidget *parent, const QString& caption, bool forceExpand)
00497 {
00498 if (printer)
00499 {
00500 KPrintDialog *dlg = new KPrintDialog(parent);
00501
00502
00503
00504 dlg->d->m_printer = printer;
00505 KMFactory::self()->uiManager()->setupPrintDialog(dlg);
00506 dlg->init();
00507 if (!caption.isEmpty())
00508 dlg->setCaption(caption);
00509 if (forceExpand)
00510 {
00511
00512
00513
00514 dlg->expandDialog(true);
00515 dlg->d->m_extbtn->hide();
00516 }
00517 return dlg;
00518 }
00519 return NULL;
00520 }
00521
00522 void KPrintDialog::initialize(KPrinter *printer)
00523 {
00524 d->m_printer = printer;
00525
00526
00527 QPtrList<KMPrinter> *plist = KMFactory::self()->manager()->printerList();
00528 if (!KMManager::self()->errorMsg().isEmpty())
00529 {
00530 KMessageBox::error(parentWidget(),
00531 "<qt><nobr>"+
00532 i18n("An error occurred while retrieving the printer list:")
00533 +"</nobr><br><br>"+KMManager::self()->errorMsg()+"</qt>");
00534 }
00535
00536 if (plist)
00537 {
00538 QString oldP = d->m_printers->currentText();
00539 d->m_printers->clear();
00540 QPtrListIterator<KMPrinter> it(*plist);
00541 int defsoft(-1), defhard(-1), defsearch(-1);
00542 bool sep(false);
00543 for (;it.current();++it)
00544 {
00545
00546 if ( !it.current()->isValid() )
00547 continue;
00548
00549 if (!sep && it.current()->isSpecial())
00550 {
00551 sep = true;
00552 d->m_printers->insertItem(QPixmap(), QString::fromLatin1("--------"));
00553 }
00554 d->m_printers->insertItem(SmallIcon(it.current()->pixmap(),0,(it.current()->isValid() ? (int)KIcon::DefaultState : (int)KIcon::LockOverlay)),it.current()->name(),false);
00555 if (it.current()->isSoftDefault())
00556 defsoft = d->m_printers->count()-1;
00557 if (it.current()->isHardDefault())
00558 defhard = d->m_printers->count()-1;
00559 if (!oldP.isEmpty() && oldP == it.current()->name())
00560 defsearch = d->m_printers->count()-1;
00561 else if (defsearch == -1 && it.current()->name() == printer->searchName())
00562 defsearch = d->m_printers->count()-1;
00563 }
00564 int defindex = (defsearch != -1 ? defsearch : (defsoft != -1 ? defsoft : QMAX(defhard,0)));
00565 d->m_printers->setCurrentItem(defindex);
00566
00567 }
00568
00569
00570 if (!d->m_printer->outputFileName().isEmpty())
00571 d->m_file->setURL( d->m_printer->outputFileName() );
00572 else if (!d->m_printer->docFileName().isEmpty())
00573 d->m_file->setURL( d->m_printer->docDirectory()+"/"+d->m_printer->docFileName()+".ps" );
00574
00575 if ( d->m_printers->count() > 0 )
00576 slotPrinterSelected( d->m_printers->currentItem() );
00577
00578
00579 if (d->m_printer->option("kde-preview") == "1" || d->m_printer->previewOnly())
00580 d->m_preview->setChecked(true);
00581 d->m_preview->setEnabled(!d->m_printer->previewOnly());
00582 d->m_cmd->setText(d->m_printer->option("kde-printcommand"));
00583 QPtrListIterator<KPrintDialogPage> it(d->m_pages);
00584 for (;it.current();++it)
00585 it.current()->setOptions(d->m_printer->options());
00586 }
00587
00588 void KPrintDialog::slotPrinterSelected(int index)
00589 {
00590 bool ok(false);
00591 d->m_location->setText(QString::null);
00592 d->m_state->setText(QString::null);
00593 d->m_comment->setText(QString::null);
00594 d->m_type->setText(QString::null);
00595 if (index >= 0 && index < d->m_printers->count())
00596 {
00597 KMManager *mgr = KMFactory::self()->manager();
00598 KMPrinter *p = mgr->findPrinter(d->m_printers->text(index));
00599 if (p)
00600 {
00601 if (!p->isSpecial()) mgr->completePrinterShort(p);
00602 d->m_location->setText(p->location());
00603 d->m_comment->setText(p->driverInfo());
00604 d->m_type->setText(p->description());
00605 d->m_state->setText(p->stateString());
00606 ok = p->isValid();
00607 enableSpecial(p->isSpecial());
00608 enableOutputFile(p->option("kde-special-file") == "1");
00609 setOutputFileExtension(p->option("kde-special-extension"));
00610 }
00611 else
00612 enableOutputFile( ok );
00613 }
00614 d->m_properties->setEnabled(ok);
00615 d->m_ok->setEnabled(ok);
00616 }
00617
00618 void KPrintDialog::slotProperties()
00619 {
00620 if (!d->m_printer) return;
00621
00622 KMPrinter *prt = KMFactory::self()->manager()->findPrinter(d->m_printers->currentText());
00623 if (prt)
00624 KPrinterPropertyDialog::setupPrinter(prt, this);
00625 }
00626
00627 void KPrintDialog::slotSetDefault()
00628 {
00629 KMPrinter *p = KMFactory::self()->manager()->findPrinter(d->m_printers->currentText());
00630 if (p)
00631 KMFactory::self()->virtualManager()->setDefault(p);
00632 }
00633
00634 void KPrintDialog::done(int result)
00635 {
00636 if (result == Accepted && d->m_printer)
00637 {
00638 QMap<QString,QString> opts;
00639 KMPrinter *prt(0);
00640
00641
00642 QString msg;
00643 QPtrListIterator<KPrintDialogPage> it(d->m_pages);
00644 for (;it.current();++it)
00645 if (it.current()->isEnabled())
00646 {
00647 if (it.current()->isValid(msg))
00648 it.current()->getOptions(opts);
00649 else
00650 {
00651 KMessageBox::error(this, msg.prepend("<qt>").append("</qt>"));
00652 return;
00653 }
00654 }
00655
00656
00657
00658 prt = KMFactory::self()->manager()->findPrinter(d->m_printers->currentText());
00659 if (prt->isSpecial() && prt->option("kde-special-file") == "1")
00660 {
00661 if (!checkOutputFile()) return;
00662 d->m_printer->setOutputToFile(true);
00663
00664 d->m_printer->setOutputFileName( KURL::decode_string( d->m_file->url() ) );
00665 }
00666 else
00667 d->m_printer->setOutputToFile(false);
00668 d->m_printer->setPrinterName(prt->printerName());
00669 d->m_printer->setSearchName(prt->name());
00670 opts["kde-printcommand"] = d->m_cmd->text();
00671 opts["kde-preview"] = (d->m_preview->isChecked() ? "1" : "0");
00672 opts["kde-isspecial"] = (prt->isSpecial() ? "1" : "0");
00673 opts["kde-special-command"] = prt->option("kde-special-command");
00674
00675
00676 QMap<QString,QString> popts = (prt->isEdited() ? prt->editedOptions() : prt->defaultOptions());
00677 for (QMap<QString,QString>::ConstIterator it=popts.begin(); it!=popts.end(); ++it)
00678 opts[it.key()] = it.data();
00679
00680
00681 d->m_printer->setOptions(opts);
00682
00683 emit printRequested(d->m_printer);
00684
00685 if (!d->m_persistent->isChecked() || !d->m_persistent->isVisible())
00686 KDialog::done(result);
00687 }
00688 else
00689 KDialog::done(result);
00690 }
00691
00692 bool KPrintDialog::checkOutputFile()
00693 {
00694 bool value(false);
00695 if (d->m_file->url().isEmpty())
00696 KMessageBox::error(this,i18n("The output filename is empty."));
00697 else
00698 {
00699 KURL url( d->m_file->url() );
00700 if ( !url.isLocalFile() )
00701 return true;
00702
00703 bool anotherCheck;
00704 do
00705 {
00706 anotherCheck = false;
00707 QFileInfo f(url.path());
00708 if (f.exists())
00709 {
00710 if (f.isWritable())
00711 {
00712
00713 time_t mtimeDest = f.lastModified().toTime_t();
00714 KIO::RenameDlg dlg( this, i18n( "Print" ), QString::null, d->m_file->url(),
00715 KIO::M_OVERWRITE, ( time_t ) -1, f.size(), ( time_t ) -1, f.created().toTime_t() , mtimeDest+1, mtimeDest, true );
00716 int result = dlg.exec();
00717 switch ( result )
00718 {
00719 case KIO::R_OVERWRITE:
00720 value = true;
00721 break;
00722 default:
00723 case KIO::R_CANCEL:
00724 value = false;
00725 break;
00726 case KIO::R_RENAME:
00727 url = dlg.newDestURL();
00728 d->m_file->setURL( url.path() );
00729 value = true;
00730 anotherCheck = true;
00731 break;
00732 }
00733 }
00734 else
00735 KMessageBox::error(this,i18n("You don't have write permissions to this file."));
00736 }
00737 else
00738 {
00739 QFileInfo d( f.dirPath( true ) );
00740 if ( !d.exists() )
00741 KMessageBox::error( this, i18n( "The output directory does not exist." ) );
00742 else if ( !d.isWritable() )
00743 KMessageBox::error(this,i18n("You don't have write permissions in that directory."));
00744 else
00745 value = true;
00746 }
00747 } while( anotherCheck );
00748 }
00749 return value;
00750 }
00751
00752 void KPrintDialog::slotOptions()
00753 {
00754 if (KMManager::self()->invokeOptionsDialog(this))
00755 init();
00756 }
00757
00758 void KPrintDialog::enableOutputFile(bool on)
00759 {
00760 d->m_filelabel->setEnabled(on);
00761 d->m_file->setEnabled(on);
00762 }
00763
00764 void KPrintDialog::enableSpecial(bool on)
00765 {
00766 d->m_default->setDisabled(on);
00767 d->m_cmdlabel->setDisabled(on);
00768 d->m_cmd->setDisabled(on);
00769 KPCopiesPage *copypage = (KPCopiesPage*)child("CopiesPage","KPCopiesPage");
00770 if (copypage)
00771 copypage->initialize(!on);
00772
00773 for (d->m_pages.first(); d->m_pages.current(); d->m_pages.next())
00774 if (d->m_pages.current()->onlyRealPrinters())
00775 d->m_pages.current()->setEnabled(!on);
00776 }
00777
00778 void KPrintDialog::setOutputFileExtension(const QString& ext)
00779 {
00780 if (!ext.isEmpty())
00781 {
00782 KURL url( d->m_file->url() );
00783 QString f( url.fileName() );
00784 int p = f.findRev( '.' );
00785
00786 if ( p > 0 && p != int (f.length () - 1) )
00787 {
00788 url.setFileName( f.left( p ) + "." + ext );
00789 d->m_file->setURL( KURL::decode_string( url.url() ) );
00790 }
00791 }
00792 }
00793
00794 void KPrintDialog::slotWizard()
00795 {
00796 int result = KMFactory::self()->manager()->addPrinterWizard(this);
00797 if (result == -1)
00798 KMessageBox::error(this, KMFactory::self()->manager()->errorMsg().prepend("<qt>").append("</qt>"));
00799 else if (result == 1)
00800 initialize(d->m_printer);
00801 }
00802
00803 void KPrintDialog::reload()
00804 {
00805
00806 QTabWidget *tabs = static_cast<QTabWidget*>(d->m_dummy->child("TabWidget", "QTabWidget"));
00807 for (uint i=0; i<d->m_pages.count(); i++)
00808 if (d->m_pages.at(i)->onlyRealPrinters())
00809 {
00810 KPrintDialogPage *page = d->m_pages.take(i--);
00811 if (tabs)
00812 tabs->removePage(page);
00813 delete page;
00814 }
00815
00816 QPtrList<KPrintDialogPage> pages;
00817 pages.setAutoDelete(false);
00818 KMFactory::self()->uiManager()->setupPrintDialogPages(&pages);
00819
00820 setDialogPages(&pages);
00821 if (!d->m_reduced)
00822 d->m_dummy->show();
00823
00824 setFlags(KMFactory::self()->uiManager()->dialogFlags());
00825 connect( KMFactory::self()->manager(), SIGNAL( updatePossible( bool ) ), SLOT( slotUpdatePossible( bool ) ) );
00826 init();
00827 }
00828
00829 void KPrintDialog::configChanged()
00830 {
00831
00832
00833
00834 init();
00835
00836
00837 setFlags(KMFactory::self()->uiManager()->dialogFlags());
00838 }
00839
00840 void KPrintDialog::expandDialog(bool on)
00841 {
00842 QSize sz(size());
00843 bool needResize(isVisible());
00844
00845 if (on)
00846 {
00847 sz.setHeight(sz.height()+d->m_dummy->minimumSize().height()+d->m_plugin->minimumSize().height()+2*layout()->spacing());
00848 if (isVisible() || !d->m_dummy->isVisible() || !d->m_plugin->isVisible())
00849 {
00850 d->m_dummy->show();
00851 d->m_plugin->show();
00852 }
00853 d->m_extbtn->setIconSet(SmallIconSet("up"));
00854 d->m_extbtn->setText(i18n("Collaps&e"));
00855 d->m_reduced = false;
00856 }
00857 else
00858 {
00859 sz.setHeight(sz.height()-d->m_dummy->height()-d->m_plugin->height()-2*layout()->spacing());
00860 if (!isVisible() || d->m_dummy->isVisible() || d->m_plugin->isVisible())
00861 {
00862 d->m_dummy->hide();
00863 d->m_plugin->hide();
00864 }
00865 d->m_extbtn->setIconSet(SmallIconSet("down"));
00866 d->m_extbtn->setText(i18n("&Expand"));
00867 d->m_reduced = true;
00868 }
00869
00870 if (needResize)
00871 {
00872 layout()->activate();
00873 resize(sz);
00874 }
00875 }
00876
00877 void KPrintDialog::slotExtensionClicked()
00878 {
00879
00880 expandDialog(!(d->m_dummy->isVisible()));
00881 }
00882
00883 KPrinter* KPrintDialog::printer() const
00884 {
00885 return d->m_printer;
00886 }
00887
00888 void KPrintDialog::slotToggleFilter(bool on)
00889 {
00890 KMManager::self()->enableFilter(on);
00891 initialize(d->m_printer);
00892 }
00893
00894 void KPrintDialog::slotHelp()
00895 {
00896 kapp->invokeHelp(QString::null, "kdeprint");
00897 }
00898
00899 void KPrintDialog::slotOutputFileSelected(const QString& txt)
00900 {
00901 d->m_file->setURL( txt );
00902 }
00903
00904 void KPrintDialog::init()
00905 {
00906 d->m_ok->setEnabled( false );
00907 MessageWindow::remove( this );
00908 MessageWindow::add( this, i18n( "Initializing printing system..." ), 500 );
00909 KMFactory::self()->manager()->checkUpdatePossible();
00910 }
00911
00912 void KPrintDialog::slotUpdatePossible( bool flag )
00913 {
00914 MessageWindow::remove( this );
00915 if ( !flag )
00916 KMessageBox::error(parentWidget(),
00917 "<qt><nobr>"+
00918 i18n("An error occurred while retrieving the printer list:")
00919 +"</nobr><br><br>"+KMManager::self()->errorMsg()+"</qt>");
00920 initialize( d->m_printer );
00921 }
00922
00923 void KPrintDialog::enableDialogPage( int index, bool flag )
00924 {
00925 if ( index < 0 || index >= ( int )d->m_pages.count() )
00926 {
00927 kdWarning() << "KPrintDialog: page index out of bound" << endl;
00928 return;
00929 }
00930
00931 if ( d->m_pages.count() > 1 )
00932 {
00933 QTabWidget *tabs = static_cast<QTabWidget*>(d->m_dummy->child("TabWidget", "QTabWidget"));
00934 tabs->setTabEnabled( d->m_pages.at( index ), flag );
00935 }
00936 else
00937 d->m_pages.at( 0 )->setEnabled( flag );
00938 }
00939
00940 void KPrintDialog::slotOpenFileDialog()
00941 {
00942 KFileDialog *dialog = d->m_file->fileDialog();
00943
00944 dialog->setCaption(i18n("Print to File"));
00945 dialog->setMode(d->m_file->fileDialog()->mode() & ~KFile::LocalOnly);
00946 dialog->setOperationMode( KFileDialog::Saving );
00947
00948 KMPrinter *prt = KMFactory::self()->manager()->findPrinter(d->m_printers->currentText());
00949 if (prt)
00950 {
00951 QString mimetype(prt->option("kde-special-mimetype"));
00952 QString ext(prt->option("kde-special-extension"));
00953
00954 if (!mimetype.isEmpty())
00955 {
00956 QStringList filter;
00957 filter << mimetype;
00958 filter << "all/allfiles";
00959 dialog->setMimeFilter (filter, mimetype);
00960 }
00961 else if (!ext.isEmpty())
00962 dialog->setFilter ("*." + ext + "\n*|" + i18n ("All Files"));
00963 }
00964 }
00965
00966 #include "kprintdialog.moc"