home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / infoserv / gopher / Unix / xvgopher / v0.5beta / xvgopher.shar.01.Z / xvgopher.shar.01 / GWBookmarks.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-25  |  8.7 KB  |  370 lines

  1. //
  2. // GWBookmarks.cc
  3. //
  4. // (c) Copyright 1993, San Diego State University -- College of Sciences
  5. //       (See the COPYRIGHT file for more Copyright information)
  6. //
  7. //   This  file  contains  the  routines  of the GWBookmarks class which
  8. //   deal with the opening of windows.
  9. //
  10. #include "GWBookmarks.h"
  11. #include "xvgopher.h"
  12. #include "cursor.h"
  13. #include "icons.h"
  14. #include <fstream.h>
  15. #include <string.h>
  16. #include <xview/svrimage.h>
  17. #include <xview/icon.h>
  18. #include <xview/defaults.h>
  19.  
  20.  
  21. #define    KEY_SAVE_BTN        30010
  22. #define    KEY_SAVE_TXT        30011
  23.  
  24.  
  25. GWBookmarks    *bookmarks;
  26.  
  27.  
  28. //***************************************************************************
  29. // GWBookmarks::GWBookmarks(Frame par)
  30. //
  31. GWBookmarks::GWBookmarks(Frame par)
  32. {
  33.     parent = par;
  34. }
  35.  
  36.  
  37. //***************************************************************************
  38. // GWBookmarks::~GWBookmarks()
  39. //
  40. GWBookmarks::~GWBookmarks()
  41. {
  42.     write_bookmarks();
  43. }
  44.  
  45.  
  46. //***************************************************************************
  47. // int GWBookmarks::open(Response *resp)
  48. // PURPOSE:
  49. //   This  will  create a window in which the bookmarks directory will be
  50. //   displayed.
  51. //
  52. int GWBookmarks::open(Response *resp)
  53. {
  54.     if (GWList::open(resp) != OK)
  55.         return NOTOK;
  56.  
  57.     int    left = 8;        // Position of leftmost button in window
  58.     
  59.     //
  60.     // Create the save button and save text field.
  61.     //
  62.     Panel_item save_btn = (Panel_item) xv_create(panel, PANEL_BUTTON,
  63.         XV_X,                            left,
  64.         XV_Y,                            8,
  65.         PANEL_LABEL_STRING,                "Save",
  66.         PANEL_INACTIVE,                    TRUE,
  67.         NULL);
  68.     Panel_item save_txt = (Panel_item) xv_create(panel, PANEL_TEXT,
  69.         XV_X,                            left + 56,
  70.         XV_Y,                            8,
  71.         PANEL_LABEL_STRING,                "Save in:",
  72.         PANEL_VALUE_DISPLAY_WIDTH,        260,
  73.         PANEL_INACTIVE,                    TRUE,
  74.         NULL);
  75.  
  76.     xv_set(dir_list,
  77.         XV_KEY_DATA,                    KEY_SAVE_BTN,    save_btn,
  78.         XV_KEY_DATA,                    KEY_SAVE_TXT,    save_txt,
  79.         NULL);
  80.  
  81.     //
  82.     //   Now  some  things  which  need  to  be  set for both FRAMEs and
  83.     //   FRAME_CMDs
  84.     //
  85.     xv_set(frame,
  86.         FRAME_SHOW_FOOTER,                TRUE,
  87.         FRAME_RIGHT_FOOTER,                "Local bookmarks",
  88.         FRAME_DONE_PROC,                (void (*)(Frame)) done_proc,
  89.         XV_KEY_DATA,                    KEY_GWINDOW,    this,
  90.         XV_SHOW,                        preferences.get_popup_bookmarks(),
  91.         FRAME_LABEL,                    "Xvgopher Bookmarks",
  92.         NULL);
  93.  
  94.     modify_list_menu();
  95.  
  96.     read_bookmarks();
  97.  
  98.     return OK;
  99. }
  100.  
  101.  
  102. //***************************************************************************
  103. // void GWBookmarks::read_bookmarks()
  104. //
  105. void GWBookmarks::read_bookmarks()
  106. {
  107.     char        *home = getenv("HOME");
  108.     if (!home)
  109.         home = ".";
  110.     char        filename[100];
  111.     sprintf(filename, "%s/.xvgopher-bm", home);
  112.     ifstream    in(filename);
  113.     if (in.fail())
  114.         return;
  115.  
  116.     xv_set(dir_list,
  117.         XV_SHOW,                    FALSE,
  118.         NULL);
  119.     char    buffer[10000];
  120.     int        i = 0;
  121.     while (!in.eof())
  122.     {
  123.         in.getline(buffer, 10000);
  124.         if (in.eof())
  125.             break;
  126.         if (i == 0)    // First line of the file
  127.         {
  128.             //
  129.             // The first line of the file is supposed to contain the version number.
  130.             // However, if the file does not contain one, we will just assume that
  131.             // the file was created with version 1.0
  132.             //
  133.             if (strncmp(buffer, "XvGo", 4) == 0)
  134.             {
  135.                 //
  136.                 // We got a version number!
  137.                 //
  138.                 continue;    // There is only one version, so what are we worrying about???
  139.             }
  140.         }
  141.         Response    *r = new Response(buffer);
  142.         xv_set(dir_list,
  143.             PANEL_LIST_INSERT,            i,
  144.             PANEL_LIST_STRING,            i, r->get_title(),
  145.             PANEL_LIST_GLYPH,            i, get_image(r->get_type()),
  146.             PANEL_LIST_CLIENT_DATA,        i, r,
  147.             NULL);
  148.         i++;
  149.     }
  150.     xv_set(dir_list,
  151.         XV_SHOW,                    TRUE,
  152.         NULL);
  153. }
  154.  
  155.  
  156. //***************************************************************************
  157. // void GWBookmarks::write_bookmarks()
  158. //
  159. void GWBookmarks::write_bookmarks()
  160. {
  161.     char        *home = getenv("HOME");
  162.     if (!home)
  163.         home = ".";
  164.     char        filename[100];
  165.     sprintf(filename, "%s/.xvgopher-bm", home);
  166.     ofstream    out(filename);
  167.     if (out.fail())
  168.         return;
  169.  
  170.     int        n = (int) xv_get(dir_list, PANEL_LIST_NROWS);
  171.  
  172.     //
  173.     // First put the version number in the file so that we can determine later on
  174.     // if the file needs conversion or not.
  175.     //
  176.     out << "XvGo " << VERSION << "\n";
  177.  
  178.     for (int i = 0; i < n; i++)
  179.     {
  180.         Response    *r = (Response *) xv_get(dir_list, PANEL_LIST_CLIENT_DATA, i);
  181.         out << r->get_type() << r->get_title() << "\t" << r->get_command() << "\t" <<
  182.                 r->get_server() << "\t" << r->get_port() << "\n";
  183.     }
  184.     out.close();
  185. }
  186.  
  187.  
  188. //***************************************************************************
  189. // void GWBookmarks::row_deselect(int row, Response *resp)
  190. //
  191. void GWBookmarks::row_deselect(int row, Response *resp)
  192. {
  193.     Panel_item        save_btn = (Panel_item) xv_get(dir_list, XV_KEY_DATA, KEY_SAVE_BTN);
  194.     Panel_item        save_txt = (Panel_item) xv_get(dir_list, XV_KEY_DATA, KEY_SAVE_TXT);
  195.  
  196.     row = row;
  197.     resp = resp;
  198.     //
  199.     // Hide the button and the text item
  200.     //
  201.     xv_set(save_btn,
  202.         PANEL_INACTIVE,        TRUE,
  203.         NULL);
  204.     xv_set(save_txt,
  205.         PANEL_INACTIVE,        TRUE,
  206.         NULL);
  207.  
  208.     //
  209.     // The menu of the dir_list has the remove item.  This needs to be made inactive.
  210.     //
  211.     xv_set(bookmark_mi,
  212.         MENU_INACTIVE,                    TRUE,
  213.         NULL);
  214.     xv_set(show_info_mi,
  215.         MENU_INACTIVE,                    TRUE,
  216.         NULL);
  217. }
  218.  
  219.  
  220. //***************************************************************************
  221. // void GWBookmarks::row_select(int row, Response *resp)
  222. //
  223. void GWBookmarks::row_select(int row, Response *resp)
  224. {
  225.     Panel_item        save_btn = (Panel_item) xv_get(dir_list, XV_KEY_DATA, KEY_SAVE_BTN);
  226.     Panel_item        save_txt = (Panel_item) xv_get(dir_list, XV_KEY_DATA, KEY_SAVE_TXT);
  227.  
  228.     row = row;
  229.     resp = resp;
  230.     //
  231.     // Show the button and the text item if the selected item is savable
  232.     //
  233.     if (strchr("09", resp->get_type()))
  234.     {
  235.         xv_set(save_btn,
  236.             PANEL_INACTIVE,        FALSE,
  237.             NULL);
  238.         xv_set(save_txt,
  239.             PANEL_INACTIVE,        FALSE,
  240.             NULL);
  241.     }
  242.  
  243.     //
  244.     // The menu of the dir_list has the remove item.  This needs to be made active.
  245.     //
  246.     xv_set(bookmark_mi,
  247.         MENU_INACTIVE,                    FALSE,
  248.         NULL);
  249.     xv_set(show_info_mi,
  250.         MENU_INACTIVE,                    FALSE,
  251.         NULL);
  252. }
  253.  
  254.  
  255. //***************************************************************************
  256. // void GWBookmarks::show()
  257. //
  258. void GWBookmarks::show()
  259. {
  260.     xv_set(frame,
  261.         XV_SHOW,                TRUE,
  262.         NULL);
  263. }
  264.  
  265.  
  266. //***************************************************************************
  267. // void GWBookmarks::add(Response *resp)
  268. // PURPOSE:
  269. //   Add a line to the list of bookmarks.
  270. //
  271. void GWBookmarks::add(Response *resp)
  272. {
  273.     xv_set(dir_list,
  274.         XV_SHOW,                    FALSE,
  275.         NULL);
  276.  
  277.     int    n = (int) xv_get(dir_list, PANEL_LIST_NROWS);
  278.     xv_set(dir_list,
  279.         PANEL_LIST_INSERT,            n,
  280.         PANEL_LIST_STRING,            n, resp->get_title(),
  281.         PANEL_LIST_GLYPH,            n, get_image(resp->get_type()),
  282.         PANEL_LIST_CLIENT_DATA,        n, resp,
  283.         NULL);
  284.  
  285.     xv_set(dir_list,
  286.         XV_SHOW,                    TRUE,
  287.         NULL);
  288. }
  289.  
  290.  
  291. //***************************************************************************
  292. // void GWBookmarks::modify_list_menu()
  293. //   Modify the Bookmarks List menu so that if contains the following items:
  294. //        Remove bookmark
  295. //        Show item info
  296. //
  297. void GWBookmarks::modify_list_menu()
  298. {
  299.     //
  300.     // We want to modify the scrolling list's menu, so we need to get it,
  301.     // add the items and then put it back.
  302.     //
  303.     Menu    m = (Menu) xv_get(dir_list, PANEL_ITEM_MENU);
  304.     bookmark_mi = (Menu_item) xv_create(NULL, MENUITEM,
  305.         MENU_STRING,                    "Remove bookmark",
  306.         MENU_NOTIFY_PROC,                remove_bookmark_proc,
  307.         MENU_INACTIVE,                    TRUE,
  308.         XV_KEY_DATA,                    KEY_GWINDOW, this,
  309.         NULL);
  310.     xv_set(m,
  311.         MENU_APPEND_ITEM,                bookmark_mi,
  312.         NULL);
  313.     show_info_mi = (Menu_item) xv_create(NULL, MENUITEM,
  314.         MENU_STRING,                    "Show item info",
  315.         MENU_NOTIFY_PROC,                show_item_info_proc,
  316.         MENU_INACTIVE,                    FALSE,
  317.         XV_KEY_DATA,                    KEY_GWINDOW, this,
  318.         NULL);
  319.     xv_set(m,
  320.         MENU_APPEND_ITEM,                show_info_mi,
  321.         NULL);
  322.     xv_set(dir_list,
  323.         PANEL_ITEM_MENU,                m,
  324.         NULL);
  325. }
  326.  
  327.  
  328. //***************************************************************************
  329. // void GWBookmarks::remove_bookmark_proc(Menu menu, Menu_item mi)
  330. //
  331. void GWBookmarks::remove_bookmark_proc(Menu menu, Menu_item mi)
  332. {
  333.     GWBookmarks        *gwindow = (GWBookmarks *) xv_get(mi, XV_KEY_DATA, KEY_GWINDOW);
  334.  
  335.     //
  336.     // First make sure that there really is a row selected...
  337.     //
  338.     if (!xv_get(gwindow->dir_list, PANEL_LIST_SELECTED, gwindow->current_selected))
  339.         return;
  340.  
  341.     menu = menu;
  342.     //
  343.     // Some row was selected and we need to remove the row from the list.
  344.     //
  345.     Response *r = (Response *) xv_get(gwindow->dir_list, PANEL_LIST_CLIENT_DATA, gwindow->current_selected);
  346.     delete r;
  347.     xv_set(gwindow->dir_list,
  348.         PANEL_LIST_DELETE,            gwindow->current_selected,
  349.         NULL);
  350.     xv_set(gwindow->bookmark_mi,
  351.         MENU_INACTIVE,                    TRUE,
  352.         NULL);
  353. }
  354.  
  355.  
  356. //***************************************************************************
  357. // void GWBookmarks::done_proc(Frame frame)
  358. //   This gets called whenever a command frame is destroyed.  In the case
  359. //   of the book marks window, we only need to hide the window.
  360. //
  361. void GWBookmarks::done_proc(Frame frame)
  362. {
  363.     xv_set(frame,
  364.         XV_SHOW,                FALSE,
  365.         NULL);
  366. }
  367.  
  368.  
  369. //***************************************************************************
  370.