00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "kmwlocal.h"
00021 #include "kmwizard.h"
00022 #include "kmprinter.h"
00023 #include "kmfactory.h"
00024 #include "kmmanager.h"
00025
00026 #include <klocale.h>
00027 #include <qlayout.h>
00028 #include <qlineedit.h>
00029 #include <qlabel.h>
00030 #include <qheader.h>
00031 #include <klistview.h>
00032 #include <kmessagebox.h>
00033 #include <kiconloader.h>
00034
00035 KMWLocal::KMWLocal(QWidget *parent, const char *name)
00036 : KMWizardPage(parent,name)
00037 {
00038 m_title = i18n("Local Port Selection");
00039 m_ID = KMWizard::Local;
00040 m_nextpage = KMWizard::Driver;
00041 m_initialized = false;
00042 m_block = false;
00043
00044 m_ports = new KListView(this);
00045 m_ports->setFrameStyle(QFrame::WinPanel|QFrame::Sunken);
00046 m_ports->setLineWidth(1);
00047 m_ports->header()->hide();
00048 m_ports->addColumn("");
00049 m_ports->setSorting(-1);
00050 QListViewItem *root = new QListViewItem(m_ports, i18n("Local System"));
00051 root->setPixmap(0, SmallIcon("kdeprint_computer"));
00052 root->setOpen(true);
00053 connect(m_ports, SIGNAL(selectionChanged(QListViewItem*)), SLOT(slotPortSelected(QListViewItem*)));
00054 QLabel *l1 = new QLabel(i18n("URI:"), this);
00055 m_localuri = new QLineEdit(this);
00056 connect( m_localuri, SIGNAL( textChanged( const QString& ) ), SLOT( slotTextChanged( const QString& ) ) );
00057 m_parents[0] = new QListViewItem(root, i18n("Parallel"));
00058 m_parents[1] = new QListViewItem(root, m_parents[0], i18n("Serial"));
00059 m_parents[2] = new QListViewItem(root, m_parents[1], i18n("USB"));
00060 m_parents[3] = new QListViewItem(root, m_parents[2], i18n("Others"));
00061 for (int i=0;i<4;i++)
00062 m_parents[i]->setPixmap(0, SmallIcon("input_devices_settings"));
00063 QLabel *l2 = new QLabel(i18n("<p>Select a valid detected port, or enter directly the corresponding URI in the bottom edit field.</p>"), this);
00064
00065 QVBoxLayout *lay0 = new QVBoxLayout(this, 0, 10);
00066 QHBoxLayout *lay1 = new QHBoxLayout(0, 0, 10);
00067 lay0->addWidget(l2, 0);
00068 lay0->addWidget(m_ports, 1);
00069 lay0->addLayout(lay1, 0);
00070 lay1->addWidget(l1, 0);
00071 lay1->addWidget(m_localuri, 1);
00072 }
00073
00074 bool KMWLocal::isValid(QString& msg)
00075 {
00076 if (m_localuri->text().isEmpty())
00077 {
00078 msg = i18n("The URI is empty","Empty URI.");
00079 return false;
00080 }
00081 else if (m_uris.findIndex(m_localuri->text()) == -1)
00082 {
00083 if (KMessageBox::warningContinueCancel(this, i18n("The local URI doesn't correspond to a detected port. Continue?")) == KMessageBox::Cancel)
00084 {
00085 msg = i18n("Select a valid port.");
00086 return false;
00087 }
00088 }
00089 return true;
00090 }
00091
00092 void KMWLocal::slotPortSelected(QListViewItem *item)
00093 {
00094 if ( m_block )
00095 return;
00096
00097 QString uri;
00098 if (!item || item->depth() <= 1 || item->depth() > 3)
00099 uri = QString::null;
00100 else if (item->depth() == 3)
00101 uri = item->parent()->text( 1 );
00102 else
00103 uri = item->text( 1 );
00104 m_block = true;
00105 m_localuri->setText( uri );
00106 m_block = false;
00107 }
00108
00109 void KMWLocal::updatePrinter(KMPrinter *printer)
00110 {
00111 QListViewItem *item = m_ports->selectedItem();
00112 if ( item && item->depth() == 3 )
00113 printer->setOption( "kde-autodetect", item->text( 0 ) );
00114 printer->setDevice(m_localuri->text());
00115 }
00116
00117 void KMWLocal::initPrinter(KMPrinter *printer)
00118 {
00119 if (!m_initialized)
00120 initialize();
00121
00122 if (printer)
00123 {
00124 m_localuri->setText(printer->device());
00125 }
00126 }
00127
00128 QListViewItem* KMWLocal::lookForItem( const QString& uri )
00129 {
00130 for ( int i=0; i<4; i++ )
00131 {
00132 QListViewItem *item = m_parents[ i ]->firstChild();
00133 while ( item )
00134 if ( item->text( 1 ) == uri )
00135 if ( item->firstChild() )
00136 return item->firstChild();
00137 else
00138 return item;
00139 else
00140 item = item->nextSibling();
00141 }
00142 return 0;
00143 }
00144
00145 void KMWLocal::slotTextChanged( const QString& txt )
00146 {
00147 if ( m_block )
00148 return;
00149
00150 QListViewItem *item = lookForItem( txt );
00151 if ( item )
00152 {
00153 m_block = true;
00154 m_ports->setSelected( item, true );
00155 m_block = false;
00156 }
00157 else
00158 m_ports->clearSelection();
00159 }
00160
00161 void KMWLocal::initialize()
00162 {
00163 QStringList list = KMFactory::self()->manager()->detectLocalPrinters();
00164 if (list.isEmpty() || (list.count() % 4) != 0)
00165 {
00166 KMessageBox::error(this, i18n("Unable to detect local ports."));
00167 return;
00168 }
00169 QListViewItem *last[4] = {0, 0, 0, 0};
00170 for (QStringList::Iterator it=list.begin(); it!=list.end(); ++it)
00171 {
00172 QString cl = *it;
00173 ++it;
00174
00175 QString uri = *it;
00176 int p = uri.find( ':' );
00177 QString desc = *(++it), prot = ( p != -1 ? uri.left( p ) : QString::null );
00178 QString printer = *(++it);
00179 int index(-1);
00180 if (desc.isEmpty())
00181 desc = uri;
00182 if (prot == "parallel" || prot == "file")
00183 index = 0;
00184 else if (prot == "serial")
00185 index = 1;
00186 else if (prot == "usb")
00187 index = 2;
00188 else if (cl == "direct")
00189 index = 3;
00190 else
00191 continue;
00192 last[index] = new QListViewItem(m_parents[index], last[index], desc, uri);
00193 last[index]->setPixmap(0, SmallIcon("blockdevice"));
00194 m_parents[index]->setOpen(true);
00195 m_uris << uri;
00196 if (!printer.isEmpty())
00197 {
00198 QListViewItem *pItem = new QListViewItem(last[index], printer);
00199 last[index]->setOpen(true);
00200 pItem->setPixmap(0, SmallIcon("kdeprint_printer"));
00201 }
00202 }
00203 m_initialized = true;
00204 }
00205
00206 #include "kmwlocal.moc"