Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "IRCLogBuffer.h"
28#include <AK/StringBuilder.h>
29#include <LibHTML/DOM/DocumentFragment.h>
30#include <LibHTML/DOM/DocumentType.h>
31#include <LibHTML/DOM/ElementFactory.h>
32#include <LibHTML/DOM/HTMLBodyElement.h>
33#include <LibHTML/DOM/Text.h>
34#include <LibHTML/Dump.h>
35#include <LibHTML/Parser/HTMLParser.h>
36#include <stdio.h>
37#include <time.h>
38
39NonnullRefPtr<IRCLogBuffer> IRCLogBuffer::create()
40{
41 return adopt(*new IRCLogBuffer);
42}
43
44IRCLogBuffer::IRCLogBuffer()
45{
46 m_document = adopt(*new Document);
47 m_document->append_child(adopt(*new DocumentType(document())));
48 auto html_element = create_element(document(), "html");
49 m_document->append_child(html_element);
50 auto head_element = create_element(document(), "head");
51 html_element->append_child(head_element);
52 auto style_element = create_element(document(), "style");
53 style_element->append_child(adopt(*new Text(document(), "div { font-family: Csilla; font-weight: lighter; }")));
54 head_element->append_child(style_element);
55 auto body_element = create_element(document(), "body");
56 html_element->append_child(body_element);
57 m_container_element = body_element;
58}
59
60IRCLogBuffer::~IRCLogBuffer()
61{
62}
63
64static String timestamp_string()
65{
66 auto now = time(nullptr);
67 auto* tm = localtime(&now);
68 return String::format("%02u:%02u:%02u ", tm->tm_hour, tm->tm_min, tm->tm_sec);
69}
70
71void IRCLogBuffer::add_message(char prefix, const String& name, const String& text, Color color)
72{
73 auto nick_string = String::format("<%c%s> ", prefix ? prefix : ' ', name.characters());
74 auto html = String::format(
75 "<div style=\"color: %s\">"
76 "<span>%s</span>"
77 "<b>%s</b>"
78 "<span>%s</span>"
79 "</div>",
80 color.to_string().characters(),
81 timestamp_string().characters(),
82 escape_html_entities(nick_string).characters(),
83 escape_html_entities(text).characters());
84 auto fragment = parse_html_fragment(*m_document, html);
85 m_container_element->append_child(fragment->remove_child(*fragment->first_child()));
86 m_document->force_layout();
87}
88
89void IRCLogBuffer::add_message(const String& text, Color color)
90{
91 auto html = String::format(
92 "<div style=\"color: %s\">"
93 "<span>%s</span>"
94 "<span>%s</span>"
95 "</div>",
96 color.to_string().characters(),
97 timestamp_string().characters(),
98 escape_html_entities(text).characters());
99 auto fragment = parse_html_fragment(*m_document, html);
100 m_container_element->append_child(fragment->remove_child(*fragment->first_child()));
101 m_document->force_layout();
102}
103
104void IRCLogBuffer::dump() const
105{
106 // FIXME: Remove me?
107}