/***************************************************************************
- * Copyright (C) 2005-2012 by the Quassel Project *
+ * Copyright (C) 2005-2016 by the Quassel Project *
* devel@quassel-irc.org *
* *
* This program is free software; you can redistribute it and/or modify *
***************************************************************************/
#include <QApplication>
+#include <QIcon>
#include "buffersettings.h"
-#include "iconloader.h"
#include "qssparser.h"
#include "quassel.h"
#include "uistyle.h"
#include "util.h"
QHash<QString, UiStyle::FormatType> UiStyle::_formatCodes;
-QString UiStyle::_timestampFormatString;
+bool UiStyle::_useCustomTimestampFormat; /// If true, use the custom timestamp format
+QString UiStyle::_timestampFormatString; /// Timestamp format
+QString UiStyle::_systemTimestampFormatString; /// Cached copy of system locale timestamp format
+bool UiStyle::_showSenderPrefixes; /// If true, show prefixmodes before sender names
+bool UiStyle::_showSenderBrackets; /// If true, show brackets around sender names
UiStyle::UiStyle(QObject *parent)
: QObject(parent),
- _channelJoinedIcon(SmallIcon("irc-channel-active")),
- _channelPartedIcon(SmallIcon("irc-channel-inactive")),
- _userOfflineIcon(SmallIcon("im-user-offline")),
- _userOnlineIcon(SmallIcon("im-user")),
- _userAwayIcon(SmallIcon("im-user-away")),
- _categoryOpIcon(SmallIcon("irc-operator")),
- _categoryVoiceIcon(SmallIcon("irc-voice")),
+ _channelJoinedIcon(QIcon::fromTheme("irc-channel-joined", QIcon(":/icons/irc-channel-joined.png"))),
+ _channelPartedIcon(QIcon::fromTheme("irc-channel-parted", QIcon(":/icons/irc-channel-parted.png"))),
+ _userOfflineIcon(QIcon::fromTheme("im-user-offline", QIcon::fromTheme("user-offline", QIcon(":/icons/im-user-offline.png")))),
+ _userOnlineIcon(QIcon::fromTheme("im-user", QIcon::fromTheme("user-available", QIcon(":/icons/im-user.png")))), // im-user-* are non-standard oxygen extensions
+ _userAwayIcon(QIcon::fromTheme("im-user-away", QIcon::fromTheme("user-away", QIcon(":/icons/im-user-away.png")))),
+ _categoryOpIcon(QIcon::fromTheme("irc-operator")),
+ _categoryVoiceIcon(QIcon::fromTheme("irc-voice")),
_opIconLimit(UserCategoryItem::categoryFromModes("o")),
_voiceIconLimit(UserCategoryItem::categoryFromModes("v"))
{
_formatCodes["%DM"] = ModeFlags;
_formatCodes["%DU"] = Url;
- setTimestampFormatString("[hh:mm:ss]");
+ // Initialize fallback defaults
+ // NOTE: If you change this, update qtui/chatviewsettings.h, too. More explanations available
+ // in there.
+ setUseCustomTimestampFormat(false);
+ setTimestampFormatString(" hh:mm:ss");
+ enableSenderPrefixes(false);
+ enableSenderBrackets(true);
// BufferView / NickView settings
UiStyleSettings s;
QString styleSheet;
styleSheet += loadStyleSheet("file:///" + Quassel::findDataFilePath("stylesheets/default.qss"));
styleSheet += loadStyleSheet("file:///" + Quassel::configDirPath() + "settings.qss");
- if (s.value("UseCustomStyleSheet", false).toBool())
- styleSheet += loadStyleSheet("file:///" + s.value("CustomStyleSheetPath").toString(), true);
+ if (s.value("UseCustomStyleSheet", false).toBool()) {
+ QString customSheetPath(s.value("CustomStyleSheetPath").toString());
+ QString customSheet = loadStyleSheet("file:///" + customSheetPath, true);
+ if (customSheet.isEmpty()) {
+ // MIGRATION: changed default install path for data from /usr/share/apps to /usr/share
+ if (customSheetPath.startsWith("/usr/share/apps/quassel")) {
+ customSheetPath.replace(QRegExp("^/usr/share/apps"), "/usr/share");
+ customSheet = loadStyleSheet("file:///" + customSheetPath, true);
+ if (!customSheet.isEmpty()) {
+ s.setValue("CustomStyleSheetPath", customSheetPath);
+ qDebug() << "Custom stylesheet path migrated to" << customSheetPath;
+ }
+ }
+ }
+ styleSheet += customSheet;
+ }
styleSheet += loadStyleSheet("file:///" + Quassel::optionValue("qss"), true);
if (!styleSheet.isEmpty()) {
}
+void UiStyle::updateSystemTimestampFormat()
+{
+ // Does the system locale use AM/PM designators? For example:
+ // AM/PM: h:mm AP
+ // AM/PM: hh:mm a
+ // 24-hour: h:mm
+ // 24-hour: hh:mm ADD things
+ // For timestamp format, see https://doc.qt.io/qt-5/qdatetime.html#toString
+ // This won't update if the system locale is changed while Quassel is running. If need be,
+ // Quassel could hook into notifications of changing system locale to update this.
+ //
+ // Match any AP or A designation if on a word boundary, including underscores.
+ // .*(\b|_)(A|AP)(\b|_).*
+ // .* Match any number of characters
+ // \b Match a word boundary, i.e. "AAA.BBB", "." is matched
+ // _ Match the literal character '_' (not considered a word boundary)
+ // (X|Y) Match either X or Y, exactly
+ //
+ // Note that '\' must be escaped as '\\'
+ // QRegExp does not support (?> ...), so it's replaced with standard matching, (...)
+ // Helpful interactive website for debugging and explaining: https://regex101.com/
+ const QRegExp regExpMatchAMPM(".*(\\b|_)(A|AP)(\\b|_).*", Qt::CaseInsensitive);
+
+ if (regExpMatchAMPM.exactMatch(QLocale::system().timeFormat(QLocale::ShortFormat))) {
+ // AM/PM style used
+ _systemTimestampFormatString = " h:mm:ss ap";
+ } else {
+ // 24-hour style used
+ _systemTimestampFormatString = " hh:mm:ss";
+ }
+ // Include a space to give the timestamp a small bit of padding between the border of the chat
+ // buffer window and the numbers. Helps with readability.
+ // If you change this to include brackets, e.g. "[hh:mm:ss]", also update
+ // ChatScene::updateTimestampHasBrackets() to true or false as needed!
+}
+
+
+// FIXME The following should trigger a reload/refresh of the chat view.
+void UiStyle::setUseCustomTimestampFormat(bool enabled)
+{
+ if (_useCustomTimestampFormat != enabled) {
+ _useCustomTimestampFormat = enabled;
+ }
+}
+
void UiStyle::setTimestampFormatString(const QString &format)
{
if (_timestampFormatString != format) {
_timestampFormatString = format;
- // FIXME reload
+ }
+}
+
+void UiStyle::enableSenderPrefixes(bool enabled)
+{
+ if (_showSenderPrefixes != enabled) {
+ _showSenderPrefixes = enabled;
+ }
+}
+
+void UiStyle::enableSenderBrackets(bool enabled)
+{
+ if (_showSenderBrackets != enabled) {
+ _showSenderBrackets = enabled;
}
}
UiStyle::StyledString UiStyle::styleString(const QString &s_, quint32 baseFormat)
{
QString s = s_;
+ StyledString result;
+ result.formatList.append(qMakePair((quint16)0, baseFormat));
+
if (s.length() > 65535) {
+ // We use quint16 for indexes
qWarning() << QString("String too long to be styled: %1").arg(s);
- return StyledString();
+ result.plainText = s;
+ return result;
}
- StyledString result;
- result.formatList.append(qMakePair((quint16)0, baseFormat));
+
quint32 curfmt = baseFormat;
int pos = 0; quint16 length = 0;
for (;;) {
QString UiStyle::mircToInternal(const QString &mirc_)
{
- QString mirc = mirc_;
- mirc.replace('%', "%%"); // escape % just to be sure
- mirc.replace('\t', " "); // tabs break layout, also this is italics in Konversation
- mirc.replace('\x02', "%B");
- mirc.replace('\x0f', "%O");
- mirc.replace('\x12', "%R");
- mirc.replace('\x16', "%R");
- mirc.replace('\x1d', "%S");
- mirc.replace('\x1f', "%U");
+ QString mirc;
+ mirc.reserve(mirc_.size());
+ foreach (const QChar &c, mirc_) {
+ if ((c < '\x20' || c == '\x7f') && c != '\x03') {
+ switch (c.unicode()) {
+ case '\x02':
+ mirc += "%B";
+ break;
+ case '\x0f':
+ mirc += "%O";
+ break;
+ case '\x09':
+ mirc += " ";
+ break;
+ case '\x12':
+ case '\x16':
+ mirc += "%R";
+ break;
+ case '\x1d':
+ mirc += "%S";
+ break;
+ case '\x1f':
+ mirc += "%U";
+ break;
+ case '\x7f':
+ mirc += QChar(0x2421);
+ break;
+ default:
+ mirc += QChar(0x2400 + c.unicode());
+ }
+ } else {
+ if (c == '%')
+ mirc += c;
+ mirc += c;
+ }
+ }
// Now we bring the color codes (\x03) in a sane format that can be parsed more easily later.
// %Dcfxx is foreground, %Dcbxx is background color, where xx is a 2 digit dec number denoting the color code.
}
+QString UiStyle::systemTimestampFormatString()
+{
+ if (_systemTimestampFormatString.isEmpty()) {
+ // Calculate and cache the system timestamp format string
+ updateSystemTimestampFormat();
+ }
+ return _systemTimestampFormatString;
+}
+
+
+QString UiStyle::timestampFormatString()
+{
+ if (useCustomTimestampFormat()) {
+ return _timestampFormatString;
+ } else {
+ return systemTimestampFormatString();
+ }
+}
+
+
/***********************************************************************************/
UiStyle::StyledMessage::StyledMessage(const Message &msg)
: Message(msg)
{
- if (type() == Message::Plain)
- _senderHash = 0xff;
- else
- _senderHash = 0x00; // this means we never compute the hash for msgs that aren't plain
+ switch (type()) {
+ // Don't compute the sender hash for message types without a nickname embedded
+ case Message::Server:
+ case Message::Info:
+ case Message::Error:
+ case Message::DayChange:
+ case Message::Topic:
+ case Message::Invite:
+ // Don't compute the sender hash for messages with multiple nicks
+ // Fixing this without breaking themes would be.. complex.
+ case Message::NetsplitJoin:
+ case Message::NetsplitQuit:
+ case Message::Kick:
+ // Don't compute the sender hash for message types that are not yet completed elsewhere
+ case Message::Kill:
+ _senderHash = 0x00;
+ break;
+ default:
+ // Compute the sender hash for all other message types
+ _senderHash = 0xff;
+ break;
+ }
}
QString t;
switch (type()) {
case Message::Plain:
- //: Plain Message
- t = tr("%1").arg(txt); break;
+ t = QString("%1").arg(txt); break;
case Message::Notice:
- //: Notice Message
- t = tr("%1").arg(txt); break;
+ t = QString("%1").arg(txt); break;
case Message::Action:
- //: Action Message
- t = tr("%DN%1%DN %2").arg(nick).arg(txt);
+ t = QString("%DN%1%DN %2").arg(nick).arg(txt);
break;
case Message::Nick:
//: Nick Message
//case Message::Kill: FIXME
case Message::Server:
- //: Server Message
- t = tr("%1").arg(txt); break;
+ t = QString("%1").arg(txt); break;
case Message::Info:
- //: Info Message
- t = tr("%1").arg(txt); break;
+ t = QString("%1").arg(txt); break;
case Message::Error:
- //: Error Message
- t = tr("%1").arg(txt); break;
+ t = QString("%1").arg(txt); break;
case Message::DayChange:
+ {
//: Day Change Message
- t = tr("{Day changed to %1}").arg(timestamp().toString());
+ t = tr("{Day changed to %1}").arg(timestamp().date().toString(Qt::DefaultLocaleLongDate));
+ }
break;
case Message::Topic:
- //: Topic Message
- t = tr("%1").arg(txt); break;
+ t = QString("%1").arg(txt); break;
case Message::NetsplitJoin:
{
QStringList users = txt.split("#:#");
}
break;
case Message::Invite:
- //: Invite Message
- t = tr("%1").arg(txt); break;
+ t = QString("%1").arg(txt); break;
default:
- t = tr("[%1]").arg(txt);
+ t = QString("[%1]").arg(txt);
}
_contents = UiStyle::styleString(t, UiStyle::formatType(type()));
}
QString UiStyle::StyledMessage::decoratedSender() const
{
+ QString _senderPrefixes;
+ if (_showSenderPrefixes) {
+ _senderPrefixes = senderPrefixes();
+ }
+
switch (type()) {
case Message::Plain:
- return tr("<%1>").arg(plainSender()); break;
+ if (_showSenderBrackets)
+ return QString("<%1%2>").arg(_senderPrefixes, plainSender());
+ else
+ return QString("%1%2").arg(_senderPrefixes, plainSender());
+ break;
case Message::Notice:
- return tr("[%1]").arg(plainSender()); break;
+ return QString("[%1%2]").arg(_senderPrefixes, plainSender()); break;
case Message::Action:
return "-*-"; break;
case Message::Nick:
case Message::Invite:
return "->"; break;
default:
- return QString("%1").arg(plainSender());
+ return QString("%1%2").arg(_senderPrefixes, plainSender());
}
}
if (_senderHash != 0xff)
return _senderHash;
- QString nick = nickFromMask(sender()).toLower();
+ QString nick;
+
+ // HACK: Until multiple nicknames with different colors can be solved in the theming engine,
+ // for /nick change notifications, use the color of the new nickname (if possible), not the old
+ // nickname.
+ if (type() == Message::Nick) {
+ // New nickname is given as contents. Change to that.
+ nick = stripFormatCodes(contents()).toLower();
+ } else {
+ // Just use the sender directly
+ nick = nickFromMask(sender()).toLower();
+ }
+
if (!nick.isEmpty()) {
int chopCount = 0;
while (chopCount < nick.size() && nick.at(nick.count() - 1 - chopCount) == '_')
if (chopCount < nick.size())
nick.chop(chopCount);
}
- quint16 hash = qChecksum(nick.toAscii().data(), nick.toAscii().size());
+ quint16 hash = qChecksum(nick.toLatin1().data(), nick.toLatin1().size());
return (_senderHash = (hash & 0xf) + 1);
}