1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
/*
* Copyright (c) 2016-2018 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#include <Swift/QtUI/QtHighlightNotificationConfigDialog.h>
#include <Swiften/Base/Log.h>
#include <Swift/Controllers/Highlighting/HighlightManager.h>
#include <Swift/QtUI/QtCheckBoxStyledItemDelegate.h>
#include <Swift/QtUI/QtColorSelectionStyledItemDelegate.h>
#include <Swift/QtUI/QtSoundSelectionStyledItemDelegate.h>
#include <Swift/QtUI/QtSwiftUtil.h>
namespace Swift {
QtHighlightNotificationConfigDialog::QtHighlightNotificationConfigDialog(QtSettingsProvider* settings, QWidget* parent) : QDialog(parent), settings_(settings) {
ui_.setupUi(this);
// setup custom delegates for checkboxes, color selection, and sound selection
ui_.userHighlightTreeWidget->setItemDelegateForColumn(1, new QtColorSelectionStyledItemDelegate(this));
ui_.userHighlightTreeWidget->setItemDelegateForColumn(2, new QtColorSelectionStyledItemDelegate(this));
ui_.userHighlightTreeWidget->setItemDelegateForColumn(3, new QtSoundSelectionStyledItemDelegate(this));
ui_.userHighlightTreeWidget->setItemDelegateForColumn(4, new QtCheckBoxStyledItemDelegate(this));
ui_.keywordHighlightTreeWidget->setItemDelegateForColumn(1, new QtCheckBoxStyledItemDelegate(this));
ui_.keywordHighlightTreeWidget->setItemDelegateForColumn(2, new QtColorSelectionStyledItemDelegate(this));
ui_.keywordHighlightTreeWidget->setItemDelegateForColumn(3, new QtColorSelectionStyledItemDelegate(this));
ui_.keywordHighlightTreeWidget->setItemDelegateForColumn(4, new QtSoundSelectionStyledItemDelegate(this));
ui_.keywordHighlightTreeWidget->setItemDelegateForColumn(5, new QtCheckBoxStyledItemDelegate(this));
// user highlight edit slots
connect(ui_.addUserHighlightPushButton, &QPushButton::clicked, [&](bool) {
auto item = new QTreeWidgetItem();
item->setFlags(Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
item->setData(0, Qt::EditRole, "");
item->setData(1, QtColorSelectionStyledItemDelegate::DATA_ROLE, QColor("#000000"));
item->setData(2, QtColorSelectionStyledItemDelegate::DATA_ROLE, QColor("#ffff00"));
item->setData(3, Qt::EditRole, "");
item->setData(4, QtCheckBoxStyledItemDelegate::DATA_ROLE, QVariant(true));
ui_.userHighlightTreeWidget->addTopLevelItem(item);
});
connect(ui_.removeUserHighlightPushButton, &QPushButton::clicked, [&](bool) {
auto currentItem = ui_.userHighlightTreeWidget->currentItem();
if (currentItem) {
ui_.userHighlightTreeWidget->takeTopLevelItem(ui_.userHighlightTreeWidget->indexOfTopLevelItem(currentItem));
}
});
connect(ui_.userHighlightTreeWidget, &QTreeWidget::currentItemChanged, [&](QTreeWidgetItem* current, QTreeWidgetItem* ) {
ui_.removeUserHighlightPushButton->setEnabled(current != nullptr);
});
// keyword highlight edit slots
connect(ui_.addKeywordHighlightPushButton, &QPushButton::clicked, [&](bool) {
auto item = new QTreeWidgetItem();
item->setFlags(Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
item->setData(0, Qt::EditRole, "");
item->setData(1, QtCheckBoxStyledItemDelegate::DATA_ROLE, QVariant(false));
item->setData(2, QtColorSelectionStyledItemDelegate::DATA_ROLE, QColor("#000000"));
item->setData(3, QtColorSelectionStyledItemDelegate::DATA_ROLE, QColor("#ffff00"));
item->setData(4, Qt::EditRole, "");
item->setData(5, QtCheckBoxStyledItemDelegate::DATA_ROLE, QVariant(true));
ui_.keywordHighlightTreeWidget->addTopLevelItem(item);
});
connect(ui_.removeKeywordHighlightPushButton, &QPushButton::clicked, [&](bool) {
auto currentItem = ui_.keywordHighlightTreeWidget->currentItem();
if (currentItem) {
ui_.keywordHighlightTreeWidget->takeTopLevelItem(ui_.keywordHighlightTreeWidget->indexOfTopLevelItem(currentItem));
}
});
connect(ui_.keywordHighlightTreeWidget, &QTreeWidget::currentItemChanged, [&](QTreeWidgetItem* current, QTreeWidgetItem* ) {
ui_.removeKeywordHighlightPushButton->setEnabled(current != nullptr);
});
// setup slots for main dialog buttons
connect(ui_.buttonBox, &QDialogButtonBox::clicked, [&](QAbstractButton* clickedButton) {
if (clickedButton == ui_.buttonBox->button(QDialogButtonBox::RestoreDefaults)) {
if (highlightManager_) {
highlightManager_->resetToDefaultConfiguration();
setHighlightConfigurationToDialog(*highlightManager_->getConfiguration());
}
}
});
connect(this, &QDialog::accepted, [&]() {
if (highlightManager_) {
highlightManager_->setConfiguration(getHighlightConfigurationFromDialog());
}
});
}
QtHighlightNotificationConfigDialog::~QtHighlightNotificationConfigDialog() {
}
void QtHighlightNotificationConfigDialog::show() {
if (highlightManager_) {
setHighlightConfigurationToDialog(*(highlightManager_->getConfiguration()));
}
QWidget::show();
QWidget::activateWindow();
}
void QtHighlightNotificationConfigDialog::setHighlightManager(HighlightManager* highlightManager) {
highlightManager_ = highlightManager;
}
void QtHighlightNotificationConfigDialog::setContactSuggestions(const std::vector<Contact::ref>& /*suggestions*/) {
}
HighlightConfiguration QtHighlightNotificationConfigDialog::getHighlightConfigurationFromDialog() const {
auto qtColorToOptionalString = [&](const QColor& color) {
boost::optional<std::string> colorString;
if (color.isValid()) {
colorString = Q2PSTRING(color.name(QColor::HexRgb));
}
return colorString;
};
auto getHighlightActionFromWidgetItem = [&](const QTreeWidgetItem* item, int startingColumn) {
HighlightAction action;
action.setFrontColor(qtColorToOptionalString(item->data(startingColumn, QtColorSelectionStyledItemDelegate::DATA_ROLE).value<QColor>()));
action.setBackColor(qtColorToOptionalString(item->data(startingColumn + 1, QtColorSelectionStyledItemDelegate::DATA_ROLE).value<QColor>()));
std::string soundFilePath = Q2PSTRING(item->data(startingColumn + 2, Qt::EditRole).toString());
if (soundFilePath == "defaultSound") {
action.setSoundFilePath(boost::optional<std::string>(""));
}
else if (soundFilePath.empty()) {
action.setSoundFilePath(boost::optional<std::string>());
}
else {
action.setSoundFilePath(boost::optional<std::string>(""));
}
action.setSystemNotificationEnabled(item->data(startingColumn + 3, QtCheckBoxStyledItemDelegate::DATA_ROLE).toBool());
return action;
};
HighlightConfiguration uiConfiguration;
// contact highlights
for (int i = 0; i < ui_.userHighlightTreeWidget->topLevelItemCount(); i++) {
auto item = ui_.userHighlightTreeWidget->topLevelItem(i);
HighlightConfiguration::ContactHighlight contactHighlight;
contactHighlight.name = Q2PSTRING(item->data(0, Qt::EditRole).toString());
contactHighlight.action = getHighlightActionFromWidgetItem(item, 1);
uiConfiguration.contactHighlights.push_back(contactHighlight);
}
// keyword highlights
for (int i = 0; i < ui_.keywordHighlightTreeWidget->topLevelItemCount(); i++) {
auto item = ui_.keywordHighlightTreeWidget->topLevelItem(i);
HighlightConfiguration::KeywordHightlight keywordHighlight;
keywordHighlight.keyword = Q2PSTRING(item->data(0, Qt::EditRole).toString());
keywordHighlight.matchCaseSensitive = item->data(1, QtCheckBoxStyledItemDelegate::DATA_ROLE).toBool();
keywordHighlight.action = getHighlightActionFromWidgetItem(item, 2);
uiConfiguration.keywordHighlights.push_back(keywordHighlight);
}
// general configuration
uiConfiguration.playSoundOnIncomingDirectMessages = ui_.playSoundOnDirectMessagesCheckBox->isChecked();
uiConfiguration.showNotificationOnIncomingDirectMessages = ui_.notificationOnDirectMessagesCheckBox->isChecked();
uiConfiguration.playSoundOnIncomingGroupchatMessages = ui_.playSoundOnGroupMessagesCheckBox->isChecked();
uiConfiguration.showNotificationOnIncomingGroupchatMessages = ui_.notificationOnGroupMessagesCheckBox->isChecked();
uiConfiguration.ownMentionAction.setFrontColor(qtColorToOptionalString(ui_.mentionTextColorColorButton->getColor()));
uiConfiguration.ownMentionAction.setBackColor(qtColorToOptionalString(ui_.mentionBackgroundColorButton->getColor()));
uiConfiguration.ownMentionAction.setSoundFilePath(ui_.playSoundOnMentionCheckBox->isChecked() ? boost::optional<std::string>("") : boost::optional<std::string>());
uiConfiguration.ownMentionAction.setSystemNotificationEnabled(ui_.notificationOnMentionCheckBox->isChecked());
return uiConfiguration;
}
void QtHighlightNotificationConfigDialog::setHighlightConfigurationToDialog(const HighlightConfiguration& config) {
auto optionalStringToQtColor = [](const boost::optional<std::string>& colorString) {
QColor qtColor;
if (colorString) {
qtColor = QColor(P2QSTRING(colorString.get_value_or(std::string(""))));
}
return qtColor;
};
auto optionalSoundPathStringToQString = [](const boost::optional<std::string>& soundPath) {
QString ret;
if (soundPath) {
if (soundPath.get_value_or("").empty()) {
ret = "defaultSound";
}
else {
ret = P2QSTRING(soundPath.get_value_or(""));
}
}
return ret;
};
auto setHighlightActionOnTreeWidgetItem = [&](QTreeWidgetItem* item, int startingColumn, const HighlightAction& action) {
item->setData(startingColumn, QtColorSelectionStyledItemDelegate::DATA_ROLE, optionalStringToQtColor(action.getFrontColor()));
item->setData(startingColumn + 1, QtColorSelectionStyledItemDelegate::DATA_ROLE, optionalStringToQtColor(action.getBackColor()));
item->setData(startingColumn + 2, Qt::DisplayRole, P2QSTRING(action.getSoundFilePath().get_value_or(std::string(""))));
item->setData(startingColumn + 2, Qt::EditRole, optionalSoundPathStringToQString(action.getSoundFilePath()));
item->setData(startingColumn + 3, QtCheckBoxStyledItemDelegate::DATA_ROLE, action.isSystemNotificationEnabled());
};
// contact highlights
ui_.userHighlightTreeWidget->clear();
for (const auto& contactHighlight : config.contactHighlights) {
auto item = new QTreeWidgetItem();
item->setFlags(Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
item->setData(0, Qt::DisplayRole, P2QSTRING(contactHighlight.name));
item->setData(0, Qt::EditRole, P2QSTRING(contactHighlight.name));
setHighlightActionOnTreeWidgetItem(item, 1, contactHighlight.action);
ui_.userHighlightTreeWidget->addTopLevelItem(item);
}
// keyword highlights
ui_.keywordHighlightTreeWidget->clear();
for (const auto& keywordHighlight : config.keywordHighlights) {
auto item = new QTreeWidgetItem();
item->setFlags(Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled);
item->setData(0, Qt::DisplayRole, P2QSTRING(keywordHighlight.keyword));
item->setData(0, Qt::EditRole, P2QSTRING(keywordHighlight.keyword));
item->setData(1, QtCheckBoxStyledItemDelegate::DATA_ROLE, keywordHighlight.matchCaseSensitive);
setHighlightActionOnTreeWidgetItem(item, 2, keywordHighlight.action);
ui_.keywordHighlightTreeWidget->addTopLevelItem(item);
}
// general configuration
ui_.playSoundOnDirectMessagesCheckBox->setChecked(config.playSoundOnIncomingDirectMessages);
ui_.notificationOnDirectMessagesCheckBox->setChecked(config.showNotificationOnIncomingDirectMessages);
ui_.playSoundOnGroupMessagesCheckBox->setChecked(config.playSoundOnIncomingGroupchatMessages);
ui_.notificationOnGroupMessagesCheckBox->setChecked(config.showNotificationOnIncomingGroupchatMessages);
ui_.mentionTextColorColorButton->setColor(optionalStringToQtColor(config.ownMentionAction.getFrontColor()));
ui_.mentionBackgroundColorButton->setColor(optionalStringToQtColor(config.ownMentionAction.getBackColor()));
ui_.playSoundOnMentionCheckBox->setChecked(config.ownMentionAction.getSoundFilePath().is_initialized());
ui_.notificationOnMentionCheckBox->setChecked(config.ownMentionAction.isSystemNotificationEnabled());
}
}
|