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