2 * Copyright 2024 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
6 * Privacy Browser PC is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser PC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser PC. If not, see <http://www.gnu.org/licenses/>.
20 // Application headers.
21 #include "UrlLineEdit.h"
23 // Qt toolkit headers.
24 #include <QInputMethodEvent>
26 // Construct the class.
27 UrlLineEdit::UrlLineEdit(QWidget *parentWidgetPointer) : KLineEdit(parentWidgetPointer) {}
29 void UrlLineEdit::focusInEvent(QFocusEvent *focusEventPointer)
31 // Create an input method event attribute list.
32 QList<QInputMethodEvent::Attribute> inputMethodEventAttributeList;
34 // Create an input method event with no formatting.
35 QInputMethodEvent inputMethodEvent(QString(QLatin1String("")), inputMethodEventAttributeList);
37 // Apply the empty formatting to remove the URL highlighting.
38 event(&inputMethodEvent);
40 // Run the default commands.
41 KLineEdit::focusInEvent(focusEventPointer);
44 void UrlLineEdit::focusOutEvent(QFocusEvent *focusEventPointer)
46 // Reapply the highlight to the current text.
49 // Run the default commands.
50 KLineEdit::focusOutEvent(focusEventPointer);
53 void UrlLineEdit::setText(const QString &urlString)
55 // Wipe the currently displayed text.
56 KLineEdit::setText(QLatin1String(""));
58 // Create an input method event attribute list.
59 QList<QInputMethodEvent::Attribute> inputMethodEventAttributeList;
61 // Calculate the URL string length.
62 int urlStringLength = urlString.length();
64 // Get the index of end of the schema.
65 int endOfSchemaIndex = urlString.indexOf(QLatin1String("//")) + 2;
67 // Get the index of the last `.` in the domain.
68 int lastDotIndex = urlString.lastIndexOf(QLatin1Char('.'));
70 // Get the index of the penultimate `.` in the domain. If this doesn't exist it will be set to `-1`.
71 int penultimateDotIndex = urlString.lastIndexOf(QLatin1Char('.'), lastDotIndex - urlStringLength - 1);
73 // Get the index of the `/` immediately after the domain name. If this doesn't exit it will be set to `-1`.
74 int endOfDomainNameIndex = urlString.indexOf(QLatin1Char('/'), endOfSchemaIndex);
76 // Create the text character formats.
77 QTextCharFormat grayTextCharFormat;
78 QTextCharFormat schemaTextCharFormat;
80 // Set the text character format colors.
81 grayTextCharFormat.setForeground(Qt::darkGray);
83 // Set the schema text character format color.
84 if (urlString.startsWith(QLatin1String("http://")) || urlString.startsWith(QLatin1String("view-source:http://")))
85 schemaTextCharFormat.setForeground(Qt::red);
87 schemaTextCharFormat = grayTextCharFormat;
89 // Append the schema text format format.
90 inputMethodEventAttributeList.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, 0, endOfSchemaIndex, schemaTextCharFormat));
92 // Append the subdomain format if the subdomain exists. If it doesn't, the penultimate dot index will be `-1`.
93 if (penultimateDotIndex > 0)
94 inputMethodEventAttributeList.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, endOfSchemaIndex, penultimateDotIndex + 1 - endOfSchemaIndex, grayTextCharFormat));
96 // Append the post domain formatting if text exists after the domain name. If it doesn't, the end of domain name index will be `-1`.
97 if (endOfDomainNameIndex > 0)
98 inputMethodEventAttributeList.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, endOfDomainNameIndex, urlStringLength - endOfDomainNameIndex, grayTextCharFormat));
100 // Create an input method event with an empty pre-edit string.
101 QInputMethodEvent inputMethodEvent(QString(""), inputMethodEventAttributeList);
103 // Apply the URL highlighting.
104 event(&inputMethodEvent);
106 // Run the default commands.
107 KLineEdit::setText(urlString);