00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "pmipkg.h"
00011 #include "pksettings.h"
00012 #include "package.h"
00013 #include "packagelistitem.h"
00014
00015
00016 #include <qpe/resource.h>
00017 #include <qpe/config.h>
00018 #include <qpe/stringutil.h>
00019 #include <qpe/qpeapplication.h>
00020 #include <qdir.h>
00021 #include <qfile.h>
00022 #include <qgroupbox.h>
00023 #include <qmultilineedit.h>
00024 #include <qstring.h>
00025 #include <qcheckbox.h>
00026 #include <qtextstream.h>
00027 #include <qtextview.h>
00028 #include <qmessagebox.h>
00029 #include <qprogressbar.h>
00030 #include <qpushbutton.h>
00031 #include <qlayout.h>
00032
00033 #include <stdlib.h>
00034 #include <unistd.h>
00035
00036 #include "mainwindow.h"
00037
00038
00039
00040 PmIpkg::PmIpkg( PackageManagerSettings* s, QWidget* p, const char * name, WFlags f )
00041 : QObject ( p )
00042 {
00043 settings = s;
00044 runwindow = new RunWindow( p, name, true, f );
00045
00046 Config cfg( "oipkg", Config::User );
00047 cfg.setGroup( "ipkg" );
00048 ipkg_cmd = cfg.readEntry( "cmd", "ipkg" )+" ";
00049 }
00050
00051 PmIpkg::~PmIpkg()
00052 {
00053 }
00054
00055 bool PmIpkg::runIpkg(const QString& args, const QString& dest )
00056 {
00057 bool ret=false;
00058 QDir::setCurrent("/tmp");
00059 QString cmd = ipkg_cmd;
00060 pvDebug( 3,"PmIpkg::runIpkg got dest="+dest);
00061 if (!args.contains("update"))
00062 {
00063 if ( dest == "" )
00064 cmd += " -dest "+settings->getDestinationName();
00065 else
00066 cmd += " -dest "+ dest;
00067
00068 cmd += " -force-defaults ";
00069
00070 if ( installDialog && installDialog->_force_depends )
00071 {
00072 if (installDialog->_force_depends->isChecked())
00073 cmd += " -force-depends ";
00074 if (installDialog->_force_reinstall->isChecked())
00075 cmd += " -force-reinstall ";
00076 if (installDialog->_force_remove->isChecked())
00077 cmd += " -force-removal-of-essential-packages ";
00078 if (installDialog->_force_overwrite->isChecked())
00079 cmd += " -force-overwrite ";
00080 }
00081 }
00082
00083 out( "Starting to "+ args+"\n");
00084 qApp->processEvents();
00085 cmd += args;
00086 out( "running:\n"+cmd+"\n" );
00087 pvDebug(2,"running:"+cmd);
00088 qApp->processEvents();
00089 FILE *fp;
00090 char line[130];
00091 QString lineStr, lineStrOld;
00092 sleep(1);
00093 cmd +=" 2>&1";
00094 fp = popen( (const char *) cmd, "r");
00095 if ( fp == NULL ) {
00096 qDebug("Could not execute '" + cmd + "'! err=%d", fp);
00097 out("\nError while executing "+ cmd+"\n\n");
00098 ret = false;
00099 } else {
00100 while ( fgets( line, sizeof line, fp) != NULL)
00101 {
00102 lineStr = line;
00103 lineStr=lineStr.left(lineStr.length()-1);
00104
00105 if (lineStr.contains("Done")) ret = true;
00106 if (lineStr!=lineStrOld)
00107 out(lineStr);
00108 lineStrOld = lineStr;
00109 qApp->processEvents();
00110 }
00111 }
00112 pclose(fp);
00113 pvDebug(2,QString(ret?"success\n":"failure\n"));
00114 return ret;
00115 }
00116
00117 void PmIpkg::makeLinks(OipkgPackage *pack)
00118 {
00119 pvDebug( 2, "PmIpkg::makeLinks "+ pack->name());
00120 QString pn = pack->name();
00121 linkPackage( pack->packageName(), pack->dest() );
00122 }
00123
00124 QStringList* PmIpkg::getList( QString packFileName, QString d )
00125 {
00126 QString dest = settings->getDestinationUrlByName( d );
00127 dest = dest==""?d:dest;
00128
00129 {
00130 Config cfg( "oipkg", Config::User );
00131 cfg.setGroup( "Common" );
00132 QString statusDir = cfg.readEntry( "statusDir", "" );
00133 }
00134 QString packFileDir = dest+"/"+statusDir+"/info/"+packFileName+".list";
00135 QFile f( packFileDir );
00136 qDebug("Try to open %s", packFileDir.latin1());
00137 if ( ! f.open(IO_ReadOnly) )
00138 {
00139 out( "Could not open:\n"+packFileDir );
00140 f.close();
00141 packFileDir = "/"+statusDir+"/info/"+packFileName+".list";
00142 f.setName( packFileDir );
00143 qDebug("Try to open %s", packFileDir.latin1());
00144 if ( ! f.open(IO_ReadOnly) )
00145 {
00146 qDebug(" Panik! Could not open"+packFileDir);
00147 out( "Could not open:\n"+packFileDir+"\n Panik!" );
00148 return (QStringList*)0;
00149 }
00150 }
00151 QStringList *fileList = new QStringList();
00152 QTextStream t( &f );
00153 while ( !t.eof() )
00154 {
00155 *fileList += t.readLine();
00156 }
00157 f.close();
00158 return fileList;
00159 }
00160
00161 void PmIpkg::linkPackage( QString packFileName, QString dest )
00162 {
00163 if (dest == "root" || dest == "/" ) return;
00164 QStringList *fileList = getList( packFileName, dest );
00165 processFileList( fileList, dest );
00166 delete fileList;
00167 }
00168
00169 void PmIpkg::processFileList( QStringList *fileList, QString d )
00170 {
00171 if (!fileList || fileList->isEmpty()) return;
00172 for (uint i=0; i < fileList->count(); i++)
00173 {
00174 QString dest = settings->getDestinationUrlByName( d );
00175 dest = dest==""?d:dest;
00176 processLinkDir( (*fileList)[i], dest );
00177 }
00178 }
00179
00180
00181 void PmIpkg::processLinkDir( QString file, QString dest )
00182 {
00183 pvDebug( 4,"PmIpkg::processLinkDir "+file+" to "+ dest);
00184 if (linkOpp==createLink) pvDebug( 4,"opp: createLink");
00185 if (linkOpp==removeLink) pvDebug( 4,"opp: removeLink");
00186 if ( dest == "???" || dest == "" ) return;
00187 QString destFile = file;
00188 file = dest+"/"+file;
00189 if (file == dest) return;
00190
00191
00192 QFileInfo fileInfo( file );
00193 if ( fileInfo.isDir() )
00194 {
00195 pvDebug(4, "process dir "+file);
00196 QDir destDir( destFile );
00197 if (linkOpp==createLink) destDir.mkdir( destFile, true );
00198 QDir d( file );
00199
00200 const QFileInfoList *list = d.entryInfoList();
00201 QFileInfoListIterator it( *list );
00202 QFileInfo *fi;
00203 while ( (fi=it.current()) )
00204 {
00205 pvDebug(4, "processLinkDir "+fi->absFilePath());
00206 processLinkDir( fi->absFilePath(), dest );
00207 ++it;
00208 }
00209 } else
00210 if ( fileInfo.isFile() )
00211 {
00212 const char *instFile = strdup( (file).latin1() );
00213 const char *linkFile = strdup( (destFile).latin1());
00214 if( linkOpp==createLink )
00215 {
00216 pvDebug(4, "linking: "+file+" -> "+destFile );
00217 symlink( instFile, linkFile );
00218 }
00219 } else {
00220 const char *linkFile = strdup( (destFile).latin1());
00221 if( linkOpp==removeLink )
00222 {
00223 QFileInfo toRemoveLink( destFile );
00224 if ( !QFile::exists( file ) && toRemoveLink.isSymLink() )
00225 {
00226 pvDebug(4,"removing "+destFile+" no "+file);
00227 unlink( linkFile );
00228 }
00229 }
00230 }
00231 }
00232
00233 void PmIpkg::loadList( PackageList *pl )
00234 {
00235 for( OipkgPackage *pack = pl->first();pack ; (pack = pl->next()) )
00236 {
00237 if ( pack && (pack->name() != "") && pack)
00238 {
00239 if ( pack->toInstall() )
00240 to_install.append( pack );
00241 if ( pack->toRemove() )
00242 to_remove.append( pack );
00243 }
00244 }
00245 }
00246
00247 void PmIpkg::commit()
00248 {
00249 int sizecount = 0;
00250 installDialog = new InstallDialog(settings,0,0,true);
00251 installDialog->toRemoveItem->setOpen( true );
00252 installDialog->toInstallItem->setOpen( true );
00253 for (uint i=0; i < to_remove.count(); i++)
00254 {
00255 sizecount += 1;
00256 installDialog->toRemoveItem->insertItem( new PackageListItem(installDialog->ListViewPackages, to_remove.at(i),settings) );
00257 }
00258 for (uint i=0; i < to_install.count(); i++)
00259 {
00260 sizecount += to_install.at(i)->size().toInt();
00261 installDialog->toInstallItem->insertItem( new PackageListItem(installDialog->ListViewPackages, to_install.at(i),settings) );
00262 }
00263 runwindow->progress->setTotalSteps(sizecount);
00264 qDebug("Install size %i",sizecount);
00265 installDialog->showMaximized();
00266 installDialog->show();
00267 if ( installDialog->exec() )
00268 {
00269 doIt();
00270 runwindow->showMaximized();
00271 runwindow->show();
00272 }
00273 installDialog->close();
00274 delete installDialog;
00275 installDialog = 0;
00276 out(tr("\nAll done."));
00277 }
00278
00279 void PmIpkg::doIt()
00280 {
00281 runwindow->progress->setProgress(0);
00282 show();
00283 remove();
00284 install();
00285 }
00286
00287
00288 void PmIpkg::remove()
00289 {
00290 if ( to_remove.count() == 0 ) return;
00291
00292 out(tr("Removing")+"\n"+tr("please wait")+"\n\n");
00293
00294 QStringList *fileList = new QStringList;
00295 for (uint i=0; i < to_remove.count(); i++)
00296 {
00297 if ( to_remove.at(i)->link() ) fileList = getList( to_remove.at(i)->name(), to_remove.at(i)->dest() );
00298 if ( runIpkg("remove " + to_remove.at(i)->installName(), to_remove.at(i)->dest() ))
00299 {
00300 runwindow->progress->setProgress( 1 );
00301 linkOpp = removeLink;
00302 to_remove.at(i)->processed();
00303 pvDebug(3,"link "+QString::number(i));
00304 if ( to_remove.at(i)->link() )
00305 processFileList( fileList, to_remove.at(i)->dest() );
00306
00307
00308
00309 out("\n");
00310 }else{
00311 out(tr("Error while removing ")+to_remove.at(i)->name()+"\n");
00312 if ( to_remove.at(i)->link() )
00313 processFileList( fileList, to_remove.at(i)->dest() );
00314 }
00315 if ( to_remove.at(i)->link() )
00316 processFileList( fileList, to_remove.at(i)->dest() );
00317 if ( to_remove.at(i)->link() )delete fileList;
00318 }
00319 to_remove.clear();
00320 out("\n");
00321 }
00322
00323
00324 void PmIpkg::install()
00325 {
00326 if ( to_install.count() == 0 ) return;
00327 out(tr("Installing")+"\n"+tr("please wait")+"\n");
00328 for (uint i=0; i < to_install.count(); i++)
00329 {
00330 qDebug("install loop %i of %i installing %s",i,to_install.count(),to_install.at(i)->installName().latin1());
00331 if (to_install.at(i)->link())
00332 {
00333
00334
00335 QString rds = settings->getDestinationUrlByName("root");
00336 QString lds = settings->getDestinationUrlByName(to_install.at(i)->dest());
00337 QString listFile = "usr/lib/ipkg/lists/"+to_install.at(i)->name()+".list";
00338 rds += listFile;
00339 lds += listFile;
00340 const char *rd = rds.latin1();
00341 const char *ld = lds.latin1();
00342 pvDebug(4, "linking: "+rds+" -> "+lds );
00343 symlink( rd, ld );
00344 }
00345 if ( runIpkg("install " + to_install.at(i)->installName(), to_install.at(i)->dest() ))
00346 {
00347 runwindow->progress->setProgress( to_install.at(i)->size().toInt() + runwindow->progress->progress());
00348 to_install.at(i)->processed();
00349 linkOpp = createLink;
00350 if ( to_install.at(i)->link() )
00351 makeLinks( to_install.at(i) );
00352
00353 out("\n");
00354 }else{
00355 out(tr("Error while installing")+to_install.at(i)->name()+"\n");
00356 linkOpp = createLink;
00357 if ( to_install.at(i)->link() )
00358 makeLinks( to_install.at(i) );
00359 }
00360 }
00361 out("\n");
00362 to_install.clear();
00363 }
00364
00365 void PmIpkg::createLinks( const QString &dest )
00366 {
00367 pvDebug(2,"PmIpkg::createLinks "+dest);
00368 linkOpp=createLink;
00369 QString url = settings->getDestinationUrlByName( dest );
00370 url = url==""?dest:url;
00371 processLinkDir( "/opt", url );
00372 processLinkDir( "/usr", url );
00373 }
00374
00375 void PmIpkg::removeLinks( const QString &dest )
00376 {
00377 pvDebug(2,"PmIpkg::removeLinks "+dest);
00378 linkOpp=removeLink;
00379 QString url = settings->getDestinationUrlByName( dest );
00380 url = url==""?dest:url;
00381 processLinkDir( "/opt", url );
00382 processLinkDir( "/usr", url );
00383 }
00384
00385 void PmIpkg::update()
00386 {
00387 show();
00388 runIpkg( "update" );
00389 }
00390
00391 void PmIpkg::out( QString o )
00392 {
00393
00394 runwindow->outPut->setText(runwindow->outPut->text()+o);
00395 runwindow->outPut->setCursorPosition(runwindow->outPut->numLines() + 1,0,FALSE);
00396 }
00397
00398
00399
00400
00401 void PmIpkg::show()
00402 {
00403 if (!runwindow->isVisible())
00404 {
00405 runwindow->showMaximized();
00406 runwindow->show();
00407 }
00408 runwindow->outPut->setText("");
00409 }
00410
00411 void PmIpkg::installFile(const QString &fileName, const QString &dest)
00412 {
00413
00414 to_install.clear();
00415 to_remove.clear();
00416 pvDebug( 2,"PmIpkg::installFile "+ fileName);
00417 OipkgPackage *p = new OipkgPackage(fileName,settings);
00418 if ( dest!="") p->setDest( dest );
00419 to_install.append( p );
00420 commit();
00421 delete p;
00422 }
00423
00424 void PmIpkg::removeFile(const QString &fileName, const QString &dest)
00425 {
00426
00427 to_install.clear();
00428 to_remove.clear();
00429 pvDebug( 2,"PmIpkg::removeFile "+ fileName);
00430 OipkgPackage *p = new OipkgPackage(fileName,settings);
00431 if ( dest!="") p->setDest( dest );
00432 to_remove.append( p );
00433 commit();
00434 delete p;
00435 }
00436
00437
00438 void PmIpkg::clearLists()
00439 {
00440 to_remove.clear();
00441 to_install.clear();
00442 }
00443
00444