X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fqtui%2Fsettingspages%2Faliasesmodel.cpp;h=416414d571b1493c2c5e5a11ad563bf1c35cb2a7;hp=6504fae5610f4413c7859aa08a659733a79af35c;hb=a7cd976ca45c83fdc2c0ba1142443bc72157a2ee;hpb=82dd90f4684db4240311edc13bd3707772e0dbe8 diff --git a/src/qtui/settingspages/aliasesmodel.cpp b/src/qtui/settingspages/aliasesmodel.cpp index 6504fae5..416414d5 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-09 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -28,11 +28,13 @@ AliasesModel::AliasesModel(QObject *parent) : QAbstractItemModel(parent), - _configChanged(false) + _configChanged(false), + _modelReady(false) { // 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())); + if(Client::isConnected()) clientConnected(); else @@ -40,6 +42,9 @@ AliasesModel::AliasesModel(QObject *parent) } QVariant AliasesModel::data(const QModelIndex &index, int role) const { + if(!_modelReady) + return QVariant(); + if(!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount()) return QVariant(); @@ -53,10 +58,13 @@ QVariant AliasesModel::data(const QModelIndex &index, int role) const { case 1: return "The string the shortcut will be expanded to
" "special variables:
" - " - $i represenents the i'th parameter.
" + " - $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.
" - " - $currentnick your current nickname
" - " - $channelname the name of the selected channel

" + " - $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: @@ -78,13 +86,16 @@ QVariant AliasesModel::data(const QModelIndex &index, int role) const { } 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: if(aliasManager().contains(newValue)) { @@ -114,6 +125,27 @@ void AliasesModel::newAlias() { 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; @@ -136,8 +168,8 @@ Qt::ItemFlags AliasesModel::flags(const QModelIndex &index) const { QVariant AliasesModel::headerData(int section, Qt::Orientation orientation, int role) const { QStringList header; header << tr("Alias") - << tr("Expansion"); - + << tr("Expansion"); + if(orientation == Qt::Horizontal && role == Qt::DisplayRole) return header[section]; @@ -157,19 +189,19 @@ const AliasManager &AliasesModel::aliasManager() const { if(_configChanged) return _clonedAliasManager; else - return _aliasManager; + return *Client::aliasManager(); } AliasManager &AliasesModel::aliasManager() { if(_configChanged) return _clonedAliasManager; else - return _aliasManager; + return *Client::aliasManager(); } AliasManager &AliasesModel::cloneAliasManager() { if(!_configChanged) { - _clonedAliasManager = _aliasManager; + _clonedAliasManager = *Client::aliasManager(); _configChanged = true; emit configChanged(true); } @@ -179,7 +211,7 @@ AliasManager &AliasesModel::cloneAliasManager() { void AliasesModel::revert() { if(!_configChanged) return; - + _configChanged = false; emit configChanged(false); reset(); @@ -189,26 +221,28 @@ void AliasesModel::commit() { if(!_configChanged) return; - _aliasManager.requestUpdate(_clonedAliasManager.toVariantMap()); + Client::aliasManager()->requestUpdate(_clonedAliasManager.toVariantMap()); revert(); -} +} void AliasesModel::initDone() { + _modelReady = true; reset(); emit modelReady(true); } 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())); + connect(Client::aliasManager(), SIGNAL(updated(QVariantMap)), SLOT(revert())); + if(Client::aliasManager()->isInitialized()) + initDone(); + else + connect(Client::aliasManager(), SIGNAL(initDone()), SLOT(initDone())); } void AliasesModel::clientDisconnected() { - // clear alias managers - _aliasManager = AliasManager(); - _clonedAliasManager = AliasManager(); + // clear + _clonedAliasManager = ClientAliasManager(); + _modelReady = false; reset(); emit modelReady(false); }