23 #include <tdetempfile.h>
25 #include <tqapplication.h>
27 #include <kiconloader.h>
28 #include <tdelocale.h>
30 #include <tdemessagebox.h>
34 #include <tqtextstream.h>
40 SmbView::SmbView(TQWidget *parent,
const char *name)
41 : TDEListView(parent,name)
43 addColumn(i18n(
"Printer"));
44 addColumn(i18n(
"Comment"));
45 setFrameStyle(TQFrame::WinPanel|TQFrame::Sunken);
47 setAllColumnsShowFocus(
true);
48 setRootIsDecorated(
true);
52 m_proc =
new TDEProcess();
53 m_proc->setUseShell(
true);
55 connect(m_proc,TQ_SIGNAL(processExited(TDEProcess*)),TQ_SLOT(slotProcessExited(TDEProcess*)));
56 connect(m_proc,TQ_SIGNAL(receivedStdout(TDEProcess*,
char*,
int)),TQ_SLOT(slotReceivedStdout(TDEProcess*,
char*,
int)));
57 connect(
this,TQ_SIGNAL(selectionChanged(TQListViewItem*)),TQ_SLOT(slotSelectionChanged(TQListViewItem*)));
66 void SmbView::setLoginInfos(
const TQString& login,
const TQString& password)
69 m_password = password;
77 m_passwdFile =
new KTempFile;
78 m_passwdFile->setAutoDelete(
true);
80 TQTextStream *passwdFile = m_passwdFile->textStream();
81 if (!passwdFile)
return;
82 (*passwdFile) <<
"username = " << m_login << endl;
83 (*passwdFile) <<
"password = " << m_password << endl;
86 m_passwdFile->close();
89 void SmbView::startProcess(
int state)
91 m_buffer = TQString::null;
93 TQApplication::setOverrideCursor(KCursor::waitCursor());
94 m_proc->start(TDEProcess::NotifyOnExit,TDEProcess::Stdout);
98 void SmbView::endProcess()
115 TQApplication::restoreOverrideCursor();
118 m_proc->clearArguments();
121 void SmbView::slotProcessExited(TDEProcess*)
126 void SmbView::slotReceivedStdout(TDEProcess*,
char *buf,
int len)
128 m_buffer.append(TQString::fromLocal8Bit(buf,len));
134 m_wins_server = TQString::null;
135 TQString wins_keyword(
"wins server");
136 TQFile smb_conf (
"/etc/samba/smb.conf");
137 if (smb_conf.exists () && smb_conf.open (IO_ReadOnly))
139 TQTextStream smb_stream (&smb_conf);
140 while (!smb_stream.atEnd ())
142 TQString smb_line = smb_stream.readLine ();
143 if (smb_line.contains (wins_keyword, FALSE) > 0)
145 TQString key = smb_line.section (
'=', 0, 0);
146 key = key.stripWhiteSpace();
147 if (key.lower() == wins_keyword)
151 m_wins_server = smb_line.section (
'=', 1, 1);
153 m_wins_server = m_wins_server.section(
',', 0, 0);
154 m_wins_server = m_wins_server.stripWhiteSpace ();
155 m_wins_server = m_wins_server.section(
' ', 0, 0);
157 if (m_wins_server.section(
':', 1, 1) != NULL)
159 m_wins_server = m_wins_server.section(
':', 1, 1);
166 m_wins_server = m_wins_server.isEmpty ()?
" " :
" -U " + m_wins_server +
" ";
167 TQString cmd (
"nmblookup" + m_wins_server +
168 "-M -- - | grep '<01>' | awk '{print $1}' | xargs nmblookup -A | grep '<1d>'");
170 startProcess(GroupListing);
173 void SmbView::setOpen(TQListViewItem *item,
bool on)
175 if (on && item->childCount() == 0)
177 if (item->depth() == 0)
180 *m_proc <<
"nmblookup"+m_wins_server+
"-M ";
181 *m_proc << TDEProcess::quote(item->text(0));
183 startProcess(ServerListing);
185 else if (item->depth() == 1)
187 char *krb5ccname = getenv (
"KRB5CCNAME");
191 *m_proc <<
"smbclient -k -N -L ";
195 *m_proc <<
"smbclient -N -L ";
197 *m_proc << TDEProcess::quote (item->text (0));
199 *m_proc << TDEProcess::quote (item->parent ()->
204 *m_proc << TDEProcess::
205 quote (m_passwdFile->name ());
207 startProcess(ShareListing);
210 TQListView::setOpen(item,on);
213 void SmbView::processGroups()
215 TQStringList grps = TQStringList::split(
'\n',m_buffer,
false);
217 for (TQStringList::ConstIterator it=grps.begin(); it!=grps.end(); ++it)
219 int p = (*it).find(
"<1d>");
222 TQListViewItem *item =
new TQListViewItem(
this,(*it).left(p).stripWhiteSpace());
223 item->setExpandable(
true);
224 item->setPixmap(0,SmallIcon(
"network"));
228 void SmbView::processServers()
230 TQStringList lines = TQStringList::split(
'\n',m_buffer,
true);
233 while (index < lines.count())
235 line = lines[index++].stripWhiteSpace();
238 TQStringList words = TQStringList::split(
' ',line,
false);
239 if (words[1] !=
"<00>" || words[3] ==
"<GROUP>")
241 TQListViewItem *item =
new TQListViewItem(m_current,words[0]);
242 item->setExpandable(
true);
243 item->setPixmap(0,SmallIcon(
"tdeprint_computer"));
247 void SmbView::processShares()
249 TQStringList lines = TQStringList::split(
'\n',m_buffer,
true);
252 for (;index < lines.count();index++)
253 if (lines[index].stripWhiteSpace().startsWith(
"Sharename"))
256 while (index < lines.count())
258 line = lines[index++].stripWhiteSpace();
261 else if ( line.startsWith(
"Error returning" ) )
263 KMessageBox::error(
this, line );
266 TQString typestr(line.mid(15, 10).stripWhiteSpace());
269 if (typestr ==
"Printer")
271 TQString comm(line.mid(25).stripWhiteSpace()), sharen(line.mid(0, 15).stripWhiteSpace());
275 TQListViewItem *item =
new TQListViewItem(m_current,sharen,comm);
276 item->setPixmap(0,SmallIcon(
"tdeprint_printer"));
281 void SmbView::slotSelectionChanged(TQListViewItem *item)
283 if (item && item->depth() == 2)
284 emit printerSelected(item->parent()->parent()->text(0),item->parent()->text(0),item->text(0));
287 void SmbView::abort()
289 if (m_proc->isRunning())
292 #include "smbview.moc"