summaryrefslogtreecommitdiff
path: root/noncore/net/opie-smb/qsmb.cpp
Unidiff
Diffstat (limited to 'noncore/net/opie-smb/qsmb.cpp') (more/less context) (show whitespace changes)
-rw-r--r--noncore/net/opie-smb/qsmb.cpp402
1 files changed, 402 insertions, 0 deletions
diff --git a/noncore/net/opie-smb/qsmb.cpp b/noncore/net/opie-smb/qsmb.cpp
new file mode 100644
index 0000000..527ab38
--- a/dev/null
+++ b/noncore/net/opie-smb/qsmb.cpp
@@ -0,0 +1,402 @@
1#include "qsmb.h"
2#include <qpushbutton.h>
3#include <qpe/qpeapplication.h>
4
5#include <string.h>
6#include <qstring.h>
7#include <qdir.h>
8#include <qfileinfo.h>
9
10#include <qlabel.h>
11#include <qlineedit.h>
12#include <qcombobox.h>
13#include <qstringlist.h>
14#include <qcheckbox.h>
15#include <qtextview.h>
16#include <qmessagebox.h>
17
18
19#include <pthread.h>
20#include <signal.h>
21#include <ctype.h>
22
23
24#include <opie2/odebug.h>
25using namespace Opie::Core;
26
27
28Qsmb::Qsmb( QWidget* parent, const char* name, WFlags fl )
29 : FormQPESMBBase( parent, name, fl )
30{
31 connect(CBHost, SIGNAL(activated(int)), this, SLOT(hostSelected(int)));
32 connect(DoItBtn, SIGNAL(clicked()), this, SLOT(DoItClicked()));
33 connect(BtnScan, SIGNAL(clicked()), this, SLOT(scanClicked()));
34 connect(BtnClear, SIGNAL(clicked()), this, SLOT(clear()));
35
36//TODO configurable mount points
37 if(!QFileInfo("/mnt/samba1").exists()) system("mkdir /mnt/samba1");
38 if(!QFileInfo("/mnt/samba2").exists()) system("mkdir /mnt/samba2");
39 if(!QFileInfo("/mnt/samba2").exists()) system("mkdir /mnt/samba3");
40
41 mountpt->insertItem("/mnt/samba1",-1);
42 mountpt->insertItem("/mnt/samba2",-1);
43 mountpt->insertItem("/mnt/samba3",-1);
44
45 top_element = NULL;
46 scanning = false;
47}
48
49Qsmb::~Qsmb()
50{
51}
52
53void Qsmb::clear()
54{
55 if (scanning) return;
56 ListViewScan->clear();
57 TextViewOutput->setText("");
58 top_element = NULL;
59}
60
61void Qsmb::scanClicked()
62{
63 if (scanning) return;
64 pthread_create(&tpid, NULL, runit, (void *)this);
65}
66
67void Qsmb::DoItClicked()
68{
69
70 if(! ListViewScan->selectedItem()) {
71 QMessageBox::warning(this, tr("Error"),tr("<p>No share selected!</p>"));
72 return;
73 }
74 if (scanning) return;
75 pthread_create(&tpid, NULL, runitm, (void *)this);
76}
77
78void* runit(void* arg)
79{
80 Qsmb* caller = (Qsmb*)arg;
81 caller->scan();
82 return(0);
83}
84
85void* runitm(void* arg)
86{
87 Qsmb* caller = (Qsmb*)arg;
88 caller->DoIt();
89 return(0);
90}
91
92void Qsmb::scan()
93{
94 int i;
95
96 if (scanning) return;
97 scanning = true;
98
99 char match[512], lmhosts[512];
100 QString cmd;
101 char result[256];
102
103 FILE *pipe, *pipe2;
104
105 LScan->setText("Scanning...");
106 qApp->processEvents();
107
108 cmd = "ifconfig |grep 'addr:'|awk '{print $2}'|awk 'BEGIN{FS=\":\"}{print $2}'|sed 's/\\.[0-9]*$//'|head -n1";
109
110 owarn << "cmd: " << cmd << oendl;
111
112 /* run findsmb & read output */
113 if ((pipe = popen(cmd.latin1(), "r")) == NULL) {
114 snprintf(result, 256, "Error: Can't run %s", cmd.latin1());
115 TextViewOutput->append(result);
116 return;
117 }
118 while(fgets(result, 256, pipe) != NULL) {
119 strcpy( match, result);
120 match[5]='\0';
121 break;
122 }
123 owarn << "match: " << match << oendl;
124
125 cmd = "/usr/bin/findsmb";
126 owarn <<"cmd: " << cmd << oendl;
127
128 TextViewOutput->append(cmd);
129
130 snprintf(lmhosts, 512, "echo '127.0.0.1 localhost'>/etc/samba/lmhosts");
131
132 if ((pipe2 = popen(lmhosts, "r")) == NULL) {
133 snprintf(result, 256, "Error: Can't run %s", lmhosts);
134 //TextViewOutput->append(result);
135 return;
136 }
137
138 /* run command & read output */
139 if ((pipe = popen(cmd.latin1(), "r")) == NULL) {
140 snprintf(result, 256, "Error: Can't run %s", cmd.latin1());
141 TextViewOutput->append(result);
142 return;
143 }
144
145 /* parse output and display in ListViewScan */
146 while(fgets(result, 256, pipe) != NULL) {
147 /* put result into TextViewOutput */
148 TextViewOutput->append(result);
149
150 if( strstr(result, match) != NULL ) {
151 char ip_addr[256], host[256], *ptr1;
152
153 strcpy( ip_addr, result);
154 ptr1 = strchr(ip_addr,' ');
155 strcpy( host, ptr1);
156 ip_addr[ptr1 - ip_addr]='\0';
157
158 for(i = 0; i < 256; i++) {
159 if(host[i]!=' ') {
160 strcpy( host, host + i);
161 break;
162 }
163 }
164 ptr1 = strchr(host,' ');
165 host[ptr1 - host] = '\0';
166
167 owarn << "add host: " << host << oendl;
168
169 CBHost->insertItem( host, -1);
170 snprintf( lmhosts, 512, "echo '%s %s'>>/etc/samba/lmhosts", ip_addr,host);
171
172 owarn << "lmhosts: " << lmhosts << oendl;
173
174 if ((pipe2 = popen(lmhosts, "r")) == NULL) {
175 snprintf(result, 256, "Error: Can't run %s", lmhosts);
176 return;
177 }
178 }
179 }
180
181 TextViewOutput->append("\n\n============================================\n");
182 LScan->setText("");
183 scanning = false;
184}
185
186void Qsmb::hostSelected(int index)
187{
188 owarn << "hostSelected" << oendl;
189 int i;
190
191 QListViewItem *element;
192 QListViewItem *parent;
193
194 QString text = CBHost->currentText();
195
196 if (scanning) return;
197 scanning = true;
198
199 QString cmd;
200 char result[256];
201
202 FILE *pipe;
203
204 LScan->setText("Scanning...");
205
206 if((const char *)username->text() == '\0')
207 cmd = "/usr/bin/smbclient -L //"+CBHost->currentText()+" -N 2>&1 |grep Disk";
208 else
209 cmd = "/usr/bin/smbclient -L //"+CBHost->currentText()+" -N -U"+username->text()+":"+password->text()+" 2>&1 |grep Disk";
210
211 for(i = 0; i < 512; i++) {
212 if(cmd[i]==':') {
213 cmd[i]='%';
214 break;
215 }
216 if(cmd[i]=='\0')
217 break;
218 }
219
220 owarn << "i="<< index << "cmd:" << cmd << oendl;
221
222 TextViewOutput->append(cmd);
223
224 /* run smbclient & read output */
225 if ((pipe = popen(cmd.latin1(), "r")) == NULL) {
226 snprintf(result, 256, "Error: Can't run %s", cmd.latin1());
227// cmd = "Error: Can't run "+cmd;
228 TextViewOutput->append(result);
229 return;
230 }
231
232 /* parse output and display in ListViewScan */
233 while(fgets(result, 256, pipe) != NULL) {
234 /* put result into TextViewOutput */
235 TextViewOutput->append(result);
236
237 if( strchr(result, '$') == NULL ) {
238 char share[256], *ptr1;
239
240 strcpy(share,result);
241 ptr1 = strchr(share,' ');
242 share[ptr1 - share]='\0';
243
244 owarn<< "add share: " << share << oendl;
245
246 if(top_element != NULL) {
247 bool found = false;
248 element = top_element;
249
250 while(element != NULL && !found) {
251 if(strcmp( element->text(0).ascii(), share)==0) {
252 parent = element;
253 found = true;
254 }
255 element = element->nextSibling();
256 }
257
258 if(!found) {
259 element = new QListViewItem(ListViewScan,share);
260 element->setOpen(true);
261 parent=element;
262 }
263 } else {
264 element = new QListViewItem(ListViewScan,share);
265 element->setOpen(true);
266 top_element = element;
267 parent = element;
268 }
269 }
270 }
271
272 TextViewOutput->append("\n\n============================================\n");
273 LScan->setText("");
274 scanning = false;
275}
276
277void Qsmb::DoIt()
278{
279 QListViewItem *element;
280 element = ListViewScan->selectedItem();
281 if(!element) {
282 QMessageBox::warning(this, tr("Error!!"),tr("<p><b>No</b> share selected!!</p>"));
283 return;
284 }
285
286 if (scanning) return;
287 scanning = true;
288
289 int i;
290
291 char share[512];
292 QString cmd;
293 QString cmd2;
294 char result[256];
295// QString result;
296 QString text = mountpt->currentText();
297
298 FILE *pipe,*pipe2;
299
300 LScan->setText("Mounting...");
301 qApp->processEvents();
302
303 cmd = "mkdir -p "+ text;
304
305 owarn<<"cmd: "<< cmd << oendl;
306
307 /* make sure mount exists! */
308 if ((pipe2 = popen(cmd.latin1(), "r")) == NULL) {
309
310 snprintf(result, 256, "Error: Can't run %s", cmd.latin1());
311 // result = "Error: Can't run " + cmd;
312 TextViewOutput->append(result);
313 return;
314 }
315
316 while(fgets(result, 256, pipe2) != NULL) {
317 /* put result into TextViewOutput */
318 TextViewOutput->append(result);
319 }
320
321
322 strcpy(share,(const char *)element->text(0));
323
324 for(i = 0; i < 256; i++) {
325 if(isalpha( share[i])) {
326 strcpy( share, share + i);
327 break;
328 }
329 }
330
331 cmd = "/usr/bin/smbmount //"+CBHost->currentText()+"/"+share+" "+mountpt->currentText()+" -U"+username->text()+":"+password->text();
332
333 for(i = 0; i < 512; i++) {
334 if(cmd[i]==':') {
335 cmd[i]='%';
336 break;
337 }
338 if(cmd[i]=='\0')
339 break;
340 }
341
342 owarn << "cmd: " << cmd << oendl;
343 TextViewOutput->append(cmd.latin1());
344
345
346 if(onbootBtn->isChecked()) {
347 owarn << "Saving Setting permanently..." << oendl;
348 cmd2 = "echo '" + cmd + "'>/opt/QtPalmtop/etc/samba.env";
349
350 /* run command & read output */
351 if ((pipe = popen(cmd2.latin1(), "r")) == NULL) {
352
353 snprintf(result, 256, "Error: Can't run %s", cmd.latin1());
354// result = "Error: Can't run "+ cmd;
355 //TextViewOutput->append(result);
356 return;
357 }
358 /* parse output and display in ListViewScan */
359 while(fgets(result, 256, pipe) != NULL) {
360 /* put result into TextViewOutput */
361 //TextViewOutput->append(result);
362 }
363 }
364
365 /* run command & read output */
366 if ((pipe = popen(cmd.latin1(), "r")) == NULL) {
367
368// result = "Error: Can't run "+ cmd;
369 snprintf(result, 256, "Error: Can't run %s", cmd.latin1());
370
371 TextViewOutput->append(result);
372 return;
373 }
374
375 /* parse output and display in ListViewScan */
376 while(fgets(result, 256, pipe) != NULL) {
377 /* put result into TextViewOutput */
378 TextViewOutput->append(result);
379 }
380 TextViewOutput->append("\n\n================CheckMounts==================\n");
381 LScan->setText("");
382
383 cmd = "/bin/mount 2>&1";
384 owarn << "cmd: " << cmd << oendl;
385
386 if ((pipe2 = popen(cmd.latin1(), "r")) == NULL) {
387
388 snprintf(result, 256, "Error: Can't run %s", cmd.latin1());
389// result = "Error: Can't run "+ cmd;
390
391 TextViewOutput->append(result);
392 return;
393 }
394 /* parse output and display in ListViewScan */
395 while(fgets(result, 256, pipe2) != NULL) {
396 /* put result into TextViewOutput */
397 TextViewOutput->append(result);
398 }
399
400 TextViewOutput->append("\n\n============================================\n");
401 scanning = false;
402}