| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "miscellaneous/skinfactory.h" |
| 4 | |
| 5 | #include "miscellaneous/application.h" |
| 6 | #include "miscellaneous/settings.h" |
| 7 | #include "network-web/networkfactory.h" |
| 8 | #include "network-web/webfactory.h" |
| 9 | #include "services/abstract/rootitem.h" |
| 10 | |
| 11 | #include <QDir> |
| 12 | #include <QDomDocument> |
| 13 | #include <QDomElement> |
| 14 | #include <QFontDatabase> |
| 15 | #include <QMetaEnum> |
| 16 | #include <QMetaObject> |
| 17 | #include <QProcessEnvironment> |
| 18 | #include <QStyle> |
| 19 | #include <QStyleFactory> |
| 20 | #include <QStyleHints> |
| 21 | #include <QTextDocument> |
| 22 | #include <QToolTip> |
| 23 | |
| 24 | SkinFactory::SkinFactory(QObject* parent) : QObject(parent), m_styleIsFrozen(false), m_useSkinColors(false) {} |
| 25 | |
| 26 | void SkinFactory::loadCurrentSkin(bool lite, bool replace_existing_qss) { |
| 27 | QList<QString> skin_names_to_try = {selectedSkinName(), QSL(APP_SKIN_DEFAULT)}; |
| 28 | bool skin_parsed; |
| 29 | Skin skin_data; |
| 30 | QString skin_name; |
| 31 | |
| 32 | while (!skin_names_to_try.isEmpty()) { |
| 33 | skin_name = skin_names_to_try.takeFirst(); |
| 34 | skin_data = skinInfo(skin_name, lite, &skin_parsed); |
| 35 | |
| 36 | if (skin_parsed) { |
| 37 | loadSkinFromData(skin_data, replace_existing_qss); |
| 38 | |
| 39 | // Set this 'Skin' object as active one. |
| 40 | m_currentSkin = skin_data; |
| 41 | qDebugNN << LOGSEC_GUI << "Skin" << QUOTE_W_SPACE(skin_name) << "loaded. Lite:" << QUOTE_W_SPACE_DOT(lite); |
| 42 | return; |
| 43 | } |
| 44 | else { |
| 45 | qWarningNN << LOGSEC_GUI << "Failed to load skin" << QUOTE_W_SPACE_DOT(skin_name); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | qCriticalNN << LOGSEC_GUI << "Failed to load selected or default skin. Quitting!"; |
| 50 | } |
| 51 | |
| 52 | QVariant SkinFactory::colorForModel(SkinEnums::PaletteColors type, bool ignore_custom_colors) const { |
| 53 | return m_currentSkin.colorForModel(type, m_useSkinColors, ignore_custom_colors); |
| 54 | } |
| 55 | |
| 56 | bool SkinFactory::isStyleGoodForAlternativeStylePalette(const QString& style_name) const { |
| 57 | static QRegularExpression re = QRegularExpression(QSL("^(fusion|windows|qt[56]ct-style)$")); |
| 58 | |
| 59 | return re.match(style_name.toLower()).hasMatch(); |
| 60 | } |
| 61 | |
| 62 | // NOTE: Taken from "QPlatformThemePrivate" Qt class. |
| 63 | // This is here because in Qt 6.5.0, they hardcoded |
| 64 | // DARK palette if user has enabled "dark mode" in OS. |
| 65 | QPalette qt_fusionPalette(bool dark_appearance) { |
| 66 | const QColor window_text = dark_appearance ? QColor(240, 240, 240) : Qt::black; |
| 67 | const QColor background = dark_appearance ? QColor(50, 50, 50) : QColor(239, 239, 239); |
| 68 | const QColor light = background.lighter(150); |
| 69 | const QColor mid = (background.darker(130)); |
| 70 | const QColor midlight = mid.lighter(110); |
| 71 | const QColor base = dark_appearance ? background.darker(140) : Qt::white; |
| 72 | const QColor disabled_base(background); |
| 73 | const QColor dark = background.darker(150); |
| 74 | const QColor disabled_dark = QColor(209, 209, 209).darker(110); |
| 75 | const QColor text = dark_appearance ? window_text : Qt::black; |
| 76 | const QColor highlight = QColor(48, 140, 198); |
| 77 | const QColor hightlighted_text = dark_appearance ? window_text : Qt::white; |
| 78 | const QColor disabled_text = dark_appearance ? QColor(130, 130, 130) : QColor(190, 190, 190); |
| 79 | const QColor button = background; |
| 80 | const QColor shadow = dark.darker(135); |
| 81 | const QColor disabled_shadow = shadow.lighter(150); |
| 82 | |
| 83 | QColor placeholder = text; |
| 84 | placeholder.setAlpha(128); |
| 85 | |
| 86 | QPalette fusion_palette(window_text, background, light, dark, mid, text, base); |
| 87 | fusion_palette.setBrush(QPalette::Midlight, midlight); |
| 88 | fusion_palette.setBrush(QPalette::Button, button); |
| 89 | fusion_palette.setBrush(QPalette::Shadow, shadow); |
| 90 | fusion_palette.setBrush(QPalette::HighlightedText, hightlighted_text); |
| 91 | |
| 92 | fusion_palette.setBrush(QPalette::Disabled, QPalette::Text, disabled_text); |
| 93 | fusion_palette.setBrush(QPalette::Disabled, QPalette::WindowText, disabled_text); |
| 94 | fusion_palette.setBrush(QPalette::Disabled, QPalette::ButtonText, disabled_text); |
| 95 | fusion_palette.setBrush(QPalette::Disabled, QPalette::Base, disabled_base); |
| 96 | fusion_palette.setBrush(QPalette::Disabled, QPalette::Dark, disabled_dark); |
| 97 | fusion_palette.setBrush(QPalette::Disabled, QPalette::Shadow, disabled_shadow); |
| 98 | |
| 99 | fusion_palette.setBrush(QPalette::Active, QPalette::Highlight, highlight); |
| 100 | fusion_palette.setBrush(QPalette::Inactive, QPalette::Highlight, highlight); |
| 101 | fusion_palette.setBrush(QPalette::Disabled, QPalette::Highlight, QColor(145, 145, 145)); |
| 102 | |
| 103 | fusion_palette.setBrush(QPalette::PlaceholderText, placeholder); |
| 104 | |
| 105 | if (dark_appearance) { |
| 106 | fusion_palette.setBrush(QPalette::Link, highlight); |
| 107 | } |
| 108 | |
| 109 | return fusion_palette; |
| 110 | } |
| 111 | |
| 112 | void SkinFactory::loadSkinFromData(const Skin& skin, bool replace_existing_qss) { |
| 113 | #if QT_VERSION >= 0x060500 // Qt >= 6.5.0 |
| 114 | auto system_color_scheme = qApp->styleHints()->colorScheme(); |
| 115 | |
| 116 | qDebugNN << LOGSEC_GUI << "OS defines color scheme:" << QUOTE_W_SPACE_DOT(system_color_scheme); |
| 117 | #endif |
| 118 | |
| 119 | QString style_name = qApp->settings()->value(GROUP(GUI), SETTING(GUI::Style)).toString(); |
| 120 | auto env = QProcessEnvironment::systemEnvironment(); |
| 121 | const QString env_forced_style = env.value(QSL("QT_STYLE_OVERRIDE")); |
| 122 | const QString cli_forced_style = qApp->cmdParser()->value(QSL(CLI_STYLE_SHORT)); |
| 123 | |
| 124 | if (env_forced_style.isEmpty() && cli_forced_style.isEmpty()) { |
| 125 | m_styleIsFrozen = false; |
| 126 | |
| 127 | if (!skin.m_forcedStyles.isEmpty()) { |
| 128 | qDebugNN << LOGSEC_GUI << "Forcing one of skin's declared styles:" << QUOTE_W_SPACE_DOT(skin.m_forcedStyles); |
| 129 | |
| 130 | for (const QString& skin_forced_style : skin.m_forcedStyles) { |
| 131 | if (qApp->setStyle(skin_forced_style) != nullptr) { |
| 132 | m_currentStyle = skin_forced_style; |
| 133 | break; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | else { |
| 138 | qDebugNN << LOGSEC_GUI << "Setting style:" << QUOTE_W_SPACE_DOT(style_name); |
| 139 | qApp->setStyle(style_name); |
| 140 | m_currentStyle = style_name; |
| 141 | } |
| 142 | } |
| 143 | else { |
| 144 | m_styleIsFrozen = true; |
| 145 | m_currentStyle = qApp->style()->objectName(); |
| 146 | |
| 147 | qWarningNN << LOGSEC_GUI << "Respecting forced style(s):\n" |
| 148 | << " QT_STYLE_OVERRIDE: " QUOTE_NO_SPACE(env_forced_style) << "\n" |
| 149 | << " CLI (-style): " QUOTE_NO_SPACE(cli_forced_style); |
| 150 | } |
| 151 | |
| 152 | m_useSkinColors = |
| 153 | skin.m_forcedSkinColors || qApp->settings()->value(GROUP(GUI), SETTING(GUI::ForcedSkinColors)).toBool(); |
| 154 | |
| 155 | if (m_useSkinColors && isStyleGoodForAlternativeStylePalette(m_currentStyle)) { |
| 156 | if (!skin.m_stylePalette.isEmpty()) { |
| 157 | qDebugNN << LOGSEC_GUI << "Activating alternative palette."; |
| 158 | |
| 159 | QPalette pal = skin.extractPalette(); |
| 160 | |
| 161 | QToolTip::setPalette(pal); |
| 162 | qApp->setPalette(pal); |
| 163 | } |
| 164 | // NOTE: Very hacky way of avoiding automatic "dark mode" |
| 165 | // palettes in some styles. Also in light mode, |
| 166 | // colors are now derived from system. |
| 167 | #if QT_VERSION >= 0x060500 // Qt >= 6.5.0 |
| 168 | else /*if (qApp->styleHints()->colorScheme() == Qt::ColorScheme::Light)*/ { |
| 169 | qApp->setPalette(qt_fusionPalette(false)); |
| 170 | } |
| 171 | #endif |
| 172 | } |
| 173 | |
| 174 | QString qss_to_set = skin.m_rawForcedData; |
| 175 | |
| 176 | if (m_useSkinColors && !skin.m_rawData.isEmpty()) { |
| 177 | if (qApp->styleSheet().simplified().isEmpty()) { |
| 178 | qss_to_set += QSL("\r\n") + skin.m_rawData; |
| 179 | } |
| 180 | else { |
| 181 | qCriticalNN << LOGSEC_GUI |
| 182 | << "Skipped setting of application style and skin because there is already some style set."; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | if (replace_existing_qss) { |
| 187 | qss_to_set = qss_to_set; |
| 188 | } |
| 189 | else { |
| 190 | qss_to_set = qApp->styleSheet() + QSL("\r\n") + qss_to_set; |
| 191 | } |
| 192 | |
| 193 | qApp->setStyleSheet(qss_to_set); |
| 194 | } |
| 195 | |
| 196 | void SkinFactory::setCurrentSkinName(const QString& skin_name) { |
| 197 | qApp->settings()->setValue(GROUP(GUI), GUI::Skin, skin_name); |
| 198 | } |
| 199 | |
| 200 | QString SkinFactory::customSkinBaseFolder() const { |
| 201 | return qApp->userDataFolder() + QDir::separator() + APP_SKIN_USER_FOLDER; |
| 202 | } |
| 203 | |
| 204 | QString SkinFactory::selectedSkinName() const { |
| 205 | return qApp->settings()->value(GROUP(GUI), SETTING(GUI::Skin)).toString(); |
| 206 | } |
| 207 | |
| 208 | QString SkinFactory::adBlockedPage(const QString& url, const QString& filter) { |
| 209 | const QString& adblocked = |
| 210 | currentSkin().m_adblocked.arg(tr("This page was blocked by AdBlock"), |
| 211 | tr(R"(Blocked URL: "%1"<br/>Used filter: "%2")").arg(url, filter)); |
| 212 | |
| 213 | return currentSkin().m_layoutMarkupWrapper.arg(tr("This page was blocked by AdBlock"), adblocked); |
| 214 | } |
| 215 | |
| 216 | PreparedHtml SkinFactory::prepareHtml(const QString& inner_html, const QUrl& base_url) { |
| 217 | return {currentSkin().m_layoutMarkupWrapper.arg(QString(), inner_html), base_url}; |
| 218 | } |
| 219 | |
| 220 | PreparedHtml SkinFactory::generateHtmlOfArticles(const QList<Message>& messages, |
| 221 | RootItem* root, |
| 222 | int desired_width) const { |
| 223 | Skin skin = currentSkin(); |
| 224 | QString messages_layout; |
| 225 | QString single_message_layout = skin.m_layoutMarkup; |
| 226 | const int forced_img_height = |
| 227 | qApp->settings()->value(GROUP(Messages), SETTING(Messages::LimitArticleImagesHeight)).toInt(); |
| 228 | |
| 229 | auto* feed = root != nullptr |
| 230 | ? root->getParentServiceRoot() |
| 231 | ->getItemFromSubTree([messages](const RootItem* it) { |
| 232 | return it->kind() == RootItem::Kind::Feed && it->customId() == messages.at(0).m_feedId; |
| 233 | }) |
| 234 | ->toFeed() |
| 235 | : nullptr; |
| 236 | |
| 237 | for (const Message& message : messages) { |
| 238 | QString enclosures; |
| 239 | QString enclosure_images; |
| 240 | bool is_plain = !TextFactory::couldBeHtml(message.m_contents); |
| 241 | |
| 242 | if (root == nullptr || root->getParentServiceRoot()->displaysEnclosures()) { |
| 243 | for (const Enclosure& enclosure : message.m_enclosures) { |
| 244 | QString enc_url = QUrl::fromPercentEncoding(enclosure.m_url.toUtf8()); |
| 245 | |
| 246 | enclosures += skin.m_enclosureMarkup.arg(enc_url, enclosure.m_mimeType); |
| 247 | |
| 248 | if (qApp->settings()->value(GROUP(Messages), SETTING(Messages::DisplayEnclosuresInMessage)).toBool()) { |
| 249 | if (enclosure.m_mimeType.startsWith(QSL("image/")) && |
| 250 | qApp->settings()->value(GROUP(Messages), SETTING(Messages::DisplayEnclosuresInMessage)).toBool()) { |
| 251 | // Add thumbnail image. |
| 252 | enclosure_images += |
| 253 | skin.m_enclosureImageMarkup.arg(enclosure.m_url, |
| 254 | enclosure.m_mimeType, |
| 255 | forced_img_height <= 0 ? QString::number(-1) |
| 256 | : QString::number(forced_img_height)); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | QString msg_date = |
| 263 | qApp->settings()->value(GROUP(Messages), SETTING(Messages::UseCustomDate)).toBool() |
| 264 | ? message.m_created.toLocalTime() |
| 265 | .toString(qApp->settings()->value(GROUP(Messages), SETTING(Messages::CustomDateFormat)).toString()) |
| 266 | : qApp->localization()->loadedLocale().toString(message.m_created.toLocalTime(), |
| 267 | QLocale::FormatType::ShortFormat); |
| 268 | |
| 269 | QString msg_contents = is_plain ? Qt::convertFromPlainText(message.m_contents, Qt::WhiteSpaceMode::WhiteSpaceNormal) |
| 270 | : message.m_contents; |
| 271 | |
| 272 | if (!is_plain) { |
| 273 | msg_contents = qApp->web()->limitSizeOfHtmlImages(msg_contents, desired_width, forced_img_height); |
| 274 | } |
| 275 | |
| 276 | messages_layout.append(single_message_layout.arg(message.m_title, |
| 277 | tr("Written by ") + (message.m_author.isEmpty() |
| 278 | ? tr("unknown author") |
| 279 | : message.m_author), |
| 280 | message.m_url, |
| 281 | msg_contents, |
| 282 | msg_date, |
| 283 | enclosures, |
| 284 | enclosure_images, |
| 285 | QString::number(message.m_id), |
| 286 | (message.m_rtlBehavior == RtlBehavior::Everywhere || |
| 287 | message.m_rtlBehavior == RtlBehavior::EverywhereExceptFeedList || |
| 288 | message.m_rtlBehavior == RtlBehavior::OnlyViewer) |
| 289 | ? QSL("rtl") |
| 290 | : QSL("ltr"))); |
| 291 | } |
| 292 | |
| 293 | QString msg_contents = |
| 294 | skin.m_layoutMarkupWrapper.arg(messages.size() == 1 ? messages.at(0).m_title : tr("Newspaper view"), |
| 295 | messages_layout); |
| 296 | QString base_url; |
| 297 | |
| 298 | if (feed != nullptr) { |
| 299 | QUrl url(NetworkFactory::sanitizeUrl(feed->source())); |
| 300 | |
| 301 | if (url.isValid()) { |
| 302 | if (url.isLocalFile()) { |
| 303 | base_url = url.scheme() + QSL("://") + url.toLocalFile(); |
| 304 | } |
| 305 | else { |
| 306 | base_url = url.scheme() + QSL("://") + url.host(); |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | return {msg_contents, base_url}; |
| 312 | } |
| 313 | |
| 314 | Skin SkinFactory::skinInfo(const QString& skin_name, bool lite, bool* ok) const { |
| 315 | Skin skin; |
| 316 | const QStringList skins_root_folders = {APP_SKIN_PATH, customSkinBaseFolder()}; |
| 317 | |
| 318 | for (const QString& skins_root_folder : skins_root_folders) { |
| 319 | const QString skin_parent = QString(skins_root_folder).replace(QDir::separator(), QL1C('/')) + QL1C('/'); |
| 320 | const QString skin_folder_no_sep = skin_parent + skin_name; |
| 321 | const QString skin_folder_with_sep = skin_folder_no_sep + QDir::separator(); |
| 322 | const QString metadata_file = skin_folder_with_sep + APP_SKIN_METADATA_FILE; |
| 323 | |
| 324 | if (QFile::exists(metadata_file)) { |
| 325 | QFile skin_file(metadata_file); |
| 326 | QDomDocument document; |
| 327 | |
| 328 | if (!skin_file.open(QIODevice::OpenModeFlag::Text | QIODevice::OpenModeFlag::ReadOnly) || |
| 329 | !document.setContent(&skin_file, true)) { |
| 330 | if (ok != nullptr) { |
| 331 | *ok = false; |
| 332 | } |
| 333 | |
| 334 | return skin; |
| 335 | } |
| 336 | |
| 337 | const QDomNode skin_node = document.namedItem(QSL("skin")); |
| 338 | const QString base_skin_name = skin_node.toElement().attribute(QSL("base")); |
| 339 | QString real_base_skin_folder; |
| 340 | |
| 341 | if (!base_skin_name.isEmpty()) { |
| 342 | // We now find folder of "base" skin. |
| 343 | for (const QString& parent_for_base : skins_root_folders) { |
| 344 | QString full_base = parent_for_base + QDir::separator() + base_skin_name; |
| 345 | |
| 346 | if (QDir().exists(full_base)) { |
| 347 | real_base_skin_folder = full_base; |
| 348 | real_base_skin_folder = real_base_skin_folder.replace(QDir::separator(), QL1C('/')); |
| 349 | |
| 350 | qDebugNN << LOGSEC_GUI << "Found path of base skin:" |
| 351 | << QUOTE_W_SPACE_DOT(QDir::toNativeSeparators(real_base_skin_folder)); |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | if (real_base_skin_folder.isEmpty()) { |
| 357 | // Base skin not found. |
| 358 | if (ok != nullptr) { |
| 359 | *ok = false; |
| 360 | } |
| 361 | |
| 362 | qCriticalNN << LOGSEC_GUI << "Base skin" << QUOTE_W_SPACE(base_skin_name) << "not found."; |
| 363 | return skin; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | // Obtain skin data. |
| 368 | skin.m_visibleName = skin_name; |
| 369 | skin.m_author = skin_node.namedItem(QSL("author")).namedItem(QSL("name")).toElement().text(); |
| 370 | skin.m_version = skin_node.attributes().namedItem(QSL("version")).toAttr().value(); |
| 371 | skin.m_description = skin_node.namedItem(QSL("description")).toElement().text(); |
| 372 | skin.m_baseName = skin_name; |
| 373 | |
| 374 | // Obtain color palette. |
| 375 | QHash<SkinEnums::PaletteColors, QColor> palette; |
| 376 | QDomNodeList colors_of_palette = skin_node.namedItem(QSL("palette")).toElement().elementsByTagName(QSL("color")); |
| 377 | const QMetaObject& mo = SkinEnums::staticMetaObject; |
| 378 | QMetaEnum enumer = mo.enumerator(mo.indexOfEnumerator(QSL("PaletteColors").toLocal8Bit().constData())); |
| 379 | |
| 380 | for (int i = 0; i < colors_of_palette.size(); i++) { |
| 381 | QDomElement elem_clr = colors_of_palette.item(i).toElement(); |
| 382 | QString en_val = elem_clr.attribute(QSL("key")); |
| 383 | SkinEnums::PaletteColors key = SkinEnums::PaletteColors(enumer.keyToValue(en_val.toLatin1())); |
| 384 | QColor value = elem_clr.text(); |
| 385 | |
| 386 | if (value.isValid()) { |
| 387 | palette.insert(key, value); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | skin.m_colorPalette = palette; |
| 392 | |
| 393 | // Obtain alternative style palette. |
| 394 | skin.m_forcedStyles = skin_node.namedItem(QSL("forced-styles")) |
| 395 | .toElement() |
| 396 | .text() |
| 397 | .split(',', |
| 398 | #if QT_VERSION >= 0x050F00 // Qt >= 5.15.0 |
| 399 | Qt::SplitBehaviorFlags::SkipEmptyParts); |
| 400 | #else |
| 401 | QString::SplitBehavior::SkipEmptyParts); |
| 402 | #endif |
| 403 | |
| 404 | skin.m_forcedSkinColors = |
| 405 | skin_node.namedItem(QSL("forced-skin-colors")).toElement().text() == QVariant(true).toString(); |
| 406 | |
| 407 | QDomElement style_palette_root = skin_node.namedItem(QSL("style-palette")).toElement(); |
| 408 | |
| 409 | if (!style_palette_root.isNull()) { |
| 410 | QMetaEnum enumerp = QMetaEnum::fromType<QPalette::ColorGroup>(); |
| 411 | QMetaEnum enumerx = QMetaEnum::fromType<QPalette::ColorRole>(); |
| 412 | QMetaEnum enumery = QMetaEnum::fromType<Qt::BrushStyle>(); |
| 413 | |
| 414 | QMultiHash<QPalette::ColorGroup, QPair<QPalette::ColorRole, QPair<QColor, Qt::BrushStyle>>> groups; |
| 415 | |
| 416 | QDomNodeList groups_of_palette = style_palette_root.elementsByTagName(QSL("group")); |
| 417 | |
| 418 | for (int i = 0; i < groups_of_palette.size(); i++) { |
| 419 | const QDomNode& group_root_nd = groups_of_palette.at(i); |
| 420 | QPalette::ColorGroup group = |
| 421 | QPalette::ColorGroup(enumerp.keyToValue(group_root_nd.toElement().attribute(QSL("id")).toLatin1())); |
| 422 | |
| 423 | QDomNodeList colors_of_group = group_root_nd.toElement().elementsByTagName(QSL("color")); |
| 424 | |
| 425 | for (int j = 0; j < colors_of_group.size(); j++) { |
| 426 | const QDomNode& color_nd = colors_of_group.at(j); |
| 427 | |
| 428 | QColor color(color_nd.toElement().text()); |
| 429 | QPalette::ColorRole role = |
| 430 | QPalette::ColorRole(enumerx.keyToValue(color_nd.toElement().attribute(QSL("role")).toLatin1())); |
| 431 | Qt::BrushStyle brush = |
| 432 | Qt::BrushStyle(enumery.keyToValue(color_nd.toElement().attribute(QSL("brush")).toLatin1())); |
| 433 | |
| 434 | groups.insert(group, QPair<QPalette::ColorRole, QPair<QColor, Qt::BrushStyle>>(role, {color, brush})); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | skin.m_stylePalette = groups; |
| 439 | } |
| 440 | |
| 441 | // Free resources. |
| 442 | skin_file.close(); |
| 443 | skin_file.deleteLater(); |
| 444 | |
| 445 | // Here we use "/" instead of QDir::separator() because CSS2.1 url field |
| 446 | // accepts '/' as path elements separator. |
| 447 | // |
| 448 | // USER_DATA_PLACEHOLDER is placeholder for the actual path to skin folder. This is needed for using |
| 449 | // images within the QSS file. |
| 450 | // So if one uses "%data%/images/border.png" in QSS then it is |
| 451 | // replaced by fully absolute path and target file can |
| 452 | // be safely loaded. |
| 453 | // |
| 454 | // %style% placeholder is used in main wrapper HTML file to be replaced with custom skin-wide CSS. |
| 455 | skin.m_layoutMarkupWrapper = |
| 456 | loadSkinFile(skin_folder_no_sep, lite, QSL("html_wrapper.html"), real_base_skin_folder); |
| 457 | |
| 458 | try { |
| 459 | auto custom_css = loadSkinFile(skin_folder_no_sep, lite, QSL("html_style.css"), real_base_skin_folder); |
| 460 | |
| 461 | skin.m_layoutMarkupWrapper = skin.m_layoutMarkupWrapper.replace(QSL(SKIN_STYLE_PLACEHOLDER), custom_css); |
| 462 | } |
| 463 | catch (...) { |
| 464 | qWarningNN << "Skin" << QUOTE_W_SPACE(skin_name) << "does not support separated custom CSS."; |
| 465 | } |
| 466 | |
| 467 | skin.m_enclosureImageMarkup = |
| 468 | loadSkinFile(skin_folder_no_sep, lite, QSL("html_enclosure_image.html"), real_base_skin_folder); |
| 469 | skin.m_layoutMarkup = |
| 470 | loadSkinFile(skin_folder_no_sep, lite, QSL("html_single_message.html"), real_base_skin_folder); |
| 471 | skin.m_enclosureMarkup = |
| 472 | loadSkinFile(skin_folder_no_sep, lite, QSL("html_enclosure_every.html"), real_base_skin_folder); |
| 473 | skin.m_rawData = loadSkinFile(skin_folder_no_sep, lite, QSL("qt_style.qss"), real_base_skin_folder); |
| 474 | |
| 475 | try { |
| 476 | skin.m_rawForcedData = |
| 477 | loadSkinFile(skin_folder_no_sep, lite, QSL("qt_style_forced.qss"), real_base_skin_folder); |
| 478 | } |
| 479 | catch (...) { |
| 480 | qWarningNN << "Skin" << QUOTE_W_SPACE(skin_name) << "does not support forced QSS."; |
| 481 | } |
| 482 | |
| 483 | skin.m_adblocked = loadSkinFile(skin_folder_no_sep, lite, QSL("html_adblocked.html"), real_base_skin_folder); |
| 484 | skin.m_skinFolder = skin_folder_no_sep; |
| 485 | |
| 486 | if (ok != nullptr) { |
| 487 | *ok = !skin.m_author.isEmpty() && !skin.m_version.isEmpty() && !skin.m_baseName.isEmpty() && |
| 488 | !skin.m_layoutMarkup.isEmpty(); |
| 489 | } |
| 490 | |
| 491 | return skin; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | if (ok != nullptr) { |
| 496 | *ok = false; |
| 497 | } |
| 498 | |
| 499 | return skin; |
| 500 | } |
| 501 | |
| 502 | QString SkinFactory::loadSkinFile(const QString& skin_folder, |
| 503 | bool lite, |
| 504 | const QString& file_name, |
| 505 | const QString& base_folder) const { |
| 506 | QStringList prefixes = {QString()}; |
| 507 | |
| 508 | if (lite) { |
| 509 | prefixes.prepend(QSL("lite_")); |
| 510 | } |
| 511 | |
| 512 | for (const QString& prefix : prefixes) { |
| 513 | QString local_file = QDir::toNativeSeparators(skin_folder + QDir::separator() + prefix + file_name); |
| 514 | QString base_file = QDir::toNativeSeparators(base_folder + QDir::separator() + prefix + file_name); |
| 515 | QString data; |
| 516 | |
| 517 | if (QFile::exists(local_file)) { |
| 518 | qDebugNN << LOGSEC_GUI << "Local file" << QUOTE_W_SPACE(local_file) << "exists, using it for the skin."; |
| 519 | data = QString::fromUtf8(IOFactory::readFile(local_file)); |
| 520 | return data.replace(QSL(USER_DATA_PLACEHOLDER), skin_folder); |
| 521 | } |
| 522 | else if (QFile::exists(base_file)) { |
| 523 | qDebugNN << LOGSEC_GUI << "Base file" << QUOTE_W_SPACE(base_file) << "exists, using it for the skin."; |
| 524 | data = QString::fromUtf8(IOFactory::readFile(base_file)); |
| 525 | return data.replace(QSL(USER_DATA_PLACEHOLDER), base_folder); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | throw ApplicationException(tr("file %1 not found").arg(file_name)); |
| 530 | } |
| 531 | |
| 532 | QString SkinFactory::currentStyle() const { |
| 533 | return m_currentStyle; |
| 534 | } |
| 535 | |
| 536 | bool SkinFactory::styleIsFrozen() const { |
| 537 | return m_styleIsFrozen; |
| 538 | } |
| 539 | |
| 540 | QList<Skin> SkinFactory::installedSkins() const { |
| 541 | QList<Skin> skins; |
| 542 | bool skin_load_ok; |
| 543 | QStringList skin_directories = |
| 544 | QDir(APP_SKIN_PATH).entryList(QDir::Filter::Dirs | QDir::Filter::NoDotAndDotDot | QDir::Filter::Readable); |
| 545 | |
| 546 | skin_directories.append(QDir(customSkinBaseFolder()) |
| 547 | .entryList(QDir::Filter::Dirs | QDir::Filter::NoDotAndDotDot | QDir::Filter::Readable)); |
| 548 | |
| 549 | for (const QString& base_directory : skin_directories) { |
| 550 | const Skin skin_info = skinInfo(base_directory, false, &skin_load_ok); |
| 551 | |
| 552 | if (skin_load_ok) { |
| 553 | skins.append(skin_info); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | return skins; |
| 558 | } |
| 559 | |
| 560 | uint qHash(const SkinEnums::PaletteColors& key) { |
| 561 | return uint(key); |
| 562 | } |
| 563 | |
| 564 | QVariant Skin::colorForModel(SkinEnums::PaletteColors type, bool use_skin_colors, bool ignore_custom_colors) const { |
| 565 | if (!ignore_custom_colors) { |
| 566 | bool enabled = qApp->settings()->value(GROUP(CustomSkinColors), SETTING(CustomSkinColors::Enabled)).toBool(); |
| 567 | |
| 568 | if (enabled) { |
| 569 | const QMetaObject& mo = SkinEnums::staticMetaObject; |
| 570 | QMetaEnum enumer = mo.enumerator(mo.indexOfEnumerator(QSL("PaletteColors").toLocal8Bit().constData())); |
| 571 | QColor custom_clr = qApp->settings()->value(GROUP(CustomSkinColors), enumer.valueToKey(int(type))).toString(); |
| 572 | |
| 573 | if (custom_clr.isValid()) { |
| 574 | return custom_clr; |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | return (use_skin_colors && m_colorPalette.contains(type)) ? m_colorPalette[type] : QVariant(); |
| 580 | } |
| 581 | |
| 582 | QPalette Skin::extractPalette() const { |
| 583 | QPalette pal; |
| 584 | QList<QPalette::ColorGroup> groups = m_stylePalette.keys(); |
| 585 | |
| 586 | if (groups.contains(QPalette::ColorGroup::All)) { |
| 587 | |
| 588 | groups.removeAll(QPalette::ColorGroup::All); |
| 589 | groups.insert(0, QPalette::ColorGroup::All); |
| 590 | } |
| 591 | |
| 592 | for (QPalette::ColorGroup grp : groups) { |
| 593 | auto roles = m_stylePalette.values(grp); |
| 594 | |
| 595 | for (const auto& rl : roles) { |
| 596 | if (rl.second.second <= 0) { |
| 597 | pal.setColor(grp, rl.first, rl.second.first); |
| 598 | } |
| 599 | else { |
| 600 | pal.setBrush(grp, rl.first, QBrush(rl.second.first, rl.second.second)); |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | return pal; |
| 606 | } |
| 607 | |
| 608 | QString SkinEnums::palleteColorText(PaletteColors col) { |
| 609 | switch (col) { |
| 610 | case SkinEnums::PaletteColors::FgInteresting: |
| 611 | return QObject::tr("interesting stuff"); |
| 612 | |
| 613 | case SkinEnums::PaletteColors::FgSelectedInteresting: |
| 614 | return QObject::tr("interesting stuff (highlighted)"); |
| 615 | |
| 616 | case SkinEnums::PaletteColors::FgError: |
| 617 | return QObject::tr("errored items"); |
| 618 | |
| 619 | case SkinEnums::PaletteColors::FgSelectedError: |
| 620 | return QObject::tr("errored items (highlighted)"); |
| 621 | |
| 622 | case SkinEnums::PaletteColors::FgNewMessages: |
| 623 | return QObject::tr("items with new articles"); |
| 624 | |
| 625 | case SkinEnums::PaletteColors::FgSelectedNewMessages: |
| 626 | return QObject::tr("items with new articles (highlighted)"); |
| 627 | |
| 628 | case SkinEnums::PaletteColors::Allright: |
| 629 | return QObject::tr("OK-ish color"); |
| 630 | |
| 631 | case SkinEnums::PaletteColors::FgDisabledFeed: |
| 632 | return QObject::tr("disabled items"); |
| 633 | |
| 634 | default: |
| 635 | return {}; |
| 636 | } |
| 637 | } |