• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeprint
 

tdeprint

  • tdeprint
  • management
smbview.cpp
1/*
2 * This file is part of the KDE libraries
3 * Copyright (c) 2001 Michael Goffioul <tdeprint@swing.be>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License version 2 as published by the Free Software Foundation.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 **/
19
20#include "smbview.h"
21
22#include <tdeprocess.h>
23#include <tdetempfile.h>
24#include <tqheader.h>
25#include <tqapplication.h>
26
27#include <kiconloader.h>
28#include <tdelocale.h>
29#include <kdebug.h>
30#include <tdemessagebox.h>
31#include <kcursor.h>
32
33#include <tqfile.h>
34#include <tqtextstream.h>
35#include <cstdlib>
36
37
38//*********************************************************************************************
39
40SmbView::SmbView(TQWidget *parent, const char *name)
41: TDEListView(parent,name)
42{
43 addColumn(i18n("Printer"));
44 addColumn(i18n("Comment"));
45 setFrameStyle(TQFrame::WinPanel|TQFrame::Sunken);
46 setLineWidth(1);
47 setAllColumnsShowFocus(true);
48 setRootIsDecorated(true);
49
50 m_state = Idle;
51 m_current = 0;
52 m_proc = new TDEProcess();
53 m_proc->setUseShell(true);
54 m_passwdFile = 0;
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*)));
58}
59
60SmbView::~SmbView()
61{
62 delete m_proc;
63 delete m_passwdFile;
64}
65
66void SmbView::setLoginInfos(const TQString& login, const TQString& password)
67{
68 m_login = login;
69 m_password = password;
70
71 // We can't pass the password via the command line or the environment
72 // because the command line is publically accessible on most OSes and
73 // the environment is publically accessible on some OSes.
74 // Therefor we write the password to a file and pass that file to
75 // smbclient with the -A option
76 delete m_passwdFile;
77 m_passwdFile = new KTempFile;
78 m_passwdFile->setAutoDelete(true);
79
80 TQTextStream *passwdFile = m_passwdFile->textStream();
81 if (!passwdFile) return; // Error
82 (*passwdFile) << "username = " << m_login << endl;
83 (*passwdFile) << "password = " << m_password << endl;
84 // (*passwdFile) << "domain = " << ???? << endl;
85
86 m_passwdFile->close();
87}
88
89void SmbView::startProcess(int state)
90{
91 m_buffer = TQString::null;
92 m_state = state;
93 TQApplication::setOverrideCursor(KCursor::waitCursor());
94 m_proc->start(TDEProcess::NotifyOnExit,TDEProcess::Stdout);
95 emit running(true);
96}
97
98void SmbView::endProcess()
99{
100 switch (m_state)
101 {
102 case GroupListing:
103 processGroups();
104 break;
105 case ServerListing:
106 processServers();
107 break;
108 case ShareListing:
109 processShares();
110 break;
111 default:
112 break;
113 }
114 m_state = Idle;
115 TQApplication::restoreOverrideCursor();
116 emit running(false);
117 // clean up for future usage
118 m_proc->clearArguments();
119}
120
121void SmbView::slotProcessExited(TDEProcess*)
122{
123 endProcess();
124}
125
126void SmbView::slotReceivedStdout(TDEProcess*, char *buf, int len)
127{
128 m_buffer.append(TQString::fromLocal8Bit(buf,len));
129}
130
131void SmbView::init()
132{
133 // Open Samba configuration file and check if a WINS server is defined
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))
138 {
139 TQTextStream smb_stream (&smb_conf);
140 while (!smb_stream.atEnd ())
141 {
142 TQString smb_line = smb_stream.readLine ();
143 if (smb_line.contains (wins_keyword, FALSE) > 0)
144 {
145 TQString key = smb_line.section ('=', 0, 0);
146 key = key.stripWhiteSpace();
147 if (key.lower() == wins_keyword)
148 {
149 continue;
150 }
151 m_wins_server = smb_line.section ('=', 1, 1);
152 // take only the first declared WINS server
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);
156 // strip any server tag (see man smb.conf(5))
157 if (m_wins_server.section(':', 1, 1) != NULL)
158 {
159 m_wins_server = m_wins_server.section(':', 1, 1);
160 }
161 break;
162 }
163 }
164 smb_conf.close ();
165 }
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>'");
169 *m_proc << cmd;
170 startProcess(GroupListing);
171}
172
173void SmbView::setOpen(TQListViewItem *item, bool on)
174{
175 if (on && item->childCount() == 0)
176 {
177 if (item->depth() == 0)
178 { // opening group
179 m_current = item;
180 *m_proc << "nmblookup"+m_wins_server+"-M ";
181 *m_proc << TDEProcess::quote(item->text(0));
182 *m_proc << " -S";
183 startProcess(ServerListing);
184 }
185 else if (item->depth() == 1)
186 { // opening server
187 char *krb5ccname = getenv ("KRB5CCNAME");
188 m_current = item;
189 if (krb5ccname)
190 {
191 *m_proc << "smbclient -k -N -L ";
192 }
193 else
194 {
195 *m_proc << "smbclient -N -L ";
196 }
197 *m_proc << TDEProcess::quote (item->text (0));
198 *m_proc << " -W ";
199 *m_proc << TDEProcess::quote (item->parent ()->
200 text (0));
201 if (!krb5ccname)
202 {
203 *m_proc << " -A ";
204 *m_proc << TDEProcess::
205 quote (m_passwdFile->name ());
206 }
207 startProcess(ShareListing);
208 }
209 }
210 TQListView::setOpen(item,on);
211}
212
213void SmbView::processGroups()
214{
215 TQStringList grps = TQStringList::split('\n',m_buffer,false);
216 clear();
217 for (TQStringList::ConstIterator it=grps.begin(); it!=grps.end(); ++it)
218 {
219 int p = (*it).find("<1d>");
220 if (p == -1)
221 continue;
222 TQListViewItem *item = new TQListViewItem(this,(*it).left(p).stripWhiteSpace());
223 item->setExpandable(true);
224 item->setPixmap(0,SmallIcon("network"));
225 }
226}
227
228void SmbView::processServers()
229{
230 TQStringList lines = TQStringList::split('\n',m_buffer,true);
231 TQString line;
232 uint index(0);
233 while (index < lines.count())
234 {
235 line = lines[index++].stripWhiteSpace();
236 if (line.isEmpty())
237 break;
238 TQStringList words = TQStringList::split(' ',line,false);
239 if (words[1] != "<00>" || words[3] == "<GROUP>")
240 continue;
241 TQListViewItem *item = new TQListViewItem(m_current,words[0]);
242 item->setExpandable(true);
243 item->setPixmap(0,SmallIcon("tdeprint_computer"));
244 }
245}
246
247void SmbView::processShares()
248{
249 TQStringList lines = TQStringList::split('\n',m_buffer,true);
250 TQString line;
251 uint index(0);
252 for (;index < lines.count();index++)
253 if (lines[index].stripWhiteSpace().startsWith("Sharename"))
254 break;
255 index += 2;
256 while (index < lines.count())
257 {
258 line = lines[index++].stripWhiteSpace();
259 if (line.isEmpty())
260 break;
261 else if ( line.startsWith( "Error returning" ) )
262 {
263 KMessageBox::error( this, line );
264 break;
265 }
266 TQString typestr(line.mid(15, 10).stripWhiteSpace());
267 //TQStringList words = TQStringList::split(' ',line,false);
268 //if (words[1] == "Printer")
269 if (typestr == "Printer")
270 {
271 TQString comm(line.mid(25).stripWhiteSpace()), sharen(line.mid(0, 15).stripWhiteSpace());
272 //for (uint i=2; i<words.count(); i++)
273 // comm += (words[i]+" ");
274 //TQListViewItem *item = new TQListViewItem(m_current,words[0],comm);
275 TQListViewItem *item = new TQListViewItem(m_current,sharen,comm);
276 item->setPixmap(0,SmallIcon("tdeprint_printer"));
277 }
278 }
279}
280
281void SmbView::slotSelectionChanged(TQListViewItem *item)
282{
283 if (item && item->depth() == 2)
284 emit printerSelected(item->parent()->parent()->text(0),item->parent()->text(0),item->text(0));
285}
286
287void SmbView::abort()
288{
289 if (m_proc->isRunning())
290 m_proc->kill();
291}
292#include "smbview.moc"

tdeprint

Skip menu "tdeprint"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

tdeprint

Skip menu "tdeprint"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeprint by doxygen 1.9.4
This website is maintained by Timothy Pearson.