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 #include <kmessagebox.h>
00031 #include <kcursor.h>
00032
00033 #include <qfile.h>
00034 #include <qtextstream.h>
00035 #include <cstdlib>
00036
00037
00038
00039
00040 SmbView::SmbView(QWidget *parent, const char *name)
00041 : KListView(parent,name)
00042 {
00043 addColumn(i18n("Printer"));
00044 addColumn(i18n("Comment"));
00045 setFrameStyle(QFrame::WinPanel|QFrame::Sunken);
00046 setLineWidth(1);
00047 setAllColumnsShowFocus(true);
00048 setRootIsDecorated(true);
00049
00050 m_state = Idle;
00051 m_current = 0;
00052 m_proc = new KProcess();
00053 m_proc->setUseShell(true);
00054 m_passwdFile = 0;
00055 connect(m_proc,SIGNAL(processExited(KProcess*)),SLOT(slotProcessExited(KProcess*)));
00056 connect(m_proc,SIGNAL(receivedStdout(KProcess*,char*,int)),SLOT(slotReceivedStdout(KProcess*,char*,int)));
00057 connect(this,SIGNAL(selectionChanged(QListViewItem*)),SLOT(slotSelectionChanged(QListViewItem*)));
00058 }
00059
00060 SmbView::~SmbView()
00061 {
00062 delete m_proc;
00063 delete m_passwdFile;
00064 }
00065
00066 void SmbView::setLoginInfos(const QString& login, const QString& password)
00067 {
00068 m_login = login;
00069 m_password = password;
00070
00071
00072
00073
00074
00075
00076 delete m_passwdFile;
00077 m_passwdFile = new KTempFile;
00078 m_passwdFile->setAutoDelete(true);
00079
00080 QTextStream *passwdFile = m_passwdFile->textStream();
00081 if (!passwdFile) return;
00082 (*passwdFile) << "username = " << m_login << endl;
00083 (*passwdFile) << "password = " << m_password << endl;
00084
00085
00086 m_passwdFile->close();
00087 }
00088
00089 void SmbView::startProcess(int state)
00090 {
00091 m_buffer = QString::null;
00092 m_state = state;
00093 QApplication::setOverrideCursor(KCursor::waitCursor());
00094 m_proc->start(KProcess::NotifyOnExit,KProcess::Stdout);
00095 emit running(true);
00096 }
00097
00098 void SmbView::endProcess()
00099 {
00100 switch (m_state)
00101 {
00102 case GroupListing:
00103 processGroups();
00104 break;
00105 case ServerListing:
00106 processServers();
00107 break;
00108 case ShareListing:
00109 processShares();
00110 break;
00111 default:
00112 break;
00113 }
00114 m_state = Idle;
00115 QApplication::restoreOverrideCursor();
00116 emit running(false);
00117
00118 m_proc->clearArguments();
00119 }
00120
00121 void SmbView::slotProcessExited(KProcess*)
00122 {
00123 endProcess();
00124 }
00125
00126 void SmbView::slotReceivedStdout(KProcess*, char *buf, int len)
00127 {
00128 m_buffer.append(QString::fromLocal8Bit(buf,len));
00129 }
00130
00131 void SmbView::init()
00132 {
00133
00134 m_wins_server = QString::null;
00135 QString wins_keyword("wins server");
00136 QFile smb_conf ("/etc/samba/smb.conf");
00137 if (smb_conf.exists () && smb_conf.open (IO_ReadOnly))
00138 {
00139 QTextStream smb_stream (&smb_conf);
00140 while (!smb_stream.atEnd ())
00141 {
00142 QString smb_line = smb_stream.readLine ();
00143 if (smb_line.contains (wins_keyword, FALSE) > 0)
00144 {
00145 QString key = smb_line.section ('=', 0, 0);
00146 key = key.stripWhiteSpace();
00147 if (key.lower() == wins_keyword)
00148 {
00149 continue;
00150 }
00151 m_wins_server = smb_line.section ('=', 1, 1);
00152
00153 m_wins_server = m_wins_server.section(',', 0, 0);
00154 m_wins_server = m_wins_server.stripWhiteSpace ();
00155 m_wins_server = m_wins_server.section(' ', 0, 0);
00156
00157 if (m_wins_server.section(':', 1, 1) != NULL)
00158 {
00159 m_wins_server = m_wins_server.section(':', 1, 1);
00160 }
00161 break;
00162 }
00163 }
00164 smb_conf.close ();
00165 }
00166 m_wins_server = m_wins_server.isEmpty ()? " " : " -U " + m_wins_server + " ";
00167 QString cmd ("nmblookup" + m_wins_server +
00168 "-M -- - | grep '<01>' | awk '{print $1}' | xargs nmblookup -A | grep '<1d>'");
00169 *m_proc << cmd;
00170 startProcess(GroupListing);
00171 }
00172
00173 void SmbView::setOpen(QListViewItem *item, bool on)
00174 {
00175 if (on && item->childCount() == 0)
00176 {
00177 if (item->depth() == 0)
00178 {
00179 m_current = item;
00180 *m_proc << "nmblookup"+m_wins_server+"-M ";
00181 *m_proc << KProcess::quote(item->text(0));
00182 *m_proc << " -S";
00183 startProcess(ServerListing);
00184 }
00185 else if (item->depth() == 1)
00186 {
00187 m_current = item;
00188 *m_proc << "smbclient -N -L ";
00189 *m_proc << KProcess::quote(item->text(0));
00190 *m_proc << " -W ";
00191 *m_proc << KProcess::quote(item->parent()->text(0));
00192 if (m_login != QString::null)
00193 {
00194 *m_proc << " -A ";
00195 *m_proc << KProcess::quote(m_passwdFile->name());
00196 }
00197 startProcess(ShareListing);
00198 }
00199 }
00200 QListView::setOpen(item,on);
00201 }
00202
00203 void SmbView::processGroups()
00204 {
00205 QStringList grps = QStringList::split('\n',m_buffer,false);
00206 clear();
00207 for (QStringList::ConstIterator it=grps.begin(); it!=grps.end(); ++it)
00208 {
00209 int p = (*it).find("<1d>");
00210 if (p == -1)
00211 continue;
00212 QListViewItem *item = new QListViewItem(this,(*it).left(p).stripWhiteSpace());
00213 item->setExpandable(true);
00214 item->setPixmap(0,SmallIcon("network"));
00215 }
00216 }
00217
00218 void SmbView::processServers()
00219 {
00220 QStringList lines = QStringList::split('\n',m_buffer,true);
00221 QString line;
00222 uint index(0);
00223 while (index < lines.count())
00224 {
00225 line = lines[index++].stripWhiteSpace();
00226 if (line.isEmpty())
00227 break;
00228 QStringList words = QStringList::split(' ',line,false);
00229 if (words[1] != "<00>" || words[3] == "<GROUP>")
00230 continue;
00231 QListViewItem *item = new QListViewItem(m_current,words[0]);
00232 item->setExpandable(true);
00233 item->setPixmap(0,SmallIcon("kdeprint_computer"));
00234 }
00235 }
00236
00237 void SmbView::processShares()
00238 {
00239 QStringList lines = QStringList::split('\n',m_buffer,true);
00240 QString line;
00241 uint index(0);
00242 for (;index < lines.count();index++)
00243 if (lines[index].stripWhiteSpace().startsWith("Sharename"))
00244 break;
00245 index += 2;
00246 while (index < lines.count())
00247 {
00248 line = lines[index++].stripWhiteSpace();
00249 if (line.isEmpty())
00250 break;
00251 else if ( line.startsWith( "Error returning" ) )
00252 {
00253 KMessageBox::error( this, line );
00254 break;
00255 }
00256 QString typestr(line.mid(15, 10).stripWhiteSpace());
00257
00258
00259 if (typestr == "Printer")
00260 {
00261 QString comm(line.mid(25).stripWhiteSpace()), sharen(line.mid(0, 15).stripWhiteSpace());
00262
00263
00264
00265 QListViewItem *item = new QListViewItem(m_current,sharen,comm);
00266 item->setPixmap(0,SmallIcon("kdeprint_printer"));
00267 }
00268 }
00269 }
00270
00271 void SmbView::slotSelectionChanged(QListViewItem *item)
00272 {
00273 if (item && item->depth() == 2)
00274 emit printerSelected(item->parent()->parent()->text(0),item->parent()->text(0),item->text(0));
00275 }
00276
00277 void SmbView::abort()
00278 {
00279 if (m_proc->isRunning())
00280 m_proc->kill();
00281 }
00282 #include "smbview.moc"