1 /***************************************************************************
2 * Copyright (C) 2005-2016 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
23 #include <QApplication>
26 #include "buffersettings.h"
27 #include "qssparser.h"
30 #include "uisettings.h"
33 QHash<QString, UiStyle::FormatType> UiStyle::_formatCodes;
34 bool UiStyle::_useCustomTimestampFormat; /// If true, use the custom timestamp format
35 QString UiStyle::_timestampFormatString; /// Timestamp format
36 QString UiStyle::_systemTimestampFormatString; /// Cached copy of system locale timestamp format
37 bool UiStyle::_showSenderPrefixes; /// If true, show prefixmodes before sender names
38 bool UiStyle::_showSenderBrackets; /// If true, show brackets around sender names
40 UiStyle::UiStyle(QObject *parent)
42 _channelJoinedIcon(QIcon::fromTheme("irc-channel-joined", QIcon(":/icons/irc-channel-joined.png"))),
43 _channelPartedIcon(QIcon::fromTheme("irc-channel-parted", QIcon(":/icons/irc-channel-parted.png"))),
44 _userOfflineIcon(QIcon::fromTheme("im-user-offline", QIcon::fromTheme("user-offline", QIcon(":/icons/im-user-offline.png")))),
45 _userOnlineIcon(QIcon::fromTheme("im-user", QIcon::fromTheme("user-available", QIcon(":/icons/im-user.png")))), // im-user-* are non-standard oxygen extensions
46 _userAwayIcon(QIcon::fromTheme("im-user-away", QIcon::fromTheme("user-away", QIcon(":/icons/im-user-away.png")))),
47 _categoryOpIcon(QIcon::fromTheme("irc-operator")),
48 _categoryVoiceIcon(QIcon::fromTheme("irc-voice")),
49 _opIconLimit(UserCategoryItem::categoryFromModes("o")),
50 _voiceIconLimit(UserCategoryItem::categoryFromModes("v"))
52 // register FormatList if that hasn't happened yet
53 // FIXME I don't think this actually avoids double registration... then again... does it hurt?
54 if (QVariant::nameToType("UiStyle::FormatList") == QVariant::Invalid) {
55 qRegisterMetaType<FormatList>("UiStyle::FormatList");
56 qRegisterMetaTypeStreamOperators<FormatList>("UiStyle::FormatList");
57 Q_ASSERT(QVariant::nameToType("UiStyle::FormatList") != QVariant::Invalid);
60 _uiStylePalette = QVector<QBrush>(static_cast<int>(ColorRole::NumRoles), QBrush());
62 // Now initialize the mapping between FormatCodes and FormatTypes...
63 _formatCodes["%O"] = FormatType::Base;
64 _formatCodes["%B"] = FormatType::Bold;
65 _formatCodes["%S"] = FormatType::Italic;
66 _formatCodes["%U"] = FormatType::Underline;
67 _formatCodes["%R"] = FormatType::Reverse;
69 _formatCodes["%DN"] = FormatType::Nick;
70 _formatCodes["%DH"] = FormatType::Hostmask;
71 _formatCodes["%DC"] = FormatType::ChannelName;
72 _formatCodes["%DM"] = FormatType::ModeFlags;
73 _formatCodes["%DU"] = FormatType::Url;
75 // Initialize fallback defaults
76 // NOTE: If you change this, update qtui/chatviewsettings.h, too. More explanations available
78 setUseCustomTimestampFormat(false);
79 setTimestampFormatString(" hh:mm:ss");
80 enableSenderPrefixes(false);
81 enableSenderBrackets(true);
83 // BufferView / NickView settings
85 _showBufferViewIcons = _showNickViewIcons = s.value("ShowItemViewIcons", true).toBool();
86 s.notify("ShowItemViewIcons", this, SLOT(showItemViewIconsChanged(QVariant)));
88 _allowMircColors = s.value("AllowMircColors", true).toBool();
89 s.notify("AllowMircColors", this, SLOT(allowMircColorsChanged(QVariant)));
97 qDeleteAll(_metricsCache);
101 void UiStyle::reload()
107 void UiStyle::loadStyleSheet()
109 qDeleteAll(_metricsCache);
110 _metricsCache.clear();
111 _formatCache.clear();
117 styleSheet += loadStyleSheet("file:///" + Quassel::findDataFilePath("stylesheets/default.qss"));
118 styleSheet += loadStyleSheet("file:///" + Quassel::configDirPath() + "settings.qss");
119 if (s.value("UseCustomStyleSheet", false).toBool()) {
120 QString customSheetPath(s.value("CustomStyleSheetPath").toString());
121 QString customSheet = loadStyleSheet("file:///" + customSheetPath, true);
122 if (customSheet.isEmpty()) {
123 // MIGRATION: changed default install path for data from /usr/share/apps to /usr/share
124 if (customSheetPath.startsWith("/usr/share/apps/quassel")) {
125 customSheetPath.replace(QRegExp("^/usr/share/apps"), "/usr/share");
126 customSheet = loadStyleSheet("file:///" + customSheetPath, true);
127 if (!customSheet.isEmpty()) {
128 s.setValue("CustomStyleSheetPath", customSheetPath);
129 qDebug() << "Custom stylesheet path migrated to" << customSheetPath;
133 styleSheet += customSheet;
135 styleSheet += loadStyleSheet("file:///" + Quassel::optionValue("qss"), true);
137 if (!styleSheet.isEmpty()) {
139 parser.processStyleSheet(styleSheet);
140 QApplication::setPalette(parser.palette());
142 _uiStylePalette = parser.uiStylePalette();
143 _formats = parser.formats();
144 _listItemFormats = parser.listItemFormats();
146 styleSheet = styleSheet.trimmed();
147 if (!styleSheet.isEmpty())
148 qApp->setStyleSheet(styleSheet); // pass the remaining sections to the application
155 QString UiStyle::loadStyleSheet(const QString &styleSheet, bool shouldExist)
157 QString ss = styleSheet;
158 if (ss.startsWith("file:///")) {
164 if (file.open(QFile::ReadOnly)) {
165 QTextStream stream(&file);
166 ss = stream.readAll();
171 qWarning() << "Could not open stylesheet file:" << file.fileName();
179 void UiStyle::updateSystemTimestampFormat()
181 // Does the system locale use AM/PM designators? For example:
185 // 24-hour: hh:mm ADD things
186 // For timestamp format, see https://doc.qt.io/qt-5/qdatetime.html#toString
187 // This won't update if the system locale is changed while Quassel is running. If need be,
188 // Quassel could hook into notifications of changing system locale to update this.
190 // Match any AP or A designation if on a word boundary, including underscores.
191 // .*(\b|_)(A|AP)(\b|_).*
192 // .* Match any number of characters
193 // \b Match a word boundary, i.e. "AAA.BBB", "." is matched
194 // _ Match the literal character '_' (not considered a word boundary)
195 // (X|Y) Match either X or Y, exactly
197 // Note that '\' must be escaped as '\\'
198 // QRegExp does not support (?> ...), so it's replaced with standard matching, (...)
199 // Helpful interactive website for debugging and explaining: https://regex101.com/
200 const QRegExp regExpMatchAMPM(".*(\\b|_)(A|AP)(\\b|_).*", Qt::CaseInsensitive);
202 if (regExpMatchAMPM.exactMatch(QLocale::system().timeFormat(QLocale::ShortFormat))) {
204 _systemTimestampFormatString = " h:mm:ss ap";
206 // 24-hour style used
207 _systemTimestampFormatString = " hh:mm:ss";
209 // Include a space to give the timestamp a small bit of padding between the border of the chat
210 // buffer window and the numbers. Helps with readability.
211 // If you change this to include brackets, e.g. "[hh:mm:ss]", also update
212 // ChatScene::updateTimestampHasBrackets() to true or false as needed!
216 // FIXME The following should trigger a reload/refresh of the chat view.
217 void UiStyle::setUseCustomTimestampFormat(bool enabled)
219 if (_useCustomTimestampFormat != enabled) {
220 _useCustomTimestampFormat = enabled;
224 void UiStyle::setTimestampFormatString(const QString &format)
226 if (_timestampFormatString != format) {
227 _timestampFormatString = format;
231 void UiStyle::enableSenderPrefixes(bool enabled)
233 if (_showSenderPrefixes != enabled) {
234 _showSenderPrefixes = enabled;
238 void UiStyle::enableSenderBrackets(bool enabled)
240 if (_showSenderBrackets != enabled) {
241 _showSenderBrackets = enabled;
246 void UiStyle::allowMircColorsChanged(const QVariant &v)
248 _allowMircColors = v.toBool();
253 /******** ItemView Styling *******/
255 void UiStyle::showItemViewIconsChanged(const QVariant &v)
257 _showBufferViewIcons = _showNickViewIcons = v.toBool();
261 QVariant UiStyle::bufferViewItemData(const QModelIndex &index, int role) const
263 BufferInfo::Type type = (BufferInfo::Type)index.data(NetworkModel::BufferTypeRole).toInt();
264 bool isActive = index.data(NetworkModel::ItemActiveRole).toBool();
266 if (role == Qt::DecorationRole) {
267 if (!_showBufferViewIcons)
271 case BufferInfo::ChannelBuffer:
273 return _channelJoinedIcon;
275 return _channelPartedIcon;
276 case BufferInfo::QueryBuffer:
278 return _userOfflineIcon;
279 if (index.data(NetworkModel::UserAwayRole).toBool())
280 return _userAwayIcon;
282 return _userOnlineIcon;
288 ItemFormatType fmtType = ItemFormatType::BufferViewItem;
290 case BufferInfo::StatusBuffer:
291 fmtType |= ItemFormatType::NetworkItem;
293 case BufferInfo::ChannelBuffer:
294 fmtType |= ItemFormatType::ChannelBufferItem;
296 case BufferInfo::QueryBuffer:
297 fmtType |= ItemFormatType::QueryBufferItem;
303 QTextCharFormat fmt = _listItemFormats.value(ItemFormatType::BufferViewItem);
304 fmt.merge(_listItemFormats.value(fmtType));
306 BufferInfo::ActivityLevel activity = (BufferInfo::ActivityLevel)index.data(NetworkModel::BufferActivityRole).toInt();
307 if (activity & BufferInfo::Highlight) {
308 fmt.merge(_listItemFormats.value(ItemFormatType::BufferViewItem | ItemFormatType::HighlightedBuffer));
309 fmt.merge(_listItemFormats.value(fmtType | ItemFormatType::HighlightedBuffer));
311 else if (activity & BufferInfo::NewMessage) {
312 fmt.merge(_listItemFormats.value(ItemFormatType::BufferViewItem | ItemFormatType::UnreadBuffer));
313 fmt.merge(_listItemFormats.value(fmtType | ItemFormatType::UnreadBuffer));
315 else if (activity & BufferInfo::OtherActivity) {
316 fmt.merge(_listItemFormats.value(ItemFormatType::BufferViewItem | ItemFormatType::ActiveBuffer));
317 fmt.merge(_listItemFormats.value(fmtType | ItemFormatType::ActiveBuffer));
319 else if (!isActive) {
320 fmt.merge(_listItemFormats.value(ItemFormatType::BufferViewItem | ItemFormatType::InactiveBuffer));
321 fmt.merge(_listItemFormats.value(fmtType | ItemFormatType::InactiveBuffer));
323 else if (index.data(NetworkModel::UserAwayRole).toBool()) {
324 fmt.merge(_listItemFormats.value(ItemFormatType::BufferViewItem | ItemFormatType::UserAway));
325 fmt.merge(_listItemFormats.value(fmtType | ItemFormatType::UserAway));
328 return itemData(role, fmt);
332 QVariant UiStyle::nickViewItemData(const QModelIndex &index, int role) const
334 NetworkModel::ItemType type = (NetworkModel::ItemType)index.data(NetworkModel::ItemTypeRole).toInt();
336 if (role == Qt::DecorationRole) {
337 if (!_showNickViewIcons)
341 case NetworkModel::UserCategoryItemType:
343 int categoryId = index.data(TreeModel::SortRole).toInt();
344 if (categoryId <= _opIconLimit)
345 return _categoryOpIcon;
346 if (categoryId <= _voiceIconLimit)
347 return _categoryVoiceIcon;
348 return _userOnlineIcon;
350 case NetworkModel::IrcUserItemType:
351 if (index.data(NetworkModel::ItemActiveRole).toBool())
352 return _userOnlineIcon;
354 return _userAwayIcon;
360 QTextCharFormat fmt = _listItemFormats.value(ItemFormatType::NickViewItem);
363 case NetworkModel::IrcUserItemType:
364 fmt.merge(_listItemFormats.value(ItemFormatType::NickViewItem | ItemFormatType::IrcUserItem));
365 if (!index.data(NetworkModel::ItemActiveRole).toBool()) {
366 fmt.merge(_listItemFormats.value(ItemFormatType::NickViewItem | ItemFormatType::UserAway));
367 fmt.merge(_listItemFormats.value(ItemFormatType::NickViewItem | ItemFormatType::IrcUserItem | ItemFormatType::UserAway));
370 case NetworkModel::UserCategoryItemType:
371 fmt.merge(_listItemFormats.value(ItemFormatType::NickViewItem | ItemFormatType::UserCategoryItem));
377 return itemData(role, fmt);
381 QVariant UiStyle::itemData(int role, const QTextCharFormat &format) const
385 return format.font();
386 case Qt::ForegroundRole:
387 return format.property(QTextFormat::ForegroundBrush);
388 case Qt::BackgroundRole:
389 return format.property(QTextFormat::BackgroundBrush);
396 /******** Caching *******/
398 QTextCharFormat UiStyle::format(quint64 key) const
400 return _formats.value(key, QTextCharFormat());
404 QTextCharFormat UiStyle::cachedFormat(FormatType formatType, MessageLabel messageLabel) const
406 return _formatCache.value(formatType | messageLabel, QTextCharFormat());
410 void UiStyle::setCachedFormat(const QTextCharFormat &format, FormatType formatType, MessageLabel messageLabel) const
412 _formatCache[formatType | messageLabel] = format;
416 QFontMetricsF *UiStyle::fontMetrics(FormatType ftype, MessageLabel label) const
418 // QFontMetricsF is not assignable, so we need to store pointers :/
419 quint64 key = ftype | label;
421 if (_metricsCache.contains(key))
422 return _metricsCache.value(key);
424 return (_metricsCache[key] = new QFontMetricsF(format(ftype, label).font()));
428 /******** Generate formats ********/
430 // NOTE: This and the following functions are intimately tied to the values in FormatType. Don't change this
431 // until you _really_ know what you do!
432 QTextCharFormat UiStyle::format(FormatType ftype, MessageLabel label) const
434 if (ftype == FormatType::Invalid)
437 // check if we have exactly this format readily cached already
438 QTextCharFormat fmt = cachedFormat(ftype, label);
439 if (fmt.properties().count())
442 mergeFormat(fmt, ftype, label & 0xffff0000); // keep nickhash in label
444 for (quint32 mask = 0x00000001; mask <= static_cast<quint32>(MessageLabel::Selected); mask <<= 1) {
445 if (static_cast<quint32>(label) & mask)
446 mergeFormat(fmt, ftype, label & (mask | 0xffff0000));
449 setCachedFormat(fmt, ftype, label);
454 void UiStyle::mergeFormat(QTextCharFormat &fmt, FormatType ftype, MessageLabel label) const
456 mergeSubElementFormat(fmt, ftype & 0x00ff, label);
458 // TODO: allow combinations for mirc formats and colors (each), e.g. setting a special format for "bold and italic"
459 // or "foreground 01 and background 03"
460 if ((ftype & 0xfff00) != FormatType::Base) { // element format
461 for (quint32 mask = 0x00100; mask <= 0x40000; mask <<= 1) {
462 if ((ftype & mask) != FormatType::Base) {
463 mergeSubElementFormat(fmt, ftype & (mask | 0xff), label);
468 // Now we handle color codes
469 // We assume that those can't be combined with subelement and message types.
470 if (_allowMircColors) {
471 if ((ftype & 0x00400000) != FormatType::Base)
472 mergeSubElementFormat(fmt, ftype & 0x0f400000, label); // foreground
473 if ((ftype & 0x00800000) != FormatType::Base)
474 mergeSubElementFormat(fmt, ftype & 0xf0800000, label); // background
475 if ((ftype & 0x00c00000) == static_cast<FormatType>(0x00c00000))
476 mergeSubElementFormat(fmt, ftype & 0xffc00000, label); // combination
480 if ((ftype & FormatType::Url) != FormatType::Base)
481 mergeSubElementFormat(fmt, ftype & (FormatType::Url | static_cast<FormatType>(0x000000ff)), label);
485 // Merge a subelement format into an existing message format
486 void UiStyle::mergeSubElementFormat(QTextCharFormat &fmt, FormatType ftype, MessageLabel label) const
488 quint64 key = ftype | label;
489 fmt.merge(format(key & 0x0000ffffffffff00ull)); // label + subelement
490 fmt.merge(format(key & 0x0000ffffffffffffull)); // label + subelement + msgtype
491 fmt.merge(format(key & 0xffffffffffffff00ull)); // label + subelement + nickhash
492 fmt.merge(format(key & 0xffffffffffffffffull)); // label + subelement + nickhash + msgtype
496 UiStyle::FormatType UiStyle::formatType(Message::Type msgType)
500 return FormatType::PlainMsg;
501 case Message::Notice:
502 return FormatType::NoticeMsg;
503 case Message::Action:
504 return FormatType::ActionMsg;
506 return FormatType::NickMsg;
508 return FormatType::ModeMsg;
510 return FormatType::JoinMsg;
512 return FormatType::PartMsg;
514 return FormatType::QuitMsg;
516 return FormatType::KickMsg;
518 return FormatType::KillMsg;
519 case Message::Server:
520 return FormatType::ServerMsg;
522 return FormatType::InfoMsg;
524 return FormatType::ErrorMsg;
525 case Message::DayChange:
526 return FormatType::DayChangeMsg;
528 return FormatType::TopicMsg;
529 case Message::NetsplitJoin:
530 return FormatType::NetsplitJoinMsg;
531 case Message::NetsplitQuit:
532 return FormatType::NetsplitQuitMsg;
533 case Message::Invite:
534 return FormatType::InviteMsg;
536 //Q_ASSERT(false); // we need to handle all message types
537 qWarning() << Q_FUNC_INFO << "Unknown message type:" << msgType;
538 return FormatType::ErrorMsg;
542 UiStyle::FormatType UiStyle::formatType(const QString &code)
544 if (_formatCodes.contains(code))
545 return _formatCodes.value(code);
546 return FormatType::Invalid;
550 QString UiStyle::formatCode(FormatType ftype)
552 return _formatCodes.key(ftype);
556 QList<QTextLayout::FormatRange> UiStyle::toTextLayoutList(const FormatList &formatList, int textLength, MessageLabel messageLabel) const
558 QList<QTextLayout::FormatRange> formatRanges;
559 QTextLayout::FormatRange range;
561 for (i = 0; i < formatList.size(); i++) {
562 range.format = format(formatList.at(i).second.type, messageLabel);
563 range.start = formatList.at(i).first;
565 formatRanges.last().length = range.start - formatRanges.last().start;
566 formatRanges.append(range);
569 formatRanges.last().length = textLength - formatRanges.last().start;
574 // This method expects a well-formatted string, there is no error checking!
575 // Since we create those ourselves, we should be pretty safe that nobody does something crappy here.
576 UiStyle::StyledString UiStyle::styleString(const QString &s_, FormatType baseFormat)
580 result.formatList.emplace_back(std::make_pair(quint16{0}, Format{baseFormat}));
582 if (s.length() > 65535) {
583 // We use quint16 for indexes
584 qWarning() << QString("String too long to be styled: %1").arg(s);
585 result.plainText = s;
589 FormatType curfmt = baseFormat;
591 int pos = 0; quint16 length = 0;
593 pos = s.indexOf('%', pos);
595 if (s[pos+1] == '%') { // escaped %, we just remove one and continue
600 if (s[pos+1] == 'D' && s[pos+2] == 'c') { // color code
601 if (s[pos+3] == '-') { // color off
602 curfmt &= 0x003fffff;
606 int color = 10 * s[pos+4].digitValue() + s[pos+5].digitValue();
607 //TODO: use 99 as transparent color (re mirc color "standard")
609 if (s[pos+3] == 'f') {
610 curfmt &= 0xf0ffffff;
611 curfmt |= (quint32)(color << 24) | 0x00400000;
614 curfmt &= 0x0fffffff;
615 curfmt |= (quint32)(color << 28) | 0x00800000;
620 else if (s[pos+1] == 'O') { // reset formatting
621 curfmt &= 0x000000ff; // we keep message type-specific formatting
624 else if (s[pos+1] == 'R') { // reverse
625 // TODO: implement reverse formatting
629 else { // all others are toggles
630 QString code = QString("%") + s[pos+1];
631 if (s[pos+1] == 'D') code += s[pos+2];
632 FormatType ftype = formatType(code);
633 if (ftype == FormatType::Invalid) {
635 qWarning() << (QString("Invalid format code in string: %1").arg(s));
639 length = code.length();
641 s.remove(pos, length);
642 if (pos == result.formatList.back().first)
643 result.formatList.back().second.type = curfmt;
645 result.formatList.emplace_back(std::make_pair(pos, Format{curfmt}));
647 result.plainText = s;
652 QString UiStyle::mircToInternal(const QString &mirc_)
655 mirc.reserve(mirc_.size());
656 foreach (const QChar &c, mirc_) {
657 if ((c < '\x20' || c == '\x7f') && c != '\x03') {
658 switch (c.unicode()) {
679 mirc += QChar(0x2421);
682 mirc += QChar(0x2400 + c.unicode());
691 // Now we bring the color codes (\x03) in a sane format that can be parsed more easily later.
692 // %Dcfxx is foreground, %Dcbxx is background color, where xx is a 2 digit dec number denoting the color code.
693 // %Dc- turns color off.
694 // Note: We use the "mirc standard" as described in <http://www.mirc.co.uk/help/color.txt>.
695 // This means that we don't accept something like \x03,5 (even though others, like WeeChat, do).
698 pos = mirc.indexOf('\x03', pos);
699 if (pos < 0) break; // no more mirc color codes
701 int l = mirc.length();
703 // check for fg color
704 if (i < l && mirc[i].isDigit()) {
706 if (i < l && mirc[i].isDigit()) num.append(mirc[i++]);
707 else num.prepend('0');
708 ins = QString("%Dcf%1").arg(num);
710 if (i+1 < l && mirc[i] == ',' && mirc[i+1].isDigit()) {
713 if (i < l && mirc[i].isDigit()) num.append(mirc[i++]);
714 else num.prepend('0');
715 ins += QString("%Dcb%1").arg(num);
721 mirc.replace(pos, i-pos, ins);
727 QString UiStyle::systemTimestampFormatString()
729 if (_systemTimestampFormatString.isEmpty()) {
730 // Calculate and cache the system timestamp format string
731 updateSystemTimestampFormat();
733 return _systemTimestampFormatString;
737 QString UiStyle::timestampFormatString()
739 if (useCustomTimestampFormat()) {
740 return _timestampFormatString;
742 return systemTimestampFormatString();
747 /***********************************************************************************/
748 UiStyle::StyledMessage::StyledMessage(const Message &msg)
752 // Don't compute the sender hash for message types without a nickname embedded
753 case Message::Server:
756 case Message::DayChange:
758 case Message::Invite:
759 // Don't compute the sender hash for messages with multiple nicks
760 // Fixing this without breaking themes would be.. complex.
761 case Message::NetsplitJoin:
762 case Message::NetsplitQuit:
764 // Don't compute the sender hash for message types that are not yet completed elsewhere
769 // Compute the sender hash for all other message types
776 void UiStyle::StyledMessage::style() const
778 QString user = userFromMask(sender());
779 QString host = hostFromMask(sender());
780 QString nick = nickFromMask(sender());
781 QString txt = UiStyle::mircToInternal(contents());
782 QString bufferName = bufferInfo().bufferName();
783 bufferName.replace('%', "%%"); // well, you _can_ have a % in a buffername apparently... -_-
784 host.replace('%', "%%"); // hostnames too...
785 user.replace('%', "%%"); // and the username...
786 nick.replace('%', "%%"); // ... and then there's totally RFC-violating servers like justin.tv m(
787 const int maxNetsplitNicks = 15;
792 t = QString("%1").arg(txt); break;
793 case Message::Notice:
794 t = QString("%1").arg(txt); break;
795 case Message::Action:
796 t = QString("%DN%1%DN %2").arg(nick).arg(txt);
800 if (nick == contents()) t = tr("You are now known as %DN%1%DN").arg(txt);
801 else t = tr("%DN%1%DN is now known as %DN%2%DN").arg(nick, txt);
805 if (nick.isEmpty()) t = tr("User mode: %DM%1%DM").arg(txt);
806 else t = tr("Mode %DM%1%DM by %DN%2%DN").arg(txt, nick);
810 t = tr("%DN%1%DN %DH(%2@%3)%DH has joined %DC%4%DC").arg(nick, user, host, bufferName); break;
813 t = tr("%DN%1%DN %DH(%2@%3)%DH has left %DC%4%DC").arg(nick, user, host, bufferName);
814 if (!txt.isEmpty()) t = QString("%1 (%2)").arg(t).arg(txt);
818 t = tr("%DN%1%DN %DH(%2@%3)%DH has quit").arg(nick, user, host);
819 if (!txt.isEmpty()) t = QString("%1 (%2)").arg(t).arg(txt);
823 QString victim = txt.section(" ", 0, 0);
824 QString kickmsg = txt.section(" ", 1);
826 t = tr("%DN%1%DN has kicked %DN%2%DN from %DC%3%DC").arg(nick).arg(victim).arg(bufferName);
827 if (!kickmsg.isEmpty()) t = QString("%1 (%2)").arg(t).arg(kickmsg);
830 //case Message::Kill: FIXME
832 case Message::Server:
833 t = QString("%1").arg(txt); break;
835 t = QString("%1").arg(txt); break;
837 t = QString("%1").arg(txt); break;
838 case Message::DayChange:
840 //: Day Change Message
841 t = tr("{Day changed to %1}").arg(timestamp().date().toString(Qt::DefaultLocaleLongDate));
845 t = QString("%1").arg(txt); break;
846 case Message::NetsplitJoin:
848 QStringList users = txt.split("#:#");
849 QStringList servers = users.takeLast().split(" ");
851 for (int i = 0; i < users.count() && i < maxNetsplitNicks; i++)
852 users[i] = nickFromMask(users.at(i));
854 t = tr("Netsplit between %DH%1%DH and %DH%2%DH ended. Users joined: ").arg(servers.at(0), servers.at(1));
855 if (users.count() <= maxNetsplitNicks)
856 t.append(QString("%DN%1%DN").arg(users.join(", ")));
858 t.append(tr("%DN%1%DN (%2 more)").arg(static_cast<QStringList>(users.mid(0, maxNetsplitNicks)).join(", ")).arg(users.count() - maxNetsplitNicks));
861 case Message::NetsplitQuit:
863 QStringList users = txt.split("#:#");
864 QStringList servers = users.takeLast().split(" ");
866 for (int i = 0; i < users.count() && i < maxNetsplitNicks; i++)
867 users[i] = nickFromMask(users.at(i));
869 t = tr("Netsplit between %DH%1%DH and %DH%2%DH. Users quit: ").arg(servers.at(0), servers.at(1));
871 if (users.count() <= maxNetsplitNicks)
872 t.append(QString("%DN%1%DN").arg(users.join(", ")));
874 t.append(tr("%DN%1%DN (%2 more)").arg(static_cast<QStringList>(users.mid(0, maxNetsplitNicks)).join(", ")).arg(users.count() - maxNetsplitNicks));
877 case Message::Invite:
878 t = QString("%1").arg(txt); break;
880 t = QString("[%1]").arg(txt);
882 _contents = UiStyle::styleString(t, UiStyle::formatType(type()));
886 const QString &UiStyle::StyledMessage::plainContents() const
888 if (_contents.plainText.isNull())
891 return _contents.plainText;
895 const UiStyle::FormatList &UiStyle::StyledMessage::contentsFormatList() const
897 if (_contents.plainText.isNull())
900 return _contents.formatList;
904 QString UiStyle::StyledMessage::decoratedTimestamp() const
906 return timestamp().toLocalTime().toString(UiStyle::timestampFormatString());
910 QString UiStyle::StyledMessage::plainSender() const
914 case Message::Notice:
915 return nickFromMask(sender());
922 QString UiStyle::StyledMessage::decoratedSender() const
924 QString _senderPrefixes;
925 if (_showSenderPrefixes) {
926 _senderPrefixes = senderPrefixes();
931 if (_showSenderBrackets)
932 return QString("<%1%2>").arg(_senderPrefixes, plainSender());
934 return QString("%1%2").arg(_senderPrefixes, plainSender());
935 case Message::Notice:
936 return QString("[%1%2]").arg(_senderPrefixes, plainSender());
937 case Message::Action:
953 case Message::Server:
959 case Message::DayChange:
963 case Message::NetsplitJoin:
965 case Message::NetsplitQuit:
967 case Message::Invite:
971 return QString("%1%2").arg(_senderPrefixes, plainSender());
975 // FIXME hardcoded to 16 sender hashes
976 quint8 UiStyle::StyledMessage::senderHash() const
978 if (_senderHash != 0xff)
983 // HACK: Until multiple nicknames with different colors can be solved in the theming engine,
984 // for /nick change notifications, use the color of the new nickname (if possible), not the old
986 if (type() == Message::Nick) {
987 // New nickname is given as contents. Change to that.
988 nick = stripFormatCodes(contents()).toLower();
990 // Just use the sender directly
991 nick = nickFromMask(sender()).toLower();
994 if (!nick.isEmpty()) {
996 while (chopCount < nick.size() && nick.at(nick.count() - 1 - chopCount) == '_')
998 if (chopCount < nick.size())
999 nick.chop(chopCount);
1001 quint16 hash = qChecksum(nick.toLatin1().data(), nick.toLatin1().size());
1002 return (_senderHash = (hash & 0xf) + 1);
1005 /***********************************************************************************/
1007 #if QT_VERSION < 0x050000
1008 uint qHash(UiStyle::ItemFormatType key)
1010 return qHash(static_cast<quint32>(key));
1015 uint qHash(UiStyle::ItemFormatType key, uint seed)
1017 return qHash(static_cast<quint32>(key), seed);
1021 UiStyle::FormatType operator|(UiStyle::FormatType lhs, UiStyle::FormatType rhs)
1023 return static_cast<UiStyle::FormatType>(static_cast<quint32>(lhs) | static_cast<quint32>(rhs));
1026 UiStyle::FormatType& operator|=(UiStyle::FormatType& lhs, UiStyle::FormatType rhs)
1028 lhs = static_cast<UiStyle::FormatType>(static_cast<quint32>(lhs) | static_cast<quint32>(rhs));
1033 UiStyle::FormatType operator|(UiStyle::FormatType lhs, quint32 rhs)
1035 return static_cast<UiStyle::FormatType>(static_cast<quint32>(lhs) | rhs);
1039 UiStyle::FormatType& operator|=(UiStyle::FormatType &lhs, quint32 rhs)
1041 lhs = static_cast<UiStyle::FormatType>(static_cast<quint32>(lhs) | rhs);
1046 UiStyle::FormatType operator&(UiStyle::FormatType lhs, UiStyle::FormatType rhs)
1048 return static_cast<UiStyle::FormatType>(static_cast<quint32>(lhs) & static_cast<quint32>(rhs));
1052 UiStyle::FormatType& operator&=(UiStyle::FormatType &lhs, UiStyle::FormatType rhs)
1054 lhs = static_cast<UiStyle::FormatType>(static_cast<quint32>(lhs) & static_cast<quint32>(rhs));
1059 UiStyle::FormatType operator&(UiStyle::FormatType lhs, quint32 rhs)
1061 return static_cast<UiStyle::FormatType>(static_cast<quint32>(lhs) & rhs);
1065 UiStyle::FormatType& operator&=(UiStyle::FormatType &lhs, quint32 rhs)
1067 lhs = static_cast<UiStyle::FormatType>(static_cast<quint32>(lhs) & rhs);
1072 UiStyle::FormatType& operator^=(UiStyle::FormatType &lhs, UiStyle::FormatType rhs)
1074 lhs = static_cast<UiStyle::FormatType>(static_cast<quint32>(lhs) ^ static_cast<quint32>(rhs));
1079 UiStyle::MessageLabel operator|(UiStyle::MessageLabel lhs, UiStyle::MessageLabel rhs)
1081 return static_cast<UiStyle::MessageLabel>(static_cast<quint32>(lhs) | static_cast<quint32>(rhs));
1085 UiStyle::MessageLabel& operator|=(UiStyle::MessageLabel &lhs, UiStyle::MessageLabel rhs)
1087 lhs = static_cast<UiStyle::MessageLabel>(static_cast<quint32>(lhs) | static_cast<quint32>(rhs));
1092 UiStyle::MessageLabel operator&(UiStyle::MessageLabel lhs, quint32 rhs)
1094 return static_cast<UiStyle::MessageLabel>(static_cast<quint32>(lhs) & rhs);
1098 UiStyle::MessageLabel& operator&=(UiStyle::MessageLabel &lhs, UiStyle::MessageLabel rhs)
1100 lhs = static_cast<UiStyle::MessageLabel>(static_cast<quint32>(lhs) & static_cast<quint32>(rhs));
1105 quint64 operator|(UiStyle::FormatType lhs, UiStyle::MessageLabel rhs)
1107 return static_cast<quint64>(lhs) | (static_cast<quint64>(rhs) << 32ull);
1111 UiStyle::ItemFormatType operator|(UiStyle::ItemFormatType lhs, UiStyle::ItemFormatType rhs)
1113 return static_cast<UiStyle::ItemFormatType>(static_cast<quint32>(lhs) | static_cast<quint32>(rhs));
1117 UiStyle::ItemFormatType& operator|=(UiStyle::ItemFormatType &lhs, UiStyle::ItemFormatType rhs)
1119 lhs = static_cast<UiStyle::ItemFormatType>(static_cast<quint32>(lhs) | static_cast<quint32>(rhs));
1123 /***********************************************************************************/
1125 QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList)
1127 out << static_cast<quint16>(formatList.size());
1128 UiStyle::FormatList::const_iterator it = formatList.begin();
1129 while (it != formatList.end()) {
1130 out << it->first << static_cast<quint32>(it->second.type);
1137 QDataStream &operator>>(QDataStream &in, UiStyle::FormatList &formatList)
1141 for (quint16 i = 0; i < cnt; i++) {
1142 quint16 pos; quint32 ftype;
1144 formatList.emplace_back(std::make_pair(quint16{pos}, UiStyle::Format{static_cast<UiStyle::FormatType>(ftype)}));