Subversion Repositories Kolibri OS

Rev

Rev 4164 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. //Leency & SoUrcerer, LGPL
  2.  
  3. #define LIST_INFO_H 59
  4. int status_bar_h = 15;
  5.  
  6. scroll_bar scroll1 = { 17,200,210, LIST_INFO_H-3,18,0,115,15,0,0xCCCccc,0xD2CED0,0x555555,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1};
  7. scroll_bar scroll2 = { 17,200,210, LIST_INFO_H,18,0,115,15,0,0xCCCccc,0xD2CED0,0x555555,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1};
  8.  
  9.  
  10.  
  11. char *listbuffer;
  12. char *listpointer;
  13.  
  14. char *mailstart;
  15. char *mailend;
  16. int mailsize;
  17.  
  18.  
  19. enum {
  20.         GET_MAIL = 20,
  21.         SEND_MAIL,
  22.         DELETE_LETTER,
  23.         SAVE_LETTER,
  24.         STOP_LOADING,
  25.         EXIT_MAIL,
  26.         CHANGE_CHARSET,
  27.         CLOSE_CHANGE_CHARSET
  28. };
  29.  
  30.  
  31. void MailBoxLoop()
  32. {
  33.         int key, id;
  34.         mouse m;
  35.         int panels_drag = 0;
  36.         dword line_col, text_col;
  37.  
  38.         mail_list.h = Form.cheight/4;
  39.         mail_list.ClearList();
  40.         SetMailBoxStatus( NULL , NULL);
  41.         cur_charset = 0;
  42.         aim = SEND_NSTAT;
  43.        
  44.         goto _MB_DRAW;
  45.  
  46.         loop() 
  47.         {
  48.                 WaitEventTimeout(2);
  49.                 switch(EAX & 0xFF)
  50.                 {
  51.                         case evMouse:
  52.                                 IF (!CheckActiveProcess(Form.ID)) break;
  53.                                 m.get();
  54.                                
  55.                                 if (!m.lkm) panels_drag=0;
  56.                                 if (m.lkm) && (m.y>mail_list.y+mail_list.h-1) && (m.y<mail_list.y+mail_list.h+6)
  57.                                 && (!scroll1.delta2) && (!scroll2.delta2) panels_drag = 1;
  58.                                 if (panels_drag)
  59.                                 {
  60.                                         if (m.y<mail_list.y+mail_list.min_h) || (m.y>Form.cheight-letter_view.min_h-status_bar_h-LIST_INFO_H) break;
  61.                                         mail_list.h = m.y - mail_list.y-2;
  62.                                         DrawMailBox();
  63.                                         break;
  64.                                 }
  65.  
  66.                                 if (!mail_list.count) break;
  67.                                 if (!panels_drag) { scrollbar_v_mouse (#scroll1); scrollbar_v_mouse (#scroll2); }
  68.                                
  69.                                 if (mail_list.first <> scroll1.position)
  70.                                 {
  71.                                         mail_list.first = scroll1.position;
  72.                                         DrawMailList();
  73.                                         break;
  74.                                 };
  75.                                 if (letter_view.first <> scroll2.position)
  76.                                 {
  77.                                         letter_view.first = scroll2.position;
  78.                                         DrawLetter();
  79.                                         break;
  80.                                 };
  81.                                
  82.                                 if (mail_list.y+mail_list.h + 10 > m.y)
  83.                                 {
  84.                                         if (mail_list.MouseScroll(m.vert)) DrawMailList();
  85.                                 }
  86.                                 else
  87.                                 {
  88.                                         if (letter_view.MouseScroll(m.vert)) DrawLetter();
  89.                                 }
  90.                                 //if (!m.lkm) && (menu.ProcessMouse(mm.x, mm.y)) &&
  91.                                 //if (!m.lkm) && if (menu.ProcessMouse(mm.x, mm.y)) DrawMenuList();
  92.  
  93.                                 break;                         
  94.                         case evButton:
  95.                                 id = GetButtonID();
  96.                                 if (id==1) SaveAndExit();
  97.                                 if (id==GET_MAIL) aim = SEND_NSTAT;
  98.                                 if (id==SAVE_LETTER)
  99.                                 {
  100.                                         if (!mailstart) break;
  101.                                         WriteFile(strlen(mailstart), mailstart, "mail.txt");
  102.                                         pause(10);
  103.                                         RunProgram("tinypad", "mail.txt");
  104.                                 }
  105.                                 if (id==STOP_LOADING)
  106.                                 {
  107.                                         StopLoading();
  108.                                         DrawStatusBar();
  109.                                         DrawMailList();
  110.                                 }
  111.                                 if (id==EXIT_MAIL)
  112.                                 {
  113.                                         StopLoading();
  114.                                         Close(socketnum);
  115.                                         LoginBoxLoop();
  116.                                 }
  117.                                 if (id==CHANGE_CHARSET)
  118.                                 {
  119.                                         DefineButton(0,0,Form.cwidth,Form.cheight, CLOSE_CHANGE_CHARSET+BT_HIDE+BT_NOFRAME);
  120.                                         DrawRectangle(Form.cwidth-100, Form.cheight-status_bar_h- 70, 70, 82, sc.work_graph);
  121.                                         DrawRectangle3D(Form.cwidth-99, Form.cheight-status_bar_h- 69, 68, 80, 0xFFFfff, sc.work);
  122.                                         for (id=0; id<5; id++)
  123.                                         {
  124.                                                 if (cur_charset==id+1) { line_col=sc.work_button; text_col=sc.work_button_text; }
  125.                                                 else { line_col=sc.work; text_col=sc.work_text; }
  126.                                                 DrawBar(Form.cwidth-98, id*16+Form.cheight-status_bar_h- 68, 67, 16, line_col);
  127.                                                 DrawCaptButton(Form.cwidth-100, id*16+Form.cheight-status_bar_h- 68, 70,16, 10+id+BT_HIDE,
  128.                                                 0, text_col, charsets[id+1]);
  129.                                         }
  130.                                 }
  131.                                 if (id==CLOSE_CHANGE_CHARSET) goto _MB_DRAW;
  132.  
  133.                                 if (id>=30)
  134.                                 {
  135.                                         if (aim) break;
  136.                                         mail_list.current = mail_list.first + id - 30;
  137.                                         DrawMailList();
  138.                                         aim = SEND_RETR;
  139.                                 }
  140.  
  141.                                 break;                         
  142.                         case evKey:
  143.                                 key = GetKey();
  144.  
  145.                                 if (!aim) && (mail_list.ProcessKey(key))
  146.                                 {
  147.                                         DrawMailList();
  148.                                         aim = SEND_RETR;
  149.                                 }
  150.  
  151.                                 break;
  152.                         case evReDraw: _MB_DRAW:
  153.                                 if !(DefineWindow(MAILBOX_HEADER)) break;
  154.                                 scroll1.bckg_col = scroll2.bckg_col = 0xBBBbbb;
  155.                                 scroll1.frnt_col = scroll2.frnt_col = sc.work;
  156.                                 scroll1.line_col = scroll2.line_col = sc.work_graph;
  157.                                 DrawToolbar();
  158.                                 DrawMailBox();
  159.  
  160.                                 break;
  161.                         default:
  162.                                 if (aim == SEND_NSTAT)
  163.                                 {
  164.                                         SetLoginStatus("Counting mail, awaiting answer...");                   
  165.                                         request_len = GetRequest("STAT", NULL);
  166.                                         Send(socketnum, #request, request_len, 0);
  167.                                         if (EAX == 0xffffffff) { debug("Error sending STAT. Retry..."w); break;}
  168.                                         aim = GET_ANSWER_NSTAT;
  169.                                 }
  170.                                
  171.                                 if (aim == GET_ANSWER_NSTAT)
  172.                                 {
  173.                                         ticks = Receive(socketnum, #immbuffer, BUFFERSIZE, 0);
  174.                                         if ((ticks == 0xffffff) || (ticks < 2)) break;
  175.                                                                                
  176.                                         if (immbuffer[ticks-2]=='\n')
  177.                                         {
  178.                                                 debug(#immbuffer);
  179.                                                 if (strstr(#immbuffer,"+OK"))
  180.                                                 {
  181.                                                         mail_list.count = GetMailCount();
  182.                                                         free(listbuffer);
  183.                                                         listbuffer = mem_Alloc(30*mail_list.count); //24* original
  184.                                                         listpointer = listbuffer;      
  185.                                                         aim = SEND_NLIST;
  186.                                                         debug("Receiving mail list...");
  187.                                                 }
  188.                                                 else
  189.                                                 {
  190.                                                         StopConnect("Sorry, can't recieve your mail");
  191.                                                 }
  192.                                         }
  193.                                 }
  194.  
  195.                                 if (aim == SEND_NLIST)
  196.                                 {              
  197.                                         WriteText(5, Form.cheight-11, 0x80, sc.work_text, "Send LIST, awaiting answer...");
  198.                                         request_len = GetRequest("LIST", NULL);
  199.                                         Send(socketnum, #request, request_len, 0);
  200.                                         if (EAX == 0xffffffff) {debug("Error while sending LIST. Retry..."); break;}
  201.                                         aim = GET_ANSWER_NLIST;
  202.                                 }
  203.  
  204.                                 if (aim == GET_ANSWER_NLIST)
  205.                                 {              
  206.                                         ticks = Receive(socketnum, listpointer, listbuffer + 30*mail_list.count - listpointer, MSG_DONTWAIT);
  207.                                         if (ticks == 0xffffffff) break;
  208.                                         listpointer = listpointer + ticks;     
  209.                                        
  210.                                         if (listpointer - listbuffer < 5) break;
  211.                                         if (strncmp(listpointer-5,"\n.\n",5)==0)  // note that c-- assembles "\n.\n" to 0x0d, 0x0a, 0x2e, 0x0d, 0x0a   
  212.                                         {
  213.                                                 aim = SEND_RETR;
  214.                                                 debug("Got mail list");
  215.                                                 DrawMailBox();
  216.                                                
  217.                                                 *listpointer='\0';
  218.                                                 atr.CreateArray();
  219.                                                 atr.SetSizes();
  220.                                         }
  221.                                 }
  222.                                
  223.                                 if (aim == SEND_RETR)
  224.                                 {
  225.                                         from = to = date = subj = cur_charset = NULL;
  226.                                         letter_view.ClearList();
  227.                                         DrawMailBox();
  228.                                         debug("Send RETR, awaiting answer...");
  229.                                         request_len = GetRequest("RETR", itoa(mail_list.current+1));
  230.                                         if (Send(socketnum, #request, request_len, 0) == 0xffffffff)
  231.                                         {
  232.                                                 StopConnect("Error while trying to get letter from server");
  233.                                                 break;
  234.                                         }
  235.                                         mailsize = atr.GetSize(mail_list.current+1) + 1024;
  236.                                         free(mailstart);
  237.                                         if (!mailstart = malloc(mailsize))
  238.                                         {
  239.                                                 debug("alloc error!");
  240.                                                 aim=NULL;
  241.                                                 break;
  242.                                         }
  243.                                         mailend = mailstart;
  244.                                         aim = GET_ANSWER_RETR;
  245.                                 }
  246.                                
  247.                                 if (aim == GET_ANSWER_RETR)
  248.                                 {
  249.                                         ticks = Receive(socketnum, mailend, mailsize + mailstart - mailend, MSG_DONTWAIT);
  250.                                         if (ticks == 0xffffffff) break;
  251.                                        
  252.                                         mailend = mailend + ticks;
  253.  
  254.                                         if (!aim) break;
  255.  
  256.                                         if (mailsize + mailstart - mailend - 2 < 0)
  257.                                         {
  258.                                                 debug("Resizing buffer");
  259.                                                 mailsize += 4096;
  260.                                                 mailstart = realloc(mailstart, mailsize);
  261.                                                 if (!mailstart) { StopConnect("Realloc error!"); break;}
  262.                                         }
  263.  
  264.                                         if (mailsize>9000)
  265.                                         {
  266.                                                 id = mailend - mailstart * 100 ;
  267.                                                 id /= mailsize - 1024;
  268.                                                 if (id!=cur_st_percent) SetMailBoxStatus( id , NULL);
  269.                                         }
  270.  
  271.                                         //debug(mailstart);
  272.                                         //pause(10);
  273.                                         //debug("======================");
  274.                                         ParseMail();
  275.                                 }
  276.  
  277.                 }
  278.         }
  279. }
  280.  
  281.  
  282. void DrawMailBox()
  283. {
  284.         DrawMailList();
  285.         DrawLetterInfo();
  286.         DrawLetter();
  287.         DrawStatusBar();
  288. }
  289.  
  290.  
  291. void DrawToolbar()
  292. {
  293.         #define BUT_Y 7
  294.         #define BUT_H 22
  295.         #define BUT_W 74
  296.         #define BUT_SPACE 11
  297.         int toolbar_w = BUT_Y + BUT_H + BUT_Y + 3;
  298.         mail_list.SetSizes(0, toolbar_w, Form.cwidth - scroll1.size_x - 1, mail_list.h, 60,18);
  299.  
  300.         DrawBar(0,0, Form.cwidth,toolbar_w-3, sc.work);
  301.         DrawCaptButton(10                    , BUT_Y, BUT_W, BUT_H, GET_MAIL,      sc.work_button, sc.work_button_text,"Get mail");
  302.         DrawCaptButton(BUT_W+BUT_SPACE   + 10, BUT_Y, BUT_W, BUT_H, SEND_MAIL,     sc.work_button, sc.work_button_text,"Send Email");
  303.         DrawCaptButton(BUT_W+BUT_SPACE*2 + 10, BUT_Y, BUT_W, BUT_H, DELETE_LETTER, sc.work_button, sc.work_button_text,"Delete");
  304.         DrawCaptButton(BUT_W+BUT_SPACE*3 + 10, BUT_Y, BUT_W, BUT_H, SAVE_LETTER,   sc.work_button, sc.work_button_text,"Save");
  305.         DrawCaptButton(Form.cwidth-BUT_W - 10, BUT_Y, BUT_W, BUT_H, EXIT_MAIL,     sc.work_button, sc.work_button_text,"< Exit");
  306.  
  307.         DrawBar(0, mail_list.y-3, mail_list.w,1, sc.work_graph);
  308.         DrawBar(0, mail_list.y-2, mail_list.w,1, 0xdfdfdf);
  309.         DrawBar(0, mail_list.y-1, mail_list.w,1, 0xf0f0f0);
  310. }
  311.  
  312. void DrawMailList()
  313. {
  314.         int i, on_y, on_x, direction;
  315.         dword sel_col;
  316.         mail_list.visible = mail_list.h / mail_list.line_h;
  317.  
  318.         for (i=30; i<150; i++) DeleteButton(i);
  319.         for (i=0; (i<mail_list.visible) && (i+mail_list.first<mail_list.count); i++)
  320.         {
  321.                 on_y = i*mail_list.line_h + mail_list.y;
  322.                 if (mail_list.current==mail_list.first+i) sel_col=0xEEEeee; else sel_col=0xFFFfff;
  323.                 DrawBar(0, on_y, mail_list.w, mail_list.line_h-1, sel_col);
  324.                 direction = atr.GetDirection(i+mail_list.first+1);
  325.                 on_x = strlen(itoa(i+mail_list.first+1))*6;
  326.                 letter_icons_pal[0]=sel_col;
  327.                 PutPaletteImage(sizeof(letter_icons)/3*direction + #letter_icons, 18,12, on_x+18,
  328.                         mail_list.line_h-12/2+ on_y, 8, #letter_icons_pal);
  329.                 WriteText(on_x + 42, on_y+5, 0x80, 0, atr.GetSubject(i+mail_list.first+1));
  330.                 DefineButton(0, on_y, mail_list.w-1, mail_list.line_h, 30+i+BT_HIDE+BT_NOFRAME);
  331.                 DrawBar(0, on_y + mail_list.line_h-1, mail_list.w, 1, 0xCCCccc);
  332.                 WriteText(10, on_y+5, 0x80, 0, itoa(i+mail_list.first+1));
  333.                 WriteText(mail_list.w - 40, on_y+5, 0x80, 0, ConvertMemSize(atr.GetSize(i+mail_list.first+1)));
  334.         }
  335.         DrawBar(0, i*mail_list.line_h + mail_list.y, mail_list.w, -i*mail_list.line_h+mail_list.h, 0xFFFfff);
  336.         DrawScroller1();
  337. }
  338.  
  339. void DrawLetterInfo()
  340. {
  341.         int lt_y = mail_list.y+mail_list.h;
  342.         DrawBar(0, lt_y, mail_list.w, 1, sc.work_graph);
  343.         DrawBar(0, lt_y+1, Form.cwidth, 1, LBUMP);
  344.         DrawBar(0, lt_y+2, Form.cwidth, LIST_INFO_H-4, sc.work);
  345.         WriteText(mail_list.w-30/2, lt_y, 0x80, 0x888888, "= = =");
  346.         WriteText(mail_list.w-30/2, lt_y+1, 0x80, 0xEeeeee, "= = =");
  347.         DrawBar(0, lt_y+LIST_INFO_H-2, mail_list.w, 1, sc.work_graph); //bottom
  348.         DrawBar(0, lt_y+LIST_INFO_H-1, mail_list.w, 1, 0xdfdfdf);
  349.         DrawBar(0, lt_y+LIST_INFO_H  , mail_list.w, 1, 0xf0f0f0);
  350.         WriteTextB(10, lt_y+8 , 0x80, sc.work_text, "From:");
  351.         WriteText (45, lt_y+8 , 0x80, sc.work_text, #from);
  352.         WriteTextB(10, lt_y+20, 0x80, sc.work_text, "To:");
  353.         WriteText (45, lt_y+20, 0x80, sc.work_text, #to);
  354.         WriteTextB(10, lt_y+32, 0x80, sc.work_text, "Date:");
  355.         WriteText (45, lt_y+32, 0x80, sc.work_text, #date);
  356.         WriteTextB(10, lt_y+44, 0x80, sc.work_text, "Subject:");
  357.         WriteText (66, lt_y+44, 0x80, sc.work_text, #subj);
  358. }
  359.  
  360. void DrawLetter()
  361. {
  362.         int i=0;
  363.         dword cur_line, next_line, line_text;
  364.         cur_line = mdata;
  365.  
  366.         letter_view.SetSizes(0, mail_list.y+mail_list.h+LIST_INFO_H+1, Form.cwidth - scroll2.size_x - 1,
  367.                 Form.cheight - mail_list.y - mail_list.h - LIST_INFO_H - 1 - status_bar_h, 60, 12);
  368.  
  369.         if (mailstart) && (!aim)
  370.         {
  371.                 for ( ; i < letter_view.first; i++) cur_line = GetNextLine(cur_line);
  372.  
  373.                 for (i=0; i < letter_view.h / letter_view.line_h; i++)
  374.                 {
  375.                         next_line = GetNextLine(cur_line);
  376.                         line_text = CopyBetweenOffsets(cur_line, next_line);
  377.                         cur_line = next_line;
  378.                         if (cur_line >= mailend) || (cur_line==1) break;
  379.                         DrawBar(letter_view.x, i*letter_view.line_h + letter_view.y, letter_view.w, letter_view.line_h, 0xFFFfff);
  380.                         if (line_text) { WriteText(letter_view.x+5, i*letter_view.line_h+letter_view.y+3, 0x80, 0, line_text); free(line_text);}
  381.                 }
  382.         }
  383.         DrawBar(letter_view.x, i*letter_view.line_h + letter_view.y, letter_view.w, -i*letter_view.line_h + letter_view.h-1, 0xFFFfff);
  384.         DrawBar(letter_view.x, letter_view.y + letter_view.h-1, letter_view.w, 1, sc.work_graph);
  385.         DrawScroller2();
  386. }
  387.  
  388. void DrawScroller1()
  389. {
  390.         scroll1.max_area = mail_list.count;
  391.         scroll1.cur_area = mail_list.visible;
  392.         scroll1.position = mail_list.first;
  393.  
  394.         scroll1.all_redraw=1;
  395.         scroll1.start_x = mail_list.x + mail_list.w;
  396.         scroll1.start_y = mail_list.y - 3;
  397.         scroll1.size_y = mail_list.h + 4;
  398.         scrollbar_v_draw(#scroll1);
  399. }
  400.  
  401. void DrawScroller2()
  402. {
  403.         scroll2.max_area = letter_view.count;
  404.         scroll2.cur_area = letter_view.visible;
  405.         scroll2.position = letter_view.first;
  406.  
  407.         scroll2.all_redraw=1;
  408.         scroll2.start_x = letter_view.x + letter_view.w;
  409.         scroll2.start_y = letter_view.y - 3;
  410.         scroll2.size_y = letter_view.h + 3;
  411.         scrollbar_v_draw(#scroll2);
  412. }
  413.  
  414.  
  415.  
  416. void DrawStatusBar()
  417. {
  418.         int st_y = Form.cheight -status_bar_h;
  419.         DrawBar(0, st_y, Form.cwidth, status_bar_h, sc.work);
  420.         if (aim) {
  421.                 SetMailBoxStatus(cur_st_percent, cur_st_text);
  422.                 DrawCaptButton(240, st_y+1, 36, status_bar_h-3, STOP_LOADING, sc.work_button, sc.work_button_text,"Stop");
  423.         }
  424.         DrawCaptButton(Form.cwidth - 100, st_y+1, 70, status_bar_h-2, CHANGE_CHARSET+BT_HIDE, sc.work, sc.work_text,charsets[cur_charset]);
  425. }
  426.  
  427.  
  428.  
  429. void SetMailBoxStatus(dword percent1, text1)
  430. {
  431.         if (text1) WriteText(3, Form.cheight -status_bar_h + 3, 0x80, sc.work_text, text1);
  432.         cur_st_text = text1;
  433. }
  434.  
  435.  
  436. void StopLoading()
  437. {
  438.         aim = NULL;
  439.         mailstart = free(mailstart);
  440.         to = from = date = subj = cur_charset = NULL;
  441. }
  442.  
  443. int GetMailCount(){
  444.         char tmpbuf4[512];
  445.         strcpyb(#immbuffer, #tmpbuf4, "+OK ", " ");
  446.         return atoi(#tmpbuf4);
  447. }
  448.  
  449. int GetLetterSize_(int number)
  450. {
  451.    char search_num[24];
  452.    char mailsize1[24];
  453.    strcpy(#search_num, "\n");       // 0x0d, 0x0a
  454.    itoa_(#search_num+1, number);
  455.    chrcat(#search_num, ' ');
  456.    strcpyb(listbuffer, #mailsize1, #search_num, "\x0d");
  457.    return atoi(#mailsize1);
  458. }
  459.  
  460.  
  461.  
  462.  
  463.