summaryrefslogtreecommitdiffstats
path: root/Swift
diff options
context:
space:
mode:
authorRichard Maudsley <richard.maudsley@isode.com>2014-07-16 13:47:27 (GMT)
committerSwift Review <review@swift.im>2014-07-29 09:48:04 (GMT)
commit4ab20a452e0af56c7ee210f863aeae31450954cc (patch)
treebe0a3777eb7bd814e4c6b9660a77dbb8fafb90d3 /Swift
parent9c5c731845881996f45b32ea6de12e0647f4634d (diff)
downloadswift-contrib-4ab20a452e0af56c7ee210f863aeae31450954cc.zip
swift-contrib-4ab20a452e0af56c7ee210f863aeae31450954cc.tar.bz2
Add ability to reorder highlight rules list.
Test-Information: Add several highlight rules. Verify that the up and down buttons reorder the items in the list. Change-Id: I6272799e2b2767ddfa01068c2ffcd1fb4651e11d
Diffstat (limited to 'Swift')
-rw-r--r--Swift/Controllers/HighlightManager.cpp8
-rw-r--r--Swift/Controllers/HighlightManager.h8
-rw-r--r--Swift/QtUI/QtHighlightEditor.cpp22
-rw-r--r--Swift/QtUI/QtHighlightEditor.h3
4 files changed, 35 insertions, 6 deletions
diff --git a/Swift/Controllers/HighlightManager.cpp b/Swift/Controllers/HighlightManager.cpp
index eac562f..ca0567e 100644
--- a/Swift/Controllers/HighlightManager.cpp
+++ b/Swift/Controllers/HighlightManager.cpp
@@ -71,61 +71,69 @@ std::string HighlightManager::rulesToString() const
std::vector<HighlightRule> HighlightManager::getDefaultRules()
{
std::vector<HighlightRule> rules;
HighlightRule r;
r.setMatchChat(true);
r.getAction().setPlaySound(true);
rules.push_back(r);
return rules;
}
HighlightRule HighlightManager::getRule(int index) const
{
assert(index >= 0 && static_cast<size_t>(index) < rules_->getSize());
return rules_->getRule(static_cast<size_t>(index));
}
void HighlightManager::setRule(int index, const HighlightRule& rule)
{
assert(index >= 0 && static_cast<size_t>(index) < rules_->getSize());
rules_->list_[static_cast<size_t>(index)] = rule;
}
void HighlightManager::insertRule(int index, const HighlightRule& rule)
{
assert(index >= 0 && boost::numeric_cast<std::vector<std::string>::size_type>(index) <= rules_->getSize());
rules_->list_.insert(rules_->list_.begin() + index, rule);
}
void HighlightManager::removeRule(int index)
{
assert(index >= 0 && boost::numeric_cast<std::vector<std::string>::size_type>(index) < rules_->getSize());
rules_->list_.erase(rules_->list_.begin() + index);
}
+void HighlightManager::swapRules(const size_t first, const size_t second) {
+ assert(first < rules_->getSize());
+ assert(second < rules_->getSize());
+ const HighlightRule swap = rules_->getRule(first);
+ rules_->setRule(first, rules_->getRule(second));
+ rules_->setRule(second, swap);
+}
+
void HighlightManager::storeSettings()
{
storingSettings_ = true; // don't reload settings while saving
settings_->storeSetting(SettingConstants::HIGHLIGHT_RULES, rulesToString());
storingSettings_ = false;
}
void HighlightManager::loadSettings()
{
std::string rulesString = settings_->getSetting(SettingConstants::HIGHLIGHT_RULES);
std::stringstream stream;
stream << rulesString;
try {
boost::archive::text_iarchive archive(stream);
archive >> rules_->list_;
} catch (boost::archive::archive_exception&) {
rules_->list_ = getDefaultRules();
}
}
Highlighter* HighlightManager::createHighlighter()
{
return new Highlighter(this);
}
}
diff --git a/Swift/Controllers/HighlightManager.h b/Swift/Controllers/HighlightManager.h
index 3da72eb..07a3fe3 100644
--- a/Swift/Controllers/HighlightManager.h
+++ b/Swift/Controllers/HighlightManager.h
@@ -1,71 +1,75 @@
/*
* 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.
*/
#pragma once
#include <vector>
#include <string>
#include <Swiften/Base/boost_bsignals.h>
#include <Swift/Controllers/HighlightRule.h>
namespace Swift {
class SettingsProvider;
class Highlighter;
class HighlightManager {
public:
class HighlightRulesList {
public:
friend class HighlightManager;
size_t getSize() const { return list_.size(); }
const HighlightRule& getRule(const size_t index) const { return list_[index]; }
- void addRule(const HighlightRule &rule) { list_.push_back(rule); }
- void combineRules(const HighlightRulesList &rhs) {
+ void addRule(const HighlightRule& rule) { list_.push_back(rule); }
+ void combineRules(const HighlightRulesList& rhs) {
list_.insert(list_.end(), rhs.list_.begin(), rhs.list_.end());
}
+ void setRule(const size_t index, const HighlightRule& rule) {
+ list_[index] = rule;
+ }
private:
std::vector<HighlightRule> list_;
};
HighlightManager(SettingsProvider* settings);
Highlighter* createHighlighter();
boost::shared_ptr<const HighlightManager::HighlightRulesList> getRules() const { return rules_; }
HighlightRule getRule(int index) const;
void setRule(int index, const HighlightRule& rule);
void insertRule(int index, const HighlightRule& rule);
void removeRule(int index);
+ void swapRules(const size_t first, const size_t second);
void storeSettings();
void loadSettings();
boost::signal<void (const HighlightAction&)> onHighlight;
private:
void handleSettingChanged(const std::string& settingPath);
std::string rulesToString() const;
static std::vector<HighlightRule> getDefaultRules();
SettingsProvider* settings_;
bool storingSettings_;
boost::shared_ptr<HighlightManager::HighlightRulesList> rules_;
};
typedef boost::shared_ptr<const HighlightManager::HighlightRulesList> HighlightRulesListPtr;
}
diff --git a/Swift/QtUI/QtHighlightEditor.cpp b/Swift/QtUI/QtHighlightEditor.cpp
index 8488d7d..134155c 100644
--- a/Swift/QtUI/QtHighlightEditor.cpp
+++ b/Swift/QtUI/QtHighlightEditor.cpp
@@ -43,73 +43,73 @@ QtHighlightEditor::QtHighlightEditor(QtSettingsProvider* settings, QWidget* pare
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 line-edit control with the roster autocompleter */
ui_.dummySenderName->setVisible(false);
jid_ = new QtSuggestingJIDInput(this, settings);
ui_.senderName->addWidget(jid_);
jid_->onUserSelected.connect(boost::bind(&QtHighlightEditor::handleOnUserSelected, this, _1));
/* handle autocomplete */
connect(jid_, SIGNAL(textEdited(QString)), SLOT(handleContactSuggestionRequested(QString)));
/* we need to be notified if any of the state changes so that we can update our textual rule description */
connect(ui_.chatRadio, SIGNAL(clicked()), SLOT(widgetClick()));
connect(ui_.roomRadio, SIGNAL(clicked()), SLOT(widgetClick()));
connect(ui_.nickIsKeyword, SIGNAL(clicked()), SLOT(widgetClick()));
connect(ui_.allMsgRadio, SIGNAL(clicked()), SLOT(widgetClick()));
connect(ui_.senderRadio, SIGNAL(clicked()), SLOT(widgetClick()));
connect(jid_, SIGNAL(textChanged(const QString&)), SLOT(widgetClick()));
connect(ui_.keywordRadio, SIGNAL(clicked()), SLOT(widgetClick()));
connect(ui_.keyword, SIGNAL(textChanged(const QString&)), SLOT(widgetClick()));
connect(ui_.matchPartialWords, SIGNAL(clicked()), SLOT(widgetClick()));
connect(ui_.matchCase, SIGNAL(clicked()), SLOT(widgetClick()));
connect(ui_.noColorRadio, SIGNAL(clicked()), SLOT(widgetClick()));
connect(ui_.customColorRadio, SIGNAL(clicked()), SLOT(widgetClick()));
connect(ui_.noSoundRadio, SIGNAL(clicked()), SLOT(widgetClick()));
connect(ui_.defaultSoundRadio, SIGNAL(clicked()), SLOT(widgetClick()));
connect(ui_.customSoundRadio, SIGNAL(clicked()), SLOT(widgetClick()));
/* allow selection of a custom sound file */
connect(ui_.soundFileButton, SIGNAL(clicked()), SLOT(selectSoundFile()));
- /* if these are not needed, then they should be removed */
- ui_.moveUpButton->setVisible(false);
- ui_.moveDownButton->setVisible(false);
+ /* allowing reordering items */
+ connect(ui_.moveUpButton, SIGNAL(clicked()), this, SLOT(onUpButtonClicked()));
+ connect(ui_.moveDownButton, SIGNAL(clicked()), this, SLOT(onDownButtonClicked()));
setWindowTitle(tr("Highlight Rules"));
}
QtHighlightEditor::~QtHighlightEditor()
{
}
std::string formatShortDescription(const HighlightRule &rule)
{
const std::string chatOrRoom = (rule.getMatchChat() ? "chat" : "room");
std::vector<std::string> senders = rule.getSenders();
std::vector<std::string> keywords = rule.getKeywords();
if (senders.empty() && keywords.empty() && !rule.getNickIsKeyword()) {
return std::string("All ") + chatOrRoom + " messages.";
}
if (rule.getNickIsKeyword()) {
return std::string("All ") + chatOrRoom + " messages that mention my name.";
}
if (!senders.empty()) {
return std::string("All ") + chatOrRoom + " messages from " + senders[0] + ".";
}
if (!keywords.empty()) {
return std::string("All ") + chatOrRoom + " messages mentioning the keyword '" + keywords[0] + "'.";
}
return "Unknown Rule";
}
void QtHighlightEditor::show()
@@ -163,70 +163,86 @@ void QtHighlightEditor::soundCustomSelect()
ui_.soundFileButton->setEnabled(true);
}
void QtHighlightEditor::onNewButtonClicked()
{
int row = getSelectedRow() + 1;
populateList();
HighlightRule newRule;
newRule.setMatchMUC(true);
highlightManager_->insertRule(row, newRule);
QListWidgetItem *item = new QListWidgetItem();
item->setText(P2QSTRING(formatShortDescription(newRule)));
ui_.listWidget->insertItem(row, item);
selectRow(row);
}
void QtHighlightEditor::onDeleteButtonClicked()
{
int selectedRow = getSelectedRow();
assert(selectedRow>=0 && selectedRow<ui_.listWidget->count());
delete ui_.listWidget->takeItem(selectedRow);
highlightManager_->removeRule(selectedRow);
if (!ui_.listWidget->count()) {
disableDialog();
ui_.deleteButton->setEnabled(false);
} else {
if (selectedRow == ui_.listWidget->count()) {
selectRow(ui_.listWidget->count() - 1);
} else {
selectRow(selectedRow);
}
}
}
+void QtHighlightEditor::onUpButtonClicked() {
+ const size_t moveFrom = ui_.listWidget->currentRow();
+ const size_t moveTo = moveFrom - 1;
+ highlightManager_->swapRules(moveFrom, moveTo);
+ populateList();
+ selectRow(moveTo);
+}
+
+void QtHighlightEditor::onDownButtonClicked() {
+ const size_t moveFrom = ui_.listWidget->currentRow();
+ const size_t moveTo = moveFrom + 1;
+ highlightManager_->swapRules(moveFrom, moveTo);
+ populateList();
+ selectRow(moveTo);
+}
+
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);
if (ui_.listWidget->currentItem()) {
ui_.listWidget->currentItem()->setText(P2QSTRING(formatShortDescription(rule)));
}
}
/* grey the dialog if we have nothing selected */
if (currentRow == -1) {
disableDialog();
}
previousRow_ = currentRow;
}
void QtHighlightEditor::onApplyButtonClick()
{
selectRow(getSelectedRow()); /* force save */
highlightManager_->storeSettings();
}
void QtHighlightEditor::onCancelButtonClick()
diff --git a/Swift/QtUI/QtHighlightEditor.h b/Swift/QtUI/QtHighlightEditor.h
index c7db464..e0595ad 100644
--- a/Swift/QtUI/QtHighlightEditor.h
+++ b/Swift/QtUI/QtHighlightEditor.h
@@ -8,62 +8,63 @@
* 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.
*/
#pragma once
#include <Swift/Controllers/HighlightRule.h>
#include <Swift/Controllers/UIInterfaces/HighlightEditorWindow.h>
#include <Swift/QtUI/ui_QtHighlightEditor.h>
namespace Swift {
class QtSettingsProvider;
class QtSuggestingJIDInput;
class QtWebKitChatView;
class QtHighlightEditor : public QWidget, public HighlightEditorWindow {
Q_OBJECT
public:
QtHighlightEditor(QtSettingsProvider* settings, QWidget* parent = NULL);
virtual ~QtHighlightEditor();
virtual void show();
virtual void setHighlightManager(HighlightManager* highlightManager);
virtual void setContactSuggestions(const std::vector<Contact::ref>& suggestions);
private slots:
void colorOtherSelect();
void colorCustomSelect();
void soundOtherSelect();
void soundCustomSelect();
void onNewButtonClicked();
void onDeleteButtonClicked();
+ void onUpButtonClicked();
+ void onDownButtonClicked();
void onCurrentRowChanged(int currentRow);
void onApplyButtonClick();
void onCancelButtonClick();
void onOkButtonClick();
void setChildWidgetStates();
void widgetClick();
void disableDialog();
void handleContactSuggestionRequested(const QString& text);
void selectSoundFile();
private:
void handleOnUserSelected(const JID& jid);
void populateList();
- void updateChatPreview();
void selectRow(int row);
int getSelectedRow() const;
HighlightRule ruleFromDialog();
void ruleToDialog(const HighlightRule& rule);
Ui::QtHighlightEditor ui_;
QtSettingsProvider* settings_;
HighlightManager* highlightManager_;
QtSuggestingJIDInput* jid_;
int previousRow_;
};
}