aboutsummaryrefslogtreecommitdiff
path: root/src/gophermaprenderer.cpp
blob: bcdf11ff3a675b42427058787c7628264d06cc81 (plain) (blame)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "gophermaprenderer.hpp"

#include <QTextList>
#include <QTextBlock>
#include <QList>
#include <QStringList>
#include <QTextImageFormat>

#include <QDebug>

//Canonical Types
//0   Text File
//1   Gopher submenu or link to another gopher server
//2   CCSO Nameserver
//3   Error code returned by a Gopher server to indicate failure
//4   BinHex-encoded file (primarily for Macintosh computers)
//5   DOS file
//6   uuencoded file
//7   Gopher full-text search
//8   Telnet
//9   Binary file
//+   Mirror or alternate server (for load balancing or in case of primary server downtime)
//g   GIF file
//I   Image file
//T   Telnet 3270
//Non-Canonical Types
//h   HTML file
//i   Informational message
//s   Sound file

std::unique_ptr<QTextDocument> GophermapRenderer::render(const QByteArray &input, const QUrl &root_url, const DocumentStyle &themed_style)
{
    QTextCharFormat standard;
    standard.setFont(themed_style.standard_font);
    standard.setForeground(themed_style.standard_color);

    QTextCharFormat standard_link;
    standard_link.setFont(themed_style.standard_font);
    standard_link.setForeground(QBrush(themed_style.internal_link_color));

    QTextCharFormat external_link;
    external_link.setFont(themed_style.standard_font);
    external_link.setForeground(QBrush(themed_style.external_link_color));

    std::unique_ptr<QTextDocument> result = std::make_unique<QTextDocument>();
    result->setDocumentMargin(themed_style.margin);

    QTextCursor cursor{result.get()};

    QTextBlockFormat non_list_format = cursor.blockFormat();

    QList<QByteArray> lines = input.split('\n');
    for (auto const & line : lines)
    {
        if (line.length() < 2) // skip lines without
            continue;

        if(line[line.size() - 1] != '\r')
            continue;

        auto items = line.mid(1, line.length() - 2).split('\t');
        if(items.size() < 2) // invalid
            continue;

        QString icon;
        switch (line.at(0))
        {
        case '0': // Text File
            icon = "text";
            break;
        case '1': // Gopher submenu or link to another gopher server
            icon = "directory";
            break;
        case '2': // CCSO Nameserver
            icon = "ns";
            break;
        case '3': // Error code returned by a Gopher server to indicate failure
            icon = "error";
            break;
        case '4': // BinHex-encoded file (primarily for Macintosh computers)
            icon = "binhex";
            break;
        case '5': // DOS file
            icon = "dos";
            break;
        case '6': // uuencoded file
            icon = "uuencoded";
            break;
        case '7': // Gopher full-text search
            icon = "full-text";
            break;
        case '8': // Telnet
            icon = "telnet";
            break;
        case '9': // Binary file
            icon = "binary";
            break;
        case '+': // Mirror or alternate server (for load balancing or in case of primary server downtime)
            icon = "mirror";
            break;
        case 'g': // GIF file
            icon = "gif";
            break;
        case 'I': // Image file
            icon = "image";
            break;
        case 'T': // Telnet 3270
            icon = "telnet";
            break;
        //Non-Canonical Types
        case 'h': // HTML file
            icon = "html";
            break;
        case 'i': // Informational message
            icon = "informational";
            break;
        case 's': // Sound file
            icon = "sound";
            break;
        default:  // unknown
            continue;
        }

        QString title = items.at(0);

        // 1Phlog	/phlog	octotherp.org	70	+

        if(line.at(0) == 'i')
        {
            cursor.insertText(title + "\n", standard);
        }
        else
        {
            QString dst_url;
            switch(items.size())
            {
            case 0: assert(false);
            case 1: assert(false);
            case 2:
                dst_url = root_url.resolved(QUrl(items.at(1))).toString();
                break;
            case 3:
                dst_url = "gopher://" + items.at(2) + "/" + line.mid(0,1) + items.at(1);
                break;
            default:
                dst_url = "gopher://" + items.at(2) + ":" + items.at(3) + "/" + line.mid(0,1) + items.at(1);
                break;
            }

            if(not QUrl(dst_url).isValid()) {
                // invlaid URL generated
                qDebug() << line << dst_url;
            }


            QTextCharFormat fmt = standard_link;
            fmt.setAnchor(true);
            fmt.setAnchorHref(dst_url);
            cursor.insertText(title + "\n", fmt);
        }
    }

    return result;
}