summary refs log tree commit diff
path: root/src/PowerlevelsEditModels.cpp
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2022-06-26 00:09:00 +0200
committerNicolas Werner <nicolas.werner@hotmail.de>2022-06-26 00:09:00 +0200
commitdc4a06517c775a4d0ea01048e9218bfb64c5ae65 (patch)
treef7b978d95118559d02d993013db3c6eb3211791f /src/PowerlevelsEditModels.cpp
parentFix linting (diff)
downloadnheko-dc4a06517c775a4d0ea01048e9218bfb64c5ae65.tar.xz
Add an option to define new power levels
Diffstat (limited to 'src/PowerlevelsEditModels.cpp')
-rw-r--r--src/PowerlevelsEditModels.cpp77
1 files changed, 76 insertions, 1 deletions
diff --git a/src/PowerlevelsEditModels.cpp b/src/PowerlevelsEditModels.cpp
index 77262a20..cea35495 100644
--- a/src/PowerlevelsEditModels.cpp
+++ b/src/PowerlevelsEditModels.cpp
@@ -239,6 +239,7 @@ PowerlevelsTypeListModel::remove(int row)
 
     return true;
 }
+
 void
 PowerlevelsTypeListModel::add(int row, QString type)
 {
@@ -261,6 +262,23 @@ PowerlevelsTypeListModel::add(int row, QString type)
     endInsertRows();
 }
 
+void
+PowerlevelsTypeListModel::addRole(int64_t role)
+{
+    for (int i = 0; i < types.size(); i++) {
+        if (types[i].pl < role) {
+            beginInsertRows(QModelIndex(), i, i);
+            types.insert(i, Entry{"", role});
+            endInsertRows();
+            return;
+        }
+    }
+
+    beginInsertRows(QModelIndex(), types.size(), types.size());
+    types.push_back(Entry{"", role});
+    endInsertRows();
+}
+
 bool
 PowerlevelsTypeListModel::move(int from, int to)
 {
@@ -299,10 +317,19 @@ PowerlevelsTypeListModel::moveRows(const QModelIndex &,
     auto pl         = types.at(destinationChild > 0 ? destinationChild - 1 : 0).pl;
     auto sourceItem = types.takeAt(sourceRow);
     sourceItem.pl   = pl;
+
+    auto movedType = sourceItem.type;
+
     if (destinationChild < sourceRow)
         types.insert(destinationChild, std::move(sourceItem));
     else
         types.insert(destinationChild - 1, std::move(sourceItem));
+
+    if (movedType == "m.room.power_levels")
+        emit adminLevelChanged();
+    else if (movedType == "redact")
+        emit moderatorLevelChanged();
+
     return true;
 }
 
@@ -454,6 +481,23 @@ PowerlevelsUserListModel::add(int row, QString user)
     endInsertRows();
 }
 
+void
+PowerlevelsUserListModel::addRole(int64_t role)
+{
+    for (int i = 0; i < users.size(); i++) {
+        if (users[i].pl < role) {
+            beginInsertRows(QModelIndex(), i, i);
+            users.insert(i, Entry{"", role});
+            endInsertRows();
+            return;
+        }
+    }
+
+    beginInsertRows(QModelIndex(), users.size(), users.size());
+    users.push_back(Entry{"", role});
+    endInsertRows();
+}
+
 bool
 PowerlevelsUserListModel::move(int from, int to)
 {
@@ -492,10 +536,17 @@ PowerlevelsUserListModel::moveRows(const QModelIndex &,
     auto pl         = users.at(destinationChild > 0 ? destinationChild - 1 : 0).pl;
     auto sourceItem = users.takeAt(sourceRow);
     sourceItem.pl   = pl;
+
+    auto movedType = sourceItem.mxid;
+
     if (destinationChild < sourceRow)
         users.insert(destinationChild, std::move(sourceItem));
     else
         users.insert(destinationChild - 1, std::move(sourceItem));
+
+    if (movedType == "default")
+        emit defaultUserLevelChanged();
+
     return true;
 }
 
@@ -508,7 +559,20 @@ PowerlevelEditingModels::PowerlevelEditingModels(QString room_id, QObject *paren
   , types_(room_id.toStdString(), powerLevels_, this)
   , users_(room_id.toStdString(), powerLevels_, this)
   , room_id_(room_id.toStdString())
-{}
+{
+    connect(&types_,
+            &PowerlevelsTypeListModel::adminLevelChanged,
+            this,
+            &PowerlevelEditingModels::adminLevelChanged);
+    connect(&types_,
+            &PowerlevelsTypeListModel::moderatorLevelChanged,
+            this,
+            &PowerlevelEditingModels::moderatorLevelChanged);
+    connect(&users_,
+            &PowerlevelsUserListModel::defaultUserLevelChanged,
+            this,
+            &PowerlevelEditingModels::defaultUserLevelChanged);
+}
 
 void
 PowerlevelEditingModels::commit()
@@ -532,3 +596,14 @@ PowerlevelEditingModels::commit()
           }
       });
 }
+
+void
+PowerlevelEditingModels::addRole(int pl)
+{
+    for (const auto &e : types_.types)
+        if (pl == int(e.pl))
+            return;
+
+    types_.addRole(pl);
+    users_.addRole(pl);
+}