Example #1
0
TEST(String, Shared)
{
    const char *str1, *str2, *str3;
    int count;

    count = string_hashtable_shared->items_count;

    str1 = string_shared_get ("this is a test");
    CHECK(str1);

    LONGS_EQUAL(count + 1, string_hashtable_shared->items_count);

    str2 = string_shared_get ("this is a test");
    CHECK(str2);
    POINTERS_EQUAL(str1, str2);

    LONGS_EQUAL(count + 1, string_hashtable_shared->items_count);

    str3 = string_shared_get ("this is another test");
    CHECK(str3);
    CHECK(str1 != str3);
    CHECK(str2 != str3);

    LONGS_EQUAL(count + 2, string_hashtable_shared->items_count);

    string_shared_free (str1);
    LONGS_EQUAL(count + 2, string_hashtable_shared->items_count);

    string_shared_free (str2);
    LONGS_EQUAL(count + 1, string_hashtable_shared->items_count);

    string_shared_free (str3);
    LONGS_EQUAL(count + 0, string_hashtable_shared->items_count);
}
Example #2
0
void
gui_nicklist_nick_set (struct t_gui_buffer *buffer,
                       struct t_gui_nick *nick,
                       const char *property, const char *value)
{
    long number;
    char *error;
    int nick_changed;

    if (!buffer || !nick || !property || !value)
        return;

    nick_changed = 0;

    if (string_strcasecmp (property, "color") == 0)
    {
        if (nick->color)
            string_shared_free (nick->color);
        nick->color = (value[0]) ? (char *)string_shared_get (value) : NULL;
        nick_changed = 1;
    }
    else if (string_strcasecmp (property, "prefix") == 0)
    {
        if (nick->prefix)
            string_shared_free (nick->prefix);
        nick->prefix = (value[0]) ? (char *)string_shared_get (value) : NULL;
        nick_changed = 1;
    }
    else if (string_strcasecmp (property, "prefix_color") == 0)
    {
        if (nick->prefix_color)
            string_shared_free (nick->prefix_color);
        nick->prefix_color = (value[0]) ? (char *)string_shared_get (value) : NULL;
        nick_changed = 1;
    }
    else if (string_strcasecmp (property, "visible") == 0)
    {
        error = NULL;
        number = strtol (value, &error, 10);
        if (error && !error[0])
            nick->visible = (number) ? 1 : 0;
        nick_changed = 1;
    }

    if (nick_changed)
    {
        gui_nicklist_send_signal ("nicklist_nick_changed", buffer,
                                  nick->name);
        gui_nicklist_send_hsignal ("nicklist_nick_changed", buffer, NULL, nick);
    }
}
Example #3
0
struct t_gui_nick_group *
gui_nicklist_add_group (struct t_gui_buffer *buffer,
                        struct t_gui_nick_group *parent_group, const char *name,
                        const char *color, int visible)
{
    struct t_gui_nick_group *new_group;

    if (!buffer || !name || gui_nicklist_search_group (buffer, parent_group, name))
        return NULL;

    new_group = malloc (sizeof (*new_group));
    if (!new_group)
        return NULL;

    new_group->name = (char *)string_shared_get (name);
    new_group->color = (color) ? (char *)string_shared_get (color) : NULL;
    new_group->visible = visible;
    new_group->parent = (parent_group) ? parent_group : buffer->nicklist_root;
    new_group->level = (new_group->parent) ? new_group->parent->level + 1 : 0;
    new_group->children = NULL;
    new_group->last_child = NULL;
    new_group->nicks = NULL;
    new_group->last_nick = NULL;
    new_group->prev_group = NULL;
    new_group->next_group = NULL;

    if (new_group->parent)
    {
        gui_nicklist_insert_group_sorted (&(new_group->parent->children),
                                          &(new_group->parent->last_child),
                                          new_group);
        buffer->nicklist_count++;
        buffer->nicklist_groups_count++;
    }
    else
    {
        buffer->nicklist_root = new_group;
    }

    if (buffer->nicklist_display_groups && visible)
        buffer->nicklist_visible_count++;

    gui_nicklist_send_signal ("nicklist_group_added", buffer, name);
    gui_nicklist_send_hsignal ("nicklist_group_added", buffer, new_group, NULL);

    return new_group;
}
Example #4
0
void
gui_line_clear (struct t_gui_line *line)
{
    if (line->data->prefix)
        string_shared_free (line->data->prefix);
    line->data->prefix = (char *)string_shared_get ("");

    if (line->data->message)
        free (line->data->message);
    line->data->message = strdup ("");
}
Example #5
0
struct t_gui_nick *
gui_nicklist_add_nick (struct t_gui_buffer *buffer,
                       struct t_gui_nick_group *group,
                       const char *name, const char *color,
                       const char *prefix, const char *prefix_color,
                       int visible)
{
    struct t_gui_nick *new_nick;

    if (!buffer || !name || gui_nicklist_search_nick (buffer, NULL, name))
        return NULL;

    new_nick = malloc (sizeof (*new_nick));
    if (!new_nick)
        return NULL;

    new_nick->group = (group) ? group : buffer->nicklist_root;
    new_nick->name = (char *)string_shared_get (name);
    new_nick->color = (color) ? (char *)string_shared_get (color) : NULL;
    new_nick->prefix = (prefix) ? (char *)string_shared_get (prefix) : NULL;
    new_nick->prefix_color = (prefix_color) ? (char *)string_shared_get (prefix_color) : NULL;
    new_nick->visible = visible;

    gui_nicklist_insert_nick_sorted (new_nick->group, new_nick);

    buffer->nicklist_count++;
    buffer->nicklist_nicks_count++;

    if (visible)
        buffer->nicklist_visible_count++;

    if (CONFIG_BOOLEAN(config_look_color_nick_offline))
        gui_buffer_ask_chat_refresh (buffer, 1);

    gui_nicklist_send_signal ("nicklist_nick_added", buffer, name);
    gui_nicklist_send_hsignal ("nicklist_nick_added", buffer, NULL, new_nick);

    return new_nick;
}
Example #6
0
void
gui_nicklist_group_set (struct t_gui_buffer *buffer,
                        struct t_gui_nick_group *group,
                        const char *property, const char *value)
{
    long number;
    char *error;
    int group_changed;

    if (!buffer || !group || !property || !value)
        return;

    group_changed = 0;

    if (string_strcasecmp (property, "color") == 0)
    {
        if (group->color)
            string_shared_free (group->color);
        group->color = (value[0]) ? (char *)string_shared_get (value) : NULL;
        group_changed = 1;
    }
    else if (string_strcasecmp (property, "visible") == 0)
    {
        error = NULL;
        number = strtol (value, &error, 10);
        if (error && !error[0])
            group->visible = (number) ? 1 : 0;
        group_changed = 1;
    }

    if (group_changed)
    {
        gui_nicklist_send_signal ("nicklist_group_changed", buffer,
                                  group->name);
        gui_nicklist_send_hsignal ("nicklist_group_changed", buffer, group, NULL);
    }
}
Example #7
0
struct t_gui_line *
gui_line_add (struct t_gui_buffer *buffer, time_t date,
              time_t date_printed, const char *tags,
              const char *prefix, const char *message)
{
    struct t_gui_line *new_line;
    struct t_gui_line_data *new_line_data;
    struct t_gui_window *ptr_win;
    char *message_for_signal;
    const char *nick;
    int notify_level, *max_notify_level, lines_removed;
    time_t current_time;

    /*
     * remove line(s) if necessary, according to history options:
     *   max_lines:   if > 0, keep only N lines in buffer
     *   max_minutes: if > 0, keep only lines from last N minutes
     */
    lines_removed = 0;
    current_time = time (NULL);
    while (buffer->own_lines->first_line
           && (((CONFIG_INTEGER(config_history_max_buffer_lines_number) > 0)
                && (buffer->own_lines->lines_count + 1 >
                    CONFIG_INTEGER(config_history_max_buffer_lines_number)))
               || ((CONFIG_INTEGER(config_history_max_buffer_lines_minutes) > 0)
                   && (current_time - buffer->own_lines->first_line->data->date_printed >
                       CONFIG_INTEGER(config_history_max_buffer_lines_minutes) * 60))))
    {
        gui_line_free (buffer, buffer->own_lines->first_line);
        lines_removed++;
    }

    /* create new line */
    new_line = malloc (sizeof (*new_line));
    if (!new_line)
    {
        log_printf (_("Not enough memory for new line"));
        return NULL;
    }

    /* create data for line */
    new_line_data = malloc (sizeof (*(new_line->data)));
    if (!new_line_data)
    {
        free (new_line);
        log_printf (_("Not enough memory for new line"));
        return NULL;
    }
    new_line->data = new_line_data;

    /* fill data in new line */
    new_line->data->buffer = buffer;
    new_line->data->y = -1;
    new_line->data->date = date;
    new_line->data->date_printed = date_printed;
    new_line->data->str_time = gui_chat_get_time_string (date);
    gui_line_tags_alloc (new_line->data, tags);
    new_line->data->refresh_needed = 0;
    new_line->data->prefix = (prefix) ?
        (char *)string_shared_get (prefix) : ((date != 0) ? (char *)string_shared_get ("") : NULL);
    new_line->data->prefix_length = (prefix) ?
        gui_chat_strlen_screen (prefix) : 0;
    new_line->data->message = (message) ? strdup (message) : strdup ("");

    /* get notify level and max notify level for nick in buffer */
    notify_level = gui_line_get_notify_level (new_line);
    max_notify_level = NULL;
    nick = gui_line_get_nick_tag (new_line);
    if (nick)
        max_notify_level = hashtable_get (buffer->hotlist_max_level_nicks, nick);
    if (max_notify_level
        && (*max_notify_level < notify_level))
        notify_level = *max_notify_level;

    if (notify_level == GUI_HOTLIST_HIGHLIGHT)
        new_line->data->highlight = 1;
    else if (max_notify_level && (*max_notify_level < GUI_HOTLIST_HIGHLIGHT))
        new_line->data->highlight = 0;
    else
        new_line->data->highlight = gui_line_has_highlight (new_line);

    /* check if line is filtered or not */
    new_line->data->displayed = gui_filter_check_line (new_line->data);

    /* add line to lines list */
    gui_line_add_to_list (buffer->own_lines, new_line);

    /* update hotlist and/or send signals for line */
    if (new_line->data->displayed)
    {
        if (new_line->data->highlight)
        {
            (void) gui_hotlist_add (buffer, GUI_HOTLIST_HIGHLIGHT, NULL);
            if (!weechat_upgrading)
            {
                message_for_signal = gui_chat_build_string_prefix_message (new_line);
                if (message_for_signal)
                {
                    hook_signal_send ("weechat_highlight",
                                      WEECHAT_HOOK_SIGNAL_STRING,
                                      message_for_signal);
                    free (message_for_signal);
                }
            }
        }
        else
        {
            if (!weechat_upgrading && (notify_level == GUI_HOTLIST_PRIVATE))
            {
                message_for_signal = gui_chat_build_string_prefix_message (new_line);
                if (message_for_signal)
                {
                    hook_signal_send ("weechat_pv",
                                      WEECHAT_HOOK_SIGNAL_STRING,
                                      message_for_signal);
                    free (message_for_signal);
                }
            }
            if (notify_level >= GUI_HOTLIST_MIN)
                (void) gui_hotlist_add (buffer, notify_level, NULL);
        }
    }
    else
    {
        buffer->own_lines->lines_hidden++;
        if (buffer->mixed_lines)
            buffer->mixed_lines->lines_hidden++;
        hook_signal_send ("buffer_lines_hidden",
                          WEECHAT_HOOK_SIGNAL_POINTER, buffer);
    }

    /* add mixed line, if buffer is attached to at least one other buffer */
    if (buffer->mixed_lines)
    {
        gui_line_mixed_add (buffer->mixed_lines, new_line->data);
    }

    /*
     * if some lines were removed, force a full refresh if at least one window
     * is displaying buffer and that number of lines in buffer is lower than
     * window height
     */
    if (lines_removed > 0)
    {
        for (ptr_win = gui_windows; ptr_win; ptr_win = ptr_win->next_window)
        {
            if ((ptr_win->buffer == buffer)
                && (buffer->own_lines->lines_count < ptr_win->win_chat_height))
            {
                gui_buffer_ask_chat_refresh (buffer, 2);
                break;
            }
        }
    }

    hook_signal_send ("buffer_line_added",
                      WEECHAT_HOOK_SIGNAL_POINTER, new_line);

    return new_line;
}
Example #8
0
int
hdata_set (struct t_hdata *hdata, void *pointer, const char *name,
           const char *value)
{
    struct t_hdata_var *var;
    char **ptr_string, *error;
    long number;
    long unsigned int ptr;
    int rc;

    if (!hdata->update_pending)
        return 0;

    var = hashtable_get (hdata->hash_var, name);
    if (!var)
        return 0;

    if (!var->update_allowed)
        return 0;

    switch (var->type)
    {
        case DOGECHAT_HDATA_OTHER:
            break;
        case DOGECHAT_HDATA_CHAR:
            *((char *)(pointer + var->offset)) = (value) ? value[0] : '\0';
            return 1;
            break;
        case DOGECHAT_HDATA_INTEGER:
            error = NULL;
            number = strtol (value, &error, 10);
            if (error && !error[0])
            {
                *((int *)(pointer + var->offset)) = (int)number;
                return 1;
            }
            break;
        case DOGECHAT_HDATA_LONG:
            error = NULL;
            number = strtol (value, &error, 10);
            if (error && !error[0])
            {
                *((long *)(pointer + var->offset)) = number;
                return 1;
            }
            break;
        case DOGECHAT_HDATA_STRING:
            ptr_string = (char **)(pointer + var->offset);
            if (*ptr_string)
                free (*ptr_string);
            *ptr_string = (value) ? strdup (value) : NULL;
            return 1;
            break;
        case DOGECHAT_HDATA_SHARED_STRING:
            ptr_string = (char **)(pointer + var->offset);
            if (*ptr_string)
                string_shared_free (*ptr_string);
            *ptr_string = (value) ? (char *)string_shared_get (value) : NULL;
            return 1;
            break;
        case DOGECHAT_HDATA_POINTER:
            rc = sscanf (value, "%lx", &ptr);
            if ((rc != EOF) && (rc != 0))
            {
                *((void **)(pointer + var->offset)) = (void *)ptr;
                return 1;
            }
            break;
        case DOGECHAT_HDATA_TIME:
            error = NULL;
            number = strtol (value, &error, 10);
            if (error && !error[0])
            {
                *((time_t *)(pointer + var->offset)) = (time_t)number;
                return 1;
            }
            break;
        case DOGECHAT_HDATA_HASHTABLE:
            break;
    }
    return 0;
}