home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 217 / DPCS0306DVD.ISO / Toolkit / Internet / FileZilla / Server / FileZilla_Server-0.9.11.exe / source / interface / Options.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-12-01  |  8.7 KB  |  358 lines

  1. // FileZilla Server - a Windows ftp server
  2.  
  3. // Copyright (C) 2002-2004 - Tim Kosse <tim.kosse@gmx.de>
  4.  
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9.  
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14.  
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. // Options.cpp: Implementierungsdatei
  20. //
  21.  
  22. #include "stdafx.h"
  23. #include "options.h"
  24. #include "version.h"
  25. #include "filezilla server.h"
  26. #include "misc\MarkupSTL.h"
  27.  
  28. #if defined(_DEBUG) && !defined(MMGR)
  29. #define new DEBUG_NEW
  30. #undef THIS_FILE
  31. static char THIS_FILE[] = __FILE__;
  32. #endif
  33.  
  34. BOOL COptions::m_bInitialized=FALSE;
  35.  
  36. /////////////////////////////////////////////////////////////////////////////
  37. // Dialogfeld COptions 
  38.  
  39. COptions::COptions()
  40. {
  41.     for (int i=0;i<IOPTIONS_NUM;i++)
  42.         m_OptionsCache[i].bCached=FALSE;
  43. }
  44.  
  45. COptions::~COptions()
  46. {
  47. }
  48.  
  49. struct t_Option
  50. {
  51.     TCHAR name[30];
  52.     int nType;
  53. };
  54.  
  55. static const t_Option m_Options[IOPTIONS_NUM]={    _T("Start Minimized"),            1,
  56.                                                 _T("Last Server Address"),        0,
  57.                                                 _T("Last Server Port"),            1,
  58.                                                 _T("Last Server Password"),        0,
  59.                                                 _T("Always use last server"),    1,
  60.                                                 _T("User Sorting"),                1,
  61.                                                 _T("Filename Display"),            1
  62.                                                 };
  63.  
  64. void COptions::SetOption(int nOptionID, __int64 value)
  65. {
  66.     Init();
  67.  
  68.     m_OptionsCache[nOptionID-1].bCached = TRUE;
  69.     m_OptionsCache[nOptionID-1].nType = 1;
  70.     m_OptionsCache[nOptionID-1].value = value;
  71.  
  72.     TCHAR buffer[MAX_PATH + 1000]; //Make it large enough
  73.     GetModuleFileName( 0, buffer, MAX_PATH );
  74.     LPTSTR pos=_tcsrchr(buffer, '\\');
  75.     if (pos)
  76.         *++pos=0;
  77.     _tcscat(buffer, _T("FileZilla Server Interface.xml"));
  78.     CMarkupSTL xml;
  79.     if (!xml.Load(buffer))
  80.         return;
  81.     
  82.     if (!xml.FindElem( _T("FileZillaServer") ))
  83.         return;
  84.  
  85.     if (!xml.FindChildElem( _T("Settings") ))
  86.         xml.AddChildElem( _T("Settings") );
  87.     
  88.     CString valuestr;
  89.     valuestr.Format( _T("%I64d"), value);
  90.     xml.IntoElem();
  91.     BOOL res=xml.FindChildElem();
  92.     while (res)
  93.     {
  94.         CString name=xml.GetChildAttrib( _T("name"));
  95.         if (!_tcscmp(name, m_Options[nOptionID-1].name))
  96.         {
  97.             xml.SetChildAttrib(_T("name"), m_Options[nOptionID-1].name);
  98.             xml.SetChildAttrib(_T("type"), _T("numeric"));
  99.             xml.SetChildData(valuestr);
  100.             break;
  101.         }
  102.         res=xml.FindChildElem();
  103.     }
  104.     if (!res)
  105.     {
  106.         xml.InsertChildElem(_T("Item"), valuestr);
  107.         xml.SetChildAttrib(_T("name"), m_Options[nOptionID-1].name);
  108.         xml.SetChildAttrib(_T("type"), _T("numeric"));
  109.     }
  110.     xml.Save(buffer);
  111. }
  112.  
  113. void COptions::SetOption(int nOptionID, CString value)
  114. {
  115.     Init();
  116.  
  117.     m_OptionsCache[nOptionID-1].bCached = TRUE;
  118.     m_OptionsCache[nOptionID-1].nType = 0;
  119.     m_OptionsCache[nOptionID-1].str = value;
  120.  
  121.     TCHAR buffer[MAX_PATH + 1000]; //Make it large enough
  122.     GetModuleFileName( 0, buffer, MAX_PATH );
  123.     LPTSTR pos=_tcsrchr(buffer, '\\');
  124.     if (pos)
  125.         *++pos=0;
  126.     _tcscat(buffer, _T("FileZilla Server Interface.xml"));
  127.     CMarkupSTL xml;
  128.     if (!xml.Load(buffer))
  129.         return;
  130.     
  131.     if (!xml.FindElem( _T("FileZillaServer") ))
  132.         return;
  133.  
  134.     if (!xml.FindChildElem( _T("Settings") ))
  135.         xml.AddChildElem( _T("Settings") );
  136.     
  137.     xml.IntoElem();
  138.     BOOL res=xml.FindChildElem();
  139.     while (res)
  140.     {
  141.         CString name=xml.GetChildAttrib( _T("name"));
  142.         if (!_tcscmp(name, m_Options[nOptionID-1].name))
  143.         {
  144.             xml.SetChildAttrib(_T("name"), m_Options[nOptionID-1].name);
  145.             xml.SetChildAttrib(_T("type"), _T("string"));
  146.             xml.SetChildData(value);
  147.             break;
  148.         }
  149.         res=xml.FindChildElem();
  150.     }
  151.     if (!res)
  152.     {
  153.         xml.InsertChildElem( _T("Item"), value );
  154.         xml.SetChildAttrib(_T("name"), m_Options[nOptionID-1].name);
  155.         xml.SetChildAttrib(_T("type"), _T("string"));
  156.     }
  157.     xml.Save(buffer);
  158. }
  159.  
  160. CString COptions::GetOption(int nOptionID)
  161. {
  162.     ASSERT(nOptionID>0 && nOptionID<=IOPTIONS_NUM);
  163.     ASSERT(!m_Options[nOptionID-1].nType);
  164.     Init();
  165.     
  166.     if (m_OptionsCache[nOptionID-1].bCached)
  167.         return m_OptionsCache[nOptionID-1].str;
  168.  
  169.     //Verification
  170.     switch (nOptionID)
  171.     {
  172.     case IOPTION_LASTSERVERADDRESS:
  173.         m_OptionsCache[nOptionID-1].str = _T("127.0.0.1");
  174.         break;
  175.     default:
  176.         m_OptionsCache[nOptionID-1].str="";
  177.         break;
  178.     }
  179.     m_OptionsCache[nOptionID-1].bCached=TRUE;
  180.     m_OptionsCache[nOptionID-1].nType=0;
  181.     return m_OptionsCache[nOptionID-1].str;
  182. }
  183.  
  184. __int64 COptions::GetOptionVal(int nOptionID)
  185. {
  186.     ASSERT(nOptionID>0 && nOptionID<=IOPTIONS_NUM);
  187.     ASSERT(m_Options[nOptionID-1].nType == 1);
  188.     Init();
  189.     
  190.     if (m_OptionsCache[nOptionID-1].bCached)
  191.         return m_OptionsCache[nOptionID-1].value;
  192.  
  193.     switch (nOptionID)
  194.     {
  195.     case IOPTION_LASTSERVERPORT:
  196.         m_OptionsCache[nOptionID-1].value=14147;
  197.         break;
  198.     default:
  199.         m_OptionsCache[nOptionID-1].value=0;
  200.     }
  201.     m_OptionsCache[nOptionID-1].bCached=TRUE;
  202.     m_OptionsCache[nOptionID-1].nType=0;
  203.     return m_OptionsCache[nOptionID-1].value;
  204. }
  205.  
  206. void COptions::Init()
  207. {
  208.     if (m_bInitialized)
  209.         return;
  210.     m_bInitialized=TRUE;
  211.     CFileStatus status;
  212.     CMarkupSTL xml;
  213.     TCHAR buffer[MAX_PATH + 1000]; //Make it large enough
  214.     GetModuleFileName( 0, buffer, MAX_PATH );
  215.     LPTSTR pos=_tcsrchr(buffer, '\\');
  216.     if (pos)
  217.         *++pos=0;
  218.     _tcscat(buffer, _T("FileZilla Server Interface.xml"));
  219.     
  220.     for (int i=0; i<IOPTIONS_NUM; i++)
  221.         m_OptionsCache[i].bCached=FALSE;
  222.     
  223.     if (!CFile::GetStatus(buffer, status) )
  224.     {
  225.         xml.AddElem( _T("FileZillaServer") );
  226.         if (!xml.Save(buffer))
  227.             return;
  228.     }
  229.     else if (status.m_attribute&FILE_ATTRIBUTE_DIRECTORY)
  230.         return;
  231.         
  232.     if (xml.Load(buffer))
  233.     {
  234.         if (xml.FindElem( _T("FileZillaServer") ))
  235.         {
  236.             if (!xml.FindChildElem( _T("Settings") ))
  237.                 xml.AddChildElem( _T("Settings") );
  238.             
  239.             CString str;
  240.             xml.IntoElem();
  241.             str=xml.GetTagName();
  242.             while (xml.FindChildElem())
  243.             {
  244.                 CString value=xml.GetChildData();
  245.                 CString name=xml.GetChildAttrib( _T("name") );
  246.                 CString type=xml.GetChildAttrib( _T("type") );
  247.                 for (int i=0;i<IOPTIONS_NUM;i++)
  248.                 {
  249.                     if (!_tcscmp(name, m_Options[i].name))
  250.                     {
  251.                         if (m_OptionsCache[i].bCached)
  252.                         {
  253.                             ::AfxTrace( _T("Item '%s' is already in cache, ignoring item\n"), name);
  254.                             break;
  255.                         }
  256.                         else
  257.                         {
  258.                             if (type=="numeric")
  259.                             {
  260.                                 if (m_Options[i].nType!=1)
  261.                                 {
  262.                                     ::AfxTrace( _T("Type mismatch for option '%s'. Type is %d, should be %d"), name, m_Options[i].nType, 1);
  263.                                     break;
  264.                                 }
  265.                                 m_OptionsCache[i].bCached=TRUE;
  266.                                 m_OptionsCache[i].nType=1;
  267.                                 _int64 value64=_ttoi64(value);
  268.  
  269.                                 switch(i+1) {
  270.                                 case IOPTION_LASTSERVERPORT:
  271.                                     if (value64<1 || value64>65535)
  272.                                         value64 = 14147;
  273.                                     break;
  274.                                 default:
  275.                                     break;
  276.                                 }
  277.  
  278.                                 m_OptionsCache[i].value=value64;
  279.                             }
  280.                             else
  281.                             {
  282.                                 if (type!="string")
  283.                                     ::AfxTrace( _T("Unknown option type '%s' for item '%s', assuming string\n"), type, name);
  284.                                 if (m_Options[i].nType!=0)
  285.                                 {
  286.                                     ::AfxTrace( _T("Type mismatch for option '%s'. Type is %d, should be %d"), name, m_Options[i].nType, 0);
  287.                                     break;
  288.                                 }
  289.                                 m_OptionsCache[i].bCached=TRUE;
  290.                                 m_OptionsCache[i].nType=0;
  291.                                 
  292.                                 m_OptionsCache[i].str=value;
  293.                             }
  294.                         }
  295.                         break;
  296.                     }
  297.                 }
  298.             }
  299.             return;
  300.         }
  301.     }
  302. }
  303.  
  304. bool COptions::IsNumeric(LPCTSTR str)
  305. {
  306.     if (!str)
  307.         return false;
  308.     LPCTSTR p=str;
  309.     while(*p)
  310.     {
  311.         if (*p<'0' || *p>'9')
  312.         {
  313.             return false;
  314.         }
  315.         p++;
  316.     }
  317.     return true;
  318. }
  319.  
  320. CMarkupSTL *COptions::GetXML()
  321. {
  322.     TCHAR buffer[MAX_PATH + 1000]; //Make it large enough
  323.     GetModuleFileName( 0, buffer, MAX_PATH );
  324.     LPTSTR pos=_tcsrchr(buffer, '\\');
  325.     if (pos)
  326.         *++pos=0;
  327.     _tcscat(buffer, _T("FileZilla Server Interface.xml"));
  328.     CMarkupSTL *pXML=new CMarkupSTL;
  329.     if (!pXML->Load(buffer))
  330.     {
  331.         delete pXML;
  332.         return NULL;
  333.     }
  334.     
  335.     if (!pXML->FindElem( _T("FileZillaServer") ))
  336.     {
  337.         delete pXML;
  338.         return NULL;
  339.     }
  340.     return pXML;
  341. }
  342.  
  343. BOOL COptions::FreeXML(CMarkupSTL *pXML)
  344. {
  345.     ASSERT(pXML);
  346.     if (!pXML)
  347.         return FALSE;
  348.     TCHAR buffer[MAX_PATH + 1000]; //Make it large enough
  349.     GetModuleFileName( 0, buffer, MAX_PATH );
  350.     LPTSTR pos=_tcsrchr(buffer, '\\');
  351.     if (pos)
  352.         *++pos=0;
  353.     _tcscat(buffer, _T("FileZilla Server Interface.xml"));
  354.     if (!pXML->Save(buffer))
  355.         return FALSE;
  356.     delete pXML;
  357.     return TRUE;
  358. }