Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

qsmb.cpp

Go to the documentation of this file.
00001 /*
00002                =.            This file is part of the OPIE Project
00003              .=l.            Copyright (c)  2002-2005  Kurt Korbatits <support@midget.net.au>
00004            .>+-=             Copyright (c)  2005  L. Potter <lpotter@trolltech.com>
00005  _;:,     .>    :=|.         This program is free software; you can 
00006 .> <`_,   >  .   <=          redistribute it and/or  modify it under
00007 :`=1 )Y*s>-.--   :           the terms of the GNU General Public
00008 .="- .-=="i,     .._         License as published by the Free Software
00009  - .   .-<_>     .<>         Foundation; either version 2 of the License,
00010      ._= =}       :          or (at your option) any later version.
00011     .%`+i>       _;_.        
00012     .i_,=:_.      -<s.       This program is distributed in the hope that  
00013      +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
00014     : ..    .:,     . . .    without even the implied warranty of
00015     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00016   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
00017 ..}^=.=       =       ;      General Public License for more
00018 ++=   -.     .`     .:       details.
00019  :     =  ...= . :.=-        
00020  -.   .:....=;==+<;          You should have received a copy of the GNU
00021   -_. . .   )=.  =           General Public License along with
00022     --        :-=`           this library; see the file COPYING.BIN. 
00023                              If not, write to the Free Software Foundation,
00024                              Inc., 59 Temple Place - Suite 330,
00025                              Boston, MA 02111-1307, USA.
00026 
00027 */
00028 
00029 #include "qsmb.h"
00030 #include <qpushbutton.h>
00031 #include <qpe/qpeapplication.h>
00032 
00033 #include <string.h>
00034 #include <qstring.h>
00035 #include <qstringlist.h>
00036 #include <qdir.h>
00037 #include <qfileinfo.h>
00038 #include <qtabwidget.h>
00039 
00040 #include <qpe/process.h>
00041 #include <qlabel.h>
00042 #include <qlineedit.h>
00043 #include <qcombobox.h>
00044 #include <qstringlist.h>
00045 #include <qcheckbox.h>
00046 #include <qtextview.h>
00047 #include <qmessagebox.h>
00048 #include <qtextstream.h>
00049 
00050 
00051 #include <pthread.h>
00052 #include <signal.h>
00053 #include <ctype.h>
00054 
00055 
00056 #include <netinet/in.h>
00057 #include <arpa/inet.h>
00058 #include <rpc/clnt.h>
00059 
00060 #include <sys/vfs.h>
00061 #include <mntent.h>
00062 
00063 // #include <opie2/odebug.h>
00064 // using namespace Opie::Core;
00065 
00066 
00067 Qsmb::Qsmb( QWidget* parent,  const char* name, WFlags fl )
00068    : FormQPESMBBase( parent, name, fl )
00069 {
00070    connect(CBHost, SIGNAL(activated(int)), this, SLOT(hostSelected(int)));
00071    connect(DoItBtn, SIGNAL(clicked()), this, SLOT(DoItClicked()));
00072    connect(UnmountBtn, SIGNAL(clicked()), this, SLOT(umountIt()));
00073    connect(BtnScan, SIGNAL(clicked()), this, SLOT(scanClicked()));
00074    connect(BtnClear, SIGNAL(clicked()), this, SLOT(clear()));
00075    connect(ListViewScan, SIGNAL(clicked(QListViewItem*)), this, SLOT(TextViewClicked(QListViewItem*)));
00076 
00077    mountpt->setEditable(true);
00078    mountpt->insertItem("/mnt/samba1",-1);
00079    mountpt->insertItem("/mnt/samba2",-1);
00080    mountpt->insertItem("/mnt/samba3",-1);
00081    
00082    setTabOrder(BtnScan, username);
00083    setTabOrder(username, password);
00084    setTabOrder(password, CBHost);
00085    setTabOrder(CBHost, TextViewOutput);
00086    setTabOrder(TextViewOutput, mountpt);
00087    setTabOrder(mountpt, DoItBtn);
00088    setTabOrder(DoItBtn, UnmountBtn);
00089    
00090    top_element = NULL;
00091    scanning = false;
00092 }
00093 
00094 Qsmb::~Qsmb()
00095 {
00096 }
00097 
00098 void Qsmb::clear() 
00099 {
00100    if (scanning) return;
00101    ListViewScan->clear();       
00102    TextViewOutput->setText("");
00103    CBHost->clear();
00104    top_element = NULL;  
00105 }
00106 
00107 void Qsmb::scanClicked() 
00108 {
00109    if (scanning) return;
00110    pthread_create(&tpid, NULL, runit, (void *)this);
00111 }
00112 
00113 void Qsmb::DoItClicked() 
00114 {
00115 
00116    if( !ListViewScan->selectedItem()) {
00117       QMessageBox::warning(this, tr("Error"),tr("<p>No share selected!</p>"));
00118       return;
00119    }
00120    if (scanning) return;
00121    pthread_create(&tpid, NULL, runitm, (void *)this);
00122 }
00123 
00124 void* runit(void* arg) 
00125 {
00126    Qsmb* caller = (Qsmb*)arg;
00127    caller->scan();
00128    return(0);
00129 }
00130 
00131 void* runitm(void* arg) 
00132 {
00133    Qsmb* caller = (Qsmb*)arg;
00134    caller->DoIt();
00135    return(0);
00136 }
00137 
00138 void Qsmb::scan() 
00139 {
00140    clear();
00141 //   if (scanning) return;
00142    scanning = true;
00143 
00144    QString match;
00145    QString cmd;
00146    LScan->setText("Scanning...");
00147    qApp->processEvents();
00148 
00149    sockaddr_in my_addr;
00150    get_myaddress( &my_addr);
00151 
00152    QString ip = inet_ntoa( my_addr.sin_addr);
00153    qWarning("IP Address : "+ip);
00154 
00155    match = ip.left(5);
00156 
00157    QStringList ccmd;
00158    TextViewOutput->append("smbfind");
00159 
00160    QFile lmhosts("/etc/samba/lmhosts");
00161    QTextStream lms(&lmhosts);
00162    lmhosts.open(IO_WriteOnly);
00163    lms << "127.0.0.1 localhost\n";
00164     
00165    /* parse output and display in ListViewScan */
00166    ccmd = "smbfind";
00167          runCommand(ccmd);
00168 
00169    QTextStream s(&out, IO_ReadOnly);
00170 
00171    while ( !s.atEnd() ) {
00172       QString ip_addr, host, output;
00173       QString tmp = s.readLine();
00174       bool ok;
00175       tmp.left(1).toInt( &ok, 10 );
00176       if(ok) {
00177          QStringList token = QStringList::split(' ',  tmp );
00178          ip_addr =  token[0];
00179          host = token[1];
00180          CBHost->insertItem( host, -1);
00181          lms <<  ip_addr+" "+host+"\n";
00182       }
00183    }
00184    lmhosts.close();
00185 
00186    TextViewOutput->append("\n\n============================================\n");
00187    LScan->setText("");
00188    scanning = false;
00189 }
00190 
00191 void Qsmb::hostSelected(int /*index*/ )
00192 {
00193    QListViewItem *element;
00194 //   QListViewItem *parent;
00195 
00196    QString text = CBHost->currentText();
00197    ListViewScan->clear();
00198 
00199    if (scanning) return;
00200    scanning = true;
00201 
00202    QString cmd;
00203    QStringList ccmd;
00204 
00205    LScan->setText("Scanning...");
00206 
00207    ccmd << "/usr/bin/smbclient";
00208    ccmd << "-L";
00209    ccmd << CBHost->currentText();
00210    ccmd << "-N";
00211    
00212    if(username->text().isEmpty()) {
00213       //do nothing
00214    } else {
00215       ccmd << "-U";
00216       ccmd << username->text()+"\%"+ password->text();
00217    }
00218    runCommand(ccmd);
00219    QTextStream s(&out, IO_ReadOnly);
00220 
00221    while ( !s.atEnd() ) {
00222       QString share;
00223       QString comment;
00224       QString mount;
00225       QString tmp = s.readLine();
00226       
00227       if( tmp.find("$") == -1 && tmp.find("Disk") != -1) {
00228          QStringList token = QStringList::split(' ',  tmp );
00229          share = token[0];
00230          comment = token[2];
00231          share = share.stripWhiteSpace();
00232          comment = comment.stripWhiteSpace();
00233 //         if(isMounted(share))
00234 
00235          mount = getMount(share);
00236          element = new QListViewItem(ListViewScan, share, comment, mount);
00237          element->setOpen(true);
00238 //             top_element = element;
00239 //             parent = element;
00240       }
00241 
00242    }
00243 //   owarn << "i="<< index << "cmd:" <<  cmd << oendl;
00244 
00245    TextViewOutput->append(cmd);
00246 
00247    /* run smbclient & read output */
00248 //    if ((pipe = popen(cmd.latin1(), "r")) == NULL) {
00249 //       snprintf(result, 256, "Error: Can't run %s", cmd.latin1());
00250 // //      cmd = "Error: Can't run "+cmd;
00251 //       TextViewOutput->append(result);
00252 //       return;
00253 //    }
00254 
00255    /* parse output and display in ListViewScan */
00256 //    while(fgets(result, 256, pipe) != NULL) {
00257 //       /* put result into TextViewOutput */
00258 //       TextViewOutput->append(result);
00259 
00260 //       if( strchr(result, '$') == NULL ) { 
00261 //          char share[256], *ptr1;
00262 
00263 //          strcpy(share,result);
00264 //          ptr1 = strchr(share,' ');
00265 //          share[ptr1 - share]='\0';
00266 
00267 //          owarn<< "add share: " << share << oendl;
00268 
00269 //          if(top_element != NULL) {
00270 //             bool found = false;
00271 //             element = top_element;
00272 
00273 //             while(element != NULL && !found) {
00274 //                if(strcmp( element->text(0).ascii(), share)==0) {
00275 //                   parent = element;
00276 //                   found = true;
00277 //                }
00278 //                element = element->nextSibling();
00279 //             }
00280 
00281 //             if(!found) {
00282 //                element = new QListViewItem(ListViewScan,share);
00283 //                element->setOpen(true);
00284 //                parent=element;
00285 //             }
00286 //          } else {
00287 //             element = new QListViewItem(ListViewScan,share);
00288 //             element->setOpen(true);
00289 //             top_element = element;
00290 //             parent = element;
00291 //          }
00292 //       }
00293 //    }
00294 
00295    TextViewOutput->append("\n\n============================================\n");
00296    LScan->setText("");
00297    scanning = false;
00298 }
00299 
00300 
00301 void Qsmb::DoIt()
00302 {
00303    
00304    QListViewItem *element;
00305    element = ListViewScan->selectedItem();
00306    if(!element) {
00307       return;
00308    }
00309 
00310    if (scanning) return;
00311    scanning = true;
00312 
00313    QString mount = mountpt->currentText();
00314    if(isMounted(mount)) {
00315       qWarning(mount +" is already mounted");
00316       TextViewOutput->append(mount +" is already mounted");
00317       return;
00318    }
00319    
00320    bool noerr = false;
00321 
00322    QString share;
00323    QString cmd;
00324    QString cmd2;
00325    QString text = mountpt->currentText();
00326    QStringList ccmd;
00327 
00328    LScan->setText("Mounting...");
00329    qApp->processEvents();
00330 
00331    if( !QFileInfo(text).exists()) {
00332       ccmd << "mkdir";
00333       ccmd << "-p";
00334       ccmd << text;
00335 
00336       qWarning( "cmd: "+ ccmd.join(" "));
00337       runCommand(ccmd);
00338    }
00339 
00340    share = element->text(0);
00341    qWarning("selected share is "+share);
00342 
00343    QString service = CBHost->currentText();
00344    service = service.stripWhiteSpace();
00345    if(mount.left(1) != "/")
00346       mount = QDir::currentDirPath()+"/"+mount;
00347    mount = mount.stripWhiteSpace();
00348    ccmd.clear();
00349    
00350    ccmd << "/usr/bin/smbmount";
00351    ccmd << "//"+ service+"/"+share;
00352    ccmd << mount;
00353    ccmd << "-o";
00354    ccmd << "username="+username->text()+",password="+password->text()+"";
00355 
00356    if(onbootBtn->isChecked()) {
00357       qWarning("Saving Setting permanently...");
00358       QFile sambenv("/opt/QtPalmtop/etc/samba.env");
00359       QTextStream smbv(&sambenv);
00360       sambenv.open(IO_WriteOnly);
00361       smbv << ccmd.join(" ") ;
00362       sambenv.close();
00363    }
00364 
00365    noerr = runCommand(ccmd);
00366    
00367    LScan->setText("");
00368 
00369    if(noerr && isMounted(mount)) {
00370       element->setText(2, mount);
00371       TextViewOutput->append("\n\n================CheckMounts==================\n");
00372       ccmd = "/bin/mount";
00373       runCommand(ccmd);
00374       TextViewOutput->append("\n\n============================================\n");
00375       qApp->processEvents();
00376    } else {
00377    //do nothing
00378  }
00379 
00380    scanning = false;
00381 }
00382 
00383 void Qsmb::umountIt()
00384 {
00385    QListViewItem *element;
00386    element = ListViewScan->selectedItem();
00387    if(!element) {
00388       return;
00389    }
00390 
00391    QString mount = mountpt->currentText();
00392    if(!isMounted(mount)) {
00393       qWarning(mount +" is not mounted");
00394       TextViewOutput->append(mount +" is not mounted");
00395       return;
00396    }
00397 
00398    QStringList ccmd;
00399    QString share;
00400    share = element->text(0);
00401    qWarning("selected share is "+share);
00402 
00403    if(mount.left(1) != "/")
00404       mount = QDir::currentDirPath()+"/"+mount;
00405    mount = mount.stripWhiteSpace();
00406 
00407    ccmd << "/usr/bin/smbumount";
00408    ccmd << mount;
00409    runCommand(ccmd);
00410 
00411    element->setText(2, "");
00412 
00413    ccmd = "/bin/mount";
00414    runCommand(ccmd);
00415 }
00416 
00417 bool Qsmb::runCommand(const QStringList & command) {
00418    qWarning( "runCommand " + command.join(" ") );
00419    TextViewOutput->append(command.join(" "));
00420    out = "";
00421    Process ipkg_status( command);
00422    bool r = ipkg_status.exec("",out);
00423 
00424    qWarning("result is %d"+ r );
00425    qWarning("Output " + out );
00426    TextViewOutput->append(out);
00427 
00428 //very hacky
00429    if(out.find("failed") !=-1) {
00430       r = false;
00431    }
00432    return r;
00433 }
00434 
00435 
00436 bool Qsmb::isMounted(const QString &mountPoint)
00437 {
00438     struct mntent *me;
00439     bool mounted = false;
00440     FILE *mntfp = setmntent( "/etc/mtab", "r" );
00441     if ( mntfp ){
00442         while ( (me = getmntent( mntfp )) != 0 ) {
00443             QString deviceName = me->mnt_fsname;
00444             QString mountDir = me->mnt_dir;
00445             QString fsType = me->mnt_type;
00446             if( fsType == "smbfs" && (mountDir.find(mountPoint) != -1 | deviceName.find(mountPoint) != -1))
00447                mounted = true;
00448         }
00449     }
00450     endmntent( mntfp );
00451     return mounted;
00452 }
00453 
00454 QString Qsmb::getMount(const QString &shareName)
00455 {
00456    struct mntent *me;
00457     QString mount;
00458     FILE *mntfp = setmntent( "/etc/mtab", "r" );
00459     if ( mntfp ){
00460         while ( (me = getmntent( mntfp )) != 0 ) {
00461             QString deviceName = me->mnt_fsname;
00462             QString mountDir = me->mnt_dir;
00463             QString fsType = me->mnt_type;
00464             if( fsType == "smbfs" &&  deviceName.find(shareName) != -1)
00465                mount = mountDir;
00466         }
00467     }
00468     endmntent( mntfp );
00469     return mount;
00470 }
00471 
00472 void Qsmb::TextViewClicked(QListViewItem* item)
00473 {
00474    if(item == NULL) return;
00475 
00476    QString text = item->text(2);
00477    qWarning(text);
00478    if( !text.isEmpty())
00479       mountpt->insertItem(text,0);
00480 }

Generated on Sat Nov 5 16:17:40 2005 for OPIE by  doxygen 1.4.2