home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1998 February / PCOnline_02_1998.iso / filesbbs / dos / listz21s.exe / LSZ_APPL.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-07  |  12.4 KB  |  477 lines

  1. /*
  2.  * This file is part of Listerz v2.0 (VE)
  3.  *
  4.  * Copyright (C) 1995-1997 by Branislav L. Slantchev
  5.  * A Product of Silicon Creations, Inc.
  6.  *
  7.  * This file is distributed under the terms and conditions of the GNU
  8.  * General Public License. For more information, refer to the file
  9.  * Copying.Doc which is included in the archive.
  10. */
  11. #include "lsz_appl.h"
  12. #include "file.h"
  13. #include "pblsdk.h"
  14. #include "proboard.h"
  15. #include "pbnames.h"
  16. #include "stdmac.h"
  17. #include "lsz_area.h"
  18. #include "kbcodes.h"
  19.  
  20. static enum { TAG_SELECT, TAG_DESELECT, TAG_TOGGLE } g_TagState;
  21.  
  22. static void
  23. ChangeTag(void *pItem, void*)
  24. {
  25.     Area *item = (Area *)pItem;
  26.  
  27.     switch( g_TagState )
  28.     {
  29.         case TAG_SELECT  : item->m_tagged = True;  break;
  30.         case TAG_DESELECT: item->m_tagged = False; break;
  31.         case TAG_TOGGLE  : item->toggleTag();      break;
  32.     }
  33. }
  34.  
  35. const char * Application::s_AppName = "Listerz";
  36. const char * Application::s_DefaultText = "Building lists in Listerz";
  37. const char * Application::s_Version = "v2.0";
  38.  
  39. Application::Application(const char *argv0)
  40.     :m_AreaList(0)
  41.     ,m_GroupList(0)
  42.     ,m_Bucket(0)
  43.     ,m_adjusted(False)
  44. {
  45.     char  fileName[80];
  46.     FILE *fp;
  47.  
  48.     // load the configuration from binary .CNF file
  49.     file_chext(fileName, argv0, ".cnf");
  50.     if( 0 == (fp = fopen(fileName, "rb")) )
  51.     {
  52.         Log(LOG_FRIEND, "%s: cannot find config '%s'", s_AppName, fileName);
  53.         exit(EXIT_FAILURE);
  54.     }
  55.     fread(&m_Config, sizeof(m_Config), 1, fp);
  56.     fclose(fp);
  57.  
  58.     // do some adjustments
  59.     AdjustBounds();
  60.     sprintf(m_MainScreen, "%s%s", m_Config.sys_Path, m_Config.sys_MainFile);
  61.     sprintf(m_ProcScreen, "%s%s", m_Config.sys_Path, m_Config.sys_ProcFile);
  62.     sprintf(m_MainHeader, "%s%s", m_Config.sys_Path, m_Config.text_HeaderAll);
  63.     sprintf(m_MainFooter, "%s%s", m_Config.sys_Path, m_Config.text_FooterAll);
  64.     sprintf(m_AreaHeader, "%s%s", m_Config.sys_Path, m_Config.text_Header);
  65.     sprintf(m_AreaFooter, "%s%s", m_Config.sys_Path, m_Config.text_Footer);
  66.     memset(&m_CurArea, 0x00, sizeof(m_CurArea));
  67.  
  68.     // load the group definitions (if any)
  69.     if( 0 != (fp = pb_fopen(fnFGROUPS, "rb", PBDIR_SYS)) )
  70.     {
  71.         GROUP_PB group;
  72.         short    num = 0;
  73.  
  74.         if( 0 == (m_GroupList = new zDoubleList(True)) )
  75.         {
  76.             Log(LOG_FRIEND, "%s: Memory allocation failure", s_AppName);
  77.             fclose(fp);
  78.             exit(EXIT_FAILURE);
  79.         }
  80.  
  81.         file_read(fp, &group);
  82.         while( !feof(fp) )
  83.         {
  84.             num++;
  85.             if( pb_access(group.level, group.flags, group.flagsNot)
  86.                 && EOS != group.name[0] )
  87.             {
  88.                 Group *link = new Group(num, group);
  89.                 if( link ) m_GroupList->link(link);
  90.             }
  91.             file_read(fp, &group);
  92.         }
  93.         fclose(fp);
  94.  
  95.         // setup the listbox
  96.         m_GroupBox = new zListViewer(m_Config.lbox_Bounds, m_GroupList);
  97.         m_GroupBox->setClearColor(m_Config.lbox_Color);
  98.     }
  99.  
  100.     // assign the bucket to the file area definitions data
  101.     strcpy(fileName, pb_getpath(fnFILECFG, PBDIR_SYS));
  102.     if( 0 == (m_Bucket = new zBucket<FILECFG>(fileName, 50, False)) )
  103.     {
  104.         if( m_GroupList ) delete m_GroupList;
  105.         Log(LOG_FRIEND, "%s: could not load area definitions.", s_AppName);
  106.         exit(EXIT_FAILURE);
  107.     }
  108.  
  109.     // load the area indices for no group, current access only
  110.     if( 0 == (m_AreaList = new zDoubleList(True)) )
  111.     {
  112.         if( m_GroupList ) delete m_GroupList;
  113.         delete m_Bucket;
  114.         exit(EXIT_FAILURE);
  115.     }
  116.     LoadAreaDefs(0);
  117.     m_AreaBox = new zListViewer(m_Config.lbox_Bounds, m_AreaList);
  118.     m_AreaBox->setClearColor(m_Config.lbox_Color);
  119.  
  120.     // now create the command bars and the listbox viewers
  121.     zCommandItem *items =
  122.          new zCommandItem("e~x~it program", 'X', cmExit) +
  123.         *new zCommandItem("select ~a~ll areas", 'A', cmSelectAll) +
  124.         *new zCommandItem("~d~on't change group", 'D', cmIgnore);
  125.     m_GroupBar = new zCommandBar(m_Config.cbar_Bounds, items);
  126.  
  127.     items = new zCommandItem("e~x~it", 'X', cmExit) +
  128.         *new zCommandItem("~g~roups", 'G', cmGroups) +
  129.         *new zCommandItem("~m~ake list", 'M', cmMakeList) +
  130.         *new zCommandItem("~s~elect", 'S', cmSelectAll) +
  131.         *new zCommandItem("~d~eselect", 'D', cmUntagAll) +
  132.         *new zCommandItem("~t~oggle", 'T', cmToggleAll);
  133.     m_AreaBar = new zCommandBar(m_Config.cbar_Bounds, items);
  134.  
  135.     items = new zCommandItem("~z~ip", 'Z', cmZip) +
  136.         *new zCommandItem("~r~ar", 'R', cmRar) +
  137.         *new zCommandItem("~a~rj", 'A', cmArj) +
  138.         *new zCommandItem("~l~ha", 'L', cmLha) +
  139.         *new zCommandItem("z~o~o", 'O', cmZoo) +
  140.         *new zCommandItem("do ~n~ot compress", 'N', cmNoCompression) +
  141.         *new zCommandItem("a~b~ort", 'B', cmExit);
  142.     m_ArchiveBar = new zCommandBar(m_Config.cbar_Bounds, items);
  143.  
  144.     // disable relevant commands
  145.     if( !m_GroupList || !m_GroupList->size() )
  146.         m_AreaBar->enableCommand(cmGroups, False);
  147.     if( !m_Config.zip.enabled || !file_exist(m_Config.zip.path) )
  148.         m_ArchiveBar->enableCommand(cmZip, False);
  149.     if( !m_Config.rar.enabled || !file_exist(m_Config.rar.path) )
  150.         m_ArchiveBar->enableCommand(cmRar, False);
  151.     if( !m_Config.arj.enabled || !file_exist(m_Config.arj.path) )
  152.         m_ArchiveBar->enableCommand(cmArj, False);
  153.     if( !m_Config.lha.enabled || !file_exist(m_Config.lha.path) )
  154.         m_ArchiveBar->enableCommand(cmLha, False);
  155.     if( !m_Config.zoo.enabled || !file_exist(m_Config.zoo.path) )
  156.         m_ArchiveBar->enableCommand(cmZoo, False);
  157.  
  158.     // set up the palettes and various options
  159.     char Palette[15];
  160.     memset(Palette, 0x00, sizeof(Palette));
  161.     Palette[0]  = m_Config.cbar_Color;
  162.     Palette[1]  = m_Config.cbar_HotkeyColor;
  163.     Palette[2]  = (m_Config.cbar_Color & 0xF0) | 0x08;
  164.     Palette[3]  = m_Config.cbar_HiliteColor;
  165.     Palette[4]  = (m_Config.cbar_HiliteColor & 0xF0) | 0x07;
  166.     Palette[5]  = m_Config.cbar_Background;
  167.     Palette[6]  = Palette[11] = ' ';
  168.     Palette[7]  = m_Config.cbar_OffColor;
  169.     Palette[8]  = Palette[7];
  170.     Palette[9]  = (m_Config.cbar_OffColor & 0xF0) | 0x08;
  171.     Palette[10] = m_Config.cbar_OffBackground;
  172.  
  173.     m_GroupBar->setPalette(Palette);
  174.     m_GroupBar->options &= ~zCommandBar::delim;
  175.     m_GroupBar->options &= ~zCommandBar::focus;
  176.     m_AreaBar->setPalette(Palette);
  177.     m_AreaBar->options &= ~zCommandBar::delim;
  178.     m_AreaBar->options &= ~zCommandBar::focus;
  179.     m_ArchiveBar->setPalette(Palette);
  180.     m_ArchiveBar->options &= ~zCommandBar::delim;
  181.     m_ArchiveBar->options &= ~zCommandBar::focus;
  182.  
  183.     // are we going to have online user information
  184.     m_Useron = new zUserOnline(m_Config.drop_UserDoes, m_Config.drop_Doing);
  185.     if( m_Useron )
  186.     {
  187.         if( EOS == m_Config.drop_Text[0] )
  188.         {
  189.             strcpy(m_Config.drop_Text, s_DefaultText);
  190.         }
  191.         m_Useron->Create(NodeNumber, m_Config.drop_Text);
  192.     }
  193.  
  194.     // setup the terminal emulators
  195.     m_Terminal.RegisterHandler(&m_avt);
  196.     m_Terminal.RegisterHandler(&m_pcb);
  197.     m_Terminal.RegisterHandler(&m_ans);
  198.  
  199.     // install the keyboard handler
  200.     m_Keyboard = new zKeyboard;
  201.  
  202.     // setup the listerz file name
  203.     sprintf(fileName, "LISTERZ.%d", NodeNumber);
  204.     strcpy(m_fileName, pb_getpath(fileName, PBDIR_SYS));
  205. }
  206.  
  207. // free up any memory we allocated and remove useron info
  208. Application::~Application()
  209. {
  210.     if( m_Bucket     ) delete m_Bucket;
  211.     if( m_AreaList   ) delete m_AreaList;
  212.     if( m_GroupList  ) delete m_GroupList;
  213.     if( m_GroupBar   ) delete m_GroupBar;
  214.     if( m_AreaBar    ) delete m_AreaBar;
  215.     if( m_ArchiveBar ) delete m_ArchiveBar;
  216.     if( m_GroupBox   ) delete m_GroupBox;
  217.     if( m_AreaBox    ) delete m_AreaBox;
  218.     if( m_Keyboard   ) delete m_Keyboard;
  219.  
  220.     if( m_Useron )
  221.     {
  222.         m_Useron->Remove(NodeNumber);
  223.         delete m_Useron;
  224.     }
  225. }
  226.  
  227. Boolean
  228. Application::BelongsToGroup(short nGroup, const FILECFG &area)
  229. {
  230.     Boolean retval = True;
  231.  
  232.     if( 0 != nGroup && !area.allGroups )
  233.     {
  234.         retval = False;
  235.         for( int i = 0; i < 4; ++i )
  236.         {
  237.             if( nGroup == area.groups[i] )
  238.             {
  239.                 retval = True;
  240.                 break;
  241.             }
  242.         }
  243.     }
  244.  
  245.     return retval;
  246. }
  247.  
  248. // load area definitions for 'nGroup'. if nGroup == 0, all areas loaded
  249. // the return value signifies whether we have any entries in the new list
  250. Boolean
  251. Application::LoadAreaDefs(short nGroup)
  252. {
  253.     FILECFG  area;
  254.  
  255.     // get rid of the old list
  256.     while( m_AreaList->size() ) m_AreaList->unlink();
  257.     // build the new list
  258.     for( int i = 0; i < m_Bucket->nelems(); ++i )
  259.     {
  260.         area = (*m_Bucket)[i];
  261.         if( pb_access(area.level, area.flags, area.flagsNot)
  262.             && EOS != area.name[0]
  263.             && BelongsToGroup(nGroup, area) )
  264.         {
  265.             Area *area = new Area(i);
  266.             if( area ) m_AreaList->link(area);
  267.         }
  268.     }
  269.  
  270.     return Boolean( 0 != m_AreaList->size() );
  271. }
  272.  
  273. // adjust the rectangles to our own system: the grid we are using is
  274. // not the cell pattern, but rather, it is the grid between the cells
  275. void
  276. Application::AdjustBounds()
  277. {
  278.     if( !m_adjusted )
  279.     {
  280.         m_Config.lbox_Bounds.b.x += 1;
  281.         m_Config.lbox_Bounds.b.y += 1;
  282.         m_Config.cbar_Bounds.b.x += 1;
  283.         m_Config.cbar_Bounds.b.y += 1;
  284.         m_Config.proc_Bounds.b.x += 1;
  285.         m_Config.proc_Bounds.b.y += 1;
  286.         m_Config.area_Bounds.b.x += 1;
  287.         m_Config.area_Bounds.b.y += 1;
  288.         m_Config.area_Percent.b.x += 1;
  289.         m_Config.area_Percent.b.y += 1;
  290.         m_Config.total_Progbar.bounds.b.x += 1;
  291.         m_Config.total_Progbar.bounds.b.y += 1;
  292.         m_Config.area_Progbar.bounds.b.x += 1;
  293.         m_Config.area_Progbar.bounds.b.y += 1;
  294.         m_Config.total_Percent.b.x += 1;
  295.         m_Config.total_Percent.b.y += 1;
  296.         m_Config.total_Areas.b.x += 1;
  297.         m_Config.total_Areas.b.y += 1;
  298.         m_Config.total_Files.b.x += 1;
  299.         m_Config.total_Files.b.y += 1;
  300.         m_Config.total_Size.b.x += 1;
  301.         m_Config.total_Size.b.y += 1;
  302.         m_Config.total_ListSize.b.x += 1;
  303.         m_Config.total_ListSize.b.y += 1;
  304.  
  305.         m_adjusted = True;
  306.     }
  307. }
  308.  
  309. ushort
  310. Application::Process()
  311. {
  312.     ushort retval = cmCancel;
  313.  
  314.     m_AreaBar->setState(True);
  315.  
  316.     for( ;; )
  317.     {
  318.         ushort key = m_Keyboard->get();
  319.  
  320.         retval = m_AreaBar->handle(key);
  321.  
  322.         switch( retval )
  323.         {
  324.             case cmSelectAll:
  325.                 g_TagState = TAG_SELECT;
  326.                 m_AreaList->forEach(ChangeTag, 0);
  327.                 m_AreaBox->draw();
  328.                 break;
  329.  
  330.             case cmToggleAll:
  331.                 g_TagState = TAG_TOGGLE;
  332.                 m_AreaList->forEach(ChangeTag, 0);
  333.                 m_AreaBox->draw();
  334.                 break;
  335.  
  336.             case cmUntagAll:
  337.                 g_TagState = TAG_DESELECT;
  338.                 m_AreaList->forEach(ChangeTag, 0);
  339.                 m_AreaBox->draw();
  340.                 break;
  341.  
  342.             case cmMakeList:
  343.                 MakeAreaList();
  344.                 retval = cmCancel;
  345.                 goto FunctionExit;
  346.  
  347.             case cmGroups:
  348.                 retval = SetNewGroup();
  349.                 /* fall-through to default case */
  350.             case cmCancel:
  351.             case cmExit  :
  352.                 goto FunctionExit;
  353.         }
  354.     }
  355.  
  356. FunctionExit:
  357.     m_AreaBar->setState(False);
  358.     return retval;
  359. }
  360.  
  361. ushort
  362. Application::ProcessGroups()
  363. {
  364.     ushort       retval = cmCancel;
  365.  
  366.     m_GroupBar->setState(True);
  367.  
  368.     for( ;; )
  369.     {
  370.         ushort key = m_Keyboard->get();
  371.  
  372.         retval = m_GroupBar->handle(key);
  373.  
  374.         switch( retval )
  375.         {
  376.             case cmSelectAll:
  377.                 LoadAreaDefs(0);
  378.                 m_AreaBox->newList(m_AreaList);
  379.                 /* fall-through to the next case */
  380.             case cmIgnore:
  381.             case cmCancel:
  382.             case cmExit  :
  383.                 goto FunctionExit;
  384.         }
  385.     }
  386.  
  387. FunctionExit:
  388.     m_GroupBar->setState(False);
  389.     return retval;
  390. }
  391.  
  392. // select a new group (note that this ASSUMES that m_GroupList and
  393. // m_GroupBox are valid). since we disable the group command in the
  394. // other case, there should be no problems as long as cmGroup is checked
  395. ushort
  396. Application::SetNewGroup()
  397. {
  398.     int          retval = 0;
  399.     Boolean      done = False;
  400.  
  401.     // display the group listbox and command bar
  402.     m_GroupBox->draw();
  403.     m_GroupBar->draw();
  404.  
  405.     while( !done )
  406.     {
  407.         ushort key = m_Keyboard->get();
  408.  
  409.         switch( key )
  410.         {
  411.             case kbEsc:
  412.                 retval = ProcessGroups();
  413.                 if( retval != cmCancel ) done = True;
  414.                 break;
  415.  
  416.             case kbEnter:
  417.                 LoadAreaDefs(((Group*)m_GroupBox->focused())->m_num);
  418.                 m_AreaBox->newList(m_AreaList);
  419.                 done = True;
  420.                 break;
  421.  
  422.             default:
  423.                 m_GroupBox->handle(key);
  424.         }
  425.     }
  426.  
  427.     // display the area list box and command bar
  428.     if( cmExit != retval )
  429.     {
  430.         m_AreaBox->draw();
  431.         m_AreaBar->draw();
  432.     }
  433.  
  434.     return retval;
  435. }
  436.  
  437. /*
  438.  ***************************************************************************
  439.  * the principal application driver: runs the whole thing
  440.  ***************************************************************************
  441. */
  442. void
  443. Application::run()
  444. {
  445.     ushort       command;
  446.     Boolean      done = False;
  447.  
  448.     // display the initial screen, list, and command bar
  449.     clrscr();
  450.     pb_showfile(m_MainScreen, NULL, &m_Terminal);
  451.     m_AreaBox->draw();
  452.     m_AreaBar->draw();
  453.  
  454.     for( ; !done; )
  455.     {
  456.         ushort key = m_Keyboard->get();
  457.  
  458.         switch( key )
  459.         {
  460.             case kbEsc:
  461.                 command = Process();
  462.                 if( cmExit == command ) done = True;
  463.                 break;
  464.  
  465.             case ' '    :
  466.             case kbEnter:
  467.             case kbIns  :
  468.                 m_AreaBox->focused()->toggleTag();
  469.                 m_AreaBox->drawFocus();
  470.                 key = kbDown;
  471.                 /* fall-through to default handler */
  472.             default:
  473.                 m_AreaBox->handle(key);
  474.         }
  475.     }
  476. }
  477.