aboutsummaryrefslogtreecommitdiff
path: root/libs/installer_bsplugins/src/BSPluginList/PluginsWidget.cpp
blob: a73725f0c4d78d161f7126a9ebd2186a1b5afbfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
#include "PluginsWidget.h"

#include "BSPluginInfo/PluginInfoDialog.h"
#include "GUI/MessageDialog.h"
#include "GUI/SelectionDialog.h"
#include "MOPlugin/Settings.h"
#include "MOTools/Loot.h"
#include "PluginListContextMenu.h"
#include "PluginSortFilterProxyModel.h"
#include "ui_pluginswidget.h"

#include <game_features/gameplugins.h>
#include <game_features/igamefeatures.h>

#include <boost/range/adaptor/reversed.hpp>

#include <QApplication>
#include <QCryptographicHash>
#include <QMenu>
#include <QMessageBox>
#include <QStandardPaths>

using namespace Qt::Literals::StringLiterals;

namespace BSPluginList
{

PluginsWidget::PluginsWidget(MOBase::IOrganizer* organizer,
                             IPanelInterface* panelInterface, QWidget* parent)
    : QWidget(parent), ui{new Ui_PluginsWidget()}, m_PanelInterface{panelInterface},
      m_Organizer{organizer}
{
  ui->setupUi(this);

  m_PluginList      = new TESData::PluginList(organizer);
  m_PluginListModel = new PluginListModel(m_PluginList);
  m_SortProxy       = new PluginSortFilterProxyModel();
  m_SortProxy->setSourceModel(m_PluginListModel);
  m_GroupProxy = new PluginGroupProxyModel(organizer);
  m_GroupProxy->setSourceModel(m_SortProxy);
  ui->pluginList->setModel(m_GroupProxy);
  ui->pluginList->setup();
  ui->pluginList->sortByColumn(PluginListModel::COL_PRIORITY, Qt::AscendingOrder);
  ui->pluginList->expandAll();
  optionsMenu = listOptionsMenu();
  ui->listOptionsBtn->setMenu(optionsMenu);

  // LOOT integration stripped for Fluorine port — sort button is useless.
  ui->sortButton->setVisible(false);

  // monitor main window for close event
  topLevelWidget()->installEventFilter(this);

  restoreState();

  connect(m_PluginList, &TESData::PluginList::pluginsListChanged, this,
          &PluginsWidget::updatePluginCount);

  connect(m_PluginListModel, &PluginListModel::pluginStatesChanged, ui->pluginList,
          &PluginListView::updateOverwriteMarkers);
  connect(m_PluginListModel, &PluginListModel::pluginOrderChanged, ui->pluginList,
          &PluginListView::updateOverwriteMarkers);
  connect(m_PluginListModel, &QAbstractItemModel::modelReset, ui->pluginList,
          &PluginListView::clearOverwriteMarkers);

  connect(m_PluginListModel, &PluginListModel::pluginStatesChanged, this,
          &PluginsWidget::updatePluginCount);

  connect(m_GroupProxy, &QAbstractItemModel::modelReset, [this]() {
    ui->pluginList->scrollToTop();
  });

  connect(ui->pluginList, &QTreeView::collapsed, this,
          &PluginsWidget::onGroupCollapsed);
  connect(ui->pluginList, &QTreeView::expanded, this, &PluginsWidget::onGroupExpanded);
  connect(ui->pluginList->selectionModel(), &QItemSelectionModel::selectionChanged,
          this, &PluginsWidget::onSelectionChanged);

  panelInterface->onPanelActivated(
      std::bind_front(&PluginsWidget::onPanelActivated, this));
  panelInterface->onSelectedOriginsChanged(
      std::bind_front(&PluginsWidget::onSelectedOriginsChanged, this));

  organizer->onAboutToRun(std::bind_front(&PluginsWidget::onAboutToRun, this));
  organizer->onFinishedRun(std::bind_front(&PluginsWidget::onFinishedRun, this));

  organizer->modList()->onModStateChanged(
      std::bind_front(&PluginsWidget::onModStateChanged, this));

  Settings::instance()->onSettingChanged(
      std::bind_front(&PluginsWidget::onSettingChanged, this));

  synchronizePluginLists(organizer);
  updatePluginCount();
}

PluginsWidget::~PluginsWidget() noexcept
{
  delete ui;
  delete optionsMenu;

  delete m_PluginList;
  delete m_PluginListModel;
  delete m_SortProxy;
  delete m_GroupProxy;
}

void PluginsWidget::updatePluginCount()
{
  int activeMasterCount       = 0;
  int activeMediumMasterCount = 0;
  int activeLightMasterCount  = 0;
  int activeRegularCount      = 0;
  int masterCount             = 0;
  int mediumMasterCount       = 0;
  int lightMasterCount        = 0;
  int regularCount            = 0;
  int activeVisibleCount      = 0;

  const auto gameFeatures = m_Organizer->gameFeatures();
  const auto tesSupport =
      gameFeatures ? gameFeatures->gameFeature<MOBase::GamePlugins>() : nullptr;

  const bool lightPluginsAreSupported =
      tesSupport && tesSupport->lightPluginsAreSupported();
  const bool mediumPluginsAreSupported =
      tesSupport && tesSupport->mediumPluginsAreSupported();

  for (int i = 0, count = m_PluginListModel->rowCount(); i < count; ++i) {
    const auto index = m_PluginListModel->index(i, 0);
    const auto id    = index.data(PluginListModel::IndexRole).toInt();
    const auto info  = m_PluginList->getPlugin(id);

    if (!info)
      continue;

    const bool active  = info->enabled() || info->isAlwaysEnabled();
    const bool visible = m_SortProxy->filterAcceptsRow(index.row(), index.parent());
    if (info->isMediumFile()) {
      ++mediumMasterCount;
      activeMediumMasterCount += active ? 1 : 0;
      activeVisibleCount += visible && active ? 1 : 0;
    } else if (info->isSmallFile()) {
      ++lightMasterCount;
      activeLightMasterCount += active ? 1 : 0;
      activeVisibleCount += visible && active ? 1 : 0;
    } else if (info->isMasterFile()) {
      ++masterCount;
      activeMasterCount += active ? 1 : 0;
      activeVisibleCount += visible && active ? 1 : 0;
    } else {
      ++regularCount;
      activeRegularCount += active ? 1 : 0;
      activeVisibleCount += visible && active ? 1 : 0;
    }
  }

  const int activeCount = activeMasterCount + activeMediumMasterCount +
                          activeLightMasterCount + activeRegularCount;
  const int totalCount =
      masterCount + mediumMasterCount + lightMasterCount + regularCount;

  ui->activePluginsCounter->display(activeVisibleCount);

  QString toolTip;
  toolTip.reserve(575);
  toolTip += uR"(<table cellspacing="6">)"_s
             uR"(<tr><th>%1</th><th>%2</th><th>%3</th></tr>)"_s.arg(tr("Type"))
                 .arg(tr("Active"), -12)
                 .arg(tr("Total"));

  const QString row = uR"(<tr><td>%1:</td><td align=right>%2    </td>)"_s
                      uR"(<td align=right>%3</td></tr>)"_s;

  toolTip += row.arg(tr("All plugins")).arg(activeCount).arg(totalCount);
  toolTip += row.arg(tr("ESMs")).arg(activeMasterCount).arg(masterCount);
  toolTip += row.arg(tr("ESPs")).arg(activeRegularCount).arg(regularCount);
  toolTip += row.arg(tr("ESMs+ESPs"))
                 .arg(activeMasterCount + activeRegularCount)
                 .arg(masterCount + regularCount);
  if (mediumPluginsAreSupported)
    toolTip += row.arg(tr("ESHs")).arg(activeMediumMasterCount).arg(mediumMasterCount);
  if (lightPluginsAreSupported)
    toolTip += row.arg(tr("ESLs")).arg(activeLightMasterCount).arg(lightMasterCount);
  toolTip += uR"(</table>)"_s;

  ui->activePluginsCounter->setToolTip(toolTip);
}

void PluginsWidget::on_espFilterEdit_textChanged(const QString& filter)
{
  m_SortProxy->updateFilter(filter);

  if (!filter.isEmpty()) {
    setStyleSheet("QTreeView { border: 2px ridge #f00; }");
    ui->activePluginsCounter->setStyleSheet("QLCDNumber { border: 2px ridge #f00; }");
  } else {
    setStyleSheet("");
    ui->activePluginsCounter->setStyleSheet("");
  }
  updatePluginCount();
}

bool PluginsWidget::eventFilter(QObject* watched, QEvent* event)
{
  if (event->type() == QEvent::Close) {
    saveState();
    m_PluginList->writePluginLists();
  }

  return QWidget::eventFilter(watched, event);
}

void PluginsWidget::changeEvent(QEvent* event)
{
  QWidget::changeEvent(event);
  switch (event->type()) {
  case QEvent::LanguageChange:
    ui->retranslateUi(this);
    break;
  default:
    break;
  }
}

void PluginsWidget::onGroupCollapsed(const QModelIndex& index)
{
  if (ui->pluginList->selectionModel()->isSelected(index)) {
    onSelectionChanged();
  }
}

void PluginsWidget::onGroupExpanded(const QModelIndex& index)
{
  if (ui->pluginList->selectionModel()->isSelected(index)) {
    onSelectionChanged();
  }
}

void PluginsWidget::onSelectionChanged()
{
  QList<QString> selectedFiles;
  std::function<void(const QModelIndex&)> addFiles;
  addFiles = [&](const QModelIndex& index) {
    if (index.model()->hasChildren(index)) {
      if (ui->pluginList->isExpanded(index)) {
        return;
      }

      for (int i = 0, count = index.model()->rowCount(index); i < count; ++i) {
        addFiles(index.model()->index(i, 0, index));
      }
    } else {
      if (const auto info =
              index.data(PluginListModel::InfoRole).value<const TESData::FileInfo*>()) {
        selectedFiles.append(info->name());
      }
    }
  };

  for (const auto& index : ui->pluginList->selectionModel()->selectedRows()) {
    addFiles(index);
  }

  m_PanelInterface->setSelectedFiles(selectedFiles);
}

void PluginsWidget::onPanelActivated()
{
  m_PluginListModel->refresh();
}

void PluginsWidget::onSelectedOriginsChanged(const QList<QString>& origins)
{
  ui->pluginList->setHighlightedOrigins(origins);
}

void PluginsWidget::toggleHideForceEnabled()
{
  const bool doHide = toggleForceEnabled->isChecked();
  m_SortProxy->hideForceEnabledFiles(doHide);
  updatePluginCount();

  Settings::instance()->set("hide_force_enabled", doHide);
}

void PluginsWidget::toggleIgnoreMasterConflicts()
{
  const bool doIgnore = toggleIgnoreMasters->isChecked();
  Settings::instance()->set("ignore_master_conflicts", doIgnore);

  m_PluginListModel->invalidateConflicts();
}

constexpr auto PATTERN_BACKUP_GLOB  = R"/(.????_??_??_??_??_??)/";
constexpr auto PATTERN_BACKUP_REGEX = R"/(\.(\d\d\d\d_\d\d_\d\d_\d\d_\d\d_\d\d))/";
constexpr auto PATTERN_BACKUP_DATE  = R"/(yyyy_MM_dd_hh_mm_ss)/";

static QString queryRestore(const QString& filePath, QWidget* parent = nullptr)
{
  QFileInfo pluginFileInfo(filePath);
  QString pattern     = pluginFileInfo.fileName() + ".*";
  QFileInfoList files = pluginFileInfo.absoluteDir().entryInfoList(
      QStringList(pattern), QDir::Files, QDir::Name);

  GUI::SelectionDialog dialog(QObject::tr("Choose backup to restore"), parent);
  QRegularExpression exp(QRegularExpression::anchoredPattern(pluginFileInfo.fileName() +
                                                             PATTERN_BACKUP_REGEX));
  QRegularExpression exp2(
      QRegularExpression::anchoredPattern(pluginFileInfo.fileName() + "\\.(.*)"));
  for (const QFileInfo& info : boost::adaptors::reverse(files)) {
    auto match  = exp.match(info.fileName());
    auto match2 = exp2.match(info.fileName());
    if (match.hasMatch()) {
      QDateTime time = QDateTime::fromString(match.captured(1), PATTERN_BACKUP_DATE);
      dialog.addChoice(time.toString(), "", match.captured(1));
    } else if (match2.hasMatch()) {
      dialog.addChoice(match2.captured(1), "", match2.captured(1));
    }
  }

  if (dialog.numChoices() == 0) {
    QMessageBox::information(parent, QObject::tr("No Backups"),
                             QObject::tr("There are no backups to restore"));
    return QString();
  }

  if (dialog.exec() == QDialog::Accepted) {
    return dialog.getChoiceData().toString();
  } else {
    return QString();
  }
}

void PluginsWidget::displayPluginInformation(const QModelIndex& index)
{
  const int id        = index.data(PluginListModel::IndexRole).toInt();
  const auto fileName = m_PluginList->getPlugin(id)->name();
  const auto parent   = topLevelWidget();
  BSPluginInfo::PluginInfoDialog dialog{m_Organizer, m_PluginList, fileName, parent};
  dialog.exec();

  const bool ignoreMasters =
      Settings::instance()->get<bool>("ignore_master_conflicts", false);
  toggleIgnoreMasters->setChecked(ignoreMasters);
  m_PluginListModel->invalidateConflicts();
}

void PluginsWidget::on_pluginList_customContextMenuRequested(const QPoint& pos)
{
  PluginListContextMenu menu{ui->pluginList->indexAt(pos), m_PluginListModel,
                             ui->pluginList, m_Organizer->modList(), m_PluginList};

  connect(&menu, &PluginListContextMenu::openModInformation,
          [this](const QModelIndex& index) {
            const int id        = index.data(PluginListModel::IndexRole).toInt();
            const auto fileName = m_PluginList->getPlugin(id)->name();
            m_PanelInterface->displayOriginInformation(fileName);
          });

  connect(&menu, &PluginListContextMenu::openPluginInformation, this,
          &PluginsWidget::displayPluginInformation);

  const QPoint p = ui->pluginList->viewport()->mapToGlobal(pos);
  menu.exec(p);
}

void PluginsWidget::on_pluginList_doubleClicked(const QModelIndex& index)
{
  bool ok;
  const int id = index.data(PluginListModel::IndexRole).toInt(&ok);
  if (ok) {
    Qt::KeyboardModifiers modifiers = QApplication::queryKeyboardModifiers();
    if (modifiers.testFlag(Qt::ControlModifier)) {
      const auto origin  = m_PluginList->getOriginName(id);
      const auto modInfo = m_Organizer->modList()->getMod(origin);

      if (modInfo) {
        MOBase::shell::Explore(modInfo->absolutePath());
      }
    } else {
      displayPluginInformation(index);
    }
  } else if (ui->pluginList->model()->hasChildren(index)) {
    ui->pluginList->setExpanded(index, !ui->pluginList->isExpanded(index));
  }
}

void PluginsWidget::on_pluginList_openOriginExplorer(const QModelIndex& index)
{
  const int id       = index.data(PluginListModel::IndexRole).toInt();
  const auto origin  = m_PluginList->getOriginName(id);
  const auto modInfo = m_Organizer->modList()->getMod(origin);

  if (modInfo == nullptr) {
    return;
  }

  MOBase::shell::Explore(modInfo->absolutePath());
}

void PluginsWidget::on_sortButton_clicked()
{
  // LOOT integration stripped for Fluorine port.
  QMessageBox::information(
      topLevelWidget(), tr("Sorting plugins"),
      tr("LOOT integration is not available in this build."));
}

static bool tryRestore(const QString& filePath, const QString& identifier,
                       bool required, QWidget* parent = nullptr)
{
  const auto backupName = filePath + "." + identifier;
  if (required || QFileInfo::exists(backupName)) {
    return MOBase::shellCopy(backupName, filePath, true, parent);
  } else {
    return !QFileInfo::exists(filePath) || MOBase::shellDeleteQuiet(filePath, parent);
  }
}

void PluginsWidget::on_restoreButton_clicked()
{
  const auto app         = this->topLevelWidget();
  const auto profilePath = QDir(m_Organizer->profilePath());
  const auto pluginsName = QDir::cleanPath(profilePath.absoluteFilePath("plugins.txt"));

  QString choice = queryRestore(pluginsName, app);
  if (!choice.isEmpty()) {
    const auto groupsName =
        QDir::cleanPath(profilePath.absoluteFilePath("plugingroups.txt"));
    const auto loadOrderName =
        QDir::cleanPath(profilePath.absoluteFilePath("loadorder.txt"));

    if (!tryRestore(pluginsName, choice, true, app) ||
        !tryRestore(loadOrderName, choice, true, app) ||
        !tryRestore(groupsName, choice, false, app)) {
      const auto e = ::GetLastError();

      QMessageBox::critical(
          this, tr("Restore failed"),
          tr("Failed to restore the backup. Errorcode: %1")
              .arg(QString::fromStdWString(MOBase::formatSystemMessage(e))));
    }
    m_PluginListModel->invalidate();
  }
}

static bool createBackup(const QString& filePath, const QString& identifier,
                         QWidget* parent = nullptr)
{
  QString outPath = filePath + "." + identifier;
  if (MOBase::shellCopy(QStringList(filePath), QStringList(outPath), parent)) {
    QFileInfo fileInfo(filePath);
    MOBase::removeOldFiles(fileInfo.absolutePath(),
                           fileInfo.fileName() + PATTERN_BACKUP_GLOB, 10, QDir::Name);
    return true;
  } else {
    return false;
  }
}

static bool createBackup(const QString& filePath, const QDateTime& time,
                         QWidget* parent = nullptr)
{
  return createBackup(filePath, time.toString(PATTERN_BACKUP_DATE), parent);
}

void PluginsWidget::on_saveButton_clicked()
{
  m_PluginList->writePluginLists();

  const auto app         = this->topLevelWidget();
  const auto profilePath = QDir(m_Organizer->profilePath());
  const auto pluginsName = QDir::cleanPath(profilePath.absoluteFilePath("plugins.txt"));
  const auto groupsName =
      QDir::cleanPath(profilePath.absoluteFilePath("plugingroups.txt"));
  const auto loadOrderName =
      QDir::cleanPath(profilePath.absoluteFilePath("loadorder.txt"));
  const auto lockedOrderName =
      QDir::cleanPath(profilePath.absoluteFilePath("lockedorder.txt"));

  const QDateTime now = QDateTime::currentDateTime();

  if (createBackup(pluginsName, now, app) && createBackup(loadOrderName, now, app) &&
      createBackup(groupsName, now, app) && createBackup(lockedOrderName, now, app)) {
    GUI::MessageDialog::showMessage(tr("Backup of load order created"), app);
  }
}

QMenu* PluginsWidget::listOptionsMenu()
{
  QMenu* const menu  = new QMenu(this);
  toggleForceEnabled = menu->addAction(tr("Hide force-enabled files"), this,
                                       &PluginsWidget::toggleHideForceEnabled);
  toggleForceEnabled->setCheckable(true);

  toggleIgnoreMasters = menu->addAction(tr("Ignore conflicts with masters"), this,
                                        &PluginsWidget::toggleIgnoreMasterConflicts);
  toggleIgnoreMasters->setCheckable(true);

  menu->addSeparator();

  menu->addAction(tr("Collapse all"), [this]() {
    ui->pluginList->collapseAll();
    ui->pluginList->scrollToTop();
  });
  menu->addAction(tr("Expand all"), [this]() {
    ui->pluginList->expandAll();
    ui->pluginList->scrollToTop();
  });

  menu->addSeparator();

  menu->addAction(tr("Enable all"), [this]() {
    if (QMessageBox::question(topLevelWidget(), tr("Confirm"),
                              tr("Really enable all plugins?"),
                              QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
      m_PluginListModel->setEnabledAll(true);
    }
  });
  menu->addAction(tr("Disable all"), [this]() {
    if (QMessageBox::question(topLevelWidget(), tr("Confirm"),
                              tr("Really disable all plugins?"),
                              QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
      m_PluginListModel->setEnabledAll(false);
    }
  });

  return menu;
}

void PluginsWidget::saveState()
{
  auto* const settings = Settings::instance();
  settings->saveState(ui->pluginList->header());
  // Fluorine port: saveTreeExpandState recurses the model, which on window
  // close is being torn down by the time the close event fires → SIGSEGV
  // inside the visitRows traversal. Column widths above still save fine.
  // Tree expansion state is a minor UX loss; not worth the crash.
}

void PluginsWidget::restoreState()
{
  const auto* const settings = Settings::instance();
  settings->restoreState(ui->pluginList->header());
  settings->restoreTreeExpandState(ui->pluginList);

  const bool doHide = settings->get<bool>("hide_force_enabled", false);
  toggleForceEnabled->setChecked(doHide);
  toggleHideForceEnabled();

  const bool doIgnore = settings->get<bool>("ignore_master_conflicts", false);
  toggleIgnoreMasters->setChecked(doIgnore);
  toggleIgnoreMasterConflicts();
}

static bool containsPlugin(const MOBase::IModInterface* mod, const MOBase::IPluginGame* game)
{
  const auto fileTree = mod ? mod->fileTree() : nullptr;
  if (!fileTree)
    return false;
  std::shared_ptr<const MOBase::IFileTree> searchDir;
  if (!game->modDataDirectory().isEmpty()) {
    searchDir = fileTree->findDirectory(game->modDataDirectory());
  } else {
    searchDir = fileTree;
  }
  if (searchDir) {
    return std::ranges::any_of(*searchDir, [&](auto&& entry) {
      if (!entry)
        return false;
      const QString filename = entry->name();
      return filename.endsWith(u".esp"_s, Qt::CaseInsensitive) ||
             filename.endsWith(u".esm"_s, Qt::CaseInsensitive) ||
             filename.endsWith(u".esl"_s, Qt::CaseInsensitive);
    });
  }
  return false;
}

void PluginsWidget::onModStateChanged(
    const std::map<QString, MOBase::IModList::ModStates>& mods)
{
  // HACK: the virtual file tree won't update until the next refresh, so keep track of
  // any mods that might be newly activated
  const auto modList = m_Organizer->modList();
  if (!modList)
    return;

  for (const auto& [modName, modState] : mods) {
    const auto mod = modList->getMod(modName);
    if (containsPlugin(mod, m_Organizer->managedGame())) {
      m_PluginList->notifyPendingState(modName, modState);
    }
  }
  m_PluginListModel->refresh();
}

bool PluginsWidget::onAboutToRun([[maybe_unused]] const QString& binary)
{
  m_PluginList->writePluginLists();

  const auto profilePath = QDir(m_Organizer->profilePath());
  const auto pluginsName = QDir::cleanPath(profilePath.absoluteFilePath("plugins.txt"));
  const auto loadOrderName =
      QDir::cleanPath(profilePath.absoluteFilePath("loadorder.txt"));
  const auto parent = this->topLevelWidget();

  if (QFileInfo::exists(pluginsName + ".snapshot")) {
    MOBase::shellDeleteQuiet(pluginsName + ".snapshot", parent);
  }

  if (QFileInfo::exists(loadOrderName + ".snapshot")) {
    MOBase::shellDeleteQuiet(loadOrderName + ".snapshot", parent);
  }

  if (QFileInfo(binary).fileName().compare("lootcli.exe") != 0) {
    createBackup(pluginsName, "snapshot", parent);
    createBackup(loadOrderName, "snapshot", parent);
    m_IsRunningApp = true;
  }

  return true;
}

void PluginsWidget::onFinishedRun(const QString& binary,
                                  [[maybe_unused]] unsigned int exitCode)
{
  const auto binaryName = QFileInfo(binary).fileName();
  if (binaryName.compare("lootcli.exe", Qt::CaseInsensitive) == 0) {
    return;
  }

  // queue up behind the vanilla callbacks which might not have run yet, so we can react
  // after loadorder.txt changes
  m_Organizer->onNextRefresh([=, this]() {
    m_PluginList->refresh();
    checkLoadOrderChanged(binaryName);
    m_IsRunningApp          = false;
    m_ExternalStatesChanged = false;
  });
}

void PluginsWidget::onSettingChanged(const QString& key,
                                     [[maybe_unused]] const QVariant& oldValue,
                                     const QVariant& newValue)
{
  if (key == u"enable_sort_button"_s) {
    // Sort button permanently hidden — no LOOT integration.
    ui->sortButton->setVisible(false);
  }
}

static QByteArray hashFile(const QString& filePath)
{
  QCryptographicHash hash{QCryptographicHash::Sha1};
  QFile file{filePath};
  if (file.open(QIODevice::ReadOnly)) {
    hash.addData(file.readAll());
  } else {
    return ""_ba;
  }

  return hash.result();
}

// Return a normalized signature of a plugins.txt / loadorder.txt so the load-
// order-changed check can tell a cosmetic rewrite (case fix, line-ending flip,
// trailing newline, BOM, stray blank line) apart from an actual reorder or add/
// remove.  On a case-sensitive filesystem MO2 often rewrites loadorder.txt
// during post-run refresh to match the on-disk case of each plugin, which
// would otherwise fire the warning on every launch.
static QStringList normalizedPluginList(const QString& filePath)
{
  QFile file{filePath};
  if (!file.open(QIODevice::ReadOnly)) {
    return {};
  }

  QStringList out;
  const QByteArray raw = file.readAll();
  for (const QByteArray& rawLine : raw.split('\n')) {
    QString line = QString::fromUtf8(rawLine).trimmed();
    if (line.isEmpty() || line.startsWith('#')) {
      continue;
    }
    // plugins.txt may prefix active entries with '*' — strip it so a re-sort
    // that flips active state order isn't treated as a load-order edit here.
    if (line.startsWith('*')) {
      line = line.mid(1);
    }
    out.append(line.toLower());
  }
  return out;
}

void PluginsWidget::checkLoadOrderChanged(const QString& binaryName)
{
  const auto profilePath = QDir(m_Organizer->profilePath());
  const auto pluginsName = QDir::cleanPath(profilePath.absoluteFilePath("plugins.txt"));
  const auto loadOrderName =
      QDir::cleanPath(profilePath.absoluteFilePath("loadorder.txt"));
  const auto parent = this->topLevelWidget();

  const auto pluginsSnapshot   = pluginsName + ".snapshot";
  const auto loadOrderSnapshot = loadOrderName + ".snapshot";

  const auto pluginsFile   = QFileInfo(pluginsName);
  const auto loadOrderFile = QFileInfo(loadOrderName);

  if (!QFileInfo(loadOrderSnapshot).exists())
    return;

  const bool enableWarning = Settings::instance()->externalChangeWarning();

  // Compare normalized plugin name lists instead of raw byte hashes: MO2's
  // post-run refresh rewrites loadorder.txt with filesystem-correct case,
  // which flips the sha1 even when the actual order/contents didn't change.
  const bool loadOrderChanged =
      normalizedPluginList(loadOrderName) != normalizedPluginList(loadOrderSnapshot);
  const bool pluginsChanged =
      normalizedPluginList(pluginsName) != normalizedPluginList(pluginsSnapshot);

  // we just refreshed and rewrote loadorder.txt if plugins.txt changed
  if (enableWarning &&
      (m_ExternalStatesChanged || loadOrderChanged || pluginsChanged)) {

    if (false) {
      // LOOT integration removed for Fluorine port.
    } else {
      const auto response = QMessageBox::warning(
          parent, tr("Load order changed"),
          tr("Load order was changed while running %1. Keep changes?").arg(binaryName),
          QMessageBox::Yes | QMessageBox::No);

      if (response == QMessageBox::No) {
        if (!tryRestore(pluginsName, "snapshot", true, parent) ||
            !tryRestore(loadOrderName, "snapshot", true, parent)) {
          const auto e = ::GetLastError();

          QMessageBox::critical(
              this, tr("Restore failed"),
              tr("Failed to restore the backup. Errorcode: %1")
                  .arg(QString::fromStdWString(MOBase::formatSystemMessage(e))));

          return;
        }
      }
    }
  }

  MOBase::shellDeleteQuiet(pluginsSnapshot, parent);
  MOBase::shellDeleteQuiet(loadOrderSnapshot, parent);
  m_PluginListModel->invalidate();
}

void PluginsWidget::importLootGroups()
{
  // LOOT integration removed for Fluorine port.
}

void PluginsWidget::synchronizePluginLists(MOBase::IOrganizer* organizer)
{
  MOBase::IPluginList* const ipluginlist = organizer->pluginList();
  if (ipluginlist == nullptr || ipluginlist == m_PluginList) {
    return;
  }

  std::function<void()> startRefresh = [this] {
    m_OrganizerRefreshing = true;
    m_PluginList->flushPendingStates();
    // if we just finished running an application, we want the vanilla plugin list to
    // finish reading and rewriting the load order files so that we don't end up
    // ignoring the change
    if (!m_IsRunningApp) {
      // Fluorine port: non-invalidating refresh. Upstream uses invalidate()
      // which re-parses every plugin through the TES reader on each organizer
      // refresh — over FUSE VFS with 100+ plugins that freezes the GUI for
      // seconds on every mod enable/disable. Incremental refresh only parses
      // new plugins; cached ones stay.
      m_PluginListModel->refresh();
    }
  };

  organizer->onNextRefresh(startRefresh, false);

  ipluginlist->onRefreshed([this, organizer, startRefresh]() {
    if (m_OrganizerRefreshing) {
      m_OrganizerRefreshing = false;
      organizer->onNextRefresh(startRefresh, false);
    }
  });

  ipluginlist->onPluginMoved(
      [this](const QString& name, int oldPriority, int newPriority) {
        if (m_OrganizerRefreshing)
          return;
        m_PluginListModel->movePlugin(name, oldPriority, newPriority);
      });

  ipluginlist->onPluginStateChanged(
      [this](const std::map<QString, MOBase::IPluginList::PluginStates>& infos) {
        if (m_OrganizerRefreshing)
          return;
        m_PluginListModel->changePluginStates(infos);
      });

  m_PluginList->onPluginMoved([=, this](const QString& name,
                                        [[maybe_unused]] int oldPriority,
                                        int newPriority) {
    if (m_PluginList->isRefreshing())
      return;

    ipluginlist->setPriority(name, newPriority);
  });

  m_PluginList->onPluginStateChanged(
      [=, this](const std::map<QString, MOBase::IPluginList::PluginStates>& infos) {
        if (m_IsRunningApp) {
          m_ExternalStatesChanged = true;
        }

        if (m_PluginList->isRefreshing() || infos.empty())
          return;

        for (const auto& [name, state] : infos) {
          m_PanelInterface->setPluginState(name,
                                           state == MOBase::IPluginList::STATE_ACTIVE);
        }
      });
}

}  // namespace BSPluginList