jussi01: can you spell aliases?
authorMarcus Eggenberger <egs@quassel-irc.org>
Tue, 22 Jul 2008 01:08:08 +0000 (03:08 +0200)
committerMarcus Eggenberger <egs@quassel-irc.org>
Tue, 22 Jul 2008 01:08:08 +0000 (03:08 +0200)
20 files changed:
src/common/CMakeLists.txt
src/common/aliasmanager.cpp [new file with mode: 0644]
src/common/aliasmanager.h [new file with mode: 0644]
src/core/CMakeLists.txt
src/core/basichandler.h
src/core/corealiasmanager.cpp [new file with mode: 0644]
src/core/corealiasmanager.h [new file with mode: 0644]
src/core/coresession.cpp
src/core/coresession.h
src/core/userinputhandler.cpp
src/core/userinputhandler.h
src/qtui/mainwin.cpp
src/qtui/settingspages/aliasesmodel.cpp [new file with mode: 0644]
src/qtui/settingspages/aliasesmodel.h [new file with mode: 0644]
src/qtui/settingspages/aliasessettingspage.cpp [new file with mode: 0644]
src/qtui/settingspages/aliasessettingspage.h [new file with mode: 0644]
src/qtui/settingspages/aliasessettingspage.ui [new file with mode: 0644]
src/qtui/settingspages/settingspages.inc
src/uisupport/settingspage.cpp
src/uisupport/settingspage.h

index 765d7ba..ddccfb9 100644 (file)
@@ -5,6 +5,7 @@ set(QT_USE_QTNETWORK 1)
 include(${QT_USE_FILE})
 
 set(SOURCES
+    aliasmanager.cpp
     backlogmanager.cpp
     bufferinfo.cpp
     buffersyncer.cpp
@@ -24,6 +25,7 @@ set(SOURCES
     cliparser.cpp)
 
 set(MOC_HDRS
+    aliasmanager.h
     backlogmanager.h
     buffersyncer.h
     bufferviewconfig.h
diff --git a/src/common/aliasmanager.cpp b/src/common/aliasmanager.cpp
new file mode 100644 (file)
index 0000000..c03c4e4
--- /dev/null
@@ -0,0 +1,78 @@
+/***************************************************************************
+ *   Copyright (C) 2005-08 by the Quassel Project                          *
+ *   devel@quassel-irc.org                                                 *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) version 3.                                           *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   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.             *
+ ***************************************************************************/
+
+#include "aliasmanager.h"
+
+#include <QDebug>
+#include <QStringList>
+
+AliasManager &AliasManager::operator=(const AliasManager &other) {
+  _aliases = other._aliases;
+  return *this;
+}
+
+int AliasManager::indexOf(const QString &name) const {
+  for(int i = 0; i < _aliases.count(); i++) {
+    if(_aliases[i].name == name)
+      return i;
+  }
+  return -1;
+}
+
+QVariantMap AliasManager::initAliases() const {
+  QVariantMap aliases;
+  QStringList names;
+  QStringList expansions;
+
+  for(int i = 0; i < _aliases.count(); i++) {
+    names << _aliases[i].name;
+    expansions << _aliases[i].expansion;
+  }
+
+  aliases["names"] = names;
+  aliases["expansions"] = expansions;
+  return aliases;
+}
+
+void AliasManager::initSetAliases(const QVariantMap &aliases) {
+  QStringList names = aliases["names"].toStringList();
+  QStringList expansions = aliases["expansions"].toStringList();
+
+  if(names.count() != expansions.count()) {
+    qWarning() << "AliasesManager::initSetAliases: received" << names.count() << "alias names but only" << expansions.count() << "expansions!";
+    return;
+  }
+
+  _aliases.clear();
+  for(int i = 0; i < names.count(); i++) {
+    _aliases << Alias(names[i], expansions[i]);
+  }
+}
+
+
+void AliasManager::addAlias(const QString &name, const QString &expansion) {
+  if(contains(name)) {
+    return;
+  }
+
+  _aliases << Alias(name, expansion);
+
+  emit aliasAdded(name, expansion);
+}
diff --git a/src/common/aliasmanager.h b/src/common/aliasmanager.h
new file mode 100644 (file)
index 0000000..37d3880
--- /dev/null
@@ -0,0 +1,67 @@
+/***************************************************************************
+ *   Copyright (C) 2005-08 by the Quassel Project                          *
+ *   devel@quassel-irc.org                                                 *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) version 3.                                           *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   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.             *
+ ***************************************************************************/
+
+#ifndef ALIASMANAGER_H
+#define ALIASMANAGER_H
+
+#include "syncableobject.h"
+
+#include <QVariantMap>
+
+class AliasManager : public SyncableObject {
+  Q_OBJECT
+
+public:
+  inline AliasManager(QObject *parent = 0) : SyncableObject(parent) { setAllowClientUpdates(true); }
+  AliasManager &operator=(const AliasManager &other);
+  
+  struct Alias {
+    QString name;
+    QString expansion;
+    Alias(const QString &name_, const QString &expansion_) : name(name_), expansion(expansion_) {}
+  };
+
+  int indexOf(const QString &name) const;
+  inline bool contains(const QString &name) const { return indexOf(name) != -1; }
+  inline bool isEmpty() const { return _aliases.isEmpty(); }
+  inline int count() const { return _aliases.count(); }
+  inline void removeAt(int index) { _aliases.removeAt(index); }
+  inline Alias &operator[](int i) { return _aliases[i]; }
+  inline const Alias &operator[](int i) const { return _aliases[i]; }
+  inline const QList<Alias> &aliases() const { return _aliases; }
+
+public slots:
+  virtual QVariantMap initAliases() const;
+  virtual void initSetAliases(const QVariantMap &aliases);
+
+  virtual void addAlias(const QString &name, const QString &expansion);
+  
+protected:
+  void setAliases(const QList<Alias> &aliases) { _aliases = aliases; }
+
+signals:
+  void aliasAdded(const QString &name, const QString &expansion);
+  
+private:
+  QList<Alias> _aliases;
+
+};
+
+#endif //ALIASMANAGER_H
index a7bbbd2..d73be7c 100644 (file)
@@ -10,6 +10,7 @@ set(SOURCES
     abstractsqlstorage.cpp
     basichandler.cpp
     core.cpp
+    corealiasmanager.cpp
     corebacklogmanager.cpp
     corebufferviewconfig.cpp
     corebufferviewmanager.cpp
@@ -31,6 +32,7 @@ set(MOC_HDRS
     abstractsqlstorage.h
     basichandler.h
     core.h
+    corealiasmanager.h
     corebacklogmanager.h
     corebufferviewconfig.h
     corebufferviewmanager.h
index 4700347..c276c96 100644 (file)
@@ -31,6 +31,8 @@
 
 #include "networkconnection.h"
 
+class CoreSession;
+
 class BasicHandler : public QObject {
   Q_OBJECT
 
@@ -71,6 +73,7 @@ protected:
 
   inline Network *network() const { return _networkConnection->network(); }
   inline NetworkConnection *networkConnection() const { return _networkConnection; }
+  inline CoreSession *coreSession() const { return _networkConnection->coreSession(); }
 
   BufferInfo::Type typeByTarget(const QString &target) const;
 
diff --git a/src/core/corealiasmanager.cpp b/src/core/corealiasmanager.cpp
new file mode 100644 (file)
index 0000000..8fcdb02
--- /dev/null
@@ -0,0 +1,58 @@
+/***************************************************************************
+ *   Copyright (C) 2005-08 by the Quassel Project                          *
+ *   devel@quassel-irc.org                                                 *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) version 3.                                           *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   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.             *
+ ***************************************************************************/
+
+#include "corealiasmanager.h"
+
+#include "core.h"
+#include "coresession.h"
+
+CoreAliasManager::CoreAliasManager(CoreSession *parent)
+  : AliasManager(parent)
+{
+  CoreSession *session = qobject_cast<CoreSession *>(parent);
+  if(!session) {
+    qWarning() << "CoreAliasManager: unable to load Aliases. Parent is not a Coresession!";
+    loadDefaults();
+    return;
+  }
+
+  QVariantMap aliases = Core::getUserSetting(session->user(), "Aliases").toMap();
+  if(aliases.isEmpty()) {
+    loadDefaults();
+  } else {
+    initSetAliases(aliases);
+  }
+}
+
+
+CoreAliasManager::~CoreAliasManager() {
+  CoreSession *session = qobject_cast<CoreSession *>(parent());
+  if(!session) {
+    qWarning() << "CoreAliasManager: unable to save Aliases. Parent is not a Coresession!";
+    return;
+  }
+
+  Core::setUserSetting(session->user(), "Aliases", initAliases());
+}
+
+void CoreAliasManager::loadDefaults() {
+  // Default Aliases:
+  addAlias("j", "/join $1");
+}
diff --git a/src/core/corealiasmanager.h b/src/core/corealiasmanager.h
new file mode 100644 (file)
index 0000000..b5fe761
--- /dev/null
@@ -0,0 +1,42 @@
+/***************************************************************************
+ *   Copyright (C) 2005-08 by the Quassel Project                          *
+ *   devel@quassel-irc.org                                                 *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) version 3.                                           *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   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.             *
+ ***************************************************************************/
+
+#ifndef COREALIASMANAGER_H
+#define COREALIASMANAGER_H
+
+#include "aliasmanager.h"
+
+class CoreSession;
+
+class CoreAliasManager : public AliasManager {
+  Q_OBJECT
+
+public:
+  CoreAliasManager(CoreSession *parent = 0);
+  ~CoreAliasManager();
+
+  inline virtual const QMetaObject *syncMetaObject() const { return &AliasManager::staticMetaObject; }
+
+private:
+  void loadDefaults();
+  
+};
+
+#endif //COREALIASMANAGER_H
index 2956d88..696825d 100644 (file)
@@ -44,6 +44,7 @@ CoreSession::CoreSession(UserId uid, bool restoreState, QObject *parent)
   : QObject(parent),
     _user(uid),
     _signalProxy(new SignalProxy(SignalProxy::Server, 0, this)),
+    _aliasManager(this),
     _bufferSyncer(new BufferSyncer(this)),
     _backlogManager(new CoreBacklogManager(this)),
     _bufferViewManager(new CoreBufferViewManager(_signalProxy, this)),
@@ -88,13 +89,16 @@ CoreSession::CoreSession(UserId uid, bool restoreState, QObject *parent)
   p->synchronize(_bufferSyncer);
 
 
-  // init BacklogManager;
+  // init alias manager
+  p->synchronize(&aliasManager());
+  
+  // init BacklogManager
   p->synchronize(_backlogManager);
 
-  // init IrcListHelper;
+  // init IrcListHelper
   p->synchronize(ircListHelper());
   
-  // init CoreInfo;
+  // init CoreInfo
   p->synchronize(&_coreInfo);
   
   // Restore session state
index 7144fe2..57c2a53 100644 (file)
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
 
-#ifndef _CORESESSION_H_
-#define _CORESESSION_H_
+#ifndef CORESESSION_H
+#define CORESESSION_H
 
 #include <QString>
 #include <QVariant>
 
 #include "corecoreinfo.h"
+#include "corealiasmanager.h"
 #include "message.h"
 
 class BufferSyncer;
@@ -56,6 +57,9 @@ public:
 
   SignalProxy *signalProxy() const;
 
+  const AliasManager &aliasManager() const { return _aliasManager; }
+  AliasManager &aliasManager() { return _aliasManager; }
+
   inline CoreIrcListHelper *ircListHelper() const { return _ircListHelper; }
   
   void attachNetworkConnection(NetworkConnection *conn);
@@ -181,6 +185,7 @@ private:
   UserId _user;
 
   SignalProxy *_signalProxy;
+  CoreAliasManager _aliasManager;
   QHash<NetworkId, NetworkConnection *> _connections;
   QHash<NetworkId, CoreNetwork *> _networks;
   //  QHash<NetworkId, CoreNetwork *> _networksToRemove;
index 73bfcce..e9b0139 100644 (file)
@@ -349,8 +349,27 @@ void UserInputHandler::handleWhowas(const BufferInfo &bufferInfo, const QString
 }
 
 void UserInputHandler::defaultHandler(QString cmd, const BufferInfo &bufferInfo, const QString &msg) {
-  Q_UNUSED(bufferInfo)
+  for(int i = 0; i < coreSession()->aliasManager().count(); i++) {
+    if(coreSession()->aliasManager()[i].name.toLower() == cmd.toLower()) {
+      expand(coreSession()->aliasManager()[i].expansion, bufferInfo, msg);
+      return;
+    }
+  }
   emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: %1 %2").arg(cmd).arg(msg));
 }
 
+void UserInputHandler::expand(const QString &alias, const BufferInfo &bufferInfo, const QString &msg) {
+  QStringList commands = alias.split(QRegExp("; ?"));
+  QStringList params = msg.split(' ');
+  for(int i = 0; i < commands.count(); i++) {
+    QString command = commands[i];
+    for(int j = params.count(); j > 0; j--) {
+      command = command.replace(QString("$%1").arg(j), params[j - 1]);
+    }
+    command = command.replace("$0", msg);
+    handleUserInput(bufferInfo, command);
+  }
+}
+
+
 
index 006e1eb..81e5c17 100644 (file)
@@ -64,6 +64,9 @@ public slots:
   void handleWhowas(const BufferInfo &bufferInfo, const QString &text);
 
   void defaultHandler(QString cmd, const BufferInfo &bufferInfo, const QString &text);
+
+private:
+  void expand(const QString &alias, const BufferInfo &bufferInfo, const QString &msg);
 };
 
 
index 9b46b73..d426e4e 100644 (file)
@@ -50,6 +50,7 @@
 #include "selectionmodelsynchronizer.h"
 #include "mappedselectionmodel.h"
 
+#include "settingspages/aliasessettingspage.h"
 #include "settingspages/appearancesettingspage.h"
 #include "settingspages/bufferviewsettingspage.h"
 #include "settingspages/colorsettingspage.h"
@@ -233,6 +234,7 @@ void MainWin::setupSettingsDlg() {
   //Category: Behaviour
   settingsDlg->registerSettingsPage(new GeneralSettingsPage(settingsDlg));
   settingsDlg->registerSettingsPage(new HighlightSettingsPage(settingsDlg));
+  settingsDlg->registerSettingsPage(new AliasesSettingsPage(settingsDlg));
   //Category: General
   settingsDlg->registerSettingsPage(new IdentitiesSettingsPage(settingsDlg));
   settingsDlg->registerSettingsPage(new NetworksSettingsPage(settingsDlg));
diff --git a/src/qtui/settingspages/aliasesmodel.cpp b/src/qtui/settingspages/aliasesmodel.cpp
new file mode 100644 (file)
index 0000000..588673c
--- /dev/null
@@ -0,0 +1,203 @@
+/***************************************************************************
+ *   Copyright (C) 2005-08 by the Quassel Project                          *
+ *   devel@quassel-irc.org                                                 *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) version 3.                                           *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   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.             *
+ ***************************************************************************/
+
+#include "aliasesmodel.h"
+
+#include <QDebug>
+#include <QStringList>
+
+#include "client.h"
+#include "signalproxy.h"
+
+AliasesModel::AliasesModel(QObject *parent)
+  : QAbstractItemModel(parent),
+    _configChanged(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()));
+  if(Client::isConnected())
+    clientConnected();
+}
+
+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 "<b>The shortcut for the alias</b><br />"
+       "It can be used as a regular slash command.<br /><br />"
+       "<b>Example:</b> \"foo\" can be used per /foo";
+    case 1:
+      return "<b>The string the shortcut will be expanded to</b><br />"
+       "$i represenents the i'th parameter. $0 the whole string.<br />"
+       "Multiple commands can be separated with semicolons<br /><br />"
+       "<b>Example:</b> \"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:
+    switch(index.column()) {
+    case 0:
+      return aliasManager()[index.row()].name;
+    case 1:
+      return aliasManager()[index.row()].expansion;
+    default:
+      return QVariant();
+    }
+  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;
+    }
+  case 1:
+    cloneAliasManager()[index.row()].expansion = newValue;
+    return true;
+  default:
+    return false;
+  }
+}
+
+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::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;
+  }
+}
+
+
+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();
+}
+
+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 _aliasManager;
+}
+
+AliasManager &AliasesModel::aliasManager() {
+  if(_configChanged)
+    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);
+  reset();
+}
+
+void AliasesModel::commit() {
+  if(!_configChanged)
+    return;
+
+  _aliasManager.requestUpdate(_clonedAliasManager.toVariantMap());
+  revert();
+}  
+
+void AliasesModel::initDone() {
+  reset();
+  emit modelReady();
+}
+
+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()));
+}
diff --git a/src/qtui/settingspages/aliasesmodel.h b/src/qtui/settingspages/aliasesmodel.h
new file mode 100644 (file)
index 0000000..f10c818
--- /dev/null
@@ -0,0 +1,75 @@
+/***************************************************************************
+ *   Copyright (C) 2005-08 by the Quassel Project                          *
+ *   devel@quassel-irc.org                                                 *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) version 3.                                           *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   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.             *
+ ***************************************************************************/
+
+#ifndef ALIASESMODEL_H
+#define ALIASESMODEL_H
+
+#include <QAbstractItemModel>
+#include <QPointer>
+
+#include "aliasmanager.h"
+
+class AliasesModel : public QAbstractItemModel {
+  Q_OBJECT
+
+public:
+  AliasesModel(QObject *parent = 0);
+
+  virtual QVariant data(const QModelIndex &index, int role) const;
+  virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
+
+  virtual Qt::ItemFlags flags(const QModelIndex &index) const;
+
+  QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
+
+  QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
+
+  inline QModelIndex parent(const QModelIndex &) const { return QModelIndex(); }
+
+  inline int rowCount(const QModelIndex &parent = QModelIndex()) const { Q_UNUSED(parent) return aliasManager().count(); }
+  inline int columnCount(const QModelIndex &parent = QModelIndex()) const { Q_UNUSED(parent) return 2; }
+
+  inline bool configChanged() const { return _configChanged; }
+
+public slots:
+  void newAlias();
+  void removeAlias(int index);
+  void revert();
+  void commit();
+
+signals:
+  void configChanged(bool);
+  void modelReady();
+  
+private:
+  AliasManager _aliasManager;
+  AliasManager _clonedAliasManager;
+  bool _configChanged;
+
+  const AliasManager &aliasManager() const;
+  AliasManager &aliasManager();
+  AliasManager &cloneAliasManager();
+
+private slots:
+  void clientConnected();
+  void initDone();
+};
+
+#endif //ALIASESMODEL_H
diff --git a/src/qtui/settingspages/aliasessettingspage.cpp b/src/qtui/settingspages/aliasessettingspage.cpp
new file mode 100644 (file)
index 0000000..7aab01c
--- /dev/null
@@ -0,0 +1,66 @@
+/***************************************************************************
+ *   Copyright (C) 2005-08 by the Quassel Project                          *
+ *   devel@quassel-irc.org                                                 *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) version 3.                                           *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   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.             *
+ ***************************************************************************/
+
+#include "aliasessettingspage.h"
+
+#include <QHeaderView>
+#include <QItemSelectionModel>
+
+AliasesSettingsPage::AliasesSettingsPage(QWidget *parent)
+  : SettingsPage(tr("Behaviour"), tr("Aliases"), parent)
+{
+  ui.setupUi(this);
+
+  ui.aliasesView->setSelectionBehavior(QAbstractItemView::SelectRows);
+  ui.aliasesView->setSelectionMode(QAbstractItemView::SingleSelection);
+  ui.aliasesView->setAlternatingRowColors(true);
+  ui.aliasesView->setTabKeyNavigation(false);
+  ui.aliasesView->setModel(&_aliasesModel);
+  // ui.aliasesView->setSortingEnabled(true);
+  ui.aliasesView->verticalHeader()->hide();
+  ui.aliasesView->horizontalHeader()->setStretchLastSection(true);
+
+  connect(ui.newAliasButton, SIGNAL(clicked()), &_aliasesModel, SLOT(newAlias()));
+  connect(ui.deleteAliasButton, SIGNAL(clicked()), this, SLOT(deleteSelectedAlias()));
+  connect(&_aliasesModel, SIGNAL(configChanged(bool)), this, SLOT(setChangedState(bool)));
+  connect(&_aliasesModel, SIGNAL(modelReady()), this, SLOT(enableDialog()));
+}
+
+void AliasesSettingsPage::load() {
+  if(_aliasesModel.configChanged())
+    _aliasesModel.revert();
+}
+
+void AliasesSettingsPage::save() {
+  if(_aliasesModel.configChanged())
+    _aliasesModel.commit();
+}
+
+void AliasesSettingsPage::enableDialog() {
+  ui.newAliasButton->setEnabled(true);
+  ui.deleteAliasButton->setEnabled(true);
+}
+
+void AliasesSettingsPage::deleteSelectedAlias() {
+  if(!ui.aliasesView->selectionModel()->hasSelection())
+    return;
+
+  _aliasesModel.removeAlias(ui.aliasesView->selectionModel()->selectedIndexes()[0].row());
+}
diff --git a/src/qtui/settingspages/aliasessettingspage.h b/src/qtui/settingspages/aliasessettingspage.h
new file mode 100644 (file)
index 0000000..de1888c
--- /dev/null
@@ -0,0 +1,55 @@
+/***************************************************************************
+ *   Copyright (C) 2005-08 by the Quassel Project                          *
+ *   devel@quassel-irc.org                                                 *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) version 3.                                           *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   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.             *
+ ***************************************************************************/
+
+#ifndef ALIASESSETTINGSPAGE_H
+#define ALIASESSETTINGSPAGE_H
+
+#include "settingspage.h"
+#include "ui_aliasessettingspage.h"
+
+#include "aliasesmodel.h"
+
+class AliasesSettingsPage : public SettingsPage {
+  Q_OBJECT
+
+public:
+  AliasesSettingsPage(QWidget *parent = 0);
+
+public slots:
+  void save();
+  void load();
+//   void defaults();
+                
+// private slots:
+//   void widgetHasChanged();
+  
+private:
+  Ui::AliasesSettingsPage ui;
+
+  AliasesModel _aliasesModel;
+
+  //   bool testHasChanged();
+
+private slots:
+  void enableDialog();
+  void deleteSelectedAlias();
+};
+
+#endif //ALIASESSETTINGSPAGE_H
diff --git a/src/qtui/settingspages/aliasessettingspage.ui b/src/qtui/settingspages/aliasessettingspage.ui
new file mode 100644 (file)
index 0000000..4948c4f
--- /dev/null
@@ -0,0 +1,70 @@
+<ui version="4.0" >
+ <class>AliasesSettingsPage</class>
+ <widget class="QWidget" name="AliasesSettingsPage" >
+  <property name="geometry" >
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>291</width>
+    <height>229</height>
+   </rect>
+  </property>
+  <property name="windowTitle" >
+   <string>Form</string>
+  </property>
+  <layout class="QVBoxLayout" name="verticalLayout" >
+   <item>
+    <widget class="QTableView" name="aliasesView" />
+   </item>
+   <item>
+    <layout class="QHBoxLayout" name="horizontalLayout" >
+     <item>
+      <widget class="QPushButton" name="newAliasButton" >
+       <property name="enabled" >
+        <bool>false</bool>
+       </property>
+       <property name="text" >
+        <string>New</string>
+       </property>
+       <property name="icon" >
+        <iconset resource="../../icons/icons.qrc" >
+         <normaloff>:/16x16/actions/oxygen/16x16/actions/list-add.png</normaloff>:/16x16/actions/oxygen/16x16/actions/list-add.png</iconset>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <widget class="QPushButton" name="deleteAliasButton" >
+       <property name="enabled" >
+        <bool>false</bool>
+       </property>
+       <property name="text" >
+        <string>Delete</string>
+       </property>
+       <property name="icon" >
+        <iconset resource="../../icons/icons.qrc" >
+         <normaloff>:/16x16/actions/oxygen/16x16/actions/edit-delete.png</normaloff>:/16x16/actions/oxygen/16x16/actions/edit-delete.png</iconset>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <spacer name="horizontalSpacer" >
+       <property name="orientation" >
+        <enum>Qt::Horizontal</enum>
+       </property>
+       <property name="sizeHint" stdset="0" >
+        <size>
+         <width>40</width>
+         <height>20</height>
+        </size>
+       </property>
+      </spacer>
+     </item>
+    </layout>
+   </item>
+  </layout>
+ </widget>
+ <resources>
+  <include location="../../icons/icons.qrc" />
+ </resources>
+ <connections/>
+</ui>
index b3d93b6..db6a240 100644 (file)
@@ -1,8 +1,8 @@
 # Putting $FOO in SETTINGSPAGES automatically includes
 # $FOOsettingspage.cpp, $FOOsettingspage.h and $FOOsettingspage.ui
-set(SETTINGSPAGES appearance bufferview color fonts general highlight identities networks)
+set(SETTINGSPAGES aliases appearance bufferview color fonts general highlight identities networks)
 
 # Specify additional files (e.g. for subdialogs) here!
-set(SP_SOURCES )
-set(SP_HEADERS )
+set(SP_SOURCES aliasesmodel.cpp)
+set(SP_HEADERS aliasesmodel.h)
 set(SP_FORMS buffervieweditdlg.ui createidentitydlg.ui saveidentitiesdlg.ui networkeditdlg.ui nickeditdlg.ui servereditdlg.ui)
index 7808a74..e752ec7 100644 (file)
 
 #include "settingspage.h"
 
-SettingsPage::SettingsPage(const QString &category, const QString &title, QWidget *parent) : QWidget(parent),
-     _category(category), _title(title) {
-
-  _changed = false;
-}
-
-QString SettingsPage::category() const {
-  return _category;
-}
-
-QString SettingsPage::title() const {
-  return _title;
-}
-
-bool SettingsPage::hasDefaults() const {
-  return false;
-}
-
-void SettingsPage::defaults() {
-
-}
-
-bool SettingsPage::hasChanged() const {
-  return _changed;
-}
-
-bool SettingsPage::aboutToSave() {
-  return true;
-}
-
-void SettingsPage::changed() {
-  setChangedState(true);
+SettingsPage::SettingsPage(const QString &category, const QString &title, QWidget *parent)
+  : QWidget(parent),
+    _category(category),
+    _title(title),
+    _changed(false)
+{
 }
 
 void SettingsPage::setChangedState(bool hasChanged) {
index f58cb37..a9f32b8 100644 (file)
 class SettingsPage : public QWidget {
   Q_OBJECT
 
-  public:
-    SettingsPage(const QString &category, const QString &name, QWidget *parent = 0);
-    virtual ~SettingsPage() {};
-
-    //! The category of this settings page.
-    virtual QString category() const;
-
-    //! The title of this settings page.
-    virtual QString title() const;
-
-    //! Derived classes need to define this and return true if they have default settings.
-    /** If this method returns true, the "Restore Defaults" button in the SettingsDlg is
-     *  enabled. You also need to provide an implementation of defaults() then.
-     *
-     * The default implementation returns false.
-     */
-    virtual bool hasDefaults() const;
-
-    //! Check if there are changes in the page, compared to the state saved in permanent storage.
-    bool hasChanged() const;
-
-    //! Called immediately before save() is called.
-    /** Derived classes should return false if saving is not possible (e.g. the current settings are invalid).
-     *  \return false, if the SettingsPage cannot be saved in its current state.
-     */
-    virtual bool aboutToSave();
-
-  public slots:
-    //! Save settings to permanent storage.
-    virtual void save() = 0;
-
-    //! Load settings from permanent storage, overriding any changes the user might have made in the dialog.
-    virtual void load() = 0;
-
-    //! Restore defaults, overriding any changes the user might have made in the dialog.
-    /** The default implementation does nothing.
+public:
+  SettingsPage(const QString &category, const QString &name, QWidget *parent = 0);
+  virtual ~SettingsPage() {};
+  
+  //! The category of this settings page.
+  inline virtual QString category() const { return _category; }
+  
+  //! The title of this settings page.
+  inline virtual QString title() const { return _title; }
+  
+  //! Derived classes need to define this and return true if they have default settings.
+  /** If this method returns true, the "Restore Defaults" button in the SettingsDlg is
+   *  enabled. You also need to provide an implementation of defaults() then.
+   *
+   * The default implementation returns false.
      */
-    virtual void defaults();
-
-  protected slots:
-    //! Calling this slot is equivalent to calling setChangedState(true).
-    void changed();
-
-  protected:
-    //! This should be called whenever the widget state changes from unchanged to change or the other way round.
-    void setChangedState(bool hasChanged = true);
-
-  signals:
-    //! Emitted whenever the widget state changes.
-    void changed(bool hasChanged);
-
-  private:
-    QString _category, _title;
-    bool _changed;
+  inline virtual bool hasDefaults() const { return false; }
+  
+  //! Check if there are changes in the page, compared to the state saved in permanent storage.
+  inline bool hasChanged() const { return _changed; }
+  
+  //! Called immediately before save() is called.
+  /** Derived classes should return false if saving is not possible (e.g. the current settings are invalid).
+   *  \return false, if the SettingsPage cannot be saved in its current state.
+   */
+  inline virtual bool aboutToSave() { return true; }
+                           
+public slots:
+  //! Save settings to permanent storage.
+  virtual void save() = 0;
+  
+  //! Load settings from permanent storage, overriding any changes the user might have made in the dialog.
+  virtual void load() = 0;
+
+  //! Restore defaults, overriding any changes the user might have made in the dialog.
+  /** The default implementation does nothing.
+   */
+  inline virtual void defaults() {}
+                        
+protected slots:
+  //! Calling this slot is equivalent to calling setChangedState(true).
+  inline void changed() { setChangedState(true); }
+  
+  //! This should be called whenever the widget state changes from unchanged to change or the other way round.
+  void setChangedState(bool hasChanged = true);
+  
+signals:
+  //! Emitted whenever the widget state changes.
+  void changed(bool hasChanged);
+  
+private:
+  QString _category, _title;
+  bool _changed;
 };
 
 #endif