kminstancepage.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License version 2 as published by the Free Software Foundation.
00008  *
00009  *  This library is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *  Library General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Library General Public License
00015  *  along with this library; see the file COPYING.LIB.  If not, write to
00016  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  *  Boston, MA 02110-1301, USA.
00018  **/
00019 
00020 #include "kminstancepage.h"
00021 #include "kmprinter.h"
00022 #include "kmfactory.h"
00023 #include "kmvirtualmanager.h"
00024 #include "kmmanager.h"
00025 #include "kprinterpropertydialog.h"
00026 #include "kprinter.h"
00027 #include "kmtimer.h"
00028 
00029 #include <qlayout.h>
00030 #include <qregexp.h>
00031 #include <qwhatsthis.h>
00032 #include <qpushbutton.h>
00033 #include <kmessagebox.h>
00034 #include <kinputdialog.h>
00035 #include <klistbox.h>
00036 #include <klocale.h>
00037 #include <kiconloader.h>
00038 #include <kstandarddirs.h>
00039 #include <kdebug.h>
00040 
00041 KMInstancePage::KMInstancePage(QWidget *parent, const char *name)
00042 : QWidget(parent,name)
00043 {
00044     m_view = new KListBox(this);
00045     m_printer = 0;
00046 
00047     initActions();
00048 
00049     QHBoxLayout *main_ = new QHBoxLayout(this, 0, 0);
00050     main_->addWidget(m_view);
00051     QVBoxLayout *sub_ = new QVBoxLayout(0, 0, 0);
00052     main_->addLayout(sub_);
00053     for (QValueList<QButton*>::Iterator it=m_buttons.begin(); it!=m_buttons.end(); ++it)
00054         if (*it)
00055             sub_->addWidget(*it);
00056         else
00057             sub_->addSpacing(10);
00058     sub_->addStretch(1);
00059 
00060     QWhatsThis::add(this,
00061         i18n("Define/Edit here instances for the current selected "
00062              "printer. An instance is a combination of a real "
00063              "(physical) printer and a set of predefined options. "
00064              "For a single InkJet printer, you could define different "
00065              "print formats like <i>DraftQuality</i>, <i>PhotoQuality</i> "
00066              "or <i>TwoSided</i>. Those instances appear as normal "
00067              "printers in the print dialog and allow you to quickly "
00068              "select the print format you want."));
00069 }
00070 
00071 KMInstancePage::~KMInstancePage()
00072 {
00073 }
00074 
00075 void KMInstancePage::addButton(const QString& txt, const QString& pixmap, const char *receiver)
00076 {
00077     QPushButton *btn = new QPushButton(this, 0L);
00078     btn->setText(txt);
00079     btn->setIconSet(BarIconSet(pixmap));
00080     btn->setFlat(true);
00081     connect(btn, SIGNAL(clicked()), receiver);
00082     m_buttons.append(btn);
00083 }
00084 
00085 void KMInstancePage::initActions()
00086 {
00087     addButton(i18n("New..."), "filenew", SLOT(slotNew()));
00088     addButton(i18n("Copy..."), "editcopy", SLOT(slotCopy()));
00089     addButton(i18n("Remove"), "edittrash", SLOT(slotRemove()));
00090     m_buttons.append(0);
00091     addButton(i18n("Set as Default"), "exec", SLOT(slotDefault()));
00092     addButton(i18n("Settings"), "configure", SLOT(slotSettings()));
00093     m_buttons.append(0);
00094     addButton(i18n("Test..."), "fileprint", SLOT(slotTest()));
00095 }
00096 
00097 void KMInstancePage::setPrinter(KMPrinter *p)
00098 {
00099     QString oldText = m_view->currentText();
00100 
00101     m_view->clear();
00102     m_printer = p;
00103     //bool  ok = (p && !p->isSpecial());
00104     bool    ok = (p != 0);
00105     if (ok)
00106     {
00107         QPtrList<KMPrinter> list;
00108         KMFactory::self()->virtualManager()->virtualList(list,p->name());
00109         QPtrListIterator<KMPrinter> it(list);
00110         for (;it.current();++it)
00111         {
00112             QStringList pair = QStringList::split('/',it.current()->name(),false);
00113             m_view->insertItem(SmallIcon((it.current()->isSoftDefault() ? "exec" : "fileprint")),(pair.count() > 1 ? pair[1] : i18n("(Default)")));
00114         }
00115         m_view->sort();
00116     }
00117 
00118     for (QValueList<QButton*>::ConstIterator it=m_buttons.begin(); it!=m_buttons.end(); ++it)
00119         if (*it)
00120             (*it)->setEnabled(ok);
00121 
00122     //iif (!oldText.isEmpty())
00123     //{
00124         QListBoxItem    *item = m_view->findItem(oldText);
00125         if (!item)
00126             item = m_view->findItem(i18n("(Default)"));
00127         if (item)
00128             m_view->setSelected(item,true);
00129     //}
00130 }
00131 
00132 void KMInstancePage::slotNew()
00133 {
00134     KMTimer::self()->hold();
00135 
00136     bool    ok(false);
00137     QString name = KInputDialog::getText(i18n("Instance Name"),i18n("Enter name for new instance (leave untouched for default):"),
00138                                  i18n("(Default)"),&ok,this);
00139     if (ok)
00140     {
00141         if (name.find(QRegExp("[/\\s]")) != -1)
00142             KMessageBox::error(this, i18n("Instance name must not contain any spaces or slashes."));
00143         else
00144         {
00145             if (name == i18n("(Default)"))
00146                 name = QString::null;
00147             KMFactory::self()->virtualManager()->create(m_printer,name);
00148             setPrinter(m_printer);
00149         }
00150     }
00151 
00152     KMTimer::self()->release();
00153 }
00154 
00155 void KMInstancePage::slotRemove()
00156 {
00157     KMTimer::self()->hold();
00158     bool    reload(false);
00159 
00160     QString src = m_view->currentText();
00161         QString msg = (src != i18n("(Default)") ? i18n("Do you really want to remove instance %1?") : i18n("You can't remove the default instance. However all settings of %1 will be discarded. Continue?"));
00162     if (!src.isEmpty() && KMessageBox::warningContinueCancel(this,msg.arg(src),QString::null,KStdGuiItem::del()) == KMessageBox::Continue)
00163     {
00164         if (src == i18n("(Default)"))
00165             src = QString::null;
00166         reload = KMFactory::self()->virtualManager()->isDefault(m_printer,src);
00167         KMFactory::self()->virtualManager()->remove(m_printer,src);
00168         setPrinter(m_printer);
00169     }
00170 
00171     KMTimer::self()->release(reload);
00172 }
00173 
00174 void KMInstancePage::slotCopy()
00175 {
00176     KMTimer::self()->hold();
00177 
00178     QString src = m_view->currentText();
00179     if (!src.isEmpty())
00180     {
00181         bool    ok(false);
00182         QString name = KInputDialog::getText(i18n("Instance Name"),i18n("Enter name for new instance (leave untouched for default):"),
00183                                      i18n("(Default)"),&ok,this);
00184         if (ok)
00185         {
00186             if (name.find(QRegExp("[/\\s]")) != -1)
00187                 KMessageBox::error(this, i18n("Instance name must not contain any spaces or slashes."));
00188             else
00189             {
00190                 if (src == i18n("(Default)"))
00191                     src = QString::null;
00192                 if (name == i18n("(Default)"))
00193                     name = QString::null;
00194                 KMFactory::self()->virtualManager()->copy(m_printer,src,name);
00195                 setPrinter(m_printer);
00196             }
00197         }
00198     }
00199 
00200     KMTimer::self()->release();
00201 }
00202 
00203 void KMInstancePage::slotSettings()
00204 {
00205     KMTimer::self()->hold();
00206 
00207     QString src = m_view->currentText();
00208     if (!src.isEmpty())
00209     {
00210         if (src == i18n("(Default)")) src = QString::null;
00211         KMPrinter   *pr = KMFactory::self()->virtualManager()->findInstance(m_printer,src);
00212         if ( !pr )
00213             KMessageBox::error( this, i18n( "Unable to find instance %1." ).arg( m_view->currentText() ) );
00214         else if ( !pr->isSpecial() && !KMFactory::self()->manager()->completePrinterShort( pr ) )
00215             KMessageBox::error( this, i18n( "Unable to retrieve printer information. Message from printing system: %1." ).arg( KMFactory::self()->manager()->errorMsg() ) );
00216         else
00217         {
00218             int oldAppType = KMFactory::self()->settings()->application;
00219             KMFactory::self()->settings()->application = -1;
00220             KPrinterPropertyDialog::setupPrinter(pr, this);
00221             KMFactory::self()->settings()->application = oldAppType;
00222             if (pr->isEdited())
00223             { // printer edited, need to save changes
00224                 pr->setDefaultOptions(pr->editedOptions());
00225                 pr->setEditedOptions(QMap<QString,QString>());
00226                 pr->setEdited(false);
00227                 KMFactory::self()->virtualManager()->triggerSave();
00228             }
00229         }
00230     }
00231     else
00232         KMessageBox::error( this, i18n( "The instance name is empty. Please select an instance." ) );
00233 
00234     KMTimer::self()->release();
00235 }
00236 
00237 void KMInstancePage::slotDefault()
00238 {
00239     KMTimer::self()->hold();
00240 
00241     QString src = m_view->currentText();
00242     if (!src.isEmpty())
00243     {
00244         if (src == i18n("(Default)"))
00245             src = QString::null;
00246         KMFactory::self()->virtualManager()->setAsDefault(m_printer,src);
00247         setPrinter(m_printer);
00248     }
00249 
00250     KMTimer::self()->release(true);
00251 }
00252 
00253 void KMInstancePage::slotTest()
00254 {
00255     KMTimer::self()->hold();
00256 
00257     QString src = m_view->currentText();
00258     if (!src.isEmpty())
00259     {
00260         if (src == i18n("(Default)"))
00261             src = QString::null;
00262         KMPrinter   *mpr = KMFactory::self()->virtualManager()->findInstance(m_printer,src);
00263         if (!mpr)
00264             KMessageBox::error(this,i18n("Internal error: printer not found."));
00265         else if (KMessageBox::warningContinueCancel(this, i18n("You are about to print a test page on %1. Do you want to continue?").arg(mpr->printerName()), QString::null, i18n("Print Test Page"), "printTestPage") == KMessageBox::Continue)
00266         {
00267             if (!KMFactory::self()->virtualManager()->testInstance(mpr))
00268                 KMessageBox::error(this, i18n("Unable to send test page to %1.").arg(mpr->printerName()));
00269             else
00270                 KMessageBox::information(this,i18n("Test page successfully sent to printer %1.").arg(mpr->printerName()));
00271         }
00272     }
00273 
00274     KMTimer::self()->release(false);
00275 }
00276 #include "kminstancepage.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys