//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "qfspop.h"
#include "qfsmail1.h"
#define INI(X) FrmQfs->li_ini->Values[X]
#define CKEY li_pop->Strings[a][1]&127
#define br FrmQfs->br
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFrmPop *FrmPop;
//---------------------------------------------------------------------------
__fastcall TFrmPop::TFrmPop(TComponent* Owner)
        : TForm(Owner)
{li_pop=new TStringList();
}
//---------------------------------------------------------------------------
AnsiString TFrmPop::GetSplit(int n,AnsiString st){
 int i,a=1,b;
 for(i=0;i<n;i++){a=AnsiPos("|",st);
  if(a){st.Delete(a,1);}}
 if((b=AnsiPos("|",st))!=0){
  st=st.SubString(a,b-a);}else{st=st.SubString(a,st.Length()-a+1);}
 return st;}

void __fastcall TFrmPop::LoadAllIni(void){
 int i=0;
 AnsiString st;
 li_pop->Clear();CmbName->Items->Clear();
 while((st=INI("Pop"+IntToStr(i)))!=""){
  li_pop->Add(st);
//  Application->MessageBox(li_ftp->Strings[i].c_str(),st.c_str(),MB_OK);
  CmbName->Items->Add(GetSplit(0,li_pop->Strings[i]));
  ++i;}
 sel=0;CmbName->ItemIndex=0;ExtractPa(0);}

void __fastcall TFrmPop::SaveAllIni(void){
 int i,j;
 for(i=0;i<FrmQfs->li_ini->Count;i++){
  if(FrmQfs->li_ini->Strings[i].SubString(1,3)=="Pop"){
   FrmQfs->li_ini->Delete(i);--i;}}

 for(i=0;i<li_pop->Count;i++){INI("Pop"+IntToStr(i))=li_pop->Strings[i];}
}

void __fastcall TFrmPop::ExtractPa(int a){
// Caption=li_ftp->Strings[a];
 CmbName->Text=P_NAME(a);
 EdtHost->Text=P_HOST(a);
 EdtUser->Text=P_USER(a);
 EdtPasswd->Text=FrmQfs->strDecrypt(P_PASSWD(a),CKEY);
 EdtPort->Text=P_PORT(a);
 ChkApop->Checked=(P_APOP(a)=="1")?true:false;
 ChkNoDelete->Checked=(P_NODELETE(a)=="1")?true:false;
 EdtDeleteDay->Text=P_DELETEDAY(a);
}

bool __fastcall TFrmPop::SavePa(int a){
 if(CmbName->Items->Strings[a]==""){Application->MessageBox("名前を付けて","設定",MB_OK);return false;}
 li_pop->Strings[a]=
  CmbName->Items->Strings[a]+"|"+
  EdtHost->Text+"|"+
  EdtUser->Text+"|"+
  FrmQfs->strEncrypt(EdtPasswd->Text,CmbName->Items->Strings[a][1]&127)+"|"+
  EdtPort->Text+"|"+
  ((ChkApop->Checked)?"1":"0")+"|"+
  ((ChkNoDelete->Checked)?"1":"0")+"|"+
  EdtDeleteDay->Text;
  return true;
}
void __fastcall TFrmPop::Button1Click(TObject *Sender)
{
if(SavePa(sel)){SaveAllIni();Close();}
}
//---------------------------------------------------------------------------
void __fastcall TFrmPop::Button2Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TFrmPop::Button3Click(TObject *Sender)
{
 if(!SavePa(sel))return;
 CmbName->Text="New"+IntToStr(CmbName->Items->Count+1);
 CmbName->Items->Add(CmbName->Text);
 li_pop->Add(CmbName->Text+"|||||");
 CmbName->ItemIndex=CmbName->Items->Count-1;sel=CmbName->ItemIndex;
 EdtHost->Text="";
 EdtUser->Text="";
 EdtPasswd->Text="";
 EdtPort->Text="110";
 ChkApop->Checked=false;
 ChkNoDelete->Checked=false;
}
//---------------------------------------------------------------------------
void __fastcall TFrmPop::Button4Click(TObject *Sender)
{
 if(CmbName->Items->Count==1){
  Application->MessageBox("最後の1個は消せません","削除",MB_ICONINFORMATION);return;}
 if(Application->MessageBox("削除?","",MB_YESNO)==ID_NO)return;
 li_pop->Delete(sel);
 CmbName->Items->Delete(sel);if(sel>CmbName->Items->Count-1)--sel;ExtractPa(sel);
}
//---------------------------------------------------------------------------
void __fastcall TFrmPop::CmbNameKeyUp(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
 switch(Key){
  case VK_UP:case VK_DOWN:case VK_RIGHT: case VK_LEFT: return;
//  case VK_RETURN:if(!SavePa(sel))return;SaveAllIni();
//  case VK_ESCAPE:Close();break;
  default:
 CmbName->Items->Strings[sel]=CmbName->Text;
// P_NAME(sel)=CmbName->Text;
 break;}

}
//---------------------------------------------------------------------------
void __fastcall TFrmPop::CmbNameChange(TObject *Sender)
{
 if(CmbName->ItemIndex!=-1&&sel!=CmbName->ItemIndex){
  if(!SavePa(sel)){CmbName->ItemIndex=sel;return;}sel=CmbName->ItemIndex;ExtractPa(sel);
  //pa->Strings[0]=IntToStr(sel);
  }
}
//---------------------------------------------------------------------------
void __fastcall TFrmPop::FormShow(TObject *Sender)
{
CmbName->SetFocus();}
//---------------------------------------------------------------------------
