kmdriverdbwidget.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 "kmdriverdbwidget.h"
00021 #include "kmdriverdb.h"
00022 #include "kmfactory.h"
00023 #include "kmmanager.h"
00024 #include "driver.h"
00025 
00026 #include <klistbox.h>
00027 #include <kpushbutton.h>
00028 #include <qcheckbox.h>
00029 #include <kcursor.h>
00030 #include <qapplication.h>
00031 #include <kmessagebox.h>
00032 #include <qlayout.h>
00033 #include <qlabel.h>
00034 #include <qstrlist.h>
00035 
00036 #include <klocale.h>
00037 #include <kcursor.h>
00038 #include <kfiledialog.h>
00039 #include <kguiitem.h>
00040 #include <kio/netaccess.h>
00041 
00042 KMDriverDbWidget::KMDriverDbWidget(QWidget *parent, const char *name)
00043 : QWidget(parent,name)
00044 {
00045     m_external = QString::null;
00046     m_valid = false;
00047 
00048     // build widget
00049     m_manu = new KListBox(this);
00050     m_model = new KListBox(this);
00051     m_postscript = new QCheckBox(i18n("&PostScript printer"),this);
00052     m_raw = new QCheckBox(i18n("&Raw printer (no driver needed)"),this);
00053     m_postscript->setCursor(KCursor::handCursor());
00054     m_raw->setCursor(KCursor::handCursor());
00055     m_other = new KPushButton(KGuiItem(i18n("&Other..."), "fileopen"), this);
00056     QLabel  *l1 = new QLabel(i18n("&Manufacturer:"), this);
00057     QLabel  *l2 = new QLabel(i18n("Mo&del:"), this);
00058     l1->setBuddy(m_manu);
00059     l2->setBuddy(m_model);
00060 
00061     // build layout
00062     QVBoxLayout *main_ = new QVBoxLayout(this, 0, 10);
00063     QGridLayout *sub1_ = new QGridLayout(0, 2, 3, 0, 0);
00064     QHBoxLayout *sub2_ = new QHBoxLayout(0, 0, 10);
00065     main_->addLayout(sub1_);
00066     main_->addLayout(sub2_);
00067     main_->addWidget(m_raw);
00068     sub1_->addWidget(l1,0,0);
00069     sub1_->addWidget(l2,0,2);
00070     sub1_->addWidget(m_manu,1,0);
00071     sub1_->addWidget(m_model,1,2);
00072     sub1_->addColSpacing(1,20);
00073     sub2_->addWidget(m_postscript,1);
00074     sub2_->addWidget(m_other,0);
00075 
00076     // build connections
00077     connect(KMDriverDB::self(),SIGNAL(dbLoaded(bool)),SLOT(slotDbLoaded(bool)));
00078     connect(KMDriverDB::self(), SIGNAL(error(const QString&)), SLOT(slotError(const QString&)));
00079     connect(m_manu,SIGNAL(highlighted(const QString&)),SLOT(slotManufacturerSelected(const QString&)));
00080     connect(m_raw,SIGNAL(toggled(bool)),m_manu,SLOT(setDisabled(bool)));
00081     connect(m_raw,SIGNAL(toggled(bool)),m_model,SLOT(setDisabled(bool)));
00082     connect(m_raw,SIGNAL(toggled(bool)),m_other,SLOT(setDisabled(bool)));
00083     connect(m_raw,SIGNAL(toggled(bool)),m_postscript,SLOT(setDisabled(bool)));
00084     connect(m_postscript,SIGNAL(toggled(bool)),m_manu,SLOT(setDisabled(bool)));
00085     connect(m_postscript,SIGNAL(toggled(bool)),m_model,SLOT(setDisabled(bool)));
00086     connect(m_postscript,SIGNAL(toggled(bool)),m_other,SLOT(setDisabled(bool)));
00087     connect(m_postscript,SIGNAL(toggled(bool)),m_raw,SLOT(setDisabled(bool)));
00088     connect(m_postscript,SIGNAL(toggled(bool)),SLOT(slotPostscriptToggled(bool)));
00089     connect(m_other,SIGNAL(clicked()),SLOT(slotOtherClicked()));
00090 }
00091 
00092 KMDriverDbWidget::~KMDriverDbWidget()
00093 {
00094 }
00095 
00096 void KMDriverDbWidget::setDriver(const QString& manu, const QString& model)
00097 {
00098     QListBoxItem    *item = m_manu->findItem(manu);
00099     QString     model_(model);
00100     if (item)
00101     {
00102         m_manu->setCurrentItem(item);
00103         item = m_model->findItem(model_);
00104         if (!item)
00105             // try by stripping the manufacturer name from
00106             // the beginning of the model string. This is
00107             // often the case with PPD files
00108             item = m_model->findItem(model_.replace(0,manu.length()+1,QString::fromLatin1("")));
00109         if (item)
00110             m_model->setCurrentItem(item);
00111     }
00112 }
00113 
00114 void KMDriverDbWidget::setHaveRaw(bool on)
00115 {
00116     if (on)
00117         m_raw->show();
00118     else
00119         m_raw->hide();
00120 }
00121 
00122 void KMDriverDbWidget::setHaveOther(bool on)
00123 {
00124     if (on)
00125         m_other->show();
00126     else
00127         m_other->hide();
00128 }
00129 
00130 QString KMDriverDbWidget::manufacturer()
00131 {
00132     return m_manu->currentText();
00133 }
00134 
00135 QString KMDriverDbWidget::model()
00136 {
00137     return m_model->currentText();
00138 }
00139 
00140 KMDBEntryList* KMDriverDbWidget::drivers()
00141 {
00142     return KMDriverDB::self()->findEntry(manufacturer(),model());
00143 }
00144 
00145 bool KMDriverDbWidget::isRaw()
00146 {
00147     return m_raw->isChecked();
00148 }
00149 
00150 void KMDriverDbWidget::init()
00151 {
00152     if (!m_valid)
00153     {
00154         QApplication::setOverrideCursor(KCursor::waitCursor());
00155         m_manu->clear();
00156         m_model->clear();
00157         m_manu->insertItem(i18n("Loading..."));
00158         KMDriverDB::self()->init(this);
00159     }
00160 }
00161 
00162 void KMDriverDbWidget::slotDbLoaded(bool reloaded)
00163 {
00164     QApplication::restoreOverrideCursor();
00165     m_valid = true;
00166     if (reloaded || m_manu->count() == 0 || (m_manu->count() == 1 && m_manu->text(0) == i18n("Loading...")))
00167     { // do something only if DB reloaded
00168         m_manu->clear();
00169         m_model->clear();
00170         QDictIterator< QDict<KMDBEntryList> >   it(KMDriverDB::self()->manufacturers());
00171         for (;it.current();++it)
00172             m_manu->insertItem(it.currentKey());
00173         m_manu->sort();
00174         m_manu->setCurrentItem(0);
00175     }
00176 }
00177 
00178 void KMDriverDbWidget::slotError(const QString& msg)
00179 {
00180     QApplication::restoreOverrideCursor();
00181     m_valid = false;
00182     m_manu->clear();
00183     KMessageBox::error(this, "<qt>"+msg+"</qt>");
00184 }
00185 
00186 void KMDriverDbWidget::slotManufacturerSelected(const QString& name)
00187 {
00188     m_model->clear();
00189     QDict<KMDBEntryList>    *models = KMDriverDB::self()->findModels(name);
00190     if (models)
00191     {
00192         QStrIList   ilist(true);
00193         QDictIterator<KMDBEntryList>    it(*models);
00194         for (;it.current();++it)
00195             ilist.append(it.currentKey().latin1());
00196         ilist.sort();
00197         m_model->insertStrList(&ilist);
00198         m_model->setCurrentItem(0);
00199     }
00200 }
00201 
00202 void KMDriverDbWidget::slotPostscriptToggled(bool on)
00203 {
00204     if (on)
00205     {
00206         QListBoxItem    *item = m_manu->findItem("GENERIC");
00207         if (item)
00208         {
00209             m_manu->setCurrentItem(item);
00210             item = m_model->findItem( "POSTSCRIPT PRINTER" );
00211             if ( item )
00212             {
00213                 m_model->setCurrentItem( item );
00214                 return;
00215             }
00216         }
00217         KMessageBox::error(this,i18n("Unable to find the PostScript driver."));
00218         m_postscript->setChecked(false);
00219     }
00220 }
00221 
00222 void KMDriverDbWidget::slotOtherClicked()
00223 {
00224     if (m_external.isEmpty())
00225     {
00226         KFileDialog dlg( QString::null, QString::null, this, 0, true );
00227         KURL url;
00228 
00229         dlg.setMode( KFile::File );
00230         dlg.setCaption( i18n( "Select Driver" ) );
00231         if ( dlg.exec() )
00232             url = dlg.selectedURL();
00233 
00234         if ( !url.isEmpty() )
00235         {
00236             QString filename;
00237             if ( KIO::NetAccess::download( url, filename, this ) )
00238             {
00239                 DrMain  *driver = KMFactory::self()->manager()->loadFileDriver(filename);
00240                 if (driver)
00241                 {
00242                     m_external = filename;
00243                     disconnect(m_manu,SIGNAL(highlighted(const QString&)),this,SLOT(slotManufacturerSelected(const QString&)));
00244                     m_manu->clear();
00245                     m_model->clear();
00246                     QString s = driver->get("manufacturer");
00247                     m_manu->insertItem((s.isEmpty() ? i18n("<Unknown>") : s));
00248                     s = driver->get("model");
00249                     m_model->insertItem((s.isEmpty() ? i18n("<Unknown>") : s));
00250                     m_manu->setCurrentItem(0);
00251                     m_model->setCurrentItem(0);
00252                     m_other->setText(i18n("Database"));
00253                     m_desc = driver->get("description");
00254                     delete driver;
00255                 }
00256                 else
00257                 {
00258                     KIO::NetAccess::removeTempFile( filename );
00259                     KMessageBox::error(this,"<qt>"+i18n("Wrong driver format.")+"<p>"+KMManager::self()->errorMsg()+"</p></qt>");
00260                 }
00261             }
00262         }
00263     }
00264     else
00265     {
00266         m_external = QString::null;
00267         connect(m_manu,SIGNAL(highlighted(const QString&)),this,SLOT(slotManufacturerSelected(const QString&)));
00268         m_other->setText(i18n("Other"));
00269         m_desc = QString::null;
00270         slotDbLoaded(true);
00271     }
00272 }
00273 #include "kmdriverdbwidget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys