X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fqtui%2Fsettingspages%2Faliasesmodel.cpp;h=39b475ab0e53b89b1399811bbe00174a1b5a83b2;hp=588673c20c2400937025685ab92312478b8e7892;hb=0a43227b8cd44625f4881cc1545d42c8c8a4876c;hpb=6579cd49c867ce3fb6c99127851a881ea82d1b1b diff --git a/src/qtui/settingspages/aliasesmodel.cpp b/src/qtui/settingspages/aliasesmodel.cpp index 588673c2..39b475ab 100644 --- a/src/qtui/settingspages/aliasesmodel.cpp +++ b/src/qtui/settingspages/aliasesmodel.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005-08 by the Quassel Project * + * Copyright (C) 2005-2016 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -15,7 +15,7 @@ * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ #include "aliasesmodel.h" @@ -27,177 +27,257 @@ #include "signalproxy.h" AliasesModel::AliasesModel(QObject *parent) - : QAbstractItemModel(parent), - _configChanged(false) + : QAbstractItemModel(parent), + _configChanged(false), + _modelReady(false) { -// _aliasManager.addAlias("a", "aa"); -// _aliasManager.addAlias("b", "bb"); -// _aliasManager.addAlias("c", "cc"); -// _aliasManager.addAlias("d", "dd"); + // we need this signal for future connects to reset the data; + connect(Client::instance(), SIGNAL(connected()), this, SLOT(clientConnected())); + connect(Client::instance(), SIGNAL(disconnected()), this, SLOT(clientDisconnected())); - // we need this signal for future connects to reset the data; - connect(Client::instance(), SIGNAL(connected()), this, SLOT(clientConnected())); - if(Client::isConnected()) - clientConnected(); + if (Client::isConnected()) + clientConnected(); + else + emit modelReady(false); } -QVariant AliasesModel::data(const QModelIndex &index, int role) const { - if(!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount()) - return QVariant(); - switch(role) { - case Qt::ToolTipRole: - switch(index.column()) { - case 0: - return "The shortcut for the alias
" - "It can be used as a regular slash command.

" - "Example: \"foo\" can be used per /foo"; - case 1: - return "The string the shortcut will be expanded to
" - "$i represenents the i'th parameter. $0 the whole string.
" - "Multiple commands can be separated with semicolons

" - "Example: \"Test $1; Test $2; Test All $0\" will be expanded to three separate messages \"Test 1\", \"Test 2\" and \"Test All 1 2 3\" when called like /test 1 2 3"; +QVariant AliasesModel::data(const QModelIndex &index, int role) const +{ + if (!_modelReady) + return QVariant(); + + if (!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount()) + return QVariant(); + + switch (role) { + case Qt::ToolTipRole: + switch (index.column()) { + case 0: + return tr("The shortcut for the alias
" + "It can be used as a regular slash command.

" + "Example: \"foo\" can be used per /foo"); + case 1: + return tr("The string the shortcut will be expanded to
" + "special variables:
" + " - $i represents the i'th parameter.
" + " - $i..j represents the i'th to j'th parameter separated by spaces.
" + " - $i.. represents all parameters from i on separated by spaces.
" + " - $i:hostname represents the hostname of the user identified by the i'th parameter or a * if unknown.
" + " - $0 the whole string.
" + " - $nick your current nickname
" + " - $channel the name of the selected channel

" + "Multiple commands can be separated with semicolons

" + "Example: \"Test $1; Test $2; Test All $0\" will be expanded to three separate messages \"Test 1\", \"Test 2\" and \"Test All 1 2 3\" when called like /test 1 2 3"); + default: + return QVariant(); + } + case Qt::DisplayRole: + case Qt::EditRole: + switch (index.column()) { + case 0: + return aliasManager()[index.row()].name; + case 1: + return aliasManager()[index.row()].expansion; + default: + return QVariant(); + } default: - return QVariant(); + return QVariant(); } - case Qt::DisplayRole: - switch(index.column()) { +} + + +bool AliasesModel::setData(const QModelIndex &index, const QVariant &value, int role) +{ + if (!_modelReady) + return false; + + if (!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount() || role != Qt::EditRole) + return false; + + QString newValue = value.toString(); + if (newValue.isEmpty()) + return false; + + switch (index.column()) { case 0: - return aliasManager()[index.row()].name; + if (aliasManager().contains(newValue)) { + return false; + } + else { + cloneAliasManager()[index.row()].name = newValue; + return true; + } case 1: - return aliasManager()[index.row()].expansion; + cloneAliasManager()[index.row()].expansion = newValue; + return true; default: - return QVariant(); + return false; } - default: - return QVariant(); - } -} - -bool AliasesModel::setData(const QModelIndex &index, const QVariant &value, int role) { - if(!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount() || role != Qt::EditRole) - return false; - - QString newValue = value.toString(); - if(newValue.isEmpty()) - return false; - - switch(index.column()) { - case 0: - if(aliasManager().contains(newValue)) { - return false; - } else { - cloneAliasManager()[index.row()].name = newValue; - return true; +} + + +void AliasesModel::newAlias() +{ + QString newName("alias"); + int i = 0; + AliasManager &manager = cloneAliasManager(); + while (manager.contains(newName)) { + i++; + newName = QString("alias%1").arg(i); } - case 1: - cloneAliasManager()[index.row()].expansion = newValue; - return true; - default: - return false; - } + beginInsertRows(QModelIndex(), rowCount(), rowCount()); + manager.addAlias(newName, "Expansion"); + endInsertRows(); } -void AliasesModel::newAlias() { - QString newName("alias"); - int i = 0; - AliasManager &manager = cloneAliasManager(); - while(manager.contains(newName)) { - i++; - newName = QString("alias%1").arg(i); - } - beginInsertRows(QModelIndex(), rowCount(), rowCount()); - manager.addAlias(newName, "Expansion"); - endInsertRows(); + +void AliasesModel::loadDefaults() +{ + if (!_modelReady) + return; + + AliasManager &manager = cloneAliasManager(); + + if (!manager.isEmpty()) { + beginRemoveRows(QModelIndex(), 0, rowCount() - 1); + for (int i = rowCount() - 1; i >= 0; i--) + manager.removeAt(i); + endRemoveRows(); + } + + AliasManager::AliasList defaults = AliasManager::defaults(); + beginInsertRows(QModelIndex(), 0, defaults.count() - 1); + foreach(AliasManager::Alias alias, defaults) { + manager.addAlias(alias.name, alias.expansion); + } + endInsertRows(); } -void AliasesModel::removeAlias(int index) { - if(index < 0 || index >= rowCount()) - return; - AliasManager &manager = cloneAliasManager(); - beginRemoveRows(QModelIndex(), index, index); - manager.removeAt(index); - endRemoveRows(); +void AliasesModel::removeAlias(int index) +{ + if (index < 0 || index >= rowCount()) + return; + + AliasManager &manager = cloneAliasManager(); + beginRemoveRows(QModelIndex(), index, index); + manager.removeAt(index); + endRemoveRows(); } -Qt::ItemFlags AliasesModel::flags(const QModelIndex &index) const { - if(!index.isValid()) { - return Qt::ItemIsDropEnabled; - } else { - return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable; - } + +Qt::ItemFlags AliasesModel::flags(const QModelIndex &index) const +{ + if (!index.isValid()) { + return Qt::ItemIsDropEnabled; + } + else { + return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable; + } +} + + +QVariant AliasesModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + QStringList header; + header << tr("Alias") + << tr("Expansion"); + + if (orientation == Qt::Horizontal && role == Qt::DisplayRole) + return header[section]; + + return QVariant(); } -QVariant AliasesModel::headerData(int section, Qt::Orientation orientation, int role) const { - QStringList header; - header << tr("Alias") - << tr("Expansion"); - - if(orientation == Qt::Horizontal && role == Qt::DisplayRole) - return header[section]; +QModelIndex AliasesModel::index(int row, int column, const QModelIndex &parent) const +{ + Q_UNUSED(parent); + if (row >= rowCount() || column >= columnCount()) + return QModelIndex(); - return QVariant(); + return createIndex(row, column); } -QModelIndex AliasesModel::index(int row, int column, const QModelIndex &parent) const { - Q_UNUSED(parent); - if(row >= rowCount() || column >= columnCount()) - return QModelIndex(); - return createIndex(row, column); +const AliasManager &AliasesModel::aliasManager() const +{ + if (_configChanged) + return _clonedAliasManager; + else + return *Client::aliasManager(); } -const AliasManager &AliasesModel::aliasManager() const { - if(_configChanged) - return _clonedAliasManager; - else - return _aliasManager; +AliasManager &AliasesModel::aliasManager() +{ + if (_configChanged) + return _clonedAliasManager; + else + return *Client::aliasManager(); } -AliasManager &AliasesModel::aliasManager() { - if(_configChanged) + +AliasManager &AliasesModel::cloneAliasManager() +{ + if (!_configChanged) { + _clonedAliasManager = *Client::aliasManager(); + _configChanged = true; + emit configChanged(true); + } return _clonedAliasManager; - else - return _aliasManager; } -AliasManager &AliasesModel::cloneAliasManager() { - if(!_configChanged) { - _clonedAliasManager = _aliasManager; - _configChanged = true; - emit configChanged(true); - } - return _clonedAliasManager; + +void AliasesModel::revert() +{ + if (!_configChanged) + return; + + _configChanged = false; + emit configChanged(false); + beginResetModel(); + endResetModel(); } -void AliasesModel::revert() { - if(!_configChanged) - return; - - _configChanged = false; - emit configChanged(false); - reset(); + +void AliasesModel::commit() +{ + if (!_configChanged) + return; + + Client::aliasManager()->requestUpdate(_clonedAliasManager.toVariantMap()); + revert(); } -void AliasesModel::commit() { - if(!_configChanged) - return; - _aliasManager.requestUpdate(_clonedAliasManager.toVariantMap()); - revert(); -} +void AliasesModel::initDone() +{ + _modelReady = true; + beginResetModel(); + endResetModel(); + emit modelReady(true); +} + -void AliasesModel::initDone() { - reset(); - emit modelReady(); +void AliasesModel::clientConnected() +{ + connect(Client::aliasManager(), SIGNAL(updated()), SLOT(revert())); + if (Client::aliasManager()->isInitialized()) + initDone(); + else + connect(Client::aliasManager(), SIGNAL(initDone()), SLOT(initDone())); } -void AliasesModel::clientConnected() { - _aliasManager = AliasManager(); - Client::signalProxy()->synchronize(&_aliasManager); - connect(&_aliasManager, SIGNAL(initDone()), this, SLOT(initDone())); - connect(&_aliasManager, SIGNAL(updated(const QVariantMap &)), this, SLOT(revert())); + +void AliasesModel::clientDisconnected() +{ + // clear + _clonedAliasManager = ClientAliasManager(); + _modelReady = false; + beginResetModel(); + endResetModel(); + emit modelReady(false); }