theme.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. /*
  2. * Copyright (C) by Klaas Freitag <freitag@owncloud.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. */
  14. #include "theme.h"
  15. #include "config.h"
  16. #include "common/utility.h"
  17. #include "version.h"
  18. #include "configfile.h"
  19. #include "common/vfs.h"
  20. #include <QtCore>
  21. #ifndef TOKEN_AUTH_ONLY
  22. #include <QtGui>
  23. #include <QStyle>
  24. #include <QApplication>
  25. #endif
  26. #include <QSslSocket>
  27. #include <QSvgRenderer>
  28. #include "nextcloudtheme.h"
  29. #ifdef THEME_INCLUDE
  30. #define Mirall OCC // namespace hack to make old themes work
  31. #define QUOTEME(M) #M
  32. #define INCLUDE_FILE(M) QUOTEME(M)
  33. #include INCLUDE_FILE(THEME_INCLUDE)
  34. #undef Mirall
  35. #endif
  36. namespace {
  37. QUrl imagePathToUrl(const QString &imagePath)
  38. {
  39. if (imagePath.startsWith(':')) {
  40. auto url = QUrl();
  41. url.setScheme(QStringLiteral("qrc"));
  42. url.setPath(imagePath.mid(1));
  43. return url;
  44. } else {
  45. return QUrl::fromLocalFile(imagePath);
  46. }
  47. }
  48. bool shouldPreferSvg()
  49. {
  50. return QByteArray(APPLICATION_ICON_SET).toUpper() == QByteArrayLiteral("SVG");
  51. }
  52. }
  53. namespace OCC {
  54. Theme *Theme::_instance = nullptr;
  55. Theme *Theme::instance()
  56. {
  57. if (!_instance) {
  58. _instance = new THEME_CLASS;
  59. // some themes may not call the base ctor
  60. _instance->_mono = false;
  61. }
  62. return _instance;
  63. }
  64. Theme::~Theme() = default;
  65. QString Theme::statusHeaderText(SyncResult::Status status) const
  66. {
  67. QString resultStr;
  68. switch (status) {
  69. case SyncResult::Undefined:
  70. resultStr = QCoreApplication::translate("theme", "Status undefined");
  71. break;
  72. case SyncResult::NotYetStarted:
  73. resultStr = QCoreApplication::translate("theme", "Waiting to start sync");
  74. break;
  75. case SyncResult::SyncRunning:
  76. resultStr = QCoreApplication::translate("theme", "Sync is running");
  77. break;
  78. case SyncResult::Success:
  79. resultStr = QCoreApplication::translate("theme", "Sync Success");
  80. break;
  81. case SyncResult::Problem:
  82. resultStr = QCoreApplication::translate("theme", "Sync Success, some files were ignored.");
  83. break;
  84. case SyncResult::Error:
  85. resultStr = QCoreApplication::translate("theme", "Sync Error");
  86. break;
  87. case SyncResult::SetupError:
  88. resultStr = QCoreApplication::translate("theme", "Setup Error");
  89. break;
  90. case SyncResult::SyncPrepare:
  91. resultStr = QCoreApplication::translate("theme", "Preparing to sync");
  92. break;
  93. case SyncResult::SyncAbortRequested:
  94. resultStr = QCoreApplication::translate("theme", "Aborting …");
  95. break;
  96. case SyncResult::Paused:
  97. resultStr = QCoreApplication::translate("theme", "Sync is paused");
  98. break;
  99. }
  100. return resultStr;
  101. }
  102. bool Theme::isBranded() const
  103. {
  104. return appNameGUI() != QStringLiteral("Nextcloud");
  105. }
  106. QString Theme::appNameGUI() const
  107. {
  108. return APPLICATION_NAME;
  109. }
  110. QString Theme::appName() const
  111. {
  112. return APPLICATION_SHORTNAME;
  113. }
  114. QUrl Theme::stateOnlineImageSource() const
  115. {
  116. return imagePathToUrl(themeImagePath("state-ok"));
  117. }
  118. QUrl Theme::stateOfflineImageSource() const
  119. {
  120. return imagePathToUrl(themeImagePath("state-offline", 16));
  121. }
  122. QUrl Theme::statusOnlineImageSource() const
  123. {
  124. return imagePathToUrl(themeImagePath("user-status-online", 16));
  125. }
  126. QUrl Theme::statusDoNotDisturbImageSource() const
  127. {
  128. return imagePathToUrl(themeImagePath("user-status-dnd", 16));
  129. }
  130. QUrl Theme::statusAwayImageSource() const
  131. {
  132. return imagePathToUrl(themeImagePath("user-status-away", 16));
  133. }
  134. QUrl Theme::statusInvisibleImageSource() const
  135. {
  136. return imagePathToUrl(themeImagePath("user-status-invisible", 64));
  137. }
  138. QUrl Theme::syncStatusOk() const
  139. {
  140. return imagePathToUrl(themeImagePath("state-ok", 16));
  141. }
  142. QUrl Theme::syncStatusError() const
  143. {
  144. return imagePathToUrl(themeImagePath("state-error", 16));
  145. }
  146. QUrl Theme::syncStatusRunning() const
  147. {
  148. return imagePathToUrl(themeImagePath("state-sync", 16));
  149. }
  150. QUrl Theme::syncStatusPause() const
  151. {
  152. return imagePathToUrl(themeImagePath("state-pause", 16));
  153. }
  154. QUrl Theme::syncStatusWarning() const
  155. {
  156. return imagePathToUrl(themeImagePath("state-warning", 16));
  157. }
  158. QUrl Theme::folderOffline() const
  159. {
  160. return imagePathToUrl(themeImagePath("state-offline"));
  161. }
  162. QString Theme::version() const
  163. {
  164. return MIRALL_VERSION_STRING;
  165. }
  166. QString Theme::configFileName() const
  167. {
  168. return QStringLiteral(APPLICATION_CONFIG_NAME ".cfg");
  169. }
  170. #ifndef TOKEN_AUTH_ONLY
  171. QIcon Theme::applicationIcon() const
  172. {
  173. return themeIcon(QStringLiteral(APPLICATION_ICON_NAME "-icon"));
  174. }
  175. /*
  176. * helper to load a icon from either the icon theme the desktop provides or from
  177. * the apps Qt resources.
  178. */
  179. QIcon Theme::themeIcon(const QString &name, bool sysTray) const
  180. {
  181. QString flavor;
  182. if (sysTray) {
  183. flavor = systrayIconFlavor(_mono);
  184. } else {
  185. flavor = QLatin1String("colored");
  186. }
  187. QString key = name + "," + flavor;
  188. QIcon &cached = _iconCache[key];
  189. if (cached.isNull()) {
  190. if (QIcon::hasThemeIcon(name)) {
  191. // use from theme
  192. return cached = QIcon::fromTheme(name);
  193. }
  194. const QString svgName = QString(Theme::themePrefix) + QString::fromLatin1("%1/%2.svg").arg(flavor).arg(name);
  195. QSvgRenderer renderer(svgName);
  196. const auto createPixmapFromSvg = [&renderer] (int size) {
  197. QImage img(size, size, QImage::Format_ARGB32);
  198. img.fill(Qt::GlobalColor::transparent);
  199. QPainter imgPainter(&img);
  200. renderer.render(&imgPainter);
  201. return QPixmap::fromImage(img);
  202. };
  203. const auto loadPixmap = [flavor, name] (int size) {
  204. const QString pixmapName = QString(Theme::themePrefix) + QString::fromLatin1("%1/%2-%3.png").arg(flavor).arg(name).arg(size);
  205. return QPixmap(pixmapName);
  206. };
  207. const auto useSvg = shouldPreferSvg();
  208. const auto sizes = useSvg ? QVector<int>{ 16, 32, 64, 128, 256 }
  209. : QVector<int>{ 16, 22, 32, 48, 64, 128, 256, 512, 1024 };
  210. for (int size : sizes) {
  211. auto px = useSvg ? createPixmapFromSvg(size) : loadPixmap(size);
  212. if (px.isNull()) {
  213. continue;
  214. }
  215. // HACK, get rid of it by supporting FDO icon themes, this is really just emulating ubuntu-mono
  216. if (qgetenv("DESKTOP_SESSION") == "ubuntu") {
  217. QBitmap mask = px.createMaskFromColor(Qt::white, Qt::MaskOutColor);
  218. QPainter p(&px);
  219. p.setPen(QColor("#dfdbd2"));
  220. p.drawPixmap(px.rect(), mask, mask.rect());
  221. }
  222. cached.addPixmap(px);
  223. }
  224. }
  225. #ifdef Q_OS_MAC
  226. #if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
  227. // This defines the icon as a template and enables automatic macOS color handling
  228. // See https://bugreports.qt.io/browse/QTBUG-42109
  229. cached.setIsMask(_mono && sysTray);
  230. #endif
  231. #endif
  232. return cached;
  233. }
  234. QString Theme::themeImagePath(const QString &name, int size, bool sysTray) const
  235. {
  236. const auto flavor = (!isBranded() && sysTray) ? systrayIconFlavor(_mono) : QLatin1String("colored");
  237. const auto useSvg = shouldPreferSvg();
  238. // branded client may have several sizes of the same icon
  239. const QString filePath = (useSvg || size <= 0)
  240. ? QString(Theme::themePrefix) + QString::fromLatin1("%1/%2").arg(flavor).arg(name)
  241. : QString(Theme::themePrefix) + QString::fromLatin1("%1/%2-%3").arg(flavor).arg(name).arg(size);
  242. const QString svgPath = filePath + ".svg";
  243. if (useSvg) {
  244. return svgPath;
  245. }
  246. const QString pngPath = filePath + ".png";
  247. // Use the SVG as fallback if a PNG is missing so that we get a chance to display something
  248. if (QFile::exists(pngPath)) {
  249. return pngPath;
  250. } else {
  251. return svgPath;
  252. }
  253. }
  254. bool Theme::isHidpi(QPaintDevice *dev)
  255. {
  256. const auto devicePixelRatio = dev ? dev->devicePixelRatio() : qApp->primaryScreen()->devicePixelRatio();
  257. return devicePixelRatio > 1;
  258. }
  259. QIcon Theme::uiThemeIcon(const QString &iconName, bool uiHasDarkBg) const
  260. {
  261. QString iconPath = QString(Theme::themePrefix) + (uiHasDarkBg ? "white/" : "black/") + iconName;
  262. std::string icnPath = iconPath.toUtf8().constData();
  263. return QIcon(QPixmap(iconPath));
  264. }
  265. QString Theme::hidpiFileName(const QString &fileName, QPaintDevice *dev)
  266. {
  267. if (!Theme::isHidpi(dev)) {
  268. return fileName;
  269. }
  270. // try to find a 2x version
  271. const int dotIndex = fileName.lastIndexOf(QLatin1Char('.'));
  272. if (dotIndex != -1) {
  273. QString at2xfileName = fileName;
  274. at2xfileName.insert(dotIndex, QStringLiteral("@2x"));
  275. if (QFile::exists(at2xfileName)) {
  276. return at2xfileName;
  277. }
  278. }
  279. return fileName;
  280. }
  281. QString Theme::hidpiFileName(const QString &iconName, const QColor &backgroundColor, QPaintDevice *dev)
  282. {
  283. const auto isDarkBackground = Theme::isDarkColor(backgroundColor);
  284. const QString iconPath = QString(Theme::themePrefix) + (isDarkBackground ? "white/" : "black/") + iconName;
  285. return Theme::hidpiFileName(iconPath, dev);
  286. }
  287. #endif
  288. Theme::Theme()
  289. : QObject(nullptr)
  290. {
  291. #if defined(Q_OS_WIN)
  292. // Windows does not provide a dark theme for Win32 apps so let's come up with a palette
  293. // Credit to https://github.com/Jorgen-VikingGod/Qt-Frameless-Window-DarkStyle
  294. reserveDarkPalette.setColor(QPalette::Window, QColor(53, 53, 53));
  295. reserveDarkPalette.setColor(QPalette::WindowText, Qt::white);
  296. reserveDarkPalette.setColor(QPalette::Disabled, QPalette::WindowText,
  297. QColor(127, 127, 127));
  298. reserveDarkPalette.setColor(QPalette::Base, QColor(42, 42, 42));
  299. reserveDarkPalette.setColor(QPalette::AlternateBase, QColor(66, 66, 66));
  300. reserveDarkPalette.setColor(QPalette::ToolTipBase, Qt::white);
  301. reserveDarkPalette.setColor(QPalette::ToolTipText, QColor(53, 53, 53));
  302. reserveDarkPalette.setColor(QPalette::Text, Qt::white);
  303. reserveDarkPalette.setColor(QPalette::Disabled, QPalette::Text, QColor(127, 127, 127));
  304. reserveDarkPalette.setColor(QPalette::Dark, QColor(35, 35, 35));
  305. reserveDarkPalette.setColor(QPalette::Shadow, QColor(20, 20, 20));
  306. reserveDarkPalette.setColor(QPalette::Button, QColor(53, 53, 53));
  307. reserveDarkPalette.setColor(QPalette::ButtonText, Qt::white);
  308. reserveDarkPalette.setColor(QPalette::Disabled, QPalette::ButtonText,
  309. QColor(127, 127, 127));
  310. reserveDarkPalette.setColor(QPalette::BrightText, Qt::red);
  311. reserveDarkPalette.setColor(QPalette::Link, QColor(42, 130, 218));
  312. reserveDarkPalette.setColor(QPalette::Highlight, QColor(42, 130, 218));
  313. reserveDarkPalette.setColor(QPalette::Disabled, QPalette::Highlight, QColor(80, 80, 80));
  314. reserveDarkPalette.setColor(QPalette::HighlightedText, Qt::white);
  315. reserveDarkPalette.setColor(QPalette::Disabled, QPalette::HighlightedText,
  316. QColor(127, 127, 127));
  317. #endif
  318. #ifdef APPLICATION_SERVER_URL_ENFORCE
  319. _forceOverrideServerUrl = true;
  320. #endif
  321. #ifdef APPLICATION_SERVER_URL
  322. _overrideServerUrl = QString::fromLatin1(APPLICATION_SERVER_URL);
  323. #endif
  324. }
  325. // If this option returns true, the client only supports one folder to sync.
  326. // The Add-Button is removed accordingly.
  327. bool Theme::singleSyncFolder() const
  328. {
  329. return false;
  330. }
  331. bool Theme::multiAccount() const
  332. {
  333. return true;
  334. }
  335. QString Theme::defaultServerFolder() const
  336. {
  337. return QLatin1String("/");
  338. }
  339. QString Theme::helpUrl() const
  340. {
  341. #ifdef APPLICATION_HELP_URL
  342. return QString::fromLatin1(APPLICATION_HELP_URL);
  343. #else
  344. return QString::fromLatin1("https://docs.nextcloud.com/desktop/%1.%2/").arg(MIRALL_VERSION_MAJOR).arg(MIRALL_VERSION_MINOR);
  345. #endif
  346. }
  347. QString Theme::conflictHelpUrl() const
  348. {
  349. auto baseUrl = helpUrl();
  350. if (baseUrl.isEmpty())
  351. return QString();
  352. if (!baseUrl.endsWith('/'))
  353. baseUrl.append('/');
  354. return baseUrl + QStringLiteral("conflicts.html");
  355. }
  356. QString Theme::overrideServerUrl() const
  357. {
  358. return _overrideServerUrl;
  359. }
  360. bool Theme::forceOverrideServerUrl() const
  361. {
  362. return _forceOverrideServerUrl;
  363. }
  364. bool Theme::startLoginFlowAutomatically() const
  365. {
  366. return _startLoginFlowAutomatically;
  367. }
  368. bool Theme::enableStaplingOCSP() const
  369. {
  370. #ifdef APPLICATION_OCSP_STAPLING_ENABLED
  371. return true;
  372. #else
  373. return false;
  374. #endif
  375. }
  376. bool Theme::forbidBadSSL() const
  377. {
  378. #ifdef APPLICATION_FORBID_BAD_SSL
  379. return true;
  380. #else
  381. return false;
  382. #endif
  383. }
  384. bool Theme::doNotUseProxy() const
  385. {
  386. #ifdef DO_NOT_USE_PROXY
  387. return true;
  388. #else
  389. return false;
  390. #endif
  391. }
  392. QString Theme::forceConfigAuthType() const
  393. {
  394. return QString();
  395. }
  396. QString Theme::defaultClientFolder() const
  397. {
  398. return appName();
  399. }
  400. QString Theme::systrayIconFlavor(bool mono) const
  401. {
  402. QString flavor;
  403. if (mono) {
  404. flavor = Utility::hasDarkSystray() ? QLatin1String("white") : QLatin1String("black");
  405. } else {
  406. flavor = QLatin1String("colored");
  407. }
  408. return flavor;
  409. }
  410. QString Theme::updateCheckUrl() const
  411. {
  412. return APPLICATION_UPDATE_URL;
  413. }
  414. qint64 Theme::newBigFolderSizeLimit() const
  415. {
  416. // Default to 500MB
  417. return 500;
  418. }
  419. bool Theme::wizardHideExternalStorageConfirmationCheckbox() const
  420. {
  421. return false;
  422. }
  423. bool Theme::wizardHideFolderSizeLimitCheckbox() const
  424. {
  425. return false;
  426. }
  427. QString Theme::gitSHA1() const
  428. {
  429. QString devString;
  430. #ifdef GIT_SHA1
  431. const QString githubPrefix(QLatin1String(
  432. "https://github.com/nextcloud/desktop/commit/"));
  433. const QString gitSha1(QLatin1String(GIT_SHA1));
  434. devString = QCoreApplication::translate("nextcloudTheme::about()",
  435. "<p><small>Built from Git revision <a href=\"%1\">%2</a>"
  436. " on %3, %4 using Qt %5, %6</small></p>")
  437. .arg(githubPrefix + gitSha1)
  438. .arg(gitSha1.left(6))
  439. .arg(__DATE__)
  440. .arg(__TIME__)
  441. .arg(qVersion())
  442. .arg(QSslSocket::sslLibraryVersionString());
  443. #endif
  444. return devString;
  445. }
  446. QString Theme::about() const
  447. {
  448. // Shorten Qt's OS name: "macOS Mojave (10.14)" -> "macOS"
  449. QStringList osStringList = Utility::platformName().split(QLatin1Char(' '));
  450. QString osName = osStringList.at(0);
  451. QString devString;
  452. //: Example text: "<p>Nextcloud Desktop Client</p>" (%1 is the application name)
  453. devString = tr("<p>%1 Desktop Client</p>")
  454. .arg(APPLICATION_NAME);
  455. devString += tr("<p>Version %1. For more information please click <a href='%2'>here</a>.</p>")
  456. .arg(QString::fromLatin1(MIRALL_STRINGIFY(MIRALL_VERSION)) + QString(" (%1)").arg(osName))
  457. .arg(helpUrl());
  458. devString += tr("<p><small>Using virtual files plugin: %1</small></p>")
  459. .arg(Vfs::modeToString(bestAvailableVfsMode()));
  460. devString += QStringLiteral("<br>%1")
  461. .arg(QSysInfo::productType() % QLatin1Char('-') % QSysInfo::kernelVersion());
  462. return devString;
  463. }
  464. QString Theme::aboutDetails() const
  465. {
  466. QString devString;
  467. devString = tr("<p>Version %1. For more information please click <a href='%2'>here</a>.</p>")
  468. .arg(MIRALL_VERSION_STRING)
  469. .arg(helpUrl());
  470. devString += tr("<p>This release was supplied by %1</p>")
  471. .arg(APPLICATION_VENDOR);
  472. devString += gitSHA1();
  473. return devString;
  474. }
  475. #ifndef TOKEN_AUTH_ONLY
  476. QVariant Theme::customMedia(CustomMediaType type)
  477. {
  478. QVariant re;
  479. QString key;
  480. switch (type) {
  481. case oCSetupTop:
  482. key = QLatin1String("oCSetupTop");
  483. break;
  484. case oCSetupSide:
  485. key = QLatin1String("oCSetupSide");
  486. break;
  487. case oCSetupBottom:
  488. key = QLatin1String("oCSetupBottom");
  489. break;
  490. case oCSetupResultTop:
  491. key = QLatin1String("oCSetupResultTop");
  492. break;
  493. }
  494. QString imgPath = QString(Theme::themePrefix) + QString::fromLatin1("colored/%1.png").arg(key);
  495. if (QFile::exists(imgPath)) {
  496. QPixmap pix(imgPath);
  497. if (pix.isNull()) {
  498. // pixmap loading hasn't succeeded. We take the text instead.
  499. re.setValue(key);
  500. } else {
  501. re.setValue(pix);
  502. }
  503. }
  504. return re;
  505. }
  506. QIcon Theme::syncStateIcon(SyncResult::Status status, bool sysTray) const
  507. {
  508. // FIXME: Mind the size!
  509. QString statusIcon;
  510. switch (status) {
  511. case SyncResult::Undefined:
  512. // this can happen if no sync connections are configured.
  513. statusIcon = QLatin1String("state-warning");
  514. break;
  515. case SyncResult::NotYetStarted:
  516. case SyncResult::SyncRunning:
  517. statusIcon = QLatin1String("state-sync");
  518. break;
  519. case SyncResult::SyncAbortRequested:
  520. case SyncResult::Paused:
  521. statusIcon = QLatin1String("state-pause");
  522. break;
  523. case SyncResult::SyncPrepare:
  524. case SyncResult::Success:
  525. statusIcon = QLatin1String("state-ok");
  526. break;
  527. case SyncResult::Problem:
  528. statusIcon = QLatin1String("state-warning");
  529. break;
  530. case SyncResult::Error:
  531. case SyncResult::SetupError:
  532. // FIXME: Use state-problem once we have an icon.
  533. default:
  534. statusIcon = QLatin1String("state-error");
  535. }
  536. return themeIcon(statusIcon, sysTray);
  537. }
  538. QIcon Theme::folderDisabledIcon() const
  539. {
  540. return themeIcon(QLatin1String("state-pause"));
  541. }
  542. QIcon Theme::folderOfflineIcon(bool sysTray) const
  543. {
  544. return themeIcon(QLatin1String("state-offline"), sysTray);
  545. }
  546. QColor Theme::wizardHeaderTitleColor() const
  547. {
  548. return {APPLICATION_WIZARD_HEADER_TITLE_COLOR};
  549. }
  550. QColor Theme::wizardHeaderBackgroundColor() const
  551. {
  552. return {APPLICATION_WIZARD_HEADER_BACKGROUND_COLOR};
  553. }
  554. QPixmap Theme::wizardApplicationLogo() const
  555. {
  556. if (!Theme::isBranded()) {
  557. return QPixmap(Theme::hidpiFileName(QString(Theme::themePrefix) + "colored/wizard-nextcloud.png"));
  558. }
  559. #ifdef APPLICATION_WIZARD_USE_CUSTOM_LOGO
  560. const auto useSvg = shouldPreferSvg();
  561. const QString logoBasePath = QString(Theme::themePrefix) + QStringLiteral("colored/wizard_logo");
  562. if (useSvg) {
  563. const auto maxHeight = Theme::isHidpi() ? 200 : 100;
  564. const auto maxWidth = 2 * maxHeight;
  565. const auto icon = QIcon(logoBasePath + ".svg");
  566. const auto size = icon.actualSize(QSize(maxWidth, maxHeight));
  567. return icon.pixmap(size);
  568. } else {
  569. return QPixmap(hidpiFileName(logoBasePath + ".png"));
  570. }
  571. #else
  572. const auto size = Theme::isHidpi() ?: 200 : 100;
  573. return applicationIcon().pixmap(size);
  574. #endif
  575. }
  576. QPixmap Theme::wizardHeaderLogo() const
  577. {
  578. #ifdef APPLICATION_WIZARD_USE_CUSTOM_LOGO
  579. const auto useSvg = shouldPreferSvg();
  580. const QString logoBasePath = QString(Theme::themePrefix) + QStringLiteral("colored/wizard_logo");
  581. if (useSvg) {
  582. const auto maxHeight = 64;
  583. const auto maxWidth = 2 * maxHeight;
  584. const auto icon = QIcon(logoBasePath + ".svg");
  585. const auto size = icon.actualSize(QSize(maxWidth, maxHeight));
  586. return icon.pixmap(size);
  587. } else {
  588. return QPixmap(hidpiFileName(logoBasePath + ".png"));
  589. }
  590. #else
  591. return applicationIcon().pixmap(64);
  592. #endif
  593. }
  594. QPixmap Theme::wizardHeaderBanner() const
  595. {
  596. QColor c = wizardHeaderBackgroundColor();
  597. if (!c.isValid())
  598. return QPixmap();
  599. QSize size(750, 78);
  600. if (auto screen = qApp->primaryScreen()) {
  601. // Adjust the the size if there is a different DPI. (Issue #6156)
  602. // Indeed, this size need to be big enough to for the banner height, and the wizard's width
  603. auto ratio = screen->logicalDotsPerInch() / 96.;
  604. if (ratio > 1.)
  605. size *= ratio;
  606. }
  607. QPixmap pix(size);
  608. pix.fill(wizardHeaderBackgroundColor());
  609. return pix;
  610. }
  611. #endif
  612. bool Theme::wizardSelectiveSyncDefaultNothing() const
  613. {
  614. return false;
  615. }
  616. bool Theme::linkSharing() const
  617. {
  618. return true;
  619. }
  620. bool Theme::userGroupSharing() const
  621. {
  622. return true;
  623. }
  624. bool Theme::forceSystemNetworkProxy() const
  625. {
  626. return false;
  627. }
  628. Theme::UserIDType Theme::userIDType() const
  629. {
  630. return UserIDType::UserIDUserName;
  631. }
  632. QString Theme::customUserID() const
  633. {
  634. return QString();
  635. }
  636. QString Theme::userIDHint() const
  637. {
  638. return QString();
  639. }
  640. QString Theme::wizardUrlPostfix() const
  641. {
  642. return QString();
  643. }
  644. QString Theme::wizardUrlHint() const
  645. {
  646. return QString();
  647. }
  648. QString Theme::quotaBaseFolder() const
  649. {
  650. return QLatin1String("/");
  651. }
  652. QString Theme::oauthClientId() const
  653. {
  654. return "xdXOt13JKxym1B1QcEncf2XDkLAexMBFwiT9j6EfhhHFJhs2KM9jbjTmf8JBXE69";
  655. }
  656. QString Theme::oauthClientSecret() const
  657. {
  658. return "UBntmLjC2yYCeHwsyj73Uwo9TAaecAetRwMw0xYcvNL9yRdLSUi0hUAHfvCHFeFh";
  659. }
  660. QString Theme::versionSwitchOutput() const
  661. {
  662. QString helpText;
  663. QTextStream stream(&helpText);
  664. stream << appName()
  665. << QLatin1String(" version ")
  666. << version() << Qt::endl;
  667. #ifdef GIT_SHA1
  668. stream << "Git revision " << GIT_SHA1 << Qt::endl;
  669. #endif
  670. stream << "Using Qt " << qVersion() << ", built against Qt " << QT_VERSION_STR << Qt::endl;
  671. if(!QGuiApplication::platformName().isEmpty())
  672. stream << "Using Qt platform plugin '" << QGuiApplication::platformName() << "'" << Qt::endl;
  673. stream << "Using '" << QSslSocket::sslLibraryVersionString() << "'" << Qt::endl;
  674. stream << "Running on " << Utility::platformName() << ", " << QSysInfo::currentCpuArchitecture() << Qt::endl;
  675. return helpText;
  676. }
  677. double Theme::getColorDarkness(const QColor &color)
  678. {
  679. // account for different sensitivity of the human eye to certain colors
  680. const double threshold = 1.0 - (0.299 * color.red() + 0.587 * color.green() + 0.114 * color.blue()) / 255.0;
  681. return threshold;
  682. }
  683. bool Theme::isDarkColor(const QColor &color)
  684. {
  685. return getColorDarkness(color) > 0.5;
  686. }
  687. QColor Theme::getBackgroundAwareLinkColor(const QColor &backgroundColor)
  688. {
  689. return {(isDarkColor(backgroundColor) ? QColor("#6193dc") : QGuiApplication::palette().color(QPalette::Link))};
  690. }
  691. QColor Theme::getBackgroundAwareLinkColor()
  692. {
  693. return getBackgroundAwareLinkColor(QGuiApplication::palette().base().color());
  694. }
  695. void Theme::replaceLinkColorStringBackgroundAware(QString &linkString, const QColor &backgroundColor)
  696. {
  697. replaceLinkColorString(linkString, getBackgroundAwareLinkColor(backgroundColor));
  698. }
  699. void Theme::replaceLinkColorStringBackgroundAware(QString &linkString)
  700. {
  701. replaceLinkColorStringBackgroundAware(linkString, QGuiApplication::palette().color(QPalette::Base));
  702. }
  703. void Theme::replaceLinkColorString(QString &linkString, const QColor &newColor)
  704. {
  705. static const QRegularExpression linkRegularExpression("(<a href|<a style='color:#([a-zA-Z0-9]{6});' href)");
  706. linkString.replace(linkRegularExpression, QString::fromLatin1("<a style='color:%1;' href").arg(newColor.name()));
  707. }
  708. QIcon Theme::createColorAwareIcon(const QString &name, const QPalette &palette)
  709. {
  710. QSvgRenderer renderer(name);
  711. QImage img(64, 64, QImage::Format_ARGB32);
  712. img.fill(Qt::GlobalColor::transparent);
  713. QPainter imgPainter(&img);
  714. QImage inverted(64, 64, QImage::Format_ARGB32);
  715. inverted.fill(Qt::GlobalColor::transparent);
  716. QPainter invPainter(&inverted);
  717. renderer.render(&imgPainter);
  718. renderer.render(&invPainter);
  719. inverted.invertPixels(QImage::InvertRgb);
  720. QIcon icon;
  721. if (Theme::isDarkColor(palette.color(QPalette::Base))) {
  722. icon.addPixmap(QPixmap::fromImage(inverted));
  723. } else {
  724. icon.addPixmap(QPixmap::fromImage(img));
  725. }
  726. if (Theme::isDarkColor(palette.color(QPalette::HighlightedText))) {
  727. icon.addPixmap(QPixmap::fromImage(img), QIcon::Normal, QIcon::On);
  728. } else {
  729. icon.addPixmap(QPixmap::fromImage(inverted), QIcon::Normal, QIcon::On);
  730. }
  731. return icon;
  732. }
  733. QIcon Theme::createColorAwareIcon(const QString &name)
  734. {
  735. return createColorAwareIcon(name, QGuiApplication::palette());
  736. }
  737. QPixmap Theme::createColorAwarePixmap(const QString &name, const QPalette &palette)
  738. {
  739. QImage img(name);
  740. QImage inverted(img);
  741. inverted.invertPixels(QImage::InvertRgb);
  742. QPixmap pixmap;
  743. if (Theme::isDarkColor(palette.color(QPalette::Base))) {
  744. pixmap = QPixmap::fromImage(inverted);
  745. } else {
  746. pixmap = QPixmap::fromImage(img);
  747. }
  748. return pixmap;
  749. }
  750. QPixmap Theme::createColorAwarePixmap(const QString &name)
  751. {
  752. return createColorAwarePixmap(name, QGuiApplication::palette());
  753. }
  754. bool Theme::showVirtualFilesOption() const
  755. {
  756. const auto vfsMode = bestAvailableVfsMode();
  757. return ConfigFile().showExperimentalOptions() || vfsMode == Vfs::WindowsCfApi;
  758. }
  759. bool Theme::enforceVirtualFilesSyncFolder() const
  760. {
  761. const auto vfsMode = bestAvailableVfsMode();
  762. return ENFORCE_VIRTUAL_FILES_SYNC_FOLDER && vfsMode != OCC::Vfs::Off;
  763. }
  764. QColor Theme::defaultColor()
  765. {
  766. return QColor{NEXTCLOUD_BACKGROUND_COLOR};
  767. }
  768. void Theme::connectToPaletteSignal()
  769. {
  770. if (!_paletteSignalsConnected) {
  771. if (const auto ptr = qobject_cast<QGuiApplication *>(QGuiApplication::instance())) {
  772. connect(ptr, &QGuiApplication::paletteChanged, this, &Theme::systemPaletteChanged);
  773. connect(ptr, &QGuiApplication::paletteChanged, this, &Theme::darkModeChanged);
  774. _paletteSignalsConnected = true;
  775. }
  776. }
  777. }
  778. QPalette Theme::systemPalette()
  779. {
  780. connectToPaletteSignal();
  781. #if defined(Q_OS_WIN)
  782. if(darkMode()) {
  783. return reserveDarkPalette;
  784. }
  785. #endif
  786. return QGuiApplication::palette();
  787. }
  788. bool Theme::darkMode()
  789. {
  790. connectToPaletteSignal();
  791. // Windows: Check registry for dark mode
  792. #if defined(Q_OS_WIN)
  793. const auto darkModeSubkey = QStringLiteral("Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize");
  794. if (Utility::registryKeyExists(HKEY_CURRENT_USER, darkModeSubkey) &&
  795. !Utility::registryGetKeyValue(HKEY_CURRENT_USER, darkModeSubkey, QStringLiteral("AppsUseLightTheme")).toBool()) {
  796. return true;
  797. }
  798. return false;
  799. #else
  800. return Theme::isDarkColor(QGuiApplication::palette().window().color());
  801. #endif
  802. }
  803. void Theme::setOverrideServerUrl(const QString &overrideServerUrl)
  804. {
  805. if (_overrideServerUrl != overrideServerUrl) {
  806. _overrideServerUrl = overrideServerUrl;
  807. emit overrideServerUrlChanged();
  808. }
  809. }
  810. void Theme::setForceOverrideServerUrl(bool forceOverride)
  811. {
  812. if (_forceOverrideServerUrl != forceOverride) {
  813. _forceOverrideServerUrl = forceOverride;
  814. emit forceOverrideServerUrlChanged();
  815. }
  816. }
  817. void Theme::setStartLoginFlowAutomatically(bool startLoginFlowAuto)
  818. {
  819. if (_startLoginFlowAutomatically != startLoginFlowAuto) {
  820. _startLoginFlowAutomatically = startLoginFlowAuto;
  821. emit startLoginFlowAutomaticallyChanged();
  822. }
  823. }
  824. } // end namespace client