/[weechat]/weechat/src/gui/gui-common.c
ViewVC logotype

Diff of /weechat/src/gui/gui-common.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.60 by flashcode, Sun Oct 30 22:56:00 2005 UTC revision 1.61 by flashcode, Fri Nov 4 11:53:25 2005 UTC
# Line 31  Line 31 
31  #include <string.h>  #include <string.h>
32  #include <signal.h>  #include <signal.h>
33  #include <time.h>  #include <time.h>
34    #include <ctype.h>
35    
36  #include "../common/weechat.h"  #include "../common/weechat.h"
37  #include "gui.h"  #include "gui.h"
# Line 302  gui_buffer_clear (t_gui_buffer *buffer) Line 303  gui_buffer_clear (t_gui_buffer *buffer)
303  {  {
304      t_gui_window *ptr_win;      t_gui_window *ptr_win;
305      t_gui_line *ptr_line;      t_gui_line *ptr_line;
     t_gui_message *ptr_message;  
306            
307      while (buffer->lines)      while (buffer->lines)
308      {      {
309          ptr_line = buffer->lines->next_line;          ptr_line = buffer->lines->next_line;
310          while (buffer->lines->messages)          if (buffer->lines->data)
311          {              free (buffer->lines->data);
             ptr_message = buffer->lines->messages->next_message;  
             if (buffer->lines->messages->message)  
                 free (buffer->lines->messages->message);  
             free (buffer->lines->messages);  
             buffer->lines->messages = ptr_message;  
         }  
312          free (buffer->lines);          free (buffer->lines);
313          buffer->lines = ptr_line;          buffer->lines = ptr_line;
314      }      }
# Line 351  gui_buffer_clear_all () Line 345  gui_buffer_clear_all ()
345          gui_buffer_clear (ptr_buffer);          gui_buffer_clear (ptr_buffer);
346  }  }
347    
 /*  
  * gui_infobar_printf: display message in infobar  
  */  
   
 void  
 gui_infobar_printf (int time_displayed, int color, char *message, ...)  
 {  
     static char buffer[1024];  
     va_list argptr;  
     t_gui_infobar *ptr_infobar;  
     char *pos, *buf2;  
       
     va_start (argptr, message);  
     vsnprintf (buffer, sizeof (buffer) - 1, message, argptr);  
     va_end (argptr);  
       
     buf2 = weechat_convert_encoding ((local_utf8) ?  
                                      cfg_look_charset_decode_iso : cfg_look_charset_decode_utf,  
                                      (cfg_look_charset_internal && cfg_look_charset_internal[0]) ?  
                                      cfg_look_charset_internal : local_charset,  
                                      buffer);  
       
     ptr_infobar = (t_gui_infobar *)malloc (sizeof (t_gui_infobar));  
     if (ptr_infobar)  
     {  
         ptr_infobar->color = color;  
         ptr_infobar->text = strdup (buf2);  
         pos = strchr (ptr_infobar->text, '\n');  
         if (pos)  
             pos[0] = '\0';  
         ptr_infobar->remaining_time = (time_displayed <= 0) ? -1 : time_displayed;  
         ptr_infobar->next_infobar = gui_infobar;  
         gui_infobar = ptr_infobar;  
         gui_draw_buffer_infobar (gui_current_window->buffer, 1);  
     }  
     else  
         wee_log_printf (_("Not enough memory for infobar message\n"));  
       
     free (buf2);  
 }  
   
348  /*  /*
349   * gui_window_free: delete a window   * gui_window_free: delete a window
350   */   */
# Line 416  gui_window_free (t_gui_window *window) Line 369  gui_window_free (t_gui_window *window)
369  }  }
370    
371  /*  /*
  * gui_infobar_remove: remove last displayed message in infobar  
  */  
   
 void  
 gui_infobar_remove ()  
 {  
     t_gui_infobar *new_infobar;  
       
     if (gui_infobar)  
     {  
         new_infobar = gui_infobar->next_infobar;  
         if (gui_infobar->text)  
             free (gui_infobar->text);  
         free (gui_infobar);  
         gui_infobar = new_infobar;  
     }  
 }  
   
 /*  
372   * gui_line_free: delete a line from a buffer   * gui_line_free: delete a line from a buffer
373   */   */
374    
# Line 442  void Line 376  void
376  gui_line_free (t_gui_line *line)  gui_line_free (t_gui_line *line)
377  {  {
378      t_gui_window *ptr_win;      t_gui_window *ptr_win;
379      t_gui_message *ptr_message;      
   
380      for (ptr_win = gui_windows; ptr_win; ptr_win = ptr_win->next_window)      for (ptr_win = gui_windows; ptr_win; ptr_win = ptr_win->next_window)
381      {      {
382          if (ptr_win->start_line == line)          if (ptr_win->start_line == line)
# Line 452  gui_line_free (t_gui_line *line) Line 385  gui_line_free (t_gui_line *line)
385              ptr_win->start_line_pos = 0;              ptr_win->start_line_pos = 0;
386          }          }
387      }      }
388      while (line->messages)      if (line->data)
389      {          free (line->data);
         ptr_message = line->messages->next_message;  
         if (line->messages->message)  
             free (line->messages->message);  
         free (line->messages);  
         line->messages = ptr_message;  
     }  
390      free (line);      free (line);
391  }  }
392    
# Line 566  gui_new_line (t_gui_buffer *buffer) Line 493  gui_new_line (t_gui_buffer *buffer)
493          new_line->log_write = 1;          new_line->log_write = 1;
494          new_line->line_with_message = 0;          new_line->line_with_message = 0;
495          new_line->line_with_highlight = 0;          new_line->line_with_highlight = 0;
496          new_line->messages = NULL;          new_line->data = NULL;
         new_line->last_message = NULL;  
497          if (!buffer->lines)          if (!buffer->lines)
498              buffer->lines = new_line;              buffer->lines = new_line;
499          else          else
# Line 602  gui_new_line (t_gui_buffer *buffer) Line 528  gui_new_line (t_gui_buffer *buffer)
528  }  }
529    
530  /*  /*
531   * gui_new_message: create a new message for last line of a buffer   * gui_word_strlen: returns length of a word
532     *                  special chars like color, bold, .. are ignored
533     */
534    
535    int
536    gui_word_strlen (t_gui_window *window, char *string)
537    {
538        int length;
539        
540        length = 0;
541        while (string && string[0])
542        {
543            string = gui_word_get_next_char (window, (unsigned char *)string, 0);
544            if (string)
545                length++;
546        }
547        return length;
548    }
549    
550    /*
551     * gui_word_real_pos: get real position in string (ignoring color/bold/.. chars)
552     */
553    
554    int
555    gui_word_real_pos (t_gui_window *window, char *string, int pos)
556    {
557        char *saved_pos;
558        int real_pos;
559        
560        if (pos <= 0)
561            return 0;
562        
563        real_pos = 0;
564        while (string && string[0] && (pos > 0))
565        {
566            saved_pos = string;
567            string = gui_word_get_next_char (window, (unsigned char *)string, 0);
568            pos--;
569            if (string)
570                real_pos += (string - saved_pos);
571        }
572        return real_pos;
573    }
574    
575    /*
576     * gui_add_to_line: add a message to last line of buffer
577     */
578    
579    void
580    gui_add_to_line (t_gui_buffer *buffer, int type, char *message)
581    {
582        char *pos;
583        int length;
584        
585        /* create new line if previous was ending by '\n' (or if 1st line) */
586        if (buffer->line_complete)
587        {
588            buffer->line_complete = 0;
589            if (!gui_new_line (buffer))
590                return;
591        }
592        
593        pos = strchr (message, '\n');
594        if (pos)
595        {
596            pos[0] = '\0';
597            buffer->line_complete = 1;
598        }
599        if (buffer->last_line->data)
600        {
601            buffer->last_line->data = (char *) realloc (buffer->last_line->data,
602                                                        strlen (buffer->last_line->data) +
603                                                        strlen (message) + 1);
604            strcat (buffer->last_line->data, message);
605        }
606        else
607            buffer->last_line->data = strdup (message);
608        
609        length = gui_word_strlen (NULL, message);
610        buffer->last_line->length += length;
611        if (type & MSG_TYPE_MSG)
612            buffer->last_line->line_with_message = 1;
613        if (type & MSG_TYPE_HIGHLIGHT)
614            buffer->last_line->line_with_highlight = 1;
615        if ((type & MSG_TYPE_TIME) || (type & MSG_TYPE_NICK) || (type & MSG_TYPE_PREFIX))
616            buffer->last_line->length_align += length;
617        if (type & MSG_TYPE_NOLOG)
618            buffer->last_line->log_write = 0;
619        if (pos)
620        {
621            pos[0] = '\n';
622            if (buffer->num_displayed > 0)
623                gui_draw_buffer_chat (buffer, 0);
624            if (gui_add_hotlist && (buffer->num_displayed == 0))
625            {
626                if (3 - buffer->last_line->line_with_message -
627                    buffer->last_line->line_with_highlight <=
628                    buffer->notify_level)
629                {
630                    if (buffer->last_line->line_with_highlight)
631                        hotlist_add (HOTLIST_HIGHLIGHT, buffer);
632                    else if (BUFFER_IS_PRIVATE(buffer) && (buffer->last_line->line_with_message))
633                        hotlist_add (HOTLIST_PRIVATE, buffer);
634                    else if (buffer->last_line->line_with_message)
635                        hotlist_add (HOTLIST_MSG, buffer);
636                    else
637                        hotlist_add (HOTLIST_LOW, buffer);
638                    gui_draw_buffer_status (gui_current_window->buffer, 1);
639                }
640            }
641        }
642        if (buffer->line_complete && buffer->log_file && buffer->last_line->log_write)
643        {
644            log_write (buffer, buffer->last_line->data);
645            log_write (buffer, "\n");
646        }
647    }
648    
649    /*
650     * gui_printf_internal: display a message in a buffer
651     *                      This function should NEVER be called directly.
652     *                      You should use macros defined in gui.h
653   */   */
654    
655  t_gui_message *  void
656  gui_new_message (t_gui_buffer *buffer)  gui_printf_internal (t_gui_buffer *buffer, int display_time, int type, char *message, ...)
657  {  {
658      t_gui_message *new_message;      static char buf[8192];
659        char text_time[1024];
660        char text_time_char[2];
661        time_t time_seconds;
662        struct tm *local_time;
663        int time_first_digit, time_last_digit;
664        char *pos, *buf2;
665        int i;
666        va_list argptr;
667        static time_t seconds;
668        struct tm *date_tmp;
669            
670      if ((new_message = (t_gui_message *) malloc (sizeof (struct t_gui_message))))      if (gui_init_ok)
671      {      {
672          if (!buffer->last_line->messages)          if (buffer == NULL)
673              buffer->last_line->messages = new_message;          {
674                type |= MSG_TYPE_NOLOG;
675                if (SERVER(gui_current_window->buffer))
676                    buffer = SERVER(gui_current_window->buffer)->buffer;
677                else
678                    buffer = gui_current_window->buffer;
679                
680                if (!buffer || buffer->dcc)
681                    buffer = gui_buffers;
682            }
683        
684            if (buffer == NULL)
685            {
686                wee_log_printf ("WARNING: gui_printf_internal without buffer! This is a bug, "
687                                "please send to developers - thanks\n");
688                return;
689            }
690            
691            if (buffer->dcc)
692                buffer = gui_buffers;
693            
694            if (buffer->dcc)
695                return;
696        }
697        
698        va_start (argptr, message);
699        vsnprintf (buf, sizeof (buf) - 1, message, argptr);
700        va_end (argptr);
701        
702        if (!buf[0])
703            return;
704        
705        if (!local_utf8 || !utf8_is_valid (buf))
706            buf2 = weechat_convert_encoding ((local_utf8) ?
707                                             cfg_look_charset_decode_iso : cfg_look_charset_decode_utf,
708                                             (cfg_look_charset_internal && cfg_look_charset_internal[0]) ?
709                                             cfg_look_charset_internal : local_charset,
710                                             buf);
711        else
712            buf2 = strdup (buf);
713        
714        if (gui_init_ok)
715        {
716            seconds = time (NULL);
717            date_tmp = localtime (&seconds);
718            
719            pos = buf2 - 1;
720            while (pos)
721            {
722                if (display_time
723                    && cfg_look_buffer_timestamp && cfg_look_buffer_timestamp[0]
724                    && ((!buffer->last_line) || (buffer->line_complete)))
725                {
726                    time_seconds = time (NULL);
727                    local_time = localtime (&time_seconds);
728                    strftime (text_time, sizeof (text_time), cfg_look_buffer_timestamp, local_time);
729                    
730                    time_first_digit = -1;
731                    time_last_digit = -1;
732                    i = 0;
733                    while (text_time[i])
734                    {
735                        if (isdigit (text_time[i]))
736                        {
737                            if (time_first_digit == -1)
738                                time_first_digit = i;
739                            time_last_digit = i;
740                        }
741                        i++;
742                    }
743                    
744                    text_time_char[1] = '\0';
745                    i = 0;
746                    while (text_time[i])
747                    {
748                        text_time_char[0] = text_time[i];
749                        if (time_first_digit < 0)
750                        {
751                            gui_add_to_line (buffer, MSG_TYPE_TIME,
752                                             GUI_COLOR(COLOR_WIN_CHAT_TIME));
753                            gui_add_to_line (buffer, MSG_TYPE_TIME, text_time_char);
754                        }
755                        else
756                        {
757                            if ((i < time_first_digit) || (i > time_last_digit))
758                            {
759                                gui_add_to_line (buffer, MSG_TYPE_TIME,
760                                                 GUI_COLOR(COLOR_WIN_CHAT_DARK));
761                                gui_add_to_line (buffer, MSG_TYPE_TIME, text_time_char);
762                            }
763                            else
764                            {
765                                if (isdigit (text_time[i]))
766                                {
767                                    gui_add_to_line (buffer, MSG_TYPE_TIME,
768                                                     GUI_COLOR(COLOR_WIN_CHAT_TIME));
769                                    gui_add_to_line (buffer, MSG_TYPE_TIME, text_time_char);
770                                }
771                                else
772                                {
773                                    gui_add_to_line (buffer, MSG_TYPE_TIME,
774                                                     GUI_COLOR(COLOR_WIN_CHAT_TIME_SEP));
775                                    gui_add_to_line (buffer, MSG_TYPE_TIME, text_time_char);
776                                }
777                            }
778                        }
779                        i++;
780                    }
781                    gui_add_to_line (buffer, MSG_TYPE_TIME, GUI_COLOR(COLOR_WIN_CHAT));
782                    gui_add_to_line (buffer, MSG_TYPE_TIME, " ");
783                }
784                gui_add_to_line (buffer, type, pos + 1);
785                pos = strchr (pos + 1, '\n');
786                if (pos && !pos[1])
787                    pos = NULL;
788            }
789        }
790        else
791            printf ("%s", buf2);
792        
793        free (buf2);
794    }
795    
796    /*
797     * gui_infobar_printf: display message in infobar
798     */
799    
800    void
801    gui_infobar_printf (int time_displayed, int color, char *message, ...)
802    {
803        static char buffer[1024];
804        va_list argptr;
805        t_gui_infobar *ptr_infobar;
806        char *pos, *buf2, *buf3;
807        
808        va_start (argptr, message);
809        vsnprintf (buffer, sizeof (buffer) - 1, message, argptr);
810        va_end (argptr);
811        
812        buf2 = (char *)gui_color_decode ((unsigned char *)buffer, 0);
813        
814        if (buf2)
815        {
816            if (!local_utf8 || !utf8_is_valid (buf2))
817                buf3 = weechat_convert_encoding ((local_utf8) ?
818                                                 cfg_look_charset_decode_iso : cfg_look_charset_decode_utf,
819                                                 (cfg_look_charset_internal && cfg_look_charset_internal[0]) ?
820                                                 cfg_look_charset_internal : local_charset,
821                                                 buf2);
822          else          else
823              buffer->last_line->last_message->next_message = new_message;              buf3 = strdup (buf2);
         new_message->prev_message = buffer->last_line->last_message;  
         new_message->next_message = NULL;  
         buffer->last_line->last_message = new_message;  
824      }      }
825      else      else
826            buf3 = NULL;
827        
828        if (buf3)
829      {      {
830          wee_log_printf (_("Not enough memory for new message\n"));          ptr_infobar = (t_gui_infobar *)malloc (sizeof (t_gui_infobar));
831          return NULL;          if (ptr_infobar)
832            {
833                ptr_infobar->color = color;
834                ptr_infobar->text = strdup (buf3);
835                pos = strchr (ptr_infobar->text, '\n');
836                if (pos)
837                    pos[0] = '\0';
838                ptr_infobar->remaining_time = (time_displayed <= 0) ? -1 : time_displayed;
839                ptr_infobar->next_infobar = gui_infobar;
840                gui_infobar = ptr_infobar;
841                gui_draw_buffer_infobar (gui_current_window->buffer, 1);
842            }
843            else
844                wee_log_printf (_("Not enough memory for infobar message\n"));
845        }
846        
847        if (buf2)
848            free (buf2);
849        if (buf3)
850            free (buf3);
851    }
852    
853    /*
854     * gui_infobar_remove: remove last displayed message in infobar
855     */
856    
857    void
858    gui_infobar_remove ()
859    {
860        t_gui_infobar *new_infobar;
861        
862        if (gui_infobar)
863        {
864            new_infobar = gui_infobar->next_infobar;
865            if (gui_infobar->text)
866                free (gui_infobar->text);
867            free (gui_infobar);
868            gui_infobar = new_infobar;
869      }      }
     return new_message;  
870  }  }
871    
872  /*  /*
# Line 1219  void Line 1460  void
1460  gui_buffer_print_log (t_gui_buffer *buffer)  gui_buffer_print_log (t_gui_buffer *buffer)
1461  {  {
1462      t_gui_line *ptr_line;      t_gui_line *ptr_line;
     t_gui_message *ptr_message;  
1463      int num;      int num;
     char buf[4096];  
1464            
1465      wee_log_printf ("[buffer (addr:0x%X)]\n", buffer);      wee_log_printf ("[buffer (addr:0x%X)]\n", buffer);
1466      wee_log_printf ("  num_displayed. . . . : %d\n",   buffer->num_displayed);      wee_log_printf ("  num_displayed. . . . : %d\n",   buffer->num_displayed);
# Line 1266  gui_buffer_print_log (t_gui_buffer *buff Line 1505  gui_buffer_print_log (t_gui_buffer *buff
1505            
1506      while (ptr_line)      while (ptr_line)
1507      {      {
         buf[0] = '\0';  
         for (ptr_message = ptr_line->messages; ptr_message;  
             ptr_message = ptr_message->next_message)  
         {  
             if (strlen (buf) + strlen (ptr_message->message) + 1 >= sizeof (buf))  
                 break;  
             strcat (buf, ptr_message->message);  
         }  
1508          num--;          num--;
1509          wee_log_printf ("       line N-%05d: %s\n", num, buf);          wee_log_printf ("       line N-%05d: %s\n",
1510                            num,
1511                            (ptr_line->data) ? ptr_line->data : "(empty)");
1512                    
1513          ptr_line = ptr_line->next_line;          ptr_line = ptr_line->next_line;
1514      }      }

Legend:
Removed from v.1.60  
changed lines
  Added in v.1.61

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26