kmwlocal.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 "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 <tqlayout.h> 00028 #include <tqlineedit.h> 00029 #include <tqlabel.h> 00030 #include <tqheader.h> 00031 #include <klistview.h> 00032 #include <kmessagebox.h> 00033 #include <kiconloader.h> 00034 00035 KMWLocal::KMWLocal(TQWidget *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(TQFrame::WinPanel|TQFrame::Sunken); 00046 m_ports->setLineWidth(1); 00047 m_ports->header()->hide(); 00048 m_ports->addColumn(""); 00049 m_ports->setSorting(-1); 00050 TQListViewItem *root = new TQListViewItem(m_ports, i18n("Local System")); 00051 root->setPixmap(0, SmallIcon("kdeprint_computer")); 00052 root->setOpen(true); 00053 connect(m_ports, TQT_SIGNAL(selectionChanged(TQListViewItem*)), TQT_SLOT(slotPortSelected(TQListViewItem*))); 00054 TQLabel *l1 = new TQLabel(i18n("URI:"), this); 00055 m_localuri = new TQLineEdit(this); 00056 connect( m_localuri, TQT_SIGNAL( textChanged( const TQString& ) ), TQT_SLOT( slotTextChanged( const TQString& ) ) ); 00057 m_parents[0] = new TQListViewItem(root, i18n("Parallel")); 00058 m_parents[1] = new TQListViewItem(root, m_parents[0], i18n("Serial")); 00059 m_parents[2] = new TQListViewItem(root, m_parents[1], i18n("USB")); 00060 m_parents[3] = new TQListViewItem(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 TQLabel *l2 = new TQLabel(i18n("<p>Select a valid detected port, or enter directly the corresponding URI in the bottom edit field.</p>"), this); 00064 00065 TQVBoxLayout *lay0 = new TQVBoxLayout(this, 0, 10); 00066 TQHBoxLayout *lay1 = new TQHBoxLayout(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(TQString& 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(TQListViewItem *item) 00093 { 00094 if ( m_block ) 00095 return; 00096 00097 TQString uri; 00098 if (!item || item->depth() <= 1 || item->depth() > 3) 00099 uri = TQString::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 TQListViewItem *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 TQListViewItem* KMWLocal::lookForItem( const TQString& uri ) 00129 { 00130 for ( int i=0; i<4; i++ ) 00131 { 00132 TQListViewItem *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 TQString& txt ) 00146 { 00147 if ( m_block ) 00148 return; 00149 00150 TQListViewItem *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 TQStringList 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 TQListViewItem *last[4] = {0, 0, 0, 0}; 00170 for (TQStringList::Iterator it=list.begin(); it!=list.end(); ++it) 00171 { 00172 TQString cl = *it; 00173 ++it; 00174 00175 TQString uri = *it; 00176 int p = uri.find( ':' ); 00177 TQString desc = *(++it), prot = ( p != -1 ? uri.left( p ) : TQString::null ); 00178 TQString 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 TQListViewItem(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 TQListViewItem *pItem = new TQListViewItem(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"