You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
293 lines
7.9 KiB
293 lines
7.9 KiB
15 years ago
|
/*
|
||
|
* This file is part of the KDE libraries
|
||
13 years ago
|
* Copyright (c) 2001 Michael Goffioul <tdeprint@swing.be>
|
||
15 years ago
|
*
|
||
|
* This library is free software; you can redistribute it and/or
|
||
|
* modify it under the terms of the GNU Library General Public
|
||
|
* License version 2 as published by the Free Software Foundation.
|
||
|
*
|
||
|
* This library is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
* Library General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Library General Public License
|
||
|
* along with this library; see the file COPYING.LIB. If not, write to
|
||
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
* Boston, MA 02110-1301, USA.
|
||
|
**/
|
||
|
|
||
|
#include "smbview.h"
|
||
|
|
||
|
#include <kprocess.h>
|
||
|
#include <ktempfile.h>
|
||
15 years ago
|
#include <tqheader.h>
|
||
|
#include <tqapplication.h>
|
||
15 years ago
|
|
||
|
#include <kiconloader.h>
|
||
|
#include <klocale.h>
|
||
|
#include <kdebug.h>
|
||
|
#include <kmessagebox.h>
|
||
|
#include <kcursor.h>
|
||
|
|
||
15 years ago
|
#include <tqfile.h>
|
||
|
#include <tqtextstream.h>
|
||
15 years ago
|
#include <cstdlib>
|
||
|
|
||
|
|
||
|
//*********************************************************************************************
|
||
|
|
||
15 years ago
|
SmbView::SmbView(TQWidget *parent, const char *name)
|
||
15 years ago
|
: KListView(parent,name)
|
||
|
{
|
||
|
addColumn(i18n("Printer"));
|
||
|
addColumn(i18n("Comment"));
|
||
15 years ago
|
setFrameStyle(TQFrame::WinPanel|TQFrame::Sunken);
|
||
15 years ago
|
setLineWidth(1);
|
||
|
setAllColumnsShowFocus(true);
|
||
|
setRootIsDecorated(true);
|
||
|
|
||
|
m_state = Idle;
|
||
|
m_current = 0;
|
||
|
m_proc = new KProcess();
|
||
|
m_proc->setUseShell(true);
|
||
|
m_passwdFile = 0;
|
||
15 years ago
|
connect(m_proc,TQT_SIGNAL(processExited(KProcess*)),TQT_SLOT(slotProcessExited(KProcess*)));
|
||
|
connect(m_proc,TQT_SIGNAL(receivedStdout(KProcess*,char*,int)),TQT_SLOT(slotReceivedStdout(KProcess*,char*,int)));
|
||
|
connect(this,TQT_SIGNAL(selectionChanged(TQListViewItem*)),TQT_SLOT(slotSelectionChanged(TQListViewItem*)));
|
||
15 years ago
|
}
|
||
|
|
||
|
SmbView::~SmbView()
|
||
|
{
|
||
|
delete m_proc;
|
||
|
delete m_passwdFile;
|
||
|
}
|
||
|
|
||
15 years ago
|
void SmbView::setLoginInfos(const TQString& login, const TQString& password)
|
||
15 years ago
|
{
|
||
|
m_login = login;
|
||
|
m_password = password;
|
||
|
|
||
|
// We can't pass the password via the command line or the environment
|
||
|
// because the command line is publically accessible on most OSes and
|
||
|
// the environment is publically accessible on some OSes.
|
||
|
// Therefor we write the password to a file and pass that file to
|
||
|
// smbclient with the -A option
|
||
|
delete m_passwdFile;
|
||
|
m_passwdFile = new KTempFile;
|
||
|
m_passwdFile->setAutoDelete(true);
|
||
|
|
||
15 years ago
|
TQTextStream *passwdFile = m_passwdFile->textStream();
|
||
15 years ago
|
if (!passwdFile) return; // Error
|
||
|
(*passwdFile) << "username = " << m_login << endl;
|
||
|
(*passwdFile) << "password = " << m_password << endl;
|
||
|
// (*passwdFile) << "domain = " << ???? << endl;
|
||
|
|
||
|
m_passwdFile->close();
|
||
|
}
|
||
|
|
||
|
void SmbView::startProcess(int state)
|
||
|
{
|
||
15 years ago
|
m_buffer = TQString::null;
|
||
15 years ago
|
m_state = state;
|
||
15 years ago
|
TQApplication::setOverrideCursor(KCursor::waitCursor());
|
||
15 years ago
|
m_proc->start(KProcess::NotifyOnExit,KProcess::Stdout);
|
||
|
emit running(true);
|
||
|
}
|
||
|
|
||
|
void SmbView::endProcess()
|
||
|
{
|
||
|
switch (m_state)
|
||
|
{
|
||
|
case GroupListing:
|
||
|
processGroups();
|
||
|
break;
|
||
|
case ServerListing:
|
||
|
processServers();
|
||
|
break;
|
||
|
case ShareListing:
|
||
|
processShares();
|
||
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
m_state = Idle;
|
||
15 years ago
|
TQApplication::restoreOverrideCursor();
|
||
15 years ago
|
emit running(false);
|
||
|
// clean up for future usage
|
||
|
m_proc->clearArguments();
|
||
|
}
|
||
|
|
||
|
void SmbView::slotProcessExited(KProcess*)
|
||
|
{
|
||
|
endProcess();
|
||
|
}
|
||
|
|
||
|
void SmbView::slotReceivedStdout(KProcess*, char *buf, int len)
|
||
|
{
|
||
15 years ago
|
m_buffer.append(TQString::fromLocal8Bit(buf,len));
|
||
15 years ago
|
}
|
||
|
|
||
|
void SmbView::init()
|
||
|
{
|
||
|
// Open Samba configuration file and check if a WINS server is defined
|
||
15 years ago
|
m_wins_server = TQString::null;
|
||
|
TQString wins_keyword("wins server");
|
||
|
TQFile smb_conf ("/etc/samba/smb.conf");
|
||
15 years ago
|
if (smb_conf.exists () && smb_conf.open (IO_ReadOnly))
|
||
|
{
|
||
15 years ago
|
TQTextStream smb_stream (&smb_conf);
|
||
15 years ago
|
while (!smb_stream.atEnd ())
|
||
|
{
|
||
15 years ago
|
TQString smb_line = smb_stream.readLine ();
|
||
13 years ago
|
if (smb_line.contains (wins_keyword, FALSE) > 0)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString key = smb_line.section ('=', 0, 0);
|
||
15 years ago
|
key = key.stripWhiteSpace();
|
||
|
if (key.lower() == wins_keyword)
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
m_wins_server = smb_line.section ('=', 1, 1);
|
||
|
// take only the first declared WINS server
|
||
|
m_wins_server = m_wins_server.section(',', 0, 0);
|
||
|
m_wins_server = m_wins_server.stripWhiteSpace ();
|
||
|
m_wins_server = m_wins_server.section(' ', 0, 0);
|
||
|
// strip any server tag (see man smb.conf(5))
|
||
|
if (m_wins_server.section(':', 1, 1) != NULL)
|
||
|
{
|
||
|
m_wins_server = m_wins_server.section(':', 1, 1);
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
smb_conf.close ();
|
||
|
}
|
||
|
m_wins_server = m_wins_server.isEmpty ()? " " : " -U " + m_wins_server + " ";
|
||
15 years ago
|
TQString cmd ("nmblookup" + m_wins_server +
|
||
15 years ago
|
"-M -- - | grep '<01>' | awk '{print $1}' | xargs nmblookup -A | grep '<1d>'");
|
||
|
*m_proc << cmd;
|
||
|
startProcess(GroupListing);
|
||
|
}
|
||
|
|
||
15 years ago
|
void SmbView::setOpen(TQListViewItem *item, bool on)
|
||
15 years ago
|
{
|
||
|
if (on && item->childCount() == 0)
|
||
|
{
|
||
|
if (item->depth() == 0)
|
||
|
{ // opening group
|
||
|
m_current = item;
|
||
|
*m_proc << "nmblookup"+m_wins_server+"-M ";
|
||
|
*m_proc << KProcess::quote(item->text(0));
|
||
15 years ago
|
*m_proc << " -S";
|
||
|
startProcess(ServerListing);
|
||
15 years ago
|
}
|
||
|
else if (item->depth() == 1)
|
||
|
{ // opening server
|
||
14 years ago
|
char *krb5ccname = getenv ("KRB5CCNAME");
|
||
15 years ago
|
m_current = item;
|
||
14 years ago
|
if (krb5ccname)
|
||
|
{
|
||
|
*m_proc << "smbclient -k -N -L ";
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
*m_proc << "smbclient -N -L ";
|
||
|
}
|
||
|
*m_proc << KProcess::quote (item->text (0));
|
||
|
*m_proc << " -W ";
|
||
13 years ago
|
*m_proc << KProcess::quote (item->parent ()->
|
||
14 years ago
|
text (0));
|
||
|
if (!krb5ccname)
|
||
|
{
|
||
15 years ago
|
*m_proc << " -A ";
|
||
14 years ago
|
*m_proc << KProcess::
|
||
|
quote (m_passwdFile->name ());
|
||
|
}
|
||
15 years ago
|
startProcess(ShareListing);
|
||
|
}
|
||
|
}
|
||
15 years ago
|
TQListView::setOpen(item,on);
|
||
15 years ago
|
}
|
||
|
|
||
|
void SmbView::processGroups()
|
||
|
{
|
||
14 years ago
|
TQStringList grps = TQStringList::split('\n',m_buffer,false);
|
||
15 years ago
|
clear();
|
||
15 years ago
|
for (TQStringList::ConstIterator it=grps.begin(); it!=grps.end(); ++it)
|
||
15 years ago
|
{
|
||
13 years ago
|
int p = (*it).find("<1d>");
|
||
15 years ago
|
if (p == -1)
|
||
|
continue;
|
||
14 years ago
|
TQListViewItem *item = new TQListViewItem(this,(*it).left(p).stripWhiteSpace());
|
||
15 years ago
|
item->setExpandable(true);
|
||
|
item->setPixmap(0,SmallIcon("network"));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void SmbView::processServers()
|
||
|
{
|
||
14 years ago
|
TQStringList lines = TQStringList::split('\n',m_buffer,true);
|
||
|
TQString line;
|
||
15 years ago
|
uint index(0);
|
||
|
while (index < lines.count())
|
||
|
{
|
||
|
line = lines[index++].stripWhiteSpace();
|
||
|
if (line.isEmpty())
|
||
|
break;
|
||
14 years ago
|
TQStringList words = TQStringList::split(' ',line,false);
|
||
15 years ago
|
if (words[1] != "<00>" || words[3] == "<GROUP>")
|
||
|
continue;
|
||
14 years ago
|
TQListViewItem *item = new TQListViewItem(m_current,words[0]);
|
||
15 years ago
|
item->setExpandable(true);
|
||
13 years ago
|
item->setPixmap(0,SmallIcon("tdeprint_computer"));
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
|
void SmbView::processShares()
|
||
|
{
|
||
14 years ago
|
TQStringList lines = TQStringList::split('\n',m_buffer,true);
|
||
|
TQString line;
|
||
15 years ago
|
uint index(0);
|
||
|
for (;index < lines.count();index++)
|
||
|
if (lines[index].stripWhiteSpace().startsWith("Sharename"))
|
||
|
break;
|
||
|
index += 2;
|
||
|
while (index < lines.count())
|
||
|
{
|
||
|
line = lines[index++].stripWhiteSpace();
|
||
|
if (line.isEmpty())
|
||
|
break;
|
||
|
else if ( line.startsWith( "Error returning" ) )
|
||
|
{
|
||
|
KMessageBox::error( this, line );
|
||
|
break;
|
||
|
}
|
||
14 years ago
|
TQString typestr(line.mid(15, 10).stripWhiteSpace());
|
||
|
//TQStringList words = TQStringList::split(' ',line,false);
|
||
15 years ago
|
//if (words[1] == "Printer")
|
||
|
if (typestr == "Printer")
|
||
|
{
|
||
14 years ago
|
TQString comm(line.mid(25).stripWhiteSpace()), sharen(line.mid(0, 15).stripWhiteSpace());
|
||
15 years ago
|
//for (uint i=2; i<words.count(); i++)
|
||
|
// comm += (words[i]+" ");
|
||
14 years ago
|
//TQListViewItem *item = new TQListViewItem(m_current,words[0],comm);
|
||
|
TQListViewItem *item = new TQListViewItem(m_current,sharen,comm);
|
||
13 years ago
|
item->setPixmap(0,SmallIcon("tdeprint_printer"));
|
||
15 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void SmbView::slotSelectionChanged(TQListViewItem *item)
|
||
15 years ago
|
{
|
||
|
if (item && item->depth() == 2)
|
||
13 years ago
|
emit printerSelected(item->parent()->parent()->text(0),item->parent()->text(0),item->text(0));
|
||
15 years ago
|
}
|
||
|
|
||
|
void SmbView::abort()
|
||
|
{
|
||
|
if (m_proc->isRunning())
|
||
|
m_proc->kill();
|
||
|
}
|
||
|
#include "smbview.moc"
|