kmwrlpr.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 "kmwrlpr.h" 00021 #include "kmprinter.h" 00022 #include "kmwizard.h" 00023 00024 #include <tqdir.h> 00025 #include <tqfile.h> 00026 #include <tqtextstream.h> 00027 #include <tqlayout.h> 00028 #include <tqlabel.h> 00029 #include <tqlineedit.h> 00030 #include <tqheader.h> 00031 #include <klistview.h> 00032 #include <klocale.h> 00033 #include <kiconloader.h> 00034 00035 static TQListViewItem* rlpr_findChild(TQListViewItem *c, const TQString& txt) 00036 { 00037 TQListViewItem *item(c); 00038 while (item) 00039 if (item->text(0) == txt) return item; 00040 else item = item->nextSibling(); 00041 return NULL; 00042 } 00043 00044 //***************************************************************************************************** 00045 00046 KMWRlpr::KMWRlpr(TQWidget *parent, const char *name) 00047 : KMWizardPage(parent,name) 00048 { 00049 m_ID = KMWizard::Custom+1; 00050 m_title = i18n("Remote LPD Queue Settings"); 00051 m_nextpage = KMWizard::Name; 00052 00053 m_view = new KListView(this); 00054 m_view->setFrameStyle(TQFrame::WinPanel|TQFrame::Sunken); 00055 m_view->setLineWidth(1); 00056 m_view->addColumn(TQString::fromLatin1("")); 00057 m_view->header()->hide(); 00058 m_view->setRootIsDecorated(true); 00059 m_view->setSorting(0); 00060 m_host = new TQLineEdit(this); 00061 m_queue = new TQLineEdit(this); 00062 TQLabel *m_hostlabel = new TQLabel(i18n("Host:"), this); 00063 TQLabel *m_queuelabel = new TQLabel(i18n("Queue:"), this); 00064 m_hostlabel->setBuddy(m_host); 00065 m_queuelabel->setBuddy(m_queue); 00066 connect(m_view,TQT_SIGNAL(selectionChanged(TQListViewItem*)),TQT_SLOT(slotPrinterSelected(TQListViewItem*))); 00067 00068 TQHBoxLayout *lay0 = new TQHBoxLayout(this, 0, 10); 00069 TQVBoxLayout *lay1 = new TQVBoxLayout(0, 0, 5); 00070 lay0->addWidget(m_view,1); 00071 lay0->addLayout(lay1,1); 00072 lay1->addWidget(m_hostlabel); 00073 lay1->addWidget(m_host); 00074 lay1->addSpacing(20); 00075 lay1->addWidget(m_queuelabel); 00076 lay1->addWidget(m_queue); 00077 lay1->addStretch(1); 00078 00079 initialize(); 00080 } 00081 00082 bool KMWRlpr::isValid(TQString& msg) 00083 { 00084 if (m_host->text().isEmpty()) 00085 msg = i18n("Empty host name."); 00086 else if (m_queue->text().isEmpty()) 00087 msg = i18n("Empty queue name."); 00088 else 00089 return true; 00090 return false; 00091 } 00092 00093 void KMWRlpr::initPrinter(KMPrinter *p) 00094 { 00095 m_host->setText(p->option("host")); 00096 m_queue->setText(p->option("queue")); 00097 TQListViewItem *item = rlpr_findChild(m_view->firstChild(),m_host->text()); 00098 if (item) 00099 { 00100 item = rlpr_findChild(item->firstChild(),m_queue->text()); 00101 if (item) 00102 { 00103 item->parent()->setOpen(true); 00104 m_view->setCurrentItem(item); 00105 m_view->ensureItemVisible(item); 00106 } 00107 } 00108 } 00109 00110 void KMWRlpr::updatePrinter(KMPrinter *p) 00111 { 00112 TQString uri = TQString::fromLatin1("lpd://%1/%2").arg(m_host->text()).arg(m_queue->text()); 00113 p->setDevice(uri); 00114 p->setOption("host",m_host->text()); 00115 p->setOption("queue",m_queue->text()); 00116 p->setOption("kde-backend-description",i18n("Remote LPD queue")); 00117 // setting default name and description, but only if the "name()" is empty 00118 // which is the case on first pass when adding a printer. This prevents from 00119 // bad side-effects when simply modifying an existing printer. 00120 if (p->name().isEmpty()) 00121 { 00122 p->setName(m_queue->text()); 00123 p->setPrinterName(p->name()); 00124 p->setDescription(i18n("Remote queue %1 on %2").arg(m_queue->text()).arg(m_host->text())); 00125 } 00126 } 00127 00128 void KMWRlpr::initialize() 00129 { 00130 m_view->clear(); 00131 TQFile f(TQDir::homeDirPath()+"/.rlprrc"); 00132 if (!f.exists()) f.setName("/etc/rlprrc"); 00133 if (f.exists() && f.open(IO_ReadOnly)) 00134 { 00135 TQTextStream t(&f); 00136 TQString line, host; 00137 int p(-1); 00138 while (!t.eof()) 00139 { 00140 line = t.readLine().stripWhiteSpace(); 00141 if (line.isEmpty()) 00142 continue; 00143 if ((p=line.find(':')) != -1) 00144 { 00145 host = line.left(p).stripWhiteSpace(); 00146 TQListViewItem *hitem = new TQListViewItem(m_view,host); 00147 hitem->setPixmap(0,SmallIcon("kdeprint_computer")); 00148 TQStringList prs = TQStringList::split(' ',line.right(line.length()-p-1),false); 00149 for (TQStringList::ConstIterator it=prs.begin(); it!=prs.end(); ++it) 00150 { 00151 TQListViewItem *pitem = new TQListViewItem(hitem,*it); 00152 pitem->setPixmap(0,SmallIcon("kdeprint_printer")); 00153 } 00154 } 00155 } 00156 f.close(); 00157 } 00158 00159 // parse printcap file for local printers 00160 f.setName("/etc/printcap"); 00161 if (f.exists() && f.open(IO_ReadOnly)) 00162 { 00163 TQTextStream t(&f); 00164 TQString line, buffer; 00165 TQListViewItem *hitem(m_view->firstChild()); 00166 while (hitem) if (hitem->text(0) == "localhost") break; else hitem = hitem->nextSibling(); 00167 while (!t.eof()) 00168 { 00169 buffer = TQString::null; 00170 while (!t.eof()) 00171 { 00172 line = t.readLine().stripWhiteSpace(); 00173 if (line.isEmpty() || line[0] == '#') 00174 continue; 00175 buffer.append(line); 00176 if (buffer.right(1) == "\\") 00177 buffer = buffer.left(buffer.length()-1).stripWhiteSpace(); 00178 else 00179 break; 00180 } 00181 if (buffer.isEmpty()) 00182 continue; 00183 int p = buffer.find(':'); 00184 if (p != -1) 00185 { 00186 TQString name = buffer.left(p); 00187 if (!hitem) 00188 { 00189 hitem = new TQListViewItem(m_view,"localhost"); 00190 hitem->setPixmap(0,SmallIcon("kdeprint_computer")); 00191 } 00192 TQListViewItem *pitem = new TQListViewItem(hitem,name); 00193 pitem->setPixmap(0,SmallIcon("kdeprint_printer")); 00194 } 00195 } 00196 } 00197 00198 if (m_view->childCount() == 0) 00199 new TQListViewItem(m_view,i18n("No Predefined Printers")); 00200 } 00201 00202 void KMWRlpr::slotPrinterSelected(TQListViewItem *item) 00203 { 00204 if (item && item->depth() == 1) 00205 { 00206 m_host->setText(item->parent()->text(0)); 00207 m_queue->setText(item->text(0)); 00208 } 00209 } 00210 #include "kmwrlpr.moc"