TrayIcon/TPropiedades.cpp
2021-09-12 22:21:22 +02:00

360 lines
12 KiB
C++

//---------------------------------------------------------------------------
#include <registry.hpp>
#include <vcl.h>
#pragma hdrstop
#include "TPropiedades.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "DialUp"
#pragma resource "*.dfm"
TPropiedades *Propiedades;
//---------------------------------------------------------------------------
__fastcall TPropiedades::TPropiedades(TComponent* Owner)
: TForm(Owner)
{
HoraL->DateTime=0;
HoraU->DateTime=0;
HoraR->DateTime=0;
HoraS->DateTime=0;
DesconectarSalir = true;
ResetEstados();
try {
mHistorial -> Lines -> LoadFromFile("Historial.log");
}catch(...){
// do nothing
}
mHistorial -> Lines -> Add( TDateTime::CurrentDateTime().DateTimeString() + " Inicio de sesión ");
// Refrescamos las conexiones disponibles...
RefrescarConexionesClick(0);
// Obtenemos los parámetros básicos
CargaEstados();
HoraD -> Time = ( HoraU -> Time ) - ( HoraL -> Time );
// Validalidamos el temporizador
TimerDialUp -> Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::HoraLChange(TObject *Sender)
{
try {
if ( HoraL->Time > HoraU->Time ) HoraU->Time = HoraL->Time;
HoraD -> Time = ( HoraU -> Time ) - ( HoraL -> Time );
if ( HoraR->Time > (HoraU->Time - HoraL->Time) ) HoraR->Time = 0;
} catch(...) { /*do nothing*/ }
bAplicar -> Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::HoraDChange(TObject *Sender)
{
try{
HoraU -> Time = ( HoraL -> Time ) + ( HoraD -> Time );
} catch(...) { /*do nothing*/ }
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::HoraUChange(TObject *Sender)
{
try {
HoraD -> Time = ( HoraU -> Time ) - ( HoraL -> Time );
if ( HoraR->Time > (HoraU->Time - HoraL->Time) ) HoraR->Time = 0;
if ( HoraS->Time <= HoraU->Time ) HoraS->Time = HoraU->Time;
} catch(...) { /*do nothing*/ }
bAplicar -> Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::RefrescarConexionesClick(TObject *Sender)
{
// Obtenemos la lista de conexiones...
ConexionesDisponibles -> Items -> Clear();
DialUp1 -> GetEntries();
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::DialUp1EntryGet(TObject *Sender,
AnsiString EntryName)
{
ConexionesDisponibles -> Items -> Add( EntryName );
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::TimerDialUpTimer(TObject *Sender)
{
TDateTime CurrentTime, dtHoraL, dtHoraU, dtHoraR;
CurrentTime = TDateTime::CurrentTime();
dtHoraL = HoraL->Time;
dtHoraU = HoraU->Time;
dtHoraR = HoraR->Time;
TimerDialUp -> Enabled = false;
// Rango en el que deberiamos estar conectados...
if ( CurrentTime >= dtHoraL && CurrentTime < dtHoraU )
{
// Conectamos, si no lo estamos ya
if ( !connected && intentos <= NumReconexiones -> Text.ToInt() )
{
if ( intentos < 0 )
{
StatusBar1 -> Panels -> Items[0] -> Text = "Conectando...";
mHistorial -> Lines -> Add( TDateTime::CurrentDateTime().DateTimeString() + " Inicio conexión (" + DialUp1 -> Entry +")" );
DialUp1 -> Dial();
} else {
if ( Reconectar -> Checked && ( (dtHoraU - dtHoraL) > dtHoraR ) )
{
StatusBar1 -> Panels -> Items[0] -> Text = "Re-Conectando... ("+ AnsiString( intentos ) +")";
mHistorial -> Lines -> Add( TDateTime::CurrentDateTime().DateTimeString() + " Re-Conectando (" + DialUp1 -> Entry +")" );
DialUp1 -> Dial();
}
}
}
} else {
if ( connected )
{
ResetEstados();
StatusBar1 -> Panels -> Items[0] -> Text = "Desconectando...";
mHistorial -> Lines -> Add( TDateTime::CurrentDateTime().DateTimeString() + " Desconectando (" + DialUp1 -> Entry +")" );
DialUp1 -> HangUp();
StatusBar1 -> Panels -> Items[0] -> Text = "Ready";
} else {
if ( ShutDown->Checked && !connected && CurrentTime == HoraS->Time )
{
ShutDown->Checked = false;
SalvaEstados();
StatusBar1 -> Panels -> Items[0] -> Text = "Apagando equipo";
mHistorial -> Lines -> Add( TDateTime::CurrentDateTime().DateTimeString() + " APAGANDO ORDENADOR" );
//******************************
//ApagarEQUIPO();
//******************************
}
}
TimerDialUp -> Enabled = true;
}
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::ResetEstados(void)
{
intentos = -1;
connected = false;
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::HoraRChange(TObject *Sender)
{
if ( HoraR->Time > (HoraU->Time - HoraL->Time) )
HoraR->Time = 0;
bAplicar -> Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::DialUp1Error(TObject *Sender, int ErrorCode,
AnsiString ErrorMessage)
{
// Prohibimos el que se pueda volver a conectar...
if ( ErrorCode == ERROR_USER_DISCONNECTION )
intentos = 20;
connected = false;
StatusBar1 -> Panels -> Items[0] -> Text = ErrorMessage;
mHistorial -> Lines -> Add( TDateTime::CurrentDateTime().DateTimeString() + " ***************** (" + DialUp1 -> Entry +")" );
mHistorial -> Lines -> Add( TDateTime::CurrentDateTime().DateTimeString() + " ERROR: (" + ErrorMessage +")" );
mHistorial -> Lines -> Add( TDateTime::CurrentDateTime().DateTimeString() + " ***************** (" + DialUp1 -> Entry +")" );
TimerDialUp -> Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::ConexionesDisponiblesChange(TObject *Sender)
{
DialUp1 -> Entry = ConexionesDisponibles -> Text;
bAplicar -> Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::FormDestroy(TObject *Sender)
{
// Cerramos cualquier comunicación abierta
if ( connected && DesconectarSalir )
DialUp1 -> HangUp();
mHistorial -> Lines -> Add( TDateTime::CurrentDateTime().DateTimeString() + " Fin de sesión ");
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::OnExitClick(TObject *Sender)
{
DesconectarSalir = OnExit -> Checked;
bAplicar -> Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::BorrarHistorialClick(TObject *Sender)
{
mHistorial -> Lines -> Clear();
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::mHistorialChange(TObject *Sender)
{
try {
if ( Historial -> Checked )
mHistorial -> Lines -> SaveToFile("Historial.log");
} catch(...) {
// do nothing
}
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::CargaEstados(void)
{
TRegistry *Registro;
Registro = new TRegistry;
// Si es la primera vez que ejecuta el programa...
if ( ! Registro -> KeyExists("\\Software\\JDsoft\\Internet Conector") )
try {
Registro -> OpenKey( "\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", false );
Registro -> WriteString( "InternetConector", Application->ExeName );
}catch(...){ /*do nothing*/ }
try{
Registro -> OpenKey( "\\Software\\JDsoft", true );
Registro -> OpenKey( "\\Software\\JDsoft\\Internet Conector", true );
Historial->Checked = Registro -> ReadBool( "Historial" );
ConexionesDisponibles->Text = Registro -> ReadString( "Conexion" );
HoraL->Time = Registro -> ReadTime( "Inicio");
HoraU->Time = Registro -> ReadTime( "Fin" );
Reconectar->Checked = Registro -> ReadBool( "Reconectar" );
bReintentos->Position = Registro -> ReadInteger( "Reintentos" );
HoraR->Time = Registro -> ReadTime( "PreInicio" );
AutoRun->Checked = Registro -> ReadBool( "AutoRun" );
OnExit->Checked = Registro -> ReadBool( "AutoDesconexion" );
ShutDown->Checked = Registro -> ReadBool( "AutoShutDown" );
HoraS->Time = Registro -> ReadTime( "HoraShutDown" );
}catch(...){
// do nothing
}
DialUp1 -> Entry = ConexionesDisponibles -> Text;
delete Registro;
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::SalvaEstados(void)
{
if ( ! TimerDialUp -> Enabled ) return;
TRegistry *Registro;
Registro = new TRegistry;
try {
Registro -> OpenKey( "\\Software\\JDsoft", true );
Registro -> OpenKey( "\\Software\\JDsoft\\Internet Conector", true );
Registro -> WriteBool( "Historial", Historial->Checked );
Registro -> WriteString( "Conexion", ConexionesDisponibles->Text );
Registro -> WriteTime( "Inicio", HoraL->Time );
Registro -> WriteTime( "Fin", HoraU->Time );
Registro -> WriteBool( "Reconectar", Reconectar->Checked );
Registro -> WriteInteger( "Reintentos", bReintentos->Position );
Registro -> WriteTime( "PreInicio", HoraR->Time );
Registro -> WriteBool( "AutoRun", AutoRun->Checked );
Registro -> WriteBool( "AutoDesconexion", OnExit->Checked );
Registro -> WriteBool( "AutoShutDown", ShutDown->Checked );
Registro -> WriteTime( "HoraShutDown", HoraS->Time );
Registro -> OpenKey( "\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", false );
// Comprobamos el estado
if ( AutoRun -> Checked )
Registro -> WriteString( "InternetConector", Application->ExeName );
else
Registro -> DeleteValue( "InternetConector" );
}catch(...){
//do nothing
}
delete Registro;
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::ReconectarClick(TObject *Sender)
{
bAplicar -> Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::NumReconexionesChange(TObject *Sender)
{
bAplicar -> Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::HistorialClick(TObject *Sender)
{
bAplicar -> Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::bReintentosClick(TObject *Sender,
TUDBtnType Button)
{
bAplicar -> Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::AutoRunClick(TObject *Sender)
{
bAplicar -> Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::ShutDownClick(TObject *Sender)
{
bAplicar -> Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::bAceptarClick(TObject *Sender)
{
SalvaEstados();
Propiedades->Visible = false;
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::bCancelarClick(TObject *Sender)
{
CargaEstados();
Propiedades->Visible = false;
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::bAplicarClick(TObject *Sender)
{
SalvaEstados();
bAplicar -> Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::DialUp1Dialing(TObject *Sender)
{
StatusBar1 -> Panels -> Items[0] -> Text = "Marcando...";
mHistorial -> Lines -> Add( TDateTime::CurrentDateTime().DateTimeString() + " Marcando... (" + DialUp1 -> Entry +")" );
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::DialUp1AsyncEvent(TObject *Sender, int State,
int Error, AnsiString MessageText)
{
if ( State == RASCS_Connected )
{
intentos++;
connected = true;
StatusBar1 -> Panels -> Items[0] -> Text = "--> Conexión ABIERTA <--";
mHistorial -> Lines -> Add( TDateTime::CurrentDateTime().DateTimeString() + " Conexión abierta (" + DialUp1 -> Entry +")" );
TimerDialUp -> Enabled = true;
} else {
StatusBar1 -> Panels -> Items[0] -> Text = MessageText;
mHistorial -> Lines -> Add( TDateTime::CurrentDateTime().DateTimeString() + MessageText + " (" + DialUp1 -> Entry +")" );
}
}
//---------------------------------------------------------------------------