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