/* * Copyright (c) 2012 Maciej Niedzielski * Licensed under the simplified BSD license. * See Documentation/Licenses/BSD-simplified.txt for more information. */ /* * Copyright (c) 2014 Kevin Smith and Remko Tronçon * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ #include #include #include #include #include #include #include #include namespace Swift { QtHighlightEditor::QtHighlightEditor(QtSettingsProvider* settings, QWidget* parent) : QWidget(parent), settings_(settings), previousRow_(-1) { ui_.setupUi(this); connect(ui_.listWidget, SIGNAL(currentRowChanged(int)), SLOT(onCurrentRowChanged(int))); connect(ui_.newButton, SIGNAL(clicked()), SLOT(onNewButtonClicked())); connect(ui_.deleteButton, SIGNAL(clicked()), SLOT(onDeleteButtonClicked())); connect(ui_.buttonBox->button(QDialogButtonBox::Apply), SIGNAL(clicked()), SLOT(onApplyButtonClick())); connect(ui_.buttonBox->button(QDialogButtonBox::Cancel), SIGNAL(clicked()), SLOT(onCancelButtonClick())); connect(ui_.buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), SLOT(onOkButtonClick())); connect(ui_.noColorRadio, SIGNAL(clicked()), SLOT(colorOtherSelect())); connect(ui_.defaultColorRadio, SIGNAL(clicked()), SLOT(colorOtherSelect())); connect(ui_.customColorRadio, SIGNAL(clicked()), SLOT(colorCustomSelect())); connect(ui_.noSoundRadio, SIGNAL(clicked()), SLOT(soundOtherSelect())); connect(ui_.defaultSoundRadio, SIGNAL(clicked()), SLOT(soundOtherSelect())); connect(ui_.customSoundRadio, SIGNAL(clicked()), SLOT(soundCustomSelect())); /* replace the static lineedit control with the roster autocompleter */ ui_.dummySenderName->setVisible(false); jid_ = new QtSuggestingJIDInput(this, settings); ui_.senderName->addWidget(jid_); /* we need to be notified if any of the state changes so that we can update our textual rule description */ connect(ui_.chatCheck, SIGNAL(clicked()), SLOT(updateRuleDescription())); connect(ui_.roomCheck, SIGNAL(clicked()), SLOT(updateRuleDescription())); connect(ui_.senderCheck, SIGNAL(clicked()), SLOT(updateRuleDescription())); connect(jid_, SIGNAL(textChanged(const QString&)), SLOT(updateRuleDescription())); connect(ui_.keywordCheck, SIGNAL(clicked()), SLOT(updateRuleDescription())); connect(ui_.keyword, SIGNAL(textChanged(const QString&)), SLOT(updateRuleDescription())); connect(ui_.nickIsKeyword, SIGNAL(clicked()), SLOT(updateRuleDescription())); connect(ui_.matchWholeWords, SIGNAL(clicked()), SLOT(updateRuleDescription())); connect(ui_.matchCase, SIGNAL(clicked()), SLOT(updateRuleDescription())); connect(ui_.noColorRadio, SIGNAL(clicked()), SLOT(updateRuleDescription())); connect(ui_.defaultColorRadio, SIGNAL(clicked()), SLOT(updateRuleDescription())); connect(ui_.customColorRadio, SIGNAL(clicked()), SLOT(updateRuleDescription())); connect(ui_.noSoundRadio, SIGNAL(clicked()), SLOT(updateRuleDescription())); connect(ui_.defaultSoundRadio, SIGNAL(clicked()), SLOT(updateRuleDescription())); connect(ui_.customSoundRadio, SIGNAL(clicked()), SLOT(updateRuleDescription())); /* if these are not needed, then they should be removed */ ui_.moveUpButton->setVisible(false); ui_.moveDownButton->setVisible(false); setWindowTitle(tr("Highlight Rules")); } QtHighlightEditor::~QtHighlightEditor() { } std::string formatNaturalDescription(const HighlightRule& rule) { //This rule will match messages either in chats or in rooms if the sender // is called 'Admin', and the message contains 'Urgent' (not case sensitive) // as a part of any word. std::string text = "This rule will match messages "; if (rule.getMatchChat() && rule.getMatchMUC()) { text += "either in chats or in rooms"; } else if (rule.getMatchChat()) { text += "in chats only"; } else if (rule.getMatchMUC()) { text += "in rooms only"; } else { return "This rule has not been marked as a chat or room rule!"; } std::vector senders = rule.getSenders(); std::vector keywords = rule.getKeywords(); if (!senders.empty() || !keywords.empty()) { text += " if"; } if (!senders.empty()) { text += " the sender is called '" + senders[0] + "'"; } if (!senders.empty() && !keywords.empty()) { text += ", and"; } if (!keywords.empty()) { text += " the message contains '" + keywords[0] + "'"; if (rule.getMatchCase()) { text += " (case sensivitive)"; } else { text += " (not case sensivitive)"; } if (rule.getMatchWholeWords()) { text += " as a whole word"; } else { text += " as a part of any word"; } } if (rule.getNickIsKeyword()) { text += ", or mentions my nickname"; } text += "."; return text; } void QtHighlightEditor::show() { ui_.listWidget->clear(); highlightManager_->loadSettings(); foreach(const HighlightRule &rule, highlightManager_->getRules()) { QListWidgetItem *item = new QListWidgetItem(); item->setText(P2QSTRING(formatNaturalDescription(rule))); ui_.listWidget->addItem(item); } if (ui_.listWidget->count()) { selectRow(0); } QWidget::show(); QWidget::activateWindow(); } void QtHighlightEditor::setHighlightManager(HighlightManager* highlightManager) { highlightManager_ = highlightManager; //itemModel_->setHighlightManager(highlightManager); //ui_.newButton->setEnabled(highlightManager != NULL); // //ui_.ruleWidget->setEnabled(false); //ui_.deleteButton->setEnabled(false); //ui_.moveUpButton->setEnabled(false); //ui_.moveDownButton->setEnabled(false); } void QtHighlightEditor::colorOtherSelect() { ui_.foregroundColor->setEnabled(false); ui_.backgroundColor->setEnabled(false); } void QtHighlightEditor::colorCustomSelect() { ui_.foregroundColor->setEnabled(true); ui_.backgroundColor->setEnabled(true); } void QtHighlightEditor::soundOtherSelect() { ui_.soundFile->setEnabled(false); ui_.soundFileButton->setEnabled(false); } void QtHighlightEditor::soundCustomSelect() { ui_.soundFile->setEnabled(true); ui_.soundFileButton->setEnabled(true); } void QtHighlightEditor::onNewButtonClicked() { int row = getSelectedRow() + 1; highlightManager_->insertRule(row, HighlightRule()); QListWidgetItem *item = new QListWidgetItem(); item->setText("New rule"); ui_.listWidget->insertItem(row, item); selectRow(row); } void QtHighlightEditor::onDeleteButtonClicked() { int selectedRow = getSelectedRow(); assert(selectedRow > 0); delete ui_.listWidget->takeItem(selectedRow); highlightManager_->removeRule(selectedRow); } void QtHighlightEditor::onCurrentRowChanged(int currentRow) { ui_.deleteButton->setEnabled(currentRow != -1); ui_.moveUpButton->setEnabled(currentRow != -1 && currentRow != 0); ui_.moveDownButton->setEnabled(currentRow != -1 && currentRow != (ui_.listWidget->count()-1)); if (previousRow_ != -1) { if (ui_.listWidget->count() > previousRow_) { highlightManager_->setRule(previousRow_, ruleFromDialog()); } } if (currentRow != -1) { HighlightRule rule = highlightManager_->getRule(currentRow); ruleToDialog(rule); ui_.ruleDescription->setText(P2QSTRING(formatNaturalDescription(rule))); } /* grey the dialog if we have nothing selected */ enableDialog(currentRow != -1); previousRow_ = currentRow; } void QtHighlightEditor::onApplyButtonClick() { selectRow(getSelectedRow()); /* force save */ highlightManager_->storeSettings(); } void QtHighlightEditor::onCancelButtonClick() { close(); } void QtHighlightEditor::onOkButtonClick() { onApplyButtonClick(); close(); } void QtHighlightEditor::updateRuleDescription() { HighlightRule rule = ruleFromDialog(); const std::string description = formatNaturalDescription(rule); ui_.ruleDescription->setText(P2QSTRING(description)); ui_.listWidget->item(getSelectedRow())->setText(P2QSTRING(description)); } void QtHighlightEditor::enableDialog(bool state) { if (!state) { /* also clear the value, if applicable */ ui_.chatCheck->setChecked(false); ui_.roomCheck->setChecked(false); ui_.senderCheck->setChecked(false); jid_->setText(""); ui_.keywordCheck->setChecked(false); ui_.keyword->setText(""); ui_.nickIsKeyword->setChecked(false); ui_.matchWholeWords->setChecked(false); ui_.matchCase->setChecked(false); ui_.noColorRadio->setChecked(true); ui_.foregroundColor->setEnabled(state); ui_.backgroundColor->setEnabled(state); ui_.noSoundRadio->setChecked(true); ui_.foregroundColor->setColor(QColor()); ui_.backgroundColor->setColor(QColor()); ui_.soundFile->setText(""); ui_.foregroundColor->setEnabled(false); ui_.backgroundColor->setEnabled(false); ui_.soundFile->setEnabled(false); ui_.soundFileButton->setEnabled(false); } ui_.chatCheck->setEnabled(state); ui_.roomCheck->setEnabled(state); ui_.senderCheck->setEnabled(state); jid_->setEnabled(state); ui_.keywordCheck->setEnabled(state); ui_.keyword->setEnabled(state); ui_.nickIsKeyword->setEnabled(state); ui_.matchWholeWords->setEnabled(state); ui_.matchCase->setEnabled(state); ui_.noColorRadio->setEnabled(state); ui_.defaultColorRadio->setEnabled(state); ui_.customColorRadio->setEnabled(state); ui_.noSoundRadio->setEnabled(state); ui_.defaultSoundRadio->setEnabled(state); ui_.customSoundRadio->setEnabled(state); } void QtHighlightEditor::selectRow(int row) { for (int i = 0; i < ui_.listWidget->count(); ++i) { if (i == row) { ui_.listWidget->item(i)->setSelected(true); onCurrentRowChanged(i); } else { ui_.listWidget->item(i)->setSelected(false); } } } int QtHighlightEditor::getSelectedRow() const { for (int i = 0; i < ui_.listWidget->count(); ++i) { if (ui_.listWidget->item(i)->isSelected()) { return i; } } return -1; } void QtHighlightEditor::setRowText(int row, const std::string &text) { if (row>=0 && rowcount()) { ui_.listWidget->item(row)->setText(P2QSTRING(text)); } } HighlightRule QtHighlightEditor::ruleFromDialog() { HighlightRule rule; rule.setMatchChat(ui_.chatCheck->isChecked()); rule.setMatchMUC(ui_.roomCheck->isChecked()); if (ui_.senderCheck->isChecked()) { QString senderName = jid_->text(); if (!senderName.isEmpty()) { std::vector senders; senders.push_back(Q2PSTRING(senderName)); rule.setSenders(senders); } } if (ui_.keywordCheck->isChecked()) { QString keywordString = ui_.keyword->text(); if (!keywordString.isEmpty()) { std::vector keywords; keywords.push_back(Q2PSTRING(keywordString)); rule.setKeywords(keywords); } } rule.setNickIsKeyword(ui_.nickIsKeyword->isChecked()); rule.setMatchWholeWords(ui_.matchWholeWords->isChecked()); rule.setMatchCase(ui_.matchCase->isChecked()); HighlightAction& action = rule.getAction(); if (ui_.noColorRadio->isChecked()) { action.setHighlightText(false); action.setTextColor(""); action.setTextBackground(""); } else if (ui_.defaultColorRadio->isChecked()) { action.setHighlightText(true); action.setTextColor(""); action.setTextBackground(""); } else { action.setHighlightText(true); action.setTextColor(Q2PSTRING(ui_.foregroundColor->getColor().name())); action.setTextBackground(Q2PSTRING(ui_.backgroundColor->getColor().name())); } if (ui_.noSoundRadio->isChecked()) { action.setPlaySound(false); } else if (ui_.defaultSoundRadio->isChecked()) { action.setPlaySound(true); action.setSoundFile(""); } else { action.setPlaySound(true); action.setSoundFile(Q2PSTRING(ui_.soundFile->text())); } return rule; } void QtHighlightEditor::ruleToDialog(const HighlightRule& rule) { ui_.chatCheck->setChecked(rule.getMatchChat()); ui_.roomCheck->setChecked(rule.getMatchMUC()); std::vector senders = rule.getSenders(); if (senders.empty()) { ui_.senderCheck->setChecked(false); jid_->setEnabled(false); jid_->setText(""); } else { ui_.senderCheck->setChecked(true); jid_->setEnabled(true); jid_->setText(P2QSTRING(senders[0])); } std::vector keywords = rule.getKeywords(); if (keywords.empty()) { ui_.keywordCheck->setChecked(false); ui_.keyword->setEnabled(false); ui_.keyword->setText(""); } else { ui_.keywordCheck->setChecked(true); ui_.keyword->setEnabled(true); ui_.keyword->setText(P2QSTRING(keywords[0])); } ui_.nickIsKeyword->setChecked(rule.getNickIsKeyword()); ui_.matchWholeWords->setChecked(rule.getMatchWholeWords()); ui_.matchCase->setChecked(rule.getMatchCase()); const HighlightAction& action = rule.getAction(); if (action.highlightText()) { if (action.getTextColor().empty() && action.getTextBackground().empty()) { ui_.defaultColorRadio->setChecked(true); ui_.foregroundColor->setEnabled(false); ui_.backgroundColor->setEnabled(false); } else { ui_.foregroundColor->setEnabled(true); ui_.backgroundColor->setEnabled(true); QColor foregroundColor(P2QSTRING(action.getTextColor())); ui_.foregroundColor->setColor(foregroundColor); QColor backgroundColor(P2QSTRING(action.getTextBackground())); ui_.backgroundColor->setColor(backgroundColor); ui_.customColorRadio->setChecked(true); } } else { ui_.noColorRadio->setChecked(true); ui_.foregroundColor->setEnabled(false); ui_.backgroundColor->setEnabled(false); } if (action.playSound()) { if (action.getSoundFile().empty()) { ui_.defaultSoundRadio->setChecked(true); } else { ui_.customSoundRadio->setChecked(true); } } else { ui_.noSoundRadio->setChecked(true); } } }