settingsdialog.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * Copyright (C) by Daniel Molkentin <danimo@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 "settingsdialog.h"
  15. #include "ui_settingsdialog.h"
  16. #include "folderman.h"
  17. #include "theme.h"
  18. #include "generalsettings.h"
  19. #include "networksettings.h"
  20. #include "accountsettings.h"
  21. #include "configfile.h"
  22. #include "progressdispatcher.h"
  23. #include "owncloudgui.h"
  24. #include "accountmanager.h"
  25. #include <QLabel>
  26. #include <QStandardItemModel>
  27. #include <QStackedWidget>
  28. #include <QPushButton>
  29. #include <QSettings>
  30. #include <QToolBar>
  31. #include <QToolButton>
  32. #include <QLayout>
  33. #include <QVBoxLayout>
  34. #include <QPixmap>
  35. #include <QImage>
  36. #include <QWidgetAction>
  37. #include <QPainter>
  38. #include <QPainterPath>
  39. namespace {
  40. const QString TOOLBAR_CSS()
  41. {
  42. return QStringLiteral("QToolBar { background: %1; margin: 0; padding: 0; border: none; border-bottom: 1px solid %2; spacing: 0; } "
  43. "QToolBar QToolButton { background: %1; border: none; border-bottom: 1px solid %2; margin: 0; padding: 5px; } "
  44. "QToolBar QToolBarExtension { padding:0; } "
  45. "QToolBar QToolButton:checked { background: %3; color: %4; }");
  46. }
  47. const float buttonSizeRatio = 1.618f; // golden ratio
  48. /** display name with two lines that is displayed in the settings
  49. * If width is bigger than 0, the string will be ellided so it does not exceed that width
  50. */
  51. QString shortDisplayNameForSettings(OCC::Account *account, int width)
  52. {
  53. QString user = account->prettyName();
  54. QString host = account->url().host();
  55. int port = account->url().port();
  56. if (port > 0 && port != 80 && port != 443) {
  57. host.append(QLatin1Char(':'));
  58. host.append(QString::number(port));
  59. }
  60. if (width > 0) {
  61. QFont f;
  62. QFontMetrics fm(f);
  63. host = fm.elidedText(host, Qt::ElideMiddle, width);
  64. user = fm.elidedText(user, Qt::ElideRight, width);
  65. }
  66. return QStringLiteral("%1\n%2").arg(user, host);
  67. }
  68. }
  69. namespace OCC {
  70. SettingsDialog::SettingsDialog(ownCloudGui *gui, QWidget *parent)
  71. : QDialog(parent)
  72. , _ui(new Ui::SettingsDialog)
  73. , _gui(gui)
  74. {
  75. ConfigFile cfg;
  76. _ui->setupUi(this);
  77. _toolBar = new QToolBar;
  78. _toolBar->setIconSize(QSize(32, 32));
  79. _toolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
  80. layout()->setMenuBar(_toolBar);
  81. // People perceive this as a Window, so also make Ctrl+W work
  82. auto *closeWindowAction = new QAction(this);
  83. closeWindowAction->setShortcut(QKeySequence("Ctrl+W"));
  84. connect(closeWindowAction, &QAction::triggered, this, &SettingsDialog::accept);
  85. addAction(closeWindowAction);
  86. setObjectName("Settings"); // required as group for saveGeometry call
  87. //: This name refers to the application name e.g Nextcloud
  88. setWindowTitle(tr("%1 Settings").arg(Theme::instance()->appNameGUI()));
  89. connect(AccountManager::instance(), &AccountManager::accountAdded,
  90. this, &SettingsDialog::accountAdded);
  91. connect(AccountManager::instance(), &AccountManager::accountRemoved,
  92. this, &SettingsDialog::accountRemoved);
  93. _actionGroup = new QActionGroup(this);
  94. _actionGroup->setExclusive(true);
  95. connect(_actionGroup, &QActionGroup::triggered, this, &SettingsDialog::slotSwitchPage);
  96. // Adds space between users + activities and general + network actions
  97. auto *spacer = new QWidget();
  98. spacer->setMinimumWidth(10);
  99. spacer->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
  100. _toolBar->addWidget(spacer);
  101. QAction *generalAction = createColorAwareAction(QLatin1String(":/client/theme/settings.svg"), tr("General"));
  102. _actionGroup->addAction(generalAction);
  103. _toolBar->addAction(generalAction);
  104. auto *generalSettings = new GeneralSettings;
  105. _ui->stack->addWidget(generalSettings);
  106. // Connect styleChanged events to our widgets, so they can adapt (Dark-/Light-Mode switching)
  107. connect(this, &SettingsDialog::styleChanged, generalSettings, &GeneralSettings::slotStyleChanged);
  108. QAction *networkAction = createColorAwareAction(QLatin1String(":/client/theme/network.svg"), tr("Network"));
  109. _actionGroup->addAction(networkAction);
  110. _toolBar->addAction(networkAction);
  111. auto *networkSettings = new NetworkSettings;
  112. _ui->stack->addWidget(networkSettings);
  113. connect(_ui->stack, &QStackedWidget::currentChanged, this, &SettingsDialog::currentPageChanged);
  114. _actionGroupWidgets.insert(generalAction, generalSettings);
  115. _actionGroupWidgets.insert(networkAction, networkSettings);
  116. foreach(auto ai, AccountManager::instance()->accounts()) {
  117. accountAdded(ai.data());
  118. }
  119. QTimer::singleShot(1, this, &SettingsDialog::showFirstPage);
  120. auto *showLogWindow = new QAction(this);
  121. showLogWindow->setShortcut(QKeySequence("F12"));
  122. connect(showLogWindow, &QAction::triggered, gui, &ownCloudGui::slotToggleLogBrowser);
  123. addAction(showLogWindow);
  124. auto *showLogWindow2 = new QAction(this);
  125. showLogWindow2->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_L));
  126. connect(showLogWindow2, &QAction::triggered, gui, &ownCloudGui::slotToggleLogBrowser);
  127. addAction(showLogWindow2);
  128. connect(this, &SettingsDialog::onActivate, gui, &ownCloudGui::slotSettingsDialogActivated);
  129. customizeStyle();
  130. setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  131. cfg.restoreGeometry(this);
  132. }
  133. SettingsDialog::~SettingsDialog()
  134. {
  135. delete _ui;
  136. }
  137. QWidget* SettingsDialog::currentPage()
  138. {
  139. return _ui->stack->currentWidget();
  140. }
  141. // close event is not being called here
  142. void SettingsDialog::reject()
  143. {
  144. ConfigFile cfg;
  145. cfg.saveGeometry(this);
  146. QDialog::reject();
  147. }
  148. void SettingsDialog::accept()
  149. {
  150. ConfigFile cfg;
  151. cfg.saveGeometry(this);
  152. QDialog::accept();
  153. }
  154. void SettingsDialog::changeEvent(QEvent *e)
  155. {
  156. switch (e->type()) {
  157. case QEvent::StyleChange:
  158. case QEvent::PaletteChange:
  159. case QEvent::ThemeChange:
  160. customizeStyle();
  161. // Notify the other widgets (Dark-/Light-Mode switching)
  162. emit styleChanged();
  163. break;
  164. case QEvent::ActivationChange:
  165. if(isActiveWindow())
  166. emit onActivate();
  167. break;
  168. default:
  169. break;
  170. }
  171. QDialog::changeEvent(e);
  172. }
  173. void SettingsDialog::slotSwitchPage(QAction *action)
  174. {
  175. _ui->stack->setCurrentWidget(_actionGroupWidgets.value(action));
  176. }
  177. void SettingsDialog::showFirstPage()
  178. {
  179. QList<QAction *> actions = _toolBar->actions();
  180. if (!actions.empty()) {
  181. actions.first()->trigger();
  182. }
  183. }
  184. void SettingsDialog::showIssuesList(AccountState *account)
  185. {
  186. const auto userModel = UserModel::instance();
  187. const auto id = userModel->findUserIdForAccount(account);
  188. UserModel::instance()->setCurrentUserId(id);
  189. Systray::instance()->showWindow();
  190. }
  191. void SettingsDialog::accountAdded(AccountState *s)
  192. {
  193. auto height = _toolBar->sizeHint().height();
  194. bool brandingSingleAccount = !Theme::instance()->multiAccount();
  195. const auto actionText = brandingSingleAccount ? tr("Account") : s->account()->displayName();
  196. const auto accountAction = createColorAwareAction(QLatin1String(":/client/theme/account.svg"), actionText);
  197. if (!brandingSingleAccount) {
  198. accountAction->setToolTip(s->account()->displayName());
  199. accountAction->setIconText(shortDisplayNameForSettings(s->account().data(), static_cast<int>(height * buttonSizeRatio)));
  200. }
  201. _toolBar->insertAction(_toolBar->actions().at(0), accountAction);
  202. auto accountSettings = new AccountSettings(s, this);
  203. QString objectName = QLatin1String("accountSettings_");
  204. objectName += s->account()->displayName();
  205. accountSettings->setObjectName(objectName);
  206. _ui->stack->insertWidget(0 , accountSettings);
  207. _actionGroup->addAction(accountAction);
  208. _actionGroupWidgets.insert(accountAction, accountSettings);
  209. _actionForAccount.insert(s->account().data(), accountAction);
  210. accountAction->trigger();
  211. connect(accountSettings, &AccountSettings::folderChanged, _gui, &ownCloudGui::slotFoldersChanged);
  212. connect(accountSettings, &AccountSettings::openFolderAlias,
  213. _gui, &ownCloudGui::slotFolderOpenAction);
  214. connect(accountSettings, &AccountSettings::showIssuesList, this, &SettingsDialog::showIssuesList);
  215. connect(s->account().data(), &Account::accountChangedAvatar, this, &SettingsDialog::slotAccountAvatarChanged);
  216. connect(s->account().data(), &Account::accountChangedDisplayName, this, &SettingsDialog::slotAccountDisplayNameChanged);
  217. // Connect styleChanged event, to adapt (Dark-/Light-Mode switching)
  218. connect(this, &SettingsDialog::styleChanged, accountSettings, &AccountSettings::slotStyleChanged);
  219. const auto userInfo = new UserInfo(s, false, true, this);
  220. connect(userInfo, &UserInfo::fetchedLastInfo, this, [userInfo](const UserInfo *fetchedInfo) {
  221. // UserInfo will go and update the account avatar
  222. Q_UNUSED(fetchedInfo);
  223. userInfo->deleteLater();
  224. });
  225. userInfo->setActive(true);
  226. userInfo->slotFetchInfo();
  227. }
  228. void SettingsDialog::slotAccountAvatarChanged()
  229. {
  230. auto *account = dynamic_cast<Account *>(sender());
  231. if (account && _actionForAccount.contains(account)) {
  232. QAction *action = _actionForAccount[account];
  233. if (action) {
  234. QImage pix = account->avatar();
  235. if (!pix.isNull()) {
  236. action->setIcon(QPixmap::fromImage(AvatarJob::makeCircularAvatar(pix)));
  237. }
  238. }
  239. }
  240. }
  241. void SettingsDialog::slotAccountDisplayNameChanged()
  242. {
  243. auto *account = dynamic_cast<Account *>(sender());
  244. if (account && _actionForAccount.contains(account)) {
  245. QAction *action = _actionForAccount[account];
  246. if (action) {
  247. QString displayName = account->displayName();
  248. action->setText(displayName);
  249. auto height = _toolBar->sizeHint().height();
  250. action->setIconText(shortDisplayNameForSettings(account, static_cast<int>(height * buttonSizeRatio)));
  251. }
  252. }
  253. }
  254. void SettingsDialog::accountRemoved(AccountState *s)
  255. {
  256. for (auto it = _actionGroupWidgets.begin(); it != _actionGroupWidgets.end(); ++it) {
  257. auto as = qobject_cast<AccountSettings *>(*it);
  258. if (!as) {
  259. continue;
  260. }
  261. if (as->accountsState() == s) {
  262. _toolBar->removeAction(it.key());
  263. if (_ui->stack->currentWidget() == it.value()) {
  264. showFirstPage();
  265. }
  266. it.key()->deleteLater();
  267. it.value()->deleteLater();
  268. _actionGroupWidgets.erase(it);
  269. break;
  270. }
  271. }
  272. if (_actionForAccount.contains(s->account().data())) {
  273. _actionForAccount.remove(s->account().data());
  274. }
  275. // Hide when the last account is deleted. We want to enter the same
  276. // state we'd be in the client was started up without an account
  277. // configured.
  278. if (AccountManager::instance()->accounts().isEmpty()) {
  279. hide();
  280. }
  281. }
  282. void SettingsDialog::customizeStyle()
  283. {
  284. QString highlightColor(palette().highlight().color().name());
  285. QString highlightTextColor(palette().highlightedText().color().name());
  286. QString dark(palette().dark().color().name());
  287. QString background(palette().base().color().name());
  288. _toolBar->setStyleSheet(TOOLBAR_CSS().arg(background, dark, highlightColor, highlightTextColor));
  289. Q_FOREACH (QAction *a, _actionGroup->actions()) {
  290. QIcon icon = Theme::createColorAwareIcon(a->property("iconPath").toString(), palette());
  291. a->setIcon(icon);
  292. auto *btn = qobject_cast<QToolButton *>(_toolBar->widgetForAction(a));
  293. if (btn)
  294. btn->setIcon(icon);
  295. }
  296. }
  297. class ToolButtonAction : public QWidgetAction
  298. {
  299. public:
  300. explicit ToolButtonAction(const QIcon &icon, const QString &text, QObject *parent)
  301. : QWidgetAction(parent)
  302. {
  303. setText(text);
  304. setIcon(icon);
  305. }
  306. QWidget *createWidget(QWidget *parent) override
  307. {
  308. auto toolbar = qobject_cast<QToolBar *>(parent);
  309. if (!toolbar) {
  310. // this means we are in the extention menu, no special action here
  311. return nullptr;
  312. }
  313. auto *btn = new QToolButton(parent);
  314. QString objectName = QLatin1String("settingsdialog_toolbutton_");
  315. objectName += text();
  316. btn->setObjectName(objectName);
  317. btn->setDefaultAction(this);
  318. btn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
  319. btn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
  320. return btn;
  321. }
  322. };
  323. QAction *SettingsDialog::createActionWithIcon(const QIcon &icon, const QString &text, const QString &iconPath)
  324. {
  325. QAction *action = new ToolButtonAction(icon, text, this);
  326. action->setCheckable(true);
  327. if (!iconPath.isEmpty()) {
  328. action->setProperty("iconPath", iconPath);
  329. }
  330. return action;
  331. }
  332. QAction *SettingsDialog::createColorAwareAction(const QString &iconPath, const QString &text)
  333. {
  334. // all buttons must have the same size in order to keep a good layout
  335. QIcon coloredIcon = Theme::createColorAwareIcon(iconPath, palette());
  336. return createActionWithIcon(coloredIcon, text, iconPath);
  337. }
  338. } // namespace OCC