home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / on-line / amster_install / source / resultview.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-18  |  13.5 KB  |  496 lines

  1. /*
  2. ** Amster - Result view
  3. ** by Jacob Laursen <laursen@myself.com>
  4. */
  5.  
  6. #include "include/config.h"
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. #include <proto/dos.h>
  13.  
  14. #include "include/mui.h"
  15. #include <MUI/Lamp_mcc.h>
  16. #include <MUI/NListview_mcc.h>
  17.  
  18. #include "include/gui.h"
  19. #include "include/resultview.h"
  20. #include "include/protos.h"
  21. #include "include/napster.h"
  22. #include "include/download.h"
  23. #include "include/panel.h"
  24. #include "include/prefs.h"
  25. #include "include/search.h"
  26. #include "include/hotlist.h"
  27. #include "amster_Cat.h"
  28. #include "include/protos.h"
  29.  
  30. /* Private prototypes */
  31.  
  32. ULONG resultview_new(struct IClass *cl, Object *obj, struct opSet *msg);
  33. MUIF resultlistdisp(REG(a2) char **array, REG(a1) song s);
  34. MUIF resultlistcomp(REG(a0) struct Hook *hook, REG(a2) Object *obj, REG(a1) struct NList_CompareMessage *ncm);
  35. void resultview_clear(struct resultdata *data, int t);
  36. void resultview_download(struct resultdata *data, int t);
  37. void resultview_nick(struct resultdata *data, char *nick);
  38. void resultview_stat(struct resultdata *data, int t);
  39. void resultview_found(struct resultdata *data, song s);
  40. void resultview_reset(struct resultdata *data);
  41.  
  42. /* Global variables */
  43.  
  44. char *gColFormat;
  45.  
  46.  
  47. MUIF resultview_dispatch(REG(a0) struct IClass *cl,REG(a2) Object *obj,REG(a1) Msg msg)
  48. {
  49.     struct resultdata *data;
  50.  
  51.     switch (msg->MethodID) {
  52.         case OM_NEW:
  53.             return(resultview_new(cl,obj,(APTR)msg));
  54.         case RESULTVIEW_CLEAR:
  55.             data = INST_DATA(cl, obj);
  56.             resultview_clear(data, (int)(((muimsg)msg)->arg1));
  57.             return NULL;
  58.         case RESULTVIEW_DOWNLOAD:
  59.             data = INST_DATA(cl, obj);
  60.             resultview_download(data, (int)(((muimsg)msg)->arg1));
  61.             return NULL;
  62.         case RESULTVIEW_FOUND:
  63.             data = INST_DATA(cl, obj);
  64.             resultview_found(data, (song)(((muimsg)msg)->arg1));
  65.             return NULL;
  66.         case RESULTVIEW_TOGGLE_BOTH:
  67.             {
  68.             u_long tmp;
  69.             data = INST_DATA(cl, obj);
  70.             GetAttr(MUIA_Selected, data->fptog, &tmp);
  71.             prf->fullpath = tmp;
  72.             DoMethod(gui->searchpanel, SEARCH_TOGGLE);
  73.             DoMethod(gui->WI_Hotlist, HOTLIST_TOGGLE);
  74.             return NULL;
  75.             }
  76.         case RESULTVIEW_TOGGLE_ONE:
  77.             data = INST_DATA(cl, obj);
  78.             set(data->fptog, MUIA_Selected, prf->fullpath);
  79.             DoMethod(data->list, MUIM_NList_Redraw, MUIV_NList_Redraw_All);
  80.             return NULL;
  81.         case RESULTVIEW_STAT:
  82.             data = INST_DATA(cl, obj);
  83.             set(data->result, MUIA_Text_Contents, (char *)(((muimsg)msg)->arg1));
  84.             return NULL;
  85.         case RESULTVIEW_STAT2:
  86.             data = INST_DATA(cl, obj);
  87.             resultview_stat(data, (int)(((muimsg)msg)->arg1));
  88.             return NULL;
  89.         case RESULTVIEW_INSERT:
  90.             data = INST_DATA(cl, obj);
  91.             DoMethod(data->list, MUIM_NList_InsertSingle, (song)(((muimsg)msg)->arg1), MUIV_NList_Insert_Sorted);
  92.             return NULL;
  93.         case RESULTVIEW_NICK:
  94.             {
  95.             data = INST_DATA(cl, obj);
  96.             resultview_nick(data, (char *)(((muimsg)msg)->arg1));
  97.             return NULL;
  98.             }
  99.         case RESULTVIEW_RESETCOUNT:
  100.             data = INST_DATA(cl, obj);
  101.             data->resultview_count = 0;
  102.             return NULL;
  103.         case RESULTVIEW_GETSTATE:
  104.             data = INST_DATA(cl, obj);
  105.             search_state = data->resultview_state;
  106.             return NULL;
  107.     }
  108.     return(DoSuperMethodA(cl, obj, msg));
  109. }
  110.  
  111.  
  112. ULONG resultview_new(struct IClass *cl, Object *obj, struct opSet *msg)
  113. {
  114.     static struct Hook resultlistdispHook = { {0,0}, &resultlistdisp, NULL, NULL };
  115.     static struct Hook resultlistcompHook = { {0,0}, &resultlistcomp, NULL, NULL };
  116.     struct resultdata *data;
  117.  
  118.     Object *result, *list;
  119.     Object *BT_Clear, *BT_ClearAll, *BT_Download, *fptog;
  120. /*    Object *BT_Resume;*/
  121.  
  122.     if (obj = (Object *)DoSuperNew(cl, obj,
  123.         Child, VGroup,
  124.             Child, HGroup,
  125.                 Child, Label(MSG_RESULT),
  126.                 Child, result = TextObject, End,
  127.             End,
  128.             Child, NListviewObject,
  129.                 MUIA_NListview_NList, list = NListObject,
  130.                     InputListFrame,
  131.                     MUIA_NList_Title, TRUE,
  132.                     MUIA_NList_Format, gColFormat,
  133.                     MUIA_NList_MultiSelect, MUIV_NList_MultiSelect_Default,
  134.                     MUIA_NList_DisplayHook, &resultlistdispHook,
  135.                     MUIA_NList_CompareHook2, &resultlistcompHook,
  136.                     MUIA_CycleChain, 1,
  137.                 End,
  138.             End,
  139.             Child, HGroup,
  140.                 Child, BT_Clear    = SimpleButton(MSG_CLEAR_GAD),
  141.                 Child, BT_ClearAll = SimpleButton(MSG_CLEARALL_GAD),
  142. /*                Child, BT_Resume   = SimpleButton(MSG_RESUME_GAD),*/
  143.                 Child, BT_Download = SimpleButton(MSG_DOWNLOAD_GAD),
  144.                 Child, TextObject,
  145.                     MUIA_HorizWeight, 25,
  146.                     MUIA_Text_PreParse, "\33r",
  147.                     MUIA_Text_Contents, MSG_SEARCH_FULLPATH,
  148.                 End,
  149.                 Child, fptog = ImageObject,
  150.                     ButtonFrame,
  151.                     MUIA_HorizWeight, 10,
  152.                     MUIA_Background, MUII_ButtonBack,
  153.                     MUIA_InputMode, MUIV_InputMode_Toggle,
  154.                     MUIA_ShowSelState, FALSE,
  155.                     MUIA_Image_Spec, "6:15",
  156.                     MUIA_Selected, prf->fullpath,
  157.                 End,
  158.             End,
  159.         End,
  160.         TAG_MORE, msg->ops_AttrList))
  161.     {
  162.         data = INST_DATA(cl,obj);
  163.         data->result = result;
  164.         data->list = list;
  165. /*        data->BT_Resume = BT_Resume;*/
  166.         data->BT_Clear = BT_Clear;
  167.         data->BT_ClearAll = BT_ClearAll;
  168.         data->BT_Download = BT_Download;
  169.         data->fptog = fptog;
  170.  
  171.         lamp_useinlist(list);
  172.  
  173.         DoMethod(BT_Clear,    MUIM_Notify, MUIA_Pressed, FALSE, obj, 2, RESULTVIEW_CLEAR, CLEAR_MARKED);
  174.         DoMethod(BT_ClearAll, MUIM_Notify, MUIA_Pressed, FALSE, obj, 2, RESULTVIEW_CLEAR, CLEAR_ALL   );
  175.         DoMethod(BT_Download, MUIM_Notify, MUIA_Pressed, FALSE, obj, 2, RESULTVIEW_DOWNLOAD, 0);
  176. /*        DoMethod(BT_Resume, MUIM_Notify, MUIA_Pressed, FALSE, obj, 2, RESULTVIEW_DOWNLOAD, 2);*/
  177.         DoMethod(list, MUIM_Notify, MUIA_NList_DoubleClick, MUIV_EveryTime, obj, 2, RESULTVIEW_DOWNLOAD, 1);
  178.  
  179.         DoMethod(fptog, MUIM_Notify, MUIA_Selected, MUIV_EveryTime, obj, 1, RESULTVIEW_TOGGLE_BOTH);
  180.  
  181.         DoMethod(list, MUIM_Notify, MUIA_NList_TitleClick,  MUIV_EveryTime, list, 4, MUIM_NList_Sort3, MUIV_TriggerValue, MUIV_NList_SortTypeAdd_2Values, MUIV_NList_Sort3_SortType_Both);
  182.         DoMethod(list, MUIM_Notify, MUIA_NList_TitleClick2, MUIV_EveryTime, list, 4, MUIM_NList_Sort3, MUIV_TriggerValue, MUIV_NList_SortTypeAdd_2Values, MUIV_NList_Sort3_SortType_2);
  183.         DoMethod(list, MUIM_Notify, MUIA_NList_SortType,    MUIV_EveryTime, list, 3, MUIM_Set, MUIA_NList_TitleMark,  MUIV_TriggerValue);
  184.         DoMethod(list, MUIM_Notify, MUIA_NList_SortType2,   MUIV_EveryTime, list, 3, MUIM_Set, MUIA_NList_TitleMark2, MUIV_TriggerValue);
  185.  
  186.         return((ULONG)obj);
  187.     }
  188.     return NULL;
  189. }
  190.  
  191.  
  192. MUIF resultlistdisp(REG(a2) char **array, REG(a1) song s)
  193. {
  194.     static char buf[50], buf2[50], buf3[50], time[40];
  195.  
  196.     if (s) {
  197.         if (s->link == 0)
  198.             *array++ = lamp_getforlist(0);
  199.         else if(s->link < 5)
  200.             *array++ = lamp_getforlist(1);
  201.         else if(s->link < 8)
  202.             *array++ = lamp_getforlist(2);
  203.         else
  204.             *array++ = lamp_getforlist(3);
  205.  
  206.         if (prf->fullpath)
  207.             *array++ = s->title;
  208.         else
  209.             *array++ = nap_strippath(s->title);
  210.  
  211.         sprintf(buf,"\33c%d",s->bit);
  212.         *array++ = buf;
  213.         sprintf(buf2,"\33c%d",s->freq);
  214.         *array++ = buf2;
  215.         sprintf(buf3,"\33r%ld",s->size);
  216.         *array++ = buf3;
  217.  
  218.         if (s->time>0) {
  219.             int secs = s->size/(s->bit*125);
  220.             if(abs(s->time - secs)*100/s->time > 3)
  221.                 sprintf(time,"\33r\33i%ld:%02ld",s->time/60,s->time%60);
  222.             else
  223.                 sprintf(time,"\33r%ld:%02ld",s->time/60,s->time%60);
  224.         }
  225.         else {
  226.             sprintf(time,"\33r0:00");
  227.         }
  228.         *array++ = time;
  229.  
  230.         *array++ = s->user;
  231.         *array = nap_linktype[s->link];
  232.     }
  233.     else {
  234.         *array++ = "\33c@";
  235.         *array++ = (char *)MSG_LH_SONG;
  236.         *array++ = (char *)MSG_LH_KBPS;
  237.         *array++ = (char *)MSG_LH_HZ;
  238.         *array++ = (char *)MSG_LH_SIZE;
  239.         *array++ = (char *)MSG_LH_TIME;
  240.         *array++ = (char *)MSG_LH_USER;
  241.         *array = (char *)MSG_LH_LINK;
  242.     }
  243.     return(0);
  244. }
  245.  
  246.  
  247. void resultview_clear(struct resultdata *data, int t)
  248. {
  249.     u_long item;
  250.  
  251.     switch(t) {
  252.         case CLEAR_MARKED:
  253.         {
  254.             u_long listid = MUIV_NList_NextSelected_Start;
  255.  
  256.             for(;;) {
  257.                 DoMethod(data->list,MUIM_NList_NextSelected,&listid);
  258.                 if(listid==MUIV_NList_NextSelected_End) break;
  259.                 DoMethod(data->list,MUIM_NList_GetEntry,listid,&item);
  260.                 if(item) {
  261.                     DoMethod(data->list,MUIM_NList_Remove,MUIV_NList_Remove_Selected);
  262.                     nap_songfree((song)item);
  263.                 }
  264.             }
  265.             break;
  266.         }
  267.  
  268.         case CLEAR_ALL:
  269.             set(data->list,MUIA_NList_Quiet, MUIV_NList_Quiet_Visual);
  270.             while(1) {
  271.                 DoMethod(data->list,MUIM_NList_GetEntry,0,&item);
  272.                 if(!item) break;
  273.                 DoMethod(data->list,MUIM_NList_Remove,MUIV_NList_Remove_First);
  274.                 nap_songfree((song)item);
  275.             }
  276.             set(data->list,MUIA_NList_Quiet, MUIV_NList_Quiet_None);
  277.             resultview_stat(data, 0);
  278.             break;
  279.     }
  280. }
  281.  
  282.  
  283. void resultview_download(struct resultdata *data, int t)
  284. {
  285.     int f=0;
  286.     u_long item, listid;
  287.  
  288.     if (!gui_napon) return;
  289.  
  290.     switch (t) {
  291.         case 0:
  292.             listid = MUIV_NList_NextSelected_Start;
  293.             for (;;) {
  294.                 DoMethod(data->list, MUIM_NList_NextSelected, &listid);
  295.                 if (listid == MUIV_NList_NextSelected_End) break;
  296.                 DoMethod(data->list, MUIM_NList_GetEntry, listid, &item);
  297.                 if (item) {
  298.                     if (!f) {
  299.                         DoMethod(gui->iconpanel, PANEL_OPENDL);
  300.                         f=1;
  301.                     }
  302.                     dl_addq((song)item);
  303.                 }
  304.             }
  305.             break;
  306.  
  307.         case 1:
  308.             GetAttr(MUIA_NList_DoubleClick, data->list, &listid);
  309.             if (listid == -1 || listid == -2) return;
  310.             DoMethod(data->list, MUIM_NList_GetEntry, listid, &item);
  311.             if (!item) return;
  312.             if (!f) {
  313.                 DoMethod(gui->iconpanel, PANEL_OPENDL);
  314.                 f=1;
  315.             }
  316.             dl_addq((song)item);
  317.             break;
  318.  
  319.         case 2:
  320.             DoMethod(data->list, MUIM_NList_GetEntry, MUIV_NList_GetEntry_Active, &item);
  321.             if (!item) return;
  322. gui_debugf("Resume attempted for: %s", ((song)item)->title);
  323.             break;
  324.     }
  325. }
  326.  
  327.  
  328. void resultview_nick(struct resultdata *data, char *nick)
  329. {
  330.     if (!gui_napon) return;
  331.  
  332.     data->resultview_count = 0;
  333.     if (prf->clrlist) resultview_clear(data, CLEAR_ALL);
  334.     nap_sendbuf(NAPC_BROWSEUSER, nick);
  335.     resultview_stat(data, 3);
  336. }
  337.  
  338.  
  339. void resultview_stat(struct resultdata *data, int t)
  340. {
  341.     static char buf[80];
  342.     char *txt;
  343.  
  344.     switch(t) {
  345.         case 0:
  346.             txt = "";
  347.             data->resultview_state = 0;
  348.             break;
  349.  
  350.         case 1:
  351.             data->resultview_state = 1;
  352.             txt = (char *)MSG_STATUS1_SEARCHING;
  353.             break;
  354.  
  355.         case 2:
  356.             if (data->resultview_count == 0)
  357.                 txt = (char*)MSG_STATUS1_NOFILES;
  358.             else if (data->resultview_count == 1)
  359.                 txt = (char*)MSG_STATUS1_ONEFOUND;
  360.             else {
  361.                 sprintf(buf, MSG_STATUS1_FOUND, data->resultview_count);
  362.                 txt = buf;
  363.             }
  364.             data->resultview_state = 2;
  365.             break;
  366.  
  367.         case 3:
  368.             txt = (char *)MSG_STATUS1_REQLIST;
  369.             break;
  370.     }
  371.  
  372.     set(data->result, MUIA_Text_Contents, txt);
  373. }
  374.  
  375.  
  376. void resultview_found(struct resultdata *data, song s)
  377. {
  378.     if (s) {
  379.         DoMethod(data->list, MUIM_NList_InsertSingle, s, MUIV_NList_Insert_Sorted);
  380.         data->resultview_count++;
  381.     }
  382.     else {
  383.         resultview_stat(data, 2);
  384.     }
  385. }
  386.  
  387.  
  388. MUIF resultlistcomp(REG(a0) struct Hook *hook, REG(a2) Object *obj, REG(a1) struct NList_CompareMessage *ncm)
  389. {
  390.     song entry1 = ncm->entry1;
  391.     song entry2 = ncm->entry2;
  392.     LONG col1 = ncm->sort_type & MUIV_NList_TitleMark_ColMask;
  393.     LONG col2 = ncm->sort_type2 & MUIV_NList_TitleMark2_ColMask;
  394.     ULONG result = 0;
  395.  
  396.     if (ncm->sort_type == MUIV_NList_SortType_None) return (0);
  397.  
  398.     if (col1 == 1) {
  399.         if (ncm->sort_type & MUIV_NList_TitleMark_TypeMask) {
  400.             if (prf->fullpath) result = (LONG) stricmp(entry2->title, entry1->title);
  401.             else result = (LONG) stricmp(nap_strippath(entry2->title), nap_strippath(entry1->title));
  402.         }
  403.         else {
  404.             if (prf->fullpath) result = (LONG) stricmp(entry1->title, entry2->title);
  405.             else result = (LONG) stricmp(nap_strippath(entry1->title), nap_strippath(entry2->title));
  406.         }
  407.     }
  408.     else if (col1 == 2) {
  409.         if (ncm->sort_type & MUIV_NList_TitleMark_TypeMask)
  410.             result = entry2->bit - entry1->bit;
  411.         else
  412.             result = entry1->bit - entry2->bit;
  413.     }
  414.     else if (col1 == 3) {
  415.         if (ncm->sort_type & MUIV_NList_TitleMark_TypeMask)
  416.             result = entry2->freq - entry1->freq;
  417.         else
  418.             result = entry1->freq - entry2->freq;
  419.     }
  420.     else if (col1 == 4) {
  421.         if (ncm->sort_type & MUIV_NList_TitleMark_TypeMask)
  422.             result = entry2->size - entry1->size;
  423.         else
  424.             result = entry1->size - entry2->size;
  425.     }
  426.     else if (col1 == 5) {
  427.         if (ncm->sort_type & MUIV_NList_TitleMark_TypeMask)
  428.             result = entry2->time - entry1->time;
  429.         else
  430.             result = entry1->time - entry2->time;
  431.     }
  432.     else if (col1 == 6) {
  433.         if (ncm->sort_type & MUIV_NList_TitleMark_TypeMask)
  434.             result = (LONG) stricmp(entry2->user, entry1->user);
  435.         else
  436.             result = (LONG) stricmp(entry1->user, entry2->user);
  437.     }
  438.     else if (col1 == 7) {
  439.         if (ncm->sort_type & MUIV_NList_TitleMark_TypeMask)
  440.             result = entry2->link - entry1->link;
  441.         else
  442.             result = entry1->link - entry2->link;
  443.     }
  444.  
  445.     if ((result != 0) || (col1 == col2)) return (result);
  446.  
  447.     if (col2 == 1) {
  448.         if (ncm->sort_type & MUIV_NList_TitleMark2_TypeMask) {
  449.             if (prf->fullpath) result = (LONG) stricmp(entry2->title, entry1->title);
  450.             else result = (LONG) stricmp(nap_strippath(entry2->title), nap_strippath(entry1->title));
  451.         }
  452.         else {
  453.             if (prf->fullpath) result = (LONG) stricmp(entry1->title, entry2->title);
  454.             else result = (LONG) stricmp(nap_strippath(entry1->title), nap_strippath(entry2->title));
  455.         }
  456.     }
  457.     else if (col2 == 2) {
  458.         if (ncm->sort_type & MUIV_NList_TitleMark2_TypeMask)
  459.             result = entry2->bit - entry1->bit;
  460.         else
  461.             result = entry1->bit - entry2->bit;
  462.     }
  463.     else if (col2 == 3) {
  464.         if (ncm->sort_type & MUIV_NList_TitleMark2_TypeMask)
  465.             result = entry2->freq - entry1->freq;
  466.         else
  467.             result = entry1->freq - entry2->freq;
  468.     }
  469.     else if (col2 == 4) {
  470.         if (ncm->sort_type & MUIV_NList_TitleMark2_TypeMask)
  471.             result = entry2->size - entry1->size;
  472.         else
  473.             result = entry1->size - entry2->size;
  474.     }
  475.     else if (col2 == 5) {
  476.         if (ncm->sort_type & MUIV_NList_TitleMark2_TypeMask)
  477.             result = entry2->time - entry1->time;
  478.         else
  479.             result = entry1->time - entry2->time;
  480.     }
  481.     else if (col2 == 6) {
  482.         if (ncm->sort_type & MUIV_NList_TitleMark2_TypeMask)
  483.             result = (LONG) stricmp(entry2->user, entry1->user);
  484.         else
  485.             result = (LONG) stricmp(entry1->user, entry2->user);
  486.     }
  487.     else if (col2 == 7) {
  488.         if (ncm->sort_type & MUIV_NList_TitleMark2_TypeMask)
  489.             result = entry2->link - entry1->link;
  490.         else
  491.             result = entry1->link - entry2->link;
  492.     }
  493.  
  494.     return (result);
  495. }
  496.