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 // Only highlight the URL if it starts with a known schema.
59 if (urlString.startsWith(QLatin1String("https://")) || urlString.startsWith(QLatin1String("http://")) || urlString.startsWith(QLatin1String("view-source:")))
61 // Create an input method event attribute list.
62 QList<QInputMethodEvent::Attribute> inputMethodEventAttributeList;
64 // Calculate the URL string length.
65 int urlStringLength = urlString.length();
67 // Get the index of end of the schema.
68 int endOfSchemaIndex = urlString.indexOf(QLatin1String("//")) + 2;
70 // Get the index of the `/` immediately after the domain name. If this doesn't exit it will be set to `-1`.
71 int endOfDomainNameIndex = urlString.indexOf(QLatin1Char('/'), endOfSchemaIndex);
73 // Get the index of the last `.` in the domain.
74 int lastDotIndex = urlString.lastIndexOf(QLatin1Char('.'), endOfDomainNameIndex - urlStringLength);
76 // Get the index of the penultimate `.` in the domain. If this doesn't exist it will be set to `-1`.
77 int penultimateDotIndex = urlString.lastIndexOf(QLatin1Char('.'), lastDotIndex - urlStringLength - 1);
79 // Create the text character formats.
80 QTextCharFormat grayTextCharFormat;
81 QTextCharFormat schemaTextCharFormat;
83 // Set the text character format colors.
84 grayTextCharFormat.setForeground(Qt::darkGray);
86 // Set the schema text character format color.
87 if (urlString.startsWith(QLatin1String("http://")) || urlString.startsWith(QLatin1String("view-source:http://")))
88 schemaTextCharFormat.setForeground(Qt::red);
90 schemaTextCharFormat = grayTextCharFormat;
92 // Append the schema text format format.
93 inputMethodEventAttributeList.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, 0, endOfSchemaIndex, schemaTextCharFormat));
95 // Append the subdomain format if the subdomain exists. If it doesn't, the penultimate dot index will be `-1`.
96 if (penultimateDotIndex > 0)
97 inputMethodEventAttributeList.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, endOfSchemaIndex, penultimateDotIndex + 1 - endOfSchemaIndex, grayTextCharFormat));
99 // 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`.
100 if (endOfDomainNameIndex > 0)
101 inputMethodEventAttributeList.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, endOfDomainNameIndex, urlStringLength - endOfDomainNameIndex, grayTextCharFormat));
103 // Create an input method event with an empty pre-edit string.
104 QInputMethodEvent inputMethodEvent(QString(""), inputMethodEventAttributeList);
106 // Apply the URL highlighting.
107 event(&inputMethodEvent);
110 // Run the default commands.
111 KLineEdit::setText(urlString);