summaryrefslogtreecommitdiff
path: root/Applications/IRCClient/IRCLogBuffer.cpp
blob: 782a69c6cc38e3a8d6266f73e9700da336d7e0f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "IRCLogBuffer.h"
#include <AK/StringBuilder.h>
#include <LibHTML/DOM/DocumentFragment.h>
#include <LibHTML/DOM/DocumentType.h>
#include <LibHTML/DOM/ElementFactory.h>
#include <LibHTML/DOM/HTMLBodyElement.h>
#include <LibHTML/DOM/Text.h>
#include <LibHTML/Dump.h>
#include <LibHTML/Parser/HTMLParser.h>
#include <stdio.h>
#include <time.h>

NonnullRefPtr<IRCLogBuffer> IRCLogBuffer::create()
{
    return adopt(*new IRCLogBuffer);
}

IRCLogBuffer::IRCLogBuffer()
{
    m_document = adopt(*new Document);
    m_document->append_child(adopt(*new DocumentType(document())));
    auto html_element = create_element(document(), "html");
    m_document->append_child(html_element);
    auto head_element = create_element(document(), "head");
    html_element->append_child(head_element);
    auto style_element = create_element(document(), "style");
    style_element->append_child(adopt(*new Text(document(), "div { font-family: Csilla; font-weight: lighter; }")));
    head_element->append_child(style_element);
    auto body_element = create_element(document(), "body");
    html_element->append_child(body_element);
    m_container_element = body_element;
}

IRCLogBuffer::~IRCLogBuffer()
{
}

static String timestamp_string()
{
    auto now = time(nullptr);
    auto* tm = localtime(&now);
    return String::format("%02u:%02u:%02u ", tm->tm_hour, tm->tm_min, tm->tm_sec);
}

void IRCLogBuffer::add_message(char prefix, const String& name, const String& text, Color color)
{
    auto nick_string = String::format("&lt;%c%s&gt; ", prefix ? prefix : ' ', name.characters());
    auto html = String::format(
        "<div style=\"color: %s\">"
        "<span>%s</span>"
        "<b>%s</b>"
        "<span>%s</span>"
        "</div>",
        color.to_string().characters(),
        timestamp_string().characters(),
        nick_string.characters(),
        text.characters());
    auto fragment = parse_html_fragment(*m_document, html);
    m_container_element->append_child(fragment->remove_child(*fragment->first_child()));
    m_document->force_layout();
}

void IRCLogBuffer::add_message(const String& text, Color color)
{
    auto html = String::format(
        "<div style=\"color: %s\">"
        "<span>%s</span>"
        "<span>%s</span>"
        "</div>",
        color.to_string().characters(),
        timestamp_string().characters(),
        text.characters());
    auto fragment = parse_html_fragment(*m_document, html);
    m_container_element->append_child(fragment->remove_child(*fragment->first_child()));
    m_document->force_layout();
}

void IRCLogBuffer::dump() const
{
    // FIXME: Remove me?
}