From 086b14b1eb7e91c6f651fc9920b83ae3b03b9b10 Mon Sep 17 00:00:00 2001
From: Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com>
Date: Thu, 11 Sep 2014 22:19:03 +0200
Subject: [PATCH] [ #55020 ] Fix account creation

---
 src/categorizedaccountmodel.cpp | 17 +++++++++++++++--
 src/categorizedaccountmodel.h   |  3 ++-
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/src/categorizedaccountmodel.cpp b/src/categorizedaccountmodel.cpp
index 39d1de89..3ae34c38 100644
--- a/src/categorizedaccountmodel.cpp
+++ b/src/categorizedaccountmodel.cpp
@@ -47,7 +47,7 @@ CategorizedAccountModel::~CategorizedAccountModel()
 
 QModelIndex CategorizedAccountModel::mapToSource(const QModelIndex& idx) const
 {
-   if (!idx.isValid() || !idx.parent().isValid())
+   if (!idx.isValid() || !idx.parent().isValid() || idx.model() != this)
       return QModelIndex();
    switch (idx.parent().row()) {
       case Categories::IP2IP:
@@ -55,7 +55,8 @@ QModelIndex CategorizedAccountModel::mapToSource(const QModelIndex& idx) const
          break;
       case Categories::SERVER: {
          const QModelIndex& ip2ipIdx = AccountListModel::instance()->ip2ip()->index();
-         return AccountListModel::instance()->index((ip2ipIdx.row() < idx.row())?idx.row()+1:idx.row(),0);
+         //TODO DHT: this will stop working
+         return AccountListModel::instance()->index((ip2ipIdx.row() <= idx.row())?idx.row()+1:idx.row(),0);
       }
          break;
       default:
@@ -65,6 +66,18 @@ QModelIndex CategorizedAccountModel::mapToSource(const QModelIndex& idx) const
    return QModelIndex();
 }
 
+QModelIndex CategorizedAccountModel::mapFromSource(const QModelIndex& idx) const
+{
+   if (!idx.isValid() || idx.model() != AccountListModel::instance())
+      return QModelIndex();
+
+   //TODO DHT: this will stop working
+   const QModelIndex& ip2ipIdx = AccountListModel::instance()->ip2ip()->index();
+   return idx.row() == ip2ipIdx.row()?index(0,0,index(Categories::IP2IP,0,QModelIndex())): (
+      index(idx.row()+(idx.row() > ip2ipIdx.row()?-1:0),0,index(Categories::SERVER,0,QModelIndex()))
+   );
+}
+
 QVariant CategorizedAccountModel::data(const QModelIndex& index, int role ) const
 {
    if (!index.isValid())
diff --git a/src/categorizedaccountmodel.h b/src/categorizedaccountmodel.h
index 6fd3708d..f74b7f30 100644
--- a/src/categorizedaccountmodel.h
+++ b/src/categorizedaccountmodel.h
@@ -39,7 +39,8 @@ public:
    virtual QVariant headerData  (int section, Qt::Orientation orientation, int role           ) const;
 
    //Getter
-   QModelIndex mapToSource(const QModelIndex& idx) const;
+   QModelIndex mapToSource  (const QModelIndex& idx) const;
+   QModelIndex mapFromSource(const QModelIndex& idx) const;
 
 
 private:
-- 
GitLab