//---------------------------------------------------------------------------
#ifndef qfsmail1H
#define qfsmail1H
//---------------------------------------------------------------------------
#define QFSVER "qfsmail ver1.20\n2014/8/1"
#define X_MAILER_VER "X-Mailer: qfsmail 1.20"
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <ExtCtrls.hpp>
#include <DBCtrls.hpp>
#include <DBGrids.hpp>
#include <Grids.hpp>
#include <IdBaseComponent.hpp>
#include <IdComponent.hpp>
#include <IdMessageClient.hpp>
#include <IdPOP3.hpp>
#include <IdSMTP.hpp>
#include <IdTCPClient.hpp>
#include <IdTCPConnection.hpp>
#include <Menus.hpp>
#include <Dialogs.hpp>
//#include "BRegExp.hpp"
//#include "jconvert.hpp"
#include <ToolWin.hpp>
#include <ImgList.hpp>
#define str(X) IntToStr(X)
#define ROWS DM1->SQLQuery2->RecordCount
#define ROWSM DM1->SQLQuery2->RecordCount
#define reStr(X) DM1->SQLQuery2->Fields->Fields[X]->AsString
#define reInt(X) DM1->SQLQuery2->Fields->Fields[X]->AsInteger
#define reStrm(X) DM1->SQLQuery2->Fields->Fields[X]->AsString
#define reIntm(X) DM1->SQLQuery2->Fields->Fields[X]->AsInteger
#define reDatem(X) DM1->SQLQuery2->Fields->Fields[X]->AsDateTime
#define FIRST DM1->SQLQuery2->First()
#define NEXT DM1->SQLQuery2->Next()
#define CLOSE DM1->SQLQuery2->Close();DM1->SQLConnection1->CloseDataSets()
#define CLOSEM DM1->SQLQuery3->Close();DM1->SQLConnection2->CloseDataSets()
#define DBLOOP while(!DM1->SQLQuery2->Eof)
#define INI(X) FrmQfs->li_ini->Values[X]
#define Spli(X,Y) FrmQfs->Split(X,Y)
#define CntSplit FrmQfs->li_split->Count
#define getkey(KY) GetKeyState(KY)&0x80

//---------------------------------------------------------------------------
class TFrmQfs : public TForm
{
__published:	// IDE 管理のコンポーネント
        TTreeView *TreeView1;
        TPanel *Panel1;
        TDBGrid *DBGrid1;
        TSplitter *Splitter1;
        TSplitter *Splitter2;
        TPanel *Panel2;
        TMainMenu *MainMenu1;
        TMenuItem *F1;
        TMenuItem *mnRecieveAll;
        TStatusBar *StatusBar1;
        TMenuItem *mnAttachment;
        TSaveDialog *SaveDialog1;
        TMenuItem *N1;
        TMenuItem *mnFuriwake;
        TPopupMenu *PopupMenu1;
        TMenuItem *popNewdir;
        TMenuItem *popDeldir;
        TMenuItem *popRendir;
        TMenuItem *popToroot;
        TPopupMenu *PopupMenu2;
        TMenuItem *popDelmail;
        TMenuItem *N2;
        TMenuItem *mnClose;
        TMenuItem *V1;
        TMenuItem *mnViewEditWnd;
        TMenuItem *popReply;
        TMenuItem *popCut;
        TMenuItem *popPaste;
        TMenuItem *mnFind;
        TMenuItem *mnSelectall;
        TMenuItem *mnAddress;
        TPopupMenu *PopupMenu3;
        TMenuItem *popMemoFont;
        TMenuItem *popPaste2;
        TMenuItem *mnPOP3;
        TMenuItem *mnRecieve;
        TMenuItem *popReplyall;
        TMenuItem *popMailSelAll;
        TMenuItem *N3;
        TMenuItem *mnFindBody;
        TFindDialog *FindDialog1;
        TMenuItem *mnBodyFindNext;
        TMenuItem *N4;
        TMenuItem *popMailHead;
        TMenuItem *mnSetFilter;
        TMenuItem *mnEnptyTrush;
        TMemo *DBMemo1;
        TMenuItem *mnNewWindow;
        TMenuItem *N6;
        TMenuItem *popWrap;
        TMenuItem *popCopySubject;
        TMenuItem *mnSetting;
        TMenuItem *popAllFilter;
        TMenuItem *mnRefresh;
        TMenuItem *popEditMailCol;
        TMenuItem *popGridFont;
        TMenuItem *popState;
        TMenuItem *N5;
        TMenuItem *mnAutoRecieve;
        TTimer *Timer1;
        TMenuItem *mnDatabase;
        TMenuItem *N7;
        TMenuItem *mnHelp;
        TMenuItem *mnQuickSave;
        TMenuItem *popMailForward;
        TMenuItem *C1;
        TMenuItem *A1;
        TMenuItem *popOpenWeb;
        TToolBar *ToolBar1;
        TToolButton *ToolButton1;
        TToolButton *ToolButton2;
        TImageList *ImageList1;
        TToolButton *ToolButton3;
        TToolButton *ToolButton4;
        TToolButton *ToolButton5;
        TToolButton *ToolButton7;
        TToolButton *ToolButton6;
        TToolButton *ToolButton8;
        TToolButton *ToolButton9;
        TToolButton *ToolButton10;
        TToolBar *ToolBar2;
        TMenuItem *mnForwardTmp;
        TMenuItem *mnReplyTmp;
        TMenuItem *mnReplyAllTmp;
        TMenuItem *mnNewMail;
        TMenuItem *mnNewMailTmp;
        TMenuItem *mnRefreshTmp;
        TMenuItem *N9;
        TMenuItem *N10;
        void __fastcall mnRecieveAllClick(TObject *Sender);
        void __fastcall DBGrid1CellClick(TColumn *Column);
        void __fastcall DBGrid1KeyUp(TObject *Sender, WORD &Key,
          TShiftState Shift);
        void __fastcall TreeView1Edited(TObject *Sender, TTreeNode *Node,
          AnsiString &S);
        void __fastcall TreeView1DragOver(TObject *Sender, TObject *Source,
          int X, int Y, TDragState State, bool &Accept);
        void __fastcall TreeView1DragDrop(TObject *Sender, TObject *Source,
          int X, int Y);
        void __fastcall TreeView1Change(TObject *Sender, TTreeNode *Node);
        void __fastcall mnFuriwakeClick(TObject *Sender);
        void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
        void __fastcall popNewdirClick(TObject *Sender);
        void __fastcall popDeldirClick(TObject *Sender);
        void __fastcall popRendirClick(TObject *Sender);
        void __fastcall popTorootClick(TObject *Sender);
        void __fastcall DBGrid1TitleClick(TColumn *Column);
        void __fastcall popDelmailClick(TObject *Sender);
        void __fastcall mnNewmailClick(TObject *Sender);
        void __fastcall mnViewEditWndClick(TObject *Sender);
        void __fastcall DBGrid1DblClick(TObject *Sender);
        void __fastcall popReplyClick(TObject *Sender);
        void __fastcall DBGrid1KeyDown(TObject *Sender, WORD &Key,
          TShiftState Shift);
        void __fastcall popCutClick(TObject *Sender);
        void __fastcall popPasteClick(TObject *Sender);
        void __fastcall mnFindClick(TObject *Sender);
        void __fastcall mnAddressClick(TObject *Sender);
        void __fastcall popMemoFontClick(TObject *Sender);
        void __fastcall mnPOP3Click(TObject *Sender);
        void __fastcall popReplyallClick(TObject *Sender);
        void __fastcall popMailSelAllClick(TObject *Sender);
        void __fastcall mnFindBodyClick(TObject *Sender);
        void __fastcall FindDialog1Find(TObject *Sender);
        void __fastcall mnBodyFindNextClick(TObject *Sender);
        void __fastcall popMailHeadClick(TObject *Sender);
        void __fastcall mnSetFilterClick(TObject *Sender);
        void __fastcall TreeView1MouseDown(TObject *Sender,
          TMouseButton Button, TShiftState Shift, int X, int Y);
        void __fastcall mnEnptyTrushClick(TObject *Sender);
        void __fastcall mnNewWindowClick(TObject *Sender);
        void __fastcall popWrapClick(TObject *Sender);
        void __fastcall popCopySubjectClick(TObject *Sender);
        void __fastcall mnSettingClick(TObject *Sender);
        void __fastcall popAllFilterClick(TObject *Sender);
        void __fastcall mnRefreshClick(TObject *Sender);
        void __fastcall popEditMailColClick(TObject *Sender);
        void __fastcall popGridFontClick(TObject *Sender);
        void __fastcall FrmMeg1popTreeFontClick(TObject *Sender);
        void __fastcall mnSelectallClick(TObject *Sender);
        void __fastcall mnCloseClick(TObject *Sender);
        void __fastcall N5Click(TObject *Sender);
        void __fastcall Timer1Timer(TObject *Sender);
        void __fastcall mnAutoRecieveClick(TObject *Sender);
        void __fastcall mnDatabaseClick(TObject *Sender);
        void __fastcall mnHelpClick(TObject *Sender);
        void __fastcall DBMemo1KeyDown(TObject *Sender, WORD &Key,
          TShiftState Shift);
        void __fastcall mnQuickSaveClick(TObject *Sender);
        void __fastcall DBGrid1DrawColumnCell(TObject *Sender,
          const TRect &Rect, int DataCol, TColumn *Column,
          TGridDrawState State);
        void __fastcall popMailForwardClick(TObject *Sender);
        void __fastcall C1Click(TObject *Sender);
        void __fastcall A1Click(TObject *Sender);
        void __fastcall popOpenWebClick(TObject *Sender);
        void __fastcall ToolButton5Click(TObject *Sender);
        void __fastcall ToolButton6Click(TObject *Sender);
        void __fastcall FormResize(TObject *Sender);
        void __fastcall mnRefreshTmpClick(TObject *Sender);
private:	// ユーザー宣言
        void __fastcall RecieveClick(TObject *Sender);
        void __fastcall RefreshRecieve(void);
        void __fastcall RefreshDir(void);
        bool __fastcall GetNode(int d_id);
        void __fastcall RefreshMailCol(void);
        void __fastcall RefreshMail(bool isFind);
        void __fastcall DelIniHead(AnsiString hd);
        void __fastcall WriteHeader(int m_id,AnsiString st);
        AnsiString di_template;
//Mail
int __fastcall sqm(AnsiString q);
void __fastcall sqm2(AnsiString q);
TTreeNode *gnd;
int statecolnum;//メール一覧の「状態」の場所
void __fastcall ListFiles(AnsiString dirName,TMenuItem *OyaItem);
void __fastcall AddTempItem(AnsiString st,TMenuItem *OyaItem);
void __fastcall LoadNewTemp(TObject *Sender);
void __fastcall LoadForwardTemp(TObject *Sender);
void __fastcall LoadReplyTemp(TObject *Sender);
void __fastcall LoadAllReplyTemp(TObject *Sender);
public:		// ユーザー宣言
        void __fastcall SaveAttachment(TObject *Sender);
        void __fastcall SaveAttachmentFromAid(int a_id,bool autoSave);
        AnsiString AttachFilename;//添付ﾌｧｲﾙ名(frmmegの転送時取得)
        __fastcall TFrmQfs(TComponent* Owner);
        void __fastcall RecieveMail(int sv);
        void __fastcall RefreshAttachment(int m_id);
//        void __fastcall RecieveMail1(void);
        bool __fastcall strReplace(AnsiString &st,AnsiString txt,AnsiString txt2);
        AnsiString di_temp,fi_ini,fi_log,fi_head,clip,st_find,st_sort;
        char noRecive;//受信番号 0〜127, -2:未実行 -1:全て
        AnsiString RecieveResult;//ﾒｰﾙ受信処理の結果文字列
        bool dblock,dbok;
        int mo_memo;//FrmMemoのモード
        TStringList *li_ini,*li_split,*li_split2,*li_sql,*li_temp;
        void __fastcall MemoClose(bool res);
        void __fastcall LoadIni(void);
        void __fastcall SaveIni(void);
int __fastcall InsertMail(int m_id,int d_id,char flag,char state,
AnsiString mailfrom,AnsiString recipients,AnsiString cc,AnsiString replyto,
TDateTime date,AnsiString subject,char priority,
AnsiString header,AnsiString body,int size,AnsiString MessageId);
int __fastcall InsertNewMail(int m_id,int d_id,char flag,char state,
AnsiString mailfrom,AnsiString recipients,AnsiString cc,AnsiString bcc,
TDateTime date,AnsiString subject,char priority,
AnsiString header,AnsiString body,int size);
void __fastcall InsertNewAttachment(int m_id,AnsiString filename);
void __fastcall SaveRecieveAttachment(int rowfile,int len,char *p);
//AnsiString __fastcall HeadVal(AnsiString na,TStringList *li);
int __fastcall InsertAttachment(int m_id,AnsiString filename,AnsiString encode);
//void __fastcall InsertAttachment(int m_id,AnsiString filename,AnsiString encode,char *p);
void __fastcall WriteLog(AnsiString st);
int __fastcall sql(AnsiString q);
void __fastcall sql2(AnsiString q);
int __fastcall CurrentDid(void);
//        TBRegExp *br,*gbr;
        AnsiString __fastcall GenChar(AnsiString st);
        void __fastcall GenCharStore(AnsiString st,TStringList *li_sql);
        void __fastcall BinStore(AnsiString st,TStringList *li_sql);
        bool __fastcall FindNode(TTreeNode *nd,int d_id);
        AnsiString __fastcall strEncrypt(AnsiString st,int a);
        AnsiString __fastcall strDecrypt(AnsiString st,int a);
        bool __fastcall FindText(AnsiString txt,bool MatchCase);
        void __fastcall Find(void);
        void __fastcall Split(AnsiString txt,char c);// General
        void __fastcall Split2(AnsiString txt,char c);//for Reciving loop
        AnsiString __fastcall GetBodyFromMid(int m_id);
        AnsiString __fastcall GetHeaderFromMid(int m_id);
        void __fastcall SetSign(void);
        AnsiString __fastcall GenFontIni(TFont *f,TColor bg);
        TColor __fastcall SetFontIni(TFont *f,AnsiString st);
        void __fastcall StateChangeClick(TObject *Sender);
        void __fastcall RefreshState(void);
        bool __fastcall ConnectDB(AnsiString host,AnsiString name,AnsiString user,AnsiString passwd,bool finfin);
        void __fastcall Exec(AnsiString fn);
        void __fastcall NoThreadRecieve(void);
        void __fastcall SelectMailFromMid(int m_id);
        void __fastcall ToSjis(AnsiString &st);
        AnsiString __fastcall Bencode(AnsiString st);
        void __fastcall ToSjisChar(char *buf,char *buf2,unsigned short cod,int len);
        void __fastcall NewMail(AnsiString fi);
        void __fastcall RefreshTemp(void);
        void __fastcall InsertTempDirect(TObject *Sender);

};
//---------------------------------------------------------------------------
extern PACKAGE TFrmQfs *FrmQfs;
//---------------------------------------------------------------------------
#endif
