Source

oberchatten / AwayMessageUnit.cpp

/*
Copyright (C) 2001-2006 Daybo Logic.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. Neither the name of the project nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
//---------------------------------------------------------------------------
#ifdef HAVE_CONFIG_H
# include "overchat_config.h"
#endif //HAVE_CONFIG_H

#include <vcl.h>
#include <time.h>
#include <stdio.h>
#include <ctype.h>
#include <string>

#ifdef HAVE_LIMITS_H
# include <limits.h>
#endif //HAVE_LIMITS_H

#ifdef HDRSTOP
# pragma hdrstop
#endif

#include "master.h"
#include "Events.h"
#include "AwayMessageUnit.h"
#include "History.h"
#include "MsgUnit.h"
#include "ChatFormUnit.h" // TChatForm
#include "StatsManagerUnit.h" // TStatsManager
#include "StatusBarManagerUnit.h" // TStatusBarManager
#include "MyUserUnit.h" // TMyUSer
#include "MainFormUnit.h" // TMainForm
#include "General.h" // For CentreMyForm()
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TAwayMessageDlg *AwayMessageDlg;
using namespace Oberchatten;

#ifdef OVERCHAT_RCS_STAMPS
static const char *rcsid =
  "$Id: AwayMessageUnit.cpp,v 0.6 2005/12/05 19:54:00 ddrp Exp $";
#endif /*OVERCHAT_RCS_STAMPS*/

#define AUTO_AWAY_MINS_DEFAULT (30)
//---------------------------------------------------------------------------
__fastcall TAwayMessageDlg::TAwayMessageDlg(TComponent* Owner)
        : TForm(Owner),
          away(false),
          autoAwayMins(AUTO_AWAY_MINS_DEFAULT)
{
  #ifdef OVERCHAT_RCS_STAMPS
    blake_Unused(rcsid);
  #endif //OVERCHAT_RCS_STAMPS
  time(&this->lastActionTime); // Update idle information
}
//---------------------------------------------------------------------------
void __fastcall TAwayMessageDlg::FormShow(TObject *Sender)
{
  this->Button1->Left = (this->Panel1->Width/2) - (this->Button1->Width/2);
  //this->Button1->Top = (this->Panel1->Height/2) - (this->Button1->Height/2);

  CentreMyForm(this, MainForm);
}
//---------------------------------------------------------------------------
void __fastcall TAwayMessageDlg::Button1Click(TObject *Sender)
{
  if ( this->away )
    this->Close();
  else
    this->Away();
}
//---------------------------------------------------------------------------
bool TAwayMessageDlg::IsAway()
{
  return this->away;
}
//---------------------------------------------------------------------------
AnsiString& TAwayMessageDlg::GetAwayMessage(AnsiString& Msg)
{
  Msg = "[Auto Response] >";
  Msg += this->AwayMessage->Text + "<";
  return Msg;
}
//---------------------------------------------------------------------------
void __fastcall TAwayMessageDlg::FormClose(TObject *Sender,
      TCloseAction &Action)
{
  this->Back(); // Assume user has returned
}
//---------------------------------------------------------------------------
void TAwayMessageDlg::Away()
{
  if ( !away ) {
    this->away = !this->away; // Marked as away
    this->Button1->Caption = "Come back";
    Caption = "Away";
    this->AwayMessage->Enabled = false;
    this->AwayMessage->Color = clBtnFace;

    MainForm->MnuOberchattenAwayNow->Enabled = false;
    MainForm->MnuOberchattenAwayReturn->Enabled = true;
  }
}
//---------------------------------------------------------------------------
void TAwayMessageDlg::Back()
{
  if ( this->away ) {
    this->away = !this->away; // Mark as present
    this->Button1->Caption = "Go away";
    this->Caption = "Set away message";
    this->AwayMessage->Enabled = true;
    this->AwayMessage->Color = clWindow;

    MainForm->MnuOberchattenAwayNow->Enabled = true;
    MainForm->MnuOberchattenAwayReturn->Enabled = false;
  }
  this->UpdateLastActionTime();
}
//---------------------------------------------------------------------------
void __fastcall TAwayMessageDlg::MinsEditChange(TObject *Sender)
{
  unsigned int autoAwayMins;

  this->UpdateLastActionTime(); // Make sure we don't immediately go away
  autoAwayMins = atoi(this->MinsEdit->Text.c_str());
  if ( autoAwayMins ) // Zero is invalid
    this->autoAwayMins = autoAwayMins;
  else
    this->autoAwayMins = AUTO_AWAY_MINS_DEFAULT;

  if ( this->autoAwayMins == 1 )
    this->MinsLabel->Caption = "minute";
  else
    this->MinsLabel->Caption = "minutes";
}
//---------------------------------------------------------------------------
time_t TAwayMessageDlg::GetLastActionTime(
  time_t *PLastActionTime
)
{
  if ( PLastActionTime )
    memcpy(
      PLastActionTime,
      &this->lastActionTime,
      sizeof(this->lastActionTime)
    );

  return this->lastActionTime;
}
//---------------------------------------------------------------------------
void TAwayMessageDlg::SetLastActionTime(
  const time_t *PLastActionTime
)
{
  if ( PLastActionTime ) {
    memcpy(
      &this->lastActionTime,
      PLastActionTime,
      sizeof(this->lastActionTime)
    );
  }
}
//---------------------------------------------------------------------------
void TAwayMessageDlg::UpdateLastActionTime()
{
  time_t currentTime;

  time(&currentTime);
  this->SetLastActionTime(&currentTime);
}
//---------------------------------------------------------------------------
void TAwayMessageDlg::CheckAutoAway()
{
  time_t currentTime;

  if ( this->AutoChk->Checked ) {
    time(&currentTime);
    if (
      (currentTime - this->lastActionTime) >=
      (time_t)((this->autoAwayMins * 60))
    ) {
      this->Away();
    }
  }
}
//---------------------------------------------------------------------------
void __fastcall TAwayMessageDlg::MinsEditKeyPress(
  TObject *Sender,
  char &Key
)
{
  if ( !isdigit(Key) && ( Key != '\b') )
    Key = '\0';
}
//---------------------------------------------------------------------------
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.