<commit_before>#include "RoomViewList.hpp"

#include <QMenu>
#include <QContextMenuEvent>
#include <QScrollBar>

#include "matrix/Room.hpp"

RoomViewList::RoomViewList(QWidget *parent) : QListWidget(parent), menu_(new QMenu(this)) {
  connect(this, &QListWidget::currentItemChanged, [this](QListWidgetItem *item, QListWidgetItem *previous) {
      (void)previous;
      if(item != nullptr) {
        auto &room = *reinterpret_cast<matrix::Room *>(item->data(Qt::UserRole).value<quintptr>());
        activated(room);
      }
    });

  auto move_up = menu_->addAction(tr("Move up"));
  connect(move_up, &QAction::triggered, [this]() {
      auto r = row(items_.at(context_));
      auto item = takeItem(r);
      insertItem(r > 0 ? r - 1 : 0, item);
      setCurrentItem(item);
    });
  auto move_down = menu_->addAction(tr("Move down"));
  connect(move_down, &QAction::triggered, [this]() {
      auto r = row(items_.at(context_));
      auto item = takeItem(r);
      insertItem(r+1, item);
      setCurrentItem(item);
    });
  auto pop_out_action = menu_->addAction(QIcon::fromTheme("window-open"), tr("&Pop out"));
  connect(pop_out_action, &QAction::triggered, [this]() {
      pop_out(*context_);
      release(*context_);
    });
  auto close = menu_->addAction(QIcon::fromTheme("window-close"), tr("&Close"));
  connect(close, &QAction::triggered, [this]() {
      release(*context_);
    });

  QSizePolicy policy(QSizePolicy::Preferred, QSizePolicy::Preferred);
  setSizePolicy(policy);
}

void RoomViewList::add(matrix::Room &room) {
  auto item = new QListWidgetItem;
  item->setToolTip(room.id());
  item->setText(room.pretty_name());
  item->setData(Qt::UserRole, QVariant::fromValue(reinterpret_cast<quintptr>(&room)));
  addItem(item);
  items_.emplace(&room, item);
  claimed(room);
  update();
}

void RoomViewList::release(matrix::Room &room) {
  delete items_.at(&room);
  released(room);
}

void RoomViewList::activate(matrix::Room &room) {
  auto &item = *items_.at(&room);
  scrollToItem(&item);
  setCurrentItem(&item);
  activated(room);
}

void RoomViewList::update_name(matrix::Room &room) {<commit_msg>Fix dangling pointers in RoomViewList<commit_after>#include "RoomViewList.hpp"

#include <QMenu>
#include <QContextMenuEvent>
#include <QScrollBar>

#include "matrix/Room.hpp"

RoomViewList::RoomViewList(QWidget *parent) : QListWidget(parent), menu_(new QMenu(this)) {
  connect(this, &QListWidget::currentItemChanged, [this](QListWidgetItem *item, QListWidgetItem *previous) {
      (void)previous;
      if(item != nullptr) {
        auto &room = *reinterpret_cast<matrix::Room *>(item->data(Qt::UserRole).value<quintptr>());
        activated(room);
      }
    });

  auto move_up = menu_->addAction(tr("Move up"));
  connect(move_up, &QAction::triggered, [this]() {
      auto r = row(items_.at(context_));
      auto item = takeItem(r);
      insertItem(r > 0 ? r - 1 : 0, item);
      setCurrentItem(item);
    });
  auto move_down = menu_->addAction(tr("Move down"));
  connect(move_down, &QAction::triggered, [this]() {
      auto r = row(items_.at(context_));
      auto item = takeItem(r);
      insertItem(r+1, item);
      setCurrentItem(item);
    });
  auto pop_out_action = menu_->addAction(QIcon::fromTheme("window-open"), tr("&Pop out"));
  connect(pop_out_action, &QAction::triggered, [this]() {
      pop_out(*context_);
      release(*context_);
    });
  auto close = menu_->addAction(QIcon::fromTheme("window-close"), tr("&Close"));
  connect(close, &QAction::triggered, [this]() {
      release(*context_);
    });

  QSizePolicy policy(QSizePolicy::Preferred, QSizePolicy::Preferred);
  setSizePolicy(policy);
}

void RoomViewList::add(matrix::Room &room) {
  auto item = new QListWidgetItem;
  item->setToolTip(room.id());
  item->setText(room.pretty_name());
  item->setData(Qt::UserRole, QVariant::fromValue(reinterpret_cast<quintptr>(&room)));
  addItem(item);
  items_.emplace(&room, item);
  claimed(room);
  update();
}

void RoomViewList::release(matrix::Room &room) {
  auto it = items_.find(&room);
  assert(it != items_.end());
  delete it->second;
  items_.erase(it);
  released(room);
}

void RoomViewList::activate(matrix::Room &room) {
  auto &item = *items_.at(&room);
  scrollToItem(&item);
  setCurrentItem(&item);
  activated(room);
}

void RoomViewList::update_name(matrix::Room &room) {
{'fraction_non_alphanumeric': 0.1764436296975252, 'fraction_numerical': 0.0018331805682859762, 'mean_word_length': 4.898648648648648, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6846850', 'n_tokens_mistral': 1483, 'n_tokens_neox': 1469, 'n_words': 252}