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

pksettings.cpp

Go to the documentation of this file.
00001 #include "pksettings.h"
00002 
00003 #include <qpe/process.h>
00004 #include <qpe/resource.h>
00005 #include <qpe/stringutil.h>
00006 #include <qpe/qpeapplication.h>
00007 #include <qpe/qcopenvelope_qws.h>
00008 #include <qpe/applnk.h>
00009 #include <qpe/config.h>
00010 
00011 #include <qprogressbar.h>
00012 #include <qcombobox.h>
00013 #include <qdict.h>
00014 #include <qfile.h>
00015 #include <qlineedit.h>
00016 #include <qpushbutton.h>
00017 #include <qlistview.h>
00018 #include <qlistbox.h>
00019 #include <qcheckbox.h>
00020 #include <qmessagebox.h>
00021 #include <qpainter.h>
00022 #include <qpixmap.h>
00023 #include <qregexp.h>
00024 #include <qstring.h>
00025 #include <qobject.h>
00026 #include <qtextstream.h>
00027 #include <qtextview.h>
00028 #include <qtoolbutton.h>
00029 #include <qtabwidget.h>
00030 
00031 #include <stdlib.h>
00032 #include <unistd.h>
00033 #include "debug.h"
00034 //#include "utils.h"
00035 
00036 PackageManagerSettings::PackageManagerSettings( QWidget* parent,  const char* name, WFlags fl )
00037   : PackageManagerSettingsBase( parent, name, fl )
00038 {
00039   connect( newserver, SIGNAL(clicked()), this, SLOT(newServer()) );
00040   connect( removeserver, SIGNAL(clicked()), this, SLOT(removeServer()) );
00041   connect( servers, SIGNAL(highlighted(int)), this, SLOT(editServer(int)) );
00042   connect( newdestination, SIGNAL(clicked()), this, SLOT(newDestination()) );
00043   connect( removedestination, SIGNAL(clicked()), this, SLOT(removeDestination()) );
00044   connect( destinations, SIGNAL(highlighted(int)), this, SLOT(editDestination(int)) );
00045   //   connect( CheckBoxLink, SIGNAL(toggled(bool)),
00046   //                                     activeLinkDestination, SLOT(setEnabled(bool)) );
00047 
00048 //  connect( settingName, SIGNAL(activated(int)), this, SLOT(installationSettingChange(int)) );
00049 //  connect( settingName, SIGNAL(textChanged(const QString&)), this, SLOT(installationSettingSetName(const QString&)) );
00050 //  connect( newsetting, SIGNAL(clicked()), this, SLOT(newInstallationSetting()) );
00051 //  connect( renamesetting, SIGNAL(clicked()), this, SLOT(renameInstallationSetting()) );
00052  // connect( removesetting, SIGNAL(clicked()), this, SLOT(removeInstallationSetting()) );
00053   servername->setEnabled(FALSE);
00054   serverurl->setEnabled(FALSE);
00055   serverurlDic.setAutoDelete(TRUE);
00056   destinationname->setEnabled(FALSE);
00057   destinationurl->setEnabled(FALSE);
00058   destinationurlDic.setAutoDelete(TRUE);
00059   readSettings();
00060         activeLinkDestination->hide();
00061   serverChanged = false;
00062 
00063   // get rid of setups
00064 //  Settings->hide();
00065 //  settingName->hide();
00066 //  newsetting->hide();
00067 //  renamesetting->hide();
00068 //  removesetting->hide();
00069 }
00070 
00071 PackageManagerSettings::~PackageManagerSettings()
00072 {
00073 }
00074 
00075 
00076 void PackageManagerSettings::newServer()
00077 {
00078   int i = servers->count();
00079   if ( servername->isEnabled() || serverurl->text().isEmpty() ) {
00080     serverurlDic.insert(i,new QString("http://"));
00081     servers->insertItem(tr("New"));
00082     activeServers->insertItem(tr("New"));
00083   } else {
00084     // allows one-level undo
00085     serverurlDic.insert(i,new QString(serverurl->text()));
00086     servers->insertItem(servername->text());
00087     activeServers->insertItem(servername->text());
00088   }
00089   changed = true;
00090   servers->setSelected(i,TRUE);
00091   editServer(i);
00092   changed = true;
00093 }
00094 
00095 void PackageManagerSettings::newDestination()
00096 {
00097   int i = destinations->count();
00098   if ( destinationname->isEnabled() || destinationurl->text().isEmpty() ) {
00099     destinationurlDic.insert(i,new QString("/"));
00100     destinations->insertItem(tr("New"));
00101     activeDestination->insertItem(tr("New"));
00102     activeLinkDestination->insertItem(tr("New"));
00103   } else {
00104     // allows one-level undo
00105     destinationurlDic.insert(i,new QString(destinationurl->text()));
00106     destinations->insertItem(destinationname->text());
00107     activeDestination->insertItem(destinationname->text());
00108     activeLinkDestination->insertItem(destinationname->text());
00109   }
00110   destinations->setSelected(i,TRUE);
00111   editDestination(i);
00112   changed = true;
00113 }
00114 
00115 
00116 void PackageManagerSettings::editServer(int i)
00117 {
00118   if ( servername->isEnabled() ) {
00119     disconnect( servername, SIGNAL(textChanged(const QString&)), this, SLOT(serverNameChanged(const QString&)) );
00120     disconnect( serverurl, SIGNAL(textChanged(const QString&)), this, SLOT(serverUrlChanged(const QString&)) );
00121   } else {
00122     servername->setEnabled(TRUE);
00123     serverurl->setEnabled(TRUE);
00124   }
00125 
00126   servername->setText( servers->text(i) );
00127   serverurl->setText( *serverurlDic[i] );
00128 
00129   editedserver = i;
00130   serverChanged = true;
00131   connect( servername, SIGNAL(textChanged(const QString&)), this, SLOT(serverNameChanged(const QString&)) );
00132   connect( serverurl, SIGNAL(textChanged(const QString&)), this, SLOT(serverUrlChanged(const QString&)) );
00133   changed = true;
00134 }
00135 
00136 
00137 void PackageManagerSettings::editDestination(int i)
00138 {
00139   if ( destinationname->isEnabled() ) {
00140     disconnect( destinationname, SIGNAL(textChanged(const QString&)), this, SLOT(destNameChanged(const QString&)) );
00141     disconnect( destinationurl, SIGNAL(textChanged(const QString&)), this, SLOT(destUrlChanged(const QString&)) );
00142   } else {
00143     destinationname->setEnabled(TRUE);
00144     destinationurl->setEnabled(TRUE);
00145         //since it does not work anyway
00146 //    createLinksButton->setEnabled(TRUE);
00147 //    removeLinksButton->setEnabled(TRUE);
00148   }
00149 
00150   destinationname->setText( destinations->text(i) );
00151   destinationurl->setText( *destinationurlDic[i] );
00152 
00153   editeddestination = i;
00154 
00155   connect( destinationname, SIGNAL(textChanged(const QString&)), this, SLOT(destNameChanged(const QString&)) );
00156   connect( destinationurl, SIGNAL(textChanged(const QString&)), this, SLOT(destUrlChanged(const QString&)) );
00157   changed = true;
00158 }
00159 
00160 void PackageManagerSettings::removeServer()
00161 {
00162   disconnect( servername, SIGNAL(textChanged(const QString&)), this, SLOT(serverNameChanged(const QString&)) );
00163   disconnect( serverurl, SIGNAL(textChanged(const QString&)), this, SLOT(serverUrlChanged(const QString&)) );
00164   servername->setText(servers->text(editedserver));
00165   serverurl->setText(*serverurlDic[editedserver]);
00166   disconnect( servers, SIGNAL(highlighted(int)), this, SLOT(editServer(int)) );
00167   servers->removeItem(editedserver);
00168   activeServers->removeItem(editedserver);
00169   connect( servers, SIGNAL(highlighted(int)), this, SLOT(editServer(int)) );
00170   servername->setEnabled(FALSE);
00171   serverurl->setEnabled(FALSE);
00172   changed = true;
00173 }
00174 
00175 void PackageManagerSettings::removeDestination()
00176 {
00177   disconnect( destinationname, SIGNAL(textChanged(const QString&)), this, SLOT(destNameChanged(const QString&)) );
00178   disconnect( destinationurl, SIGNAL(textChanged(const QString&)), this, SLOT(destUrlChanged(const QString&)) );
00179   destinationname->setText("");
00180   destinationurl->setText("");
00181   disconnect( destinations, SIGNAL(highlighted(int)), this, SLOT(editDestination(int)) );
00182   destinations->removeItem(editeddestination);
00183   activeDestination->removeItem(editeddestination);
00184   activeLinkDestination->removeItem(editeddestination);
00185   editeddestination=0;
00186   connect( destinations, SIGNAL(highlighted(int)), this, SLOT(editDestination(int)) );
00187   destinationname->setEnabled(FALSE);
00188   destinationurl->setEnabled(FALSE);
00189   changed = true;
00190 }
00191 
00192 void PackageManagerSettings::serverNameChanged(const QString& t)
00193 {
00194   disconnect( servers, SIGNAL(highlighted(int)), this, SLOT(editServer(int)) );
00195   servers->changeItem( t, editedserver );
00196   activeServers->changeItem( t, editedserver );
00197   connect( servers, SIGNAL(highlighted(int)), this, SLOT(editServer(int)) );
00198   changed = true;
00199   serverChanged = true;
00200 }
00201 
00202 void PackageManagerSettings::destNameChanged(const QString& t)
00203 {
00204   disconnect( destinations, SIGNAL(highlighted(int)), this, SLOT(editDestination(int)) );
00205   destinations->changeItem( t, editeddestination );
00206   activeDestination->changeItem( t, editeddestination );
00207   activeLinkDestination->changeItem( t, editeddestination );
00208   connect( destinations, SIGNAL(highlighted(int)), this, SLOT(editDestination(int)) );
00209   changed = true;
00210 }
00211 
00212 void PackageManagerSettings::serverUrlChanged(const QString& t)
00213 {
00214   serverurlDic.replace(editedserver, new QString(t));
00215   changed = true;
00216   serverChanged = true;
00217 }
00218 
00219 void PackageManagerSettings::destUrlChanged(const QString& t)
00220 {
00221   destinationurlDic.replace(editeddestination, new QString(t));
00222   changed = true;
00223 }
00224 
00225 void PackageManagerSettings::writeIpkgConfig(const QString& conffile)
00226 {
00227   QFile conf(conffile);
00228   if ( ! conf.open(IO_WriteOnly) ) return;
00229   QTextStream s(&conf);
00230   s << "# Written by oipkg -- the opie package manager\n";
00231   for (int i=0; i<(int)activeServers->count(); i++)
00232     {
00233       QString url = serverurlDic[i] ? *serverurlDic[i] : QString("???");
00234       if ( !activeServers->isSelected(i) ) s << "#";
00235       s << "src " << activeServers->text(i) << " " << url << "\n";
00236     }
00237   for (int i=0; i<(int)destinations->count(); i++)
00238     {
00239       QString url = destinationurlDic[i] ? *destinationurlDic[i] : QString("???");
00240       s << "dest " << destinations->text(i) << " " << url << "\n";
00241     }
00242   conf.close();
00243 }
00244 
00245 
00246 void PackageManagerSettings::readInstallationSettings()
00247 {
00248   Config cfg( "oipkg", Config::User );
00249   cfg.setGroup( "Settings" );
00250 
00251   installationSettingsCount = cfg.readNumEntry( "count", -1 );
00252   currentSetting  = cfg.readNumEntry( "current", 0 );// o should be -1
00253 
00254 //  for (int i = 0; i < installationSettingsCount; i++)
00255 //    {
00256 //      cfg.setGroup( "Setting_" + QString::number(i) );
00257 //      settingName->insertItem( cfg.readEntry( "name", "???" ), i );
00258 //    };
00259   readInstallationSetting( currentSetting );
00260 }
00261 
00262 
00263 
00265 // *  remove from conf file
00266 // */
00267 //void PackageManagerSettings::removeInstallationSetting()
00268 //{
00269 //  settingName->removeItem( settingName->currentItem() );
00270 //  Config cfg( "oipkg", Config::User );
00271 //  cfg.setGroup( "Setting_" + QString::number( installationSettingsCount ) );
00272 //  cfg.clearGroup();
00273 //  installationSettingsCount--;
00274 //  changed = true;
00275 //  settingName->setEditable( false );
00276 //}
00277 
00279 // *  write to confgile
00280 // */
00281 //void PackageManagerSettings::newInstallationSetting()
00282 //{
00283 //  installationSettingsCount++;
00284 //  settingName->insertItem( "New", installationSettingsCount );
00285 //  settingName->setCurrentItem( installationSettingsCount );
00286 //  settingName->setEditable( true );
00287 //  changed = true;
00288 //}
00289 
00290 //void PackageManagerSettings::installationSettingChange(int cs)
00291 //{
00292 //  writeCurrentInstallationSetting();
00293 //  currentSetting = cs;
00294 //  readInstallationSetting( cs );
00295 //  changed = true;
00296 //}
00297 
00298 void PackageManagerSettings::writeInstallationSettings()
00299 {
00300   {
00301     Config cfg( "oipkg", Config::User );
00302     cfg.setGroup( "Settings" );
00303     cfg.writeEntry( "count", installationSettingsCount );
00304     cfg.writeEntry( "current", currentSetting );
00305   }
00306   writeCurrentInstallationSetting();
00307 }
00308 
00309 
00310 void PackageManagerSettings::readInstallationSetting(int setting)
00311 {
00312   if ( setting < 0 ) return;
00313   Config cfg( "oipkg", Config::User );
00314   cfg.setGroup( "Setting_" + QString::number( setting ) );
00315   CheckBoxLink->setChecked( cfg.readBoolEntry( "link", true ) );
00316   QString dest = cfg.readEntry( "dest" );
00317   QString linkdest = cfg.readEntry( "linkdest" );
00318   pvDebug(3, "dest="+dest);
00319   pvDebug(3, "linkdest="+linkdest);
00320   for ( int i = 0; i < activeDestination->count(); i++)
00321   {
00322         if ( activeDestination->text( i ) == dest )
00323                         activeDestination->setCurrentItem( i );
00324                 if ( activeLinkDestination->text( i ) == linkdest )
00325                         activeLinkDestination->setCurrentItem( i );
00326    }
00327 }
00328 
00329 void PackageManagerSettings::writeCurrentInstallationSetting()
00330 {
00331   Config cfg( "oipkg", Config::User );
00332   cfg.setGroup( "Setting_"  + QString::number(currentSetting) );
00333   cfg.writeEntry( "link", CheckBoxLink->isChecked() );
00334   cfg.writeEntry( "dest", getDestinationName() );
00335   cfg.writeEntry( "linkdest" , getLinkDestinationName() );
00336   QStringList sers = getActiveServers();
00337   int srvc = 0;
00338   for ( QStringList::Iterator it = sers.begin(); it != sers.end(); ++it ) {
00339     cfg.writeEntry( "server_" + QString::number(srvc++), *it );         
00340   }
00341   cfg.writeEntry( "server_count", srvc );
00342 }
00343 
00344 //void PackageManagerSettings::renameInstallationSetting()
00345 //{
00346 //  settingName->setEditable( true );
00347 //  changed = true;
00348 //}
00349 
00350 //void PackageManagerSettings::installationSettingSetName(const QString &name)
00351 //{
00352 //  settingName->changeItem( name,  settingName->currentItem() );
00353 //  changed = true;
00354 //}
00355 
00356 
00357 bool PackageManagerSettings::readIpkgConfig(const QString& conffile)
00358 {
00359   QFile conf(conffile);
00360   changed = false;
00361   if ( conf.open(IO_ReadOnly) ) {
00362     QTextStream s(&conf);
00363     servers->clear();
00364     activeServers->clear();
00365     destinations->clear();
00366     activeDestination->clear();
00367     activeLinkDestination->clear();
00368     serverurlDic.clear();
00369     destinationurlDic.clear();
00370     ipkg_old=0;
00371     int currentserver=0;
00372     while ( !s.atEnd() ) {
00373       QString l = s.readLine();
00374       QStringList token = QStringList::split(' ', l);
00375       if ( token[0] == "src" || token[0] == "#src" ) {
00376         currentserver=servers->count();
00377         serverurlDic.insert(servers->count(),new QString(token[2]));
00378         int a = token[0] == "src" ? 1 : 0;
00379         int i = servers->count();
00380         servers->insertItem(token[1]);
00381         activeServers->insertItem( token[1] );
00382         activeServers->setSelected(i,a);
00383       } else if ( token[0] == "dest" ) {
00384         currentserver=destinations->count();
00385         destinationurlDic.insert(destinations->count(),new QString(token[2]));
00386         destinations->insertItem(token[1]);
00387         activeDestination->insertItem( token[1] );
00388         activeLinkDestination->insertItem( token[1] );
00389 
00390       } else if ( token[0] == "option" ) {
00391         // ### somehow need to use the settings from netsetup
00392         //              if ( token[1] == "http_proxy" )
00393         //                  http->setText(token[2]);
00394         //              else if ( token[1] == "ftp_proxy" )
00395         //                  ftp->setText(token[2]);
00396         //              else if ( token[1] == "proxy_username" )
00397         //                  username->setText(token[2]);
00398         //              else if ( token[1] == "proxy_password" )
00399         //                  password->setText(token[2]);
00400       } else {
00401         // Old style?
00402         int eq = l.find('=');
00403         if ( eq >= 0 ) {
00404           QString v = l.mid(eq+1).stripWhiteSpace();
00405           if ( v[0] == '"' || v[0] == '\'' ) {
00406             int cl=v.find(v[0],1);
00407             if ( cl >= 0 )
00408               v = v.mid(1,cl-1);
00409           }
00410           if ( l.left(12) == "IPKG_SOURCE=" ) {
00411             ipkg_old=1;
00412             currentserver=servers->count();
00413             serverurlDic.insert(servers->count(),new QString(v));
00414             servers->insertItem(v);
00415           } else if ( l.left(13) == "#IPKG_SOURCE=" ) {
00416             serverurlDic.insert(servers->count(),new QString(v));
00417             servers->insertItem(v);
00418           } else if ( l.left(10) == "IPKG_ROOT=" ) {
00419             // ### no UI
00420             //              } else if ( l.left(20) == "IPKG_PROXY_USERNAME=" ) {
00421             //                  username->setText(v);
00422             //              } else if ( l.left(20) == "IPKG_PROXY_PASSWORD=" ) {
00423             //                  password->setText(v);
00424             //              } else if ( l.left(16) == "IPKG_PROXY_HTTP=" ) {
00425             //                  http->setText(v);
00426             //              } else if ( l.left(16) == "IPKG_PROXY_FTP=" ) {
00427             //                  ftp->setText(v);
00428           }
00429         }
00430       }
00431     }
00432     if ( ipkg_old ) {
00433       servers->setSelectionMode(QListBox::Single);
00434       servers->setSelected(currentserver,TRUE);
00435     }
00436     return TRUE;
00437   } else {
00438     return FALSE;
00439   }
00440 }
00441 
00442 
00446 void PackageManagerSettings::readSettings()
00447 {
00448   readIpkgConfig("/etc/ipkg.conf");
00449   readInstallationSettings();
00450 }
00451 
00452 void PackageManagerSettings::writeSettings()
00453 {
00454   if ( changed ) writeIpkgConfig("/etc/ipkg.conf");
00455   writeInstallationSettings();
00456 }
00458 bool PackageManagerSettings::showDialog( int i )
00459 {
00460   TabWidget->setCurrentPage( i );
00461   showMaximized();
00462   bool ret = exec();
00463   if ( ret ) writeSettings();
00464   else readSettings();
00465   return (changed && ret);
00466 }
00468 QString PackageManagerSettings::getDestinationName()
00469 {
00470   return activeDestination->currentText();
00471 }
00473 QString PackageManagerSettings::getLinkDestinationName()
00474 {
00475   return activeLinkDestination->currentText();
00476 }
00478 QString PackageManagerSettings::getDestinationUrl()
00479 {
00480   int dnr = activeDestination->currentItem();
00481   return *destinationurlDic.find(dnr);
00482 }
00484 bool PackageManagerSettings::createLinks()
00485 {
00486   return CheckBoxLink->isChecked();
00487 }
00489 QStringList PackageManagerSettings::getActiveServers()
00490 {
00491   QStringList sl;       
00492   for (int i=0; i<(int)activeServers->count(); i++)
00493     {
00494       if ( activeServers->isSelected(i) )
00495         sl += activeServers->text(i);
00496     }
00497   return sl;
00498 }
00499  QStringList PackageManagerSettings::getServers()
00500 {
00501   QStringList sl;       
00502   for (int i=0; i<(int)activeServers->count(); i++)
00503     {
00504                         sl += activeServers->text(i);
00505     }
00506   return sl;
00507 }
00508 
00510 QStringList PackageManagerSettings::getDestinationUrls()
00511 {
00512   QStringList sl;       
00513   for (int i=0; i<(int)destinations->count(); i++)
00514     {           
00515       sl += *destinationurlDic[i];
00516     }
00517   return sl;
00518 }
00519 
00521 QString PackageManagerSettings::getDestinationUrlByName(QString n)
00522 {
00523   QStringList sl;       
00524   for (int i=0; i<(int)destinations->count(); i++)
00525     {           
00526       if ( n ==  destinations->text(i)) return*destinationurlDic[i];
00527     }
00528   return "";
00529 }
00530 
00532 QStringList PackageManagerSettings::getDestinationNames()
00533 {
00534   QStringList sl;       
00535   for (int i=0; i<(int)destinations->count(); i++)
00536     {           
00537       sl += destinations->text(i);
00538     }
00539   return sl;
00540 }
00541                                
00542 
00543 void PackageManagerSettings::linkEnabled( bool b )
00544 {
00545         pvDebug(2,"PackageManagerSettings::linkEnabled "+QString(b?"yes":"no"));
00546   activeLinkDestination->setEnabled( b );
00547   CheckBoxLink->setChecked( b );
00548 }
00549 
00550 void PackageManagerSettings::activeServerChanged()
00551 {
00552         changed = true;
00553 }
00554 
00555 void PackageManagerSettings::createLinksToDest()
00556 {
00557         pvDebug(2,"creating links...");
00558 //      emit doCreateLinks( destinationurl->text() );
00559 //      ipkg->createLinks( destinationurl );
00560 }
00561 
00562 void PackageManagerSettings::removeLinksToDest()
00563 {
00564         pvDebug(2,"removing links...");
00565 //      emit doRemoveLinks( destinationurl->text() );
00566 //      ipkg->removeLinks( destinationurl );
00567 }
00568 
00569 void PackageManagerSettings::activeDestinationChange(int i)
00570 {
00571         pvDebug(5,"activeDestinationChange "+QString::number(i));
00572         if (i > activeDestination->count()) return;
00573         activeDestination->setCurrentItem(i);
00574         pvDebug(5,"dest name "+ activeDestination->currentText());
00575 }

Generated on Sat Nov 5 16:18:10 2005 for OPIE by  doxygen 1.4.2