Main | License | Installation | FAQ | Screenshots | Contact | Links | Sf.net | Freshmeat.net | KDE-Apps.org
Requirements | Design | Modules | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

connectDialog.ui.h

Go to the documentation of this file.
00001 /* Copyright (C) 2002, 2003, 2004, 2005 Jan Wedekind.
00002    This file is part of the recipe database application AnyMeal.
00003 
00004    AnyMeal is free software; you can redistribute it and/or modify it under
00005    the terms of the GNU GENERAL PUBLIC LICENSE as published by the Free
00006    Software Foundation; either version 3 of the License, or (at your option)
00007    any later version.
00008 
00009    AnyMeal is distributed in the hope that it will be useful, but WITHOUT ANY
00010    WARRANTY; without even the implied warranty of MERCHANTIBILITY or FITNESS
00011    FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
00012    details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
00016 
00017 #ifndef NDEBUG
00018 #include <iostream>
00019 #endif
00020 
00021 void ConnectDialog::init()
00022 {
00023 #ifndef NDEBUG
00024   std::cerr << "Initialising ConnectDialog" << std::endl;
00025 #endif
00026   wallet = boost::shared_ptr< KWallet::Wallet >
00027     ( KWallet::Wallet::openWallet( KWallet::Wallet::LocalWallet(), winId() ) );
00028 
00029   if ( wallet ) {
00030     ERRORMACRO( wallet->createFolder( "anyMeal" ), Error, ,
00031                 "Could not create folder \"anyMeal\" of the wallet "
00032                 "\"kdewallet\"." );
00033     ERRORMACRO( wallet->setFolder( "anyMeal" ), Error, ,
00034                 "Could not open folder \"anyMeal\" of the wallet "
00035                 "\"kdewallet\"." );
00036 
00037     // First read user-map:
00038     // users: server x database -> (users)
00039     wallet->readMap( "users", users );
00040 
00041     // Read the database-map:
00042     // servers: server -> (database)
00043     // The slots will fill in all entries now.
00044     if ( wallet->readMap( "databases", servers ) == 0 ) {
00045       for ( QMap< QString, QString >::const_iterator i = servers.begin();
00046             i != servers.end(); i++ )
00047         serverCombo->insertItem( i.key() );
00048     };
00049 
00050     // Read the map of sockets:
00051     // sockets: socket -> (database)
00052     // The slots will fill in all entries now.
00053     if ( wallet->readMap( "sockets", sockets ) == 0 ) {
00054       for ( QMap< QString, QString >::const_iterator i = sockets.begin();
00055             i != sockets.end(); i++ )
00056         socketCombo->insertItem( i.key() );
00057     };
00058     
00059   };
00060 
00061   if ( serverCombo->count() == 0 )
00062     serverCombo->insertItem( "localhost" );
00063 
00064   if ( socketCombo->count() == 0 ) {
00065     const char *home = getenv( "HOME" );
00066 #ifndef NDEBUG
00067     std::cerr << "HOME = " << home << std::endl;
00068 #endif
00069     if ( home != NULL )
00070       socketCombo->insertItem( QString( home ) +
00071                                "/.kde/share/apps/anymeal/db/mysql.sock" );
00072   };
00073   serverWidgetStack->raiseWidget( serverTypeCombo->currentItem() );
00074 }
00075 
00076 void ConnectDialog::createDataSource()
00077 {
00078   DatabaseWizard databaseWizard( this, "databaseWizard" );
00079 
00080   if ( databaseWizard.exec() == QDialog::Accepted ) {
00081 
00082     serverTypeCombo->setCurrentItem( databaseWizard.serverTypeCombo->
00083                                      currentItem() );
00084     serverCombo->setCurrentText( databaseWizard.serverEdit->text() );
00085     socketCombo->setCurrentText( databaseWizard.socketEdit->text() );
00086     databaseCombo->setCurrentText( databaseWizard.databaseEdit->text() );
00087     userCombo->setCurrentText( databaseWizard.databaseUserEdit->text() );
00088     passWordCheckBox->setChecked( databaseWizard.databasePassWordCheckBox->
00089                                   isChecked() );
00090     passWordEdit->setText( databaseWizard.databasePassWordEdit->text() );
00091     accept();
00092 
00093   };
00094 }
00095 
00096 void ConnectDialog::setDatabase( const QString &text )
00097 {
00098   if ( wallet ) {
00099     QString key( sourceKey() + '/' + text );
00100     if ( users.contains( key ) ) {
00101       QStringList list( QStringList::split( ',', users[ key ] ) );
00102 #ifndef NDEBUG
00103       std::cerr << "Selecting database '" << key
00104                 << "' with following known users: ";
00105       for ( int i=0; i<(signed)list.size(); i++ )
00106         std::cerr << ' ' << list[ i ];
00107       std::cerr << " (extracted from " << users[ key ] << ')'
00108                 << std::endl;
00109 #endif
00110       userCombo->clear();
00111       userCombo->insertStringList( list );
00112     } else {
00113       userCombo->clear();
00114       const char *user = getenv( "USER" );
00115       if ( user != NULL )
00116         userCombo->insertItem( user );
00117 #ifndef NDEBUG
00118       std::cerr << "No users-entry for '" << key << '\'' << std::endl;
00119 #endif
00120     };
00121   } else {
00122     userCombo->clear();
00123     const char *user = getenv( "USER" );
00124     if ( user != NULL )
00125       userCombo->insertItem( user );
00126   };
00127 }
00128 
00129 void ConnectDialog::updateWallet()
00130 {
00131   if ( wallet ) {
00132 
00133     if ( serverTypeCombo->currentItem() == 0 ) {
00134       QStringList list;
00135       if ( servers.contains( serverCombo->currentText() ) )
00136         list = QStringList::split( ',',
00137                                    servers[ serverCombo->currentText() ] );
00138       if ( list.find( databaseCombo->currentText() ) == list.end() )
00139         list.push_back( databaseCombo->currentText() );
00140       servers[ serverCombo->currentText() ] = list.join( "," );
00141       wallet->writeMap( "databases", servers );
00142     } else {
00143       QStringList list;
00144       if ( sockets.contains( socketCombo->currentText() ) )
00145         list = QStringList::split( ',',
00146                                    sockets[ socketCombo->currentText() ] );
00147 #ifndef NDEBUG
00148       std::cerr << "sockets = " << sockets[ socketCombo->currentText() ]
00149                 << std::endl;
00150 #endif
00151       if ( list.find( databaseCombo->currentText() ) == list.end() )
00152         list.push_back( databaseCombo->currentText() );
00153       sockets[ socketCombo->currentText() ] = list.join( "," );
00154 #ifndef NDEBUG
00155       std::cerr << "updated sockets = " << sockets[ socketCombo->currentText() ]
00156                 << std::endl;
00157 #endif
00158       wallet->writeMap( "sockets", sockets );
00159     };
00160 
00161     {
00162       QString key( sourceKey() + '/' + databaseCombo->currentText() );
00163       QStringList list;
00164       if ( users.contains( key ) )
00165         list = QStringList::split( ',', users[ key ] );
00166       if ( list.find( userCombo->currentText() ) == list.end() )
00167         list.push_back( userCombo->currentText() );
00168       users[ key ] = list.join( "," );
00169       wallet->writeMap( "users", users );
00170     };
00171     
00172     {
00173       QString key( QString( "password/" ) +
00174                    sourceKey() + '/' + userCombo->currentText() );
00175       if ( passWordCheckBox->isChecked() )
00176         wallet->writePassword( key, passWordEdit->text() );
00177       else
00178         wallet->writePassword( key, "" );
00179     };
00180 
00181   };
00182 }
00183 
00184 
00185 void ConnectDialog::setServer( const QString &server )
00186 {
00187   if ( serverTypeCombo->currentItem() == 0 ) {
00188     if ( servers.contains( server ) ) {
00189       QStringList list( QStringList::split( ',', servers[ server ] ) );
00190       databaseCombo->clear();
00191       databaseCombo->insertStringList( list );
00192 #ifndef NDEBUG
00193       std::cerr << "Selecting server '" << server
00194                 << "' with following known databases: ";
00195       for ( int i=0; i<(signed)list.size(); i++ )
00196         std::cerr << ' ' << list[ i ];
00197       std::cerr << " (extracted from " << servers[ server ] << ')'
00198                 << std::endl;
00199 #endif
00200     } else {
00201       databaseCombo->clear();
00202       databaseCombo->insertItem( "anymeal" );
00203     };
00204   };
00205 }
00206 
00207 
00208 void ConnectDialog::setSocket( const QString &socket )
00209 {
00210   if ( serverTypeCombo->currentItem() == 1 ) {
00211     if ( sockets.contains( socket ) ) {
00212       QStringList list( QStringList::split( ',', sockets[ socket ] ) );
00213       databaseCombo->clear();
00214       databaseCombo->insertStringList( list );
00215 #ifndef NDEBUG
00216       std::cerr << "Selecting socket '" << socket
00217                 << "' with following known databases: ";
00218       for ( int i=0; i<(signed)list.size(); i++ )
00219         std::cerr << ' ' << list[ i ];
00220       std::cerr << " (extracted from " << sockets[ socket ] << ')'
00221                 << std::endl;
00222 #endif
00223     } else {
00224       databaseCombo->clear();
00225       databaseCombo->insertItem( "anymeal" );
00226     };
00227   };
00228   std::string socketstr( (const char *)socket );
00229   unsigned int pos = socketstr.find_last_of( "/" );
00230   if ( pos != std::string::npos )
00231     folderEdit->setText( std::string( socketstr, 0, pos ).c_str() );
00232 
00233 }
00234 
00235 
00236 void ConnectDialog::setUser( const QString &text )
00237 {
00238   if ( wallet ) {
00239     // Read password using constructed variable-name.
00240     // password: server x user -> password
00241     QString key( QString( "password/" ) + sourceKey() + '/' + text );
00242     QString password;
00243     wallet->readPassword( key, password );
00244     passWordEdit->setText( password );
00245     passWordCheckBox->setChecked( !password.isEmpty() );
00246   } else {
00247     passWordCheckBox->setChecked( false );
00248     passWordEdit->clear();
00249   };
00250 }
00251 
00252 
00253 void ConnectDialog::setOkEnabled()
00254 {
00255   okButton->setEnabled
00256     ( ( ( serverTypeCombo->currentItem() == 0 &&
00257           !serverCombo->currentText().isEmpty() ) ||
00258         ( serverTypeCombo->currentItem() == 1 &&
00259           !socketCombo->currentText().isEmpty() &&
00260           !folderEdit->text().isEmpty() ) ) &&
00261       !userCombo->currentText().isEmpty() &&
00262       ( !passWordCheckBox->isChecked() ||
00263         !passWordEdit->text().isEmpty() ) );
00264 }
00265 
00266 
00267 void ConnectDialog::setServerType( int type )
00268 {
00269   assert( serverTypeCombo->count() == 2 );
00270   assert( serverTypeCombo->text( 0 ) == i18n( "Network" ) );
00271   assert( serverTypeCombo->text( 1 ) == i18n( "Embedded" ) );
00272   switch ( type ) {
00273   case 0:
00274     setServer( serverCombo->currentText() );
00275     break;
00276   case 1:
00277     setSocket( socketCombo->currentText() );
00278     break;
00279   default:
00280     assert( false );
00281     break;
00282   };
00283 }
00284 
00285 
00286 QString ConnectDialog::sourceKey()
00287 {
00288   return serverTypeCombo->currentItem() == 0 ?
00289     serverCombo->currentText() :
00290     socketCombo->currentText();
00291 }
00292 
00293 
00294 void ConnectDialog::selectSocket()
00295 {
00296   QString fileName =
00297     KFileDialog::getOpenFileName( ":dbfolder",
00298                                   i18n( "*.sock|Socket (*.sock)\n"
00299                                         "*|All Files(*)" ),
00300                                   this,
00301                                   i18n( "Select database socket" ) );
00302   if ( fileName != QString::null ) {
00303     socketCombo->setCurrentText( fileName );
00304   };
00305 }
00306 
00307 
00308 void ConnectDialog::selectFolder()
00309 {
00310   QString folder =
00311     KFileDialog::getExistingDirectory( ":dbfolder",
00312                                        this,
00313                                        i18n( "Select database folder" ) );
00314   if ( folder != QString::null ) {
00315     folderEdit->setText( folder );
00316   };
00317 }


anymeal 0.30 - recipe management software - Make the most of your food! - © Jan Wedekind Sun Sep 14 16:01:26 2008 - GNU Free Documentation License