kdeprint Library API Documentation

management/smbview.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., 59 Temple Place - Suite 330,
00017  *  Boston, MA 02111-1307, USA.
00018  **/
00019 
00020 #include "smbview.h"
00021 
00022 #include <kprocess.h>
00023 #include <ktempfile.h>
00024 #include <qheader.h>
00025 #include <qapplication.h>
00026 
00027 #include <kiconloader.h>
00028 #include <klocale.h>
00029 #include <kdebug.h>
00030 #include <kmessagebox.h>
00031 
00032 //*********************************************************************************************
00033 
00034 SmbView::SmbView(QWidget *parent, const char *name)
00035 : KListView(parent,name)
00036 {
00037     addColumn(i18n("Printer"));
00038     addColumn(i18n("Comment"));
00039     setFrameStyle(QFrame::WinPanel|QFrame::Sunken);
00040     setLineWidth(1);
00041     setAllColumnsShowFocus(true);
00042     setRootIsDecorated(true);
00043 
00044     m_state = Idle;
00045     m_current = 0;
00046     m_proc = new KProcess();
00047     m_proc->setUseShell(true);
00048     m_passwdFile = 0;
00049     connect(m_proc,SIGNAL(processExited(KProcess*)),SLOT(slotProcessExited(KProcess*)));
00050     connect(m_proc,SIGNAL(receivedStdout(KProcess*,char*,int)),SLOT(slotReceivedStdout(KProcess*,char*,int)));
00051     connect(this,SIGNAL(selectionChanged(QListViewItem*)),SLOT(slotSelectionChanged(QListViewItem*)));
00052 }
00053 
00054 SmbView::~SmbView()
00055 {
00056     delete m_proc;
00057     delete m_passwdFile;
00058 }
00059 
00060 void SmbView::setLoginInfos(const QString& login, const QString& password)
00061 {
00062     m_login = login;
00063     m_password = password;
00064 
00065     // We can't pass the password via the command line or the environment 
00066     // because the command line is publically accessible on most OSes and
00067     // the environment is publically accessible on some OSes.
00068     // Therefor we write the password to a file and pass that file to 
00069     // smbclient with the -A option
00070     delete m_passwdFile;
00071     m_passwdFile = new KTempFile;
00072     m_passwdFile->setAutoDelete(true);
00073         
00074     QTextStream *passwdFile = m_passwdFile->textStream();
00075     if (!passwdFile) return; // Error
00076     (*passwdFile) << "username = " << m_login << endl;
00077     (*passwdFile) << "password = " << m_password << endl;
00078     // (*passwdFile) << "domain = " << ???? << endl; 
00079         
00080     m_passwdFile->close();
00081 }
00082 
00083 void SmbView::startProcess(int state)
00084 {
00085     m_buffer = QString::null;
00086     m_state = state;
00087     QApplication::setOverrideCursor(waitCursor);
00088     m_proc->start(KProcess::NotifyOnExit,KProcess::Stdout);
00089     emit running(true);
00090 }
00091 
00092 void SmbView::endProcess()
00093 {
00094     switch (m_state)
00095     {
00096         case GroupListing:
00097             processGroups();
00098             break;
00099         case ServerListing:
00100             processServers();
00101             break;
00102         case ShareListing:
00103             processShares();
00104             break;
00105         default:
00106             break;
00107     }
00108     m_state = Idle;
00109     QApplication::restoreOverrideCursor();
00110     emit running(false);
00111     // clean up for future usage
00112     m_proc->clearArguments();
00113 }
00114 
00115 void SmbView::slotProcessExited(KProcess*)
00116 {
00117     endProcess();
00118 }
00119 
00120 void SmbView::slotReceivedStdout(KProcess*, char *buf, int len)
00121 {
00122     m_buffer.append(QString::fromLocal8Bit(buf,len));
00123 }
00124 
00125 void SmbView::init()
00126 {
00127     QString cmd("nmblookup -M -- - | grep '<01>' | awk '{print $1}' | xargs nmblookup -A | grep '<1d>'");
00128     *m_proc << cmd;
00129     startProcess(GroupListing);
00130 }
00131 
00132 void SmbView::setOpen(QListViewItem *item, bool on)
00133 {
00134     if (on && item->childCount() == 0)
00135     {
00136         if (item->depth() == 0)
00137         { // opening group
00138             m_current = item;
00139             *m_proc << "nmblookup -M ";
00140                         *m_proc << KProcess::quote(item->text(0));
00141                         *m_proc << " -S | grep '<20>' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*<20>.*//' | xargs -iserv_name smbclient -N -L 'serv_name' -W ";
00142                         *m_proc << KProcess::quote(item->text(0));
00143             *m_proc << " -A ";
00144                         *m_proc << KProcess::quote(m_passwdFile->name());
00145             startProcess(ServerListing);
00146         }
00147         else if (item->depth() == 1)
00148         { // opening server
00149             m_current = item;
00150             *m_proc << "smbclient -N -L ";
00151                         *m_proc << KProcess::quote(item->text(0));
00152                         *m_proc << " -W ";
00153                         *m_proc << KProcess::quote(item->parent()->text(0));
00154             *m_proc << " -A ";
00155                         *m_proc << KProcess::quote(m_passwdFile->name());
00156             startProcess(ShareListing);
00157         }
00158     }
00159     QListView::setOpen(item,on);
00160 }
00161 
00162 void SmbView::processGroups()
00163 {
00164     QStringList grps = QStringList::split('\n',m_buffer,false);
00165     clear();
00166     for (QStringList::ConstIterator it=grps.begin(); it!=grps.end(); ++it)
00167     {
00168         int p = (*it).find("<1d>");
00169         if (p == -1)
00170             continue;
00171         QListViewItem   *item = new QListViewItem(this,(*it).left(p).stripWhiteSpace());
00172         item->setExpandable(true);
00173         item->setPixmap(0,SmallIcon("network"));
00174     }
00175 }
00176 
00177 void SmbView::processServers()
00178 {
00179     QStringList lines = QStringList::split('\n',m_buffer,true);
00180     QString     line;
00181     uint        index(0);
00182     for (;index < lines.count();index++)
00183         if (lines[index].stripWhiteSpace().startsWith("Server"))
00184             break;
00185     index += 2;
00186     while (index < lines.count())
00187     {
00188         line = lines[index++].stripWhiteSpace();
00189         if (line.isEmpty())
00190             break;
00191         QStringList words = QStringList::split(' ',line,false);
00192         QListViewItem   *item = new QListViewItem(m_current,words[0]);
00193         item->setExpandable(true);
00194         item->setPixmap(0,SmallIcon("kdeprint_computer"));
00195     }
00196 }
00197 
00198 void SmbView::processShares()
00199 {
00200     QStringList lines = QStringList::split('\n',m_buffer,true);
00201     QString     line;
00202     uint        index(0);
00203     for (;index < lines.count();index++)
00204         if (lines[index].stripWhiteSpace().startsWith("Sharename"))
00205             break;
00206     index += 2;
00207     while (index < lines.count())
00208     {
00209         line = lines[index++].stripWhiteSpace();
00210         if (line.isEmpty())
00211             break;
00212         else if ( line.startsWith( "Error returning" ) )
00213         {
00214             KMessageBox::error( this, line );
00215             break;
00216         }
00217         QString typestr(line.mid(15, 10).stripWhiteSpace());
00218         //QStringList   words = QStringList::split(' ',line,false);
00219         //if (words[1] == "Printer")
00220         if (typestr == "Printer")
00221         {
00222             QString comm(line.mid(25).stripWhiteSpace()), sharen(line.mid(0, 15).stripWhiteSpace());
00223             //for (uint i=2; i<words.count(); i++)
00224             //  comm += (words[i]+" ");
00225             //QListViewItem *item = new QListViewItem(m_current,words[0],comm);
00226             QListViewItem   *item = new QListViewItem(m_current,sharen,comm);
00227             item->setPixmap(0,SmallIcon("kdeprint_printer"));
00228         }
00229     }
00230 }
00231 
00232 void SmbView::slotSelectionChanged(QListViewItem *item)
00233 {
00234     if (item && item->depth() == 2)
00235         emit printerSelected(item->parent()->parent()->text(0),item->parent()->text(0),item->text(0));
00236 }
00237 
00238 void SmbView::abort()
00239 {
00240     if (m_proc->isRunning())
00241         m_proc->kill();
00242 }
00243 #include "smbview.moc"
KDE Logo
This file is part of the documentation for kdeprint Library Version 3.3.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jan 22 16:50:05 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003