/****************************************************************************
** ui.h extension file, included from the uic-generated form implementation.
**
** If you wish to add, delete or rename functions or slots use
** Qt Designer which will update this file, preserving your code. Create an
** init() function in place of a constructor, and a destroy() function in
** place of a destructor.
*****************************************************************************/
//-------------------------------------------- init ---------------------------------------
void DlgChoixUser::init()
{
 setProperty( "icon", QPixmap( G_pCApp->m_PathImages + "icones/pingoin_tete.png" ) );
}

//-------------------------------------------- pushButtonAnnuler_clicked ---------------------------------------
void DlgChoixUser::initDialog(CMoteurBase* pCMoteurBase, QString user, QString sign_user, QString *pParam)
{m_pCMoteurBase               = pCMoteurBase;
 m_User                       = user;
 m_SignUser                   = sign_user;
 m_pParam                     = pParam;
 QListViewItem *qlistViewItem = 0;
 if (m_pCMoteurBase==0) return;
 QString dumy("");
 m_pCMoteurBase->GetUserList( ListView_UserList, dumy, dumy);
 qlistViewItem = getUserItem(user, sign_user);
setCaption(tr("Utilisateur en cours : ") + user+"::"+sign_user);
 if (qlistViewItem)
    {ListView_UserList->setCurrentItem (qlistViewItem );
     ListView_UserList->setSelected (qlistViewItem, TRUE );

     lineEditPassWord->setFocus ();
   }
 else
   {qlistViewItem = ListView_UserList->firstChild();
    if (qlistViewItem>0)
       {ListView_UserList->setCurrentItem(qlistViewItem);
        ListView_UserList->setSelected (qlistViewItem, TRUE );
       }
   }

 //.................... desactiver changement d'utilisateur si ....................
 //                     un mot de passe existe pour lui
 //                     ne sera reactivé que lorsque il sera correct
 QString cripted_password;
 if (m_pCMoteurBase->GetUserPrimKey(m_User, &cripted_password) != "")
    {if (cripted_password !="")
        {pushButtonConfig->setEnabled(FALSE);
         pushButtonOK->setEnabled(FALSE);
        }
    }

//.................... positionner le spliter....................
 QValueList <int> list;
 int w = width() / 3;
 list.append(w);
 list.append(width() - w);
 splitterCentral->setSizes (list );
}
//------------------------------------ getSignUser --------------------------------------
QListViewItem *DlgChoixUser::getSignUser()
{return getSignUser( ListView_UserList->selectedItem());
}


//------------------------------------ getSignUser --------------------------------------
QListViewItem *DlgChoixUser::getSignUser(QListViewItem *pQListViewItem)
{
 if (pQListViewItem)
    {QListViewItem *pQListViewItemParent = pQListViewItem->parent();
     if (pQListViewItemParent==0) return pQListViewItem;
     else                         return pQListViewItemParent;
    }
 return 0;
}

//------------------------------------ getUserItem --------------------------------------
QListViewItem *DlgChoixUser::getUserItem(const QString &user, const QString &sign_user)
{   QListViewItemIterator it( ListView_UserList );
    while ( it.current() )
          {QListViewItem *pQListViewItem = it.current();
           if ( pQListViewItem->text(0) == user )
              { QListViewItem *pQListViewItemParent = pQListViewItem->parent();
                if (pQListViewItemParent==0) pQListViewItemParent = pQListViewItem;
                if (pQListViewItemParent->text(0) == sign_user) return pQListViewItem;
              }
            ++it;
           } //endwhile ( it.current() )
    return 0;
}

//-------------------------------------------- pushButtonAnnuler_clicked ---------------------------------------
void DlgChoixUser::pushButtonAnnuler_clicked()
{reject();
}


//-------------------------------------------- pushButtonOK_clicked ---------------------------------------
void DlgChoixUser::pushButtonOK_clicked()
{
 QListViewItem   *pQListViewItem = ListView_UserList->currentItem ( );
 if (pQListViewItem<=0) return;

 m_User = pQListViewItem->text(0);
 QListViewItem *pQListViewItemParent = pQListViewItem->parent();
 if (pQListViewItemParent==0) pQListViewItemParent = pQListViewItem;
 m_SignUser = pQListViewItemParent->text(0);

 if (pushButtonConfig->isEnabled () == TRUE)
    {if (m_User !="" && m_SignUser !="") accept();
     else                                reject();
    }
 else
    {QMessageBox::critical( this, tr("Erreur : Mot de passe"),
                                  tr("Mot de passe incorrect veuillez recommencer") ,
                                  tr("Annuler")
                          ) ;
    }
}


//-------------------------------------------- ListView_UserList_clicked ---------------------------------------
void DlgChoixUser::ListView_UserList_clicked( QListViewItem *pQListViewItem )
{if (pQListViewItem<=0) return;

 m_User = pQListViewItem->text(0);
 QListViewItem *pQListViewItemParent = pQListViewItem->parent();
 if (pQListViewItemParent==0) pQListViewItemParent = pQListViewItem;
 m_SignUser = pQListViewItemParent->text(0);

 lineEditPassWord->setText("");
 lineEditPassWord->setFocus ();
 lineEditPassWord_textChanged("");
 setCaption(tr("Utilisateur en cours : ") + m_User+"::"+m_SignUser);
}


//-------------------------------------------- ListView_UserList_doubleClicked ---------------------------------------
void DlgChoixUser::ListView_UserList_doubleClicked( QListViewItem *pQListViewItem )
{if (pQListViewItem<=0) return;
 m_User = pQListViewItem->text(0);
 if (m_User !="") accept();
 else             reject();
}


//-------------------------------------------- pushButtonConfig_clicked ----------------------------------------------
void DlgChoixUser::pushButtonConfig_clicked()
{       QListViewItem * qlistViewItem = ListView_UserList->currentItem ( );
        if (qlistViewItem)      m_User = qlistViewItem->text(0);
        QString cripted_password;
        m_pCMoteurBase->GetUserPrimKey(m_User, &cripted_password);

        QString  fileName = CGestIni::Construct_Name_Exe("gest_user",QFileInfo (qApp->argv()[0]).dirPath (true));
        QProcess*  proc = new QProcess( this );
        if (proc == 0)                        return;
        proc->addArgument( fileName );
        proc->addArgument( m_User );                      // Nom de l'utilisateur
        proc->addArgument( QString("#") + cripted_password );
        proc->start();
        delete proc;

}

//-------------------------------------------- lineEditPassWord_textChanged ----------------------------------------------
void DlgChoixUser::lineEditPassWord_textChanged( const QString &password )
{QListViewItem * qlistViewItem = ListView_UserList->currentItem ( );
 if (qlistViewItem == 0)  return;
 QString user   = qlistViewItem->text(0);

 if (m_pCMoteurBase->VerifyUserPassWord(user, password))
    {pushButtonConfig->setEnabled(TRUE);
     pushButtonOK->setEnabled(TRUE);
    }
 else
    {pushButtonConfig->setEnabled(FALSE);
     pushButtonOK->setEnabled(FALSE);
    }
}

