Source

oberchatten / IgnoreListUnit.cpp

Full commit
/*
Copyright (C) 2001-2005 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.
*/
//---------------------------------------------------------------------------
#include "overchat_config.h"
#include <vcl.h>
#include <limits.h>
#include <time.h>
#include <stdio.h>
#include <ctype.h>
#pragma hdrstop

#include "blake.h"
#include "ShadowUnit.h"
#include "IgnoreListUnit.h"
#include "InfoBoxUnit.h"
#include "General.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TIgnoreListForm *IgnoreListForm = NULL;
using namespace Oberchatten;

#ifdef OVERCHAT_RCS_STAMPS
static const char *rcsid =
  "$Id: IgnoreListUnit.cpp,v 0.3 2005/04/28 00:45:00 ddrp Exp $";
#endif /*OVERCHAT_RCS_STAMPS*/
//---------------------------------------------------------------------------
__fastcall TIgnoreListForm::TIgnoreListForm(TComponent* Owner)
        : TForm(Owner)
{
  blake_Unused(rcsid);
}
//---------------------------------------------------------------------------
void __fastcall TIgnoreListForm::FormResize(TObject *Sender)
{
  this->ExclusionList->Height =
    this->ClientHeight - this->ExclusionPanel->Height;

  this->InclusionList->Height =
    this->ClientHeight - this->InclusionPanel->Height;

  this->MovePanel->Top =
    ( this->ClientHeight /2 ) - ( this->MovePanel->Height /2 );
}
//---------------------------------------------------------------------------
void __fastcall TIgnoreListForm::IncludeSelBtnClick(TObject *Sender)
{
  this->MoveBetweenLists(
    this->ExclusionList,
    this->InclusionList,
    false
  );
}
//---------------------------------------------------------------------------
void __fastcall TIgnoreListForm::ExcludeSelBtnClick(TObject *Sender)
{
  this->MoveBetweenLists(
    this->InclusionList,
    this->ExclusionList,
    false
  );
}
//---------------------------------------------------------------------------
void TIgnoreListForm::MoveBetweenLists(
  TListBox *Source,
  TListBox *Destination,
  bool All
)
{
  unsigned int i, c, code = ID_IGNORE;

  c = Source->Items->Count;
  i = c;
  while ( i && code != ID_ABORT ) {
    if ( (i - 1) != UINT_MAX ) {
      if ( Source->Selected[i-1] || All ) {
        blake_bool_t bTooLong;
        char cInvChar;

        if (
          !blake_NickTest(
            Source->Items->Strings[i-1].c_str(),
            &bTooLong,
            &cInvChar
          )
        )
        {
          // This nickname is invalid, we shan't move it
          if ( bTooLong ) {
            code = InfoBox(
              this,
              "This nickname is too long and violates well-defined OverChat "
              "rules.  It will be removed from the source list automatically "
              "and discarded.",
              Source->Items->Strings[i-1].c_str(),
              MB_ICONSTOP | MB_ABORTRETRYIGNORE | MB_DEFBUTTON3,
              INFOBOX_NOHELP
            );
          }
          else { // Contains invalid characters
            if ( isspace(cInvChar) ) {
              if ( Source->Items->Strings[i-1].Length() == 1 ) {
                code = InfoBox(
                  this,
                  "A nickname may not only consist of a space.\n"
                  "This nickname will automatically be removed from the "
                  "source list.",
                  "Nickname is a space only",
                  MB_ICONSTOP | MB_ABORTRETRYIGNORE | MB_DEFBUTTON3,
                  INFOBOX_NOHELP
                );
              }
              else { // Leading space
                code = InfoBox(
                  this,
                  "The nickname has a number of leading spaces.\n"
                  "This is violation of well-defined rules.  The nickname "
                  "will automatically be removed from the source list.",
                  Source->Items->Strings[i-1].c_str(),
                  MB_ICONSTOP | MB_ABORTRETRYIGNORE | MB_DEFBUTTON3,
                  INFOBOX_NOHELP
                );
              }
            }
            else { // Invalid character for nicknames
              code = InfoBox(
                this,
                "This nickname contains invalid characters, in clear "
                "violation of well-defined OverChat rules.\n"
                "There may be a bug in the client which allowed it to be "
                "loaded into the list.\n"
                "It will be automatically removed from the source list.",
                "Nickname containing illegal characters",
                MB_ICONSTOP | MB_ABORTRETRYIGNORE | MB_DEFBUTTON3,
                INFOBOX_NOHELP
              );
            }
          }
          if ( code == ID_IGNORE )
            Source->Items->Delete(i-1);
        }
        else { // Nickname is acceptable
          if (
            IsNickInItems(
              Destination->Items,
              Source->Items->Strings[i-1]
            )
          ) {
            code = InfoBox(
              this,
              "The nickname is already present in the destination list",
              Source->Items->Strings[i-1].c_str(),
              MB_ICONEXCLAMATION | MB_ABORTRETRYIGNORE | MB_DEFBUTTON3,
              INFOBOX_NOHELP
            );
          }
          else {
            Destination->Items->Add(
              Source->Items->Strings[i-1]
            );
            Source->Items->Delete(i-1);
          }
        }
      }
    }
    if ( code != ID_RETRY ) i--;
  }
  return;
}
//---------------------------------------------------------------------------
void __fastcall TIgnoreListForm::IncludeAllBtnClick(TObject *Sender)
{
  this->MoveBetweenLists(
    this->ExclusionList,
    this->InclusionList,
    true
  );
}
//---------------------------------------------------------------------------
void __fastcall TIgnoreListForm::ExcludeAllBtnClick(TObject *Sender)
{
  this->MoveBetweenLists(
    this->InclusionList,
    this->ExclusionList,
    true
  );
}
//---------------------------------------------------------------------------
void __fastcall TIgnoreListForm::PopListSelAllClick(TObject *Sender)
{
  TListBox *lb = dynamic_cast<TListBox*>(PopList->PopupComponent);
  AlterListBoxSel(lb, lbsAll);
}
//---------------------------------------------------------------------------
void __fastcall TIgnoreListForm::PopListSelNoneClick(TObject *Sender)
{
  TListBox *lb = dynamic_cast<TListBox*>(PopList->PopupComponent);
  AlterListBoxSel(lb, lbsNone);
}
//---------------------------------------------------------------------------
void __fastcall TIgnoreListForm::PopListSelInvertClick(TObject *Sender)
{
  TListBox *lb = dynamic_cast<TListBox*>(PopList->PopupComponent);
  AlterListBoxSel(lb, lbsInvert);
}
//---------------------------------------------------------------------------