<commit_before>/******************************************************************************

  This source file is part of the tomviz project.

  Copyright Kitware, Inc.

  This source code is released under the New BSD License, (the "License").

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

******************************************************************************/
#include "RecentFilesMenu.h"

#include "ActiveObjects.h"
#include "DataSource.h"
#include "LoadDataReaction.h"
#include "SaveLoadStateReaction.h"
#include "Utilities.h"

#include <pqSettings.h>

#include <QDebug>
#include <QMenu>
#include <QMessageBox>

#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>

#include <string>

namespace tomviz {

static const int MAX_ITEMS = 10;

namespace {

QJsonObject loadSettings()
{
  // Load the recent files from our saved state.
  auto settings = pqApplicationCore::instance()->settings();
  auto recent = settings->value("recentFiles").toByteArray();
  auto doc = QJsonDocument::fromJson(recent);
  if (!doc.isNull() && doc.isObject()) {
    return doc.object();
  } else {
    QJsonObject json;
    json["readers"] = QJsonArray();
    json["molecules"] = QJsonArray();
    json["states"] = QJsonArray();
    return QJsonObject();
  }
}

void saveSettings(QJsonObject json)
{
  // Save recent file list to our saved state, prune lists to be <= 10 entries.
  QJsonArray readers;
  if (json.contains("readers") && json["readers"].isArray()) {
    readers = json["readers"].toArray();
  }
  QJsonArray states;
  if (json.contains("states") && json["states"].isArray()) {
    states = json["states"].toArray();
  }
  QJsonArray molecules;
  if (json.contains("molecules") && json["molecules"].isArray()) {
    molecules = json["molecules"].toArray();
  }
  if (readers.size() > MAX_ITEMS) {
    // We need to prune the list to 10.
    while (readers.size() > MAX_ITEMS) {
      readers.removeLast();
    }
  }
  if (states.size() > MAX_ITEMS) {
    // We need to prune the list to 10.
    while (states.size() > MAX_ITEMS) {
      states.removeLast();
    }
  }
  if (molecules.size() > MAX_ITEMS) {
    // We need to prune the list to 10.
    while (molecules.size() > MAX_ITEMS) {
      molecules.removeLast();
    }
  }
  json["readers"] = readers;
  json["states"] = states;
  json["molecules"] = molecules;
  QJsonDocument doc(json);

  auto settings = pqApplicationCore::instance()->settings();
  settings->setValue("recentFiles", doc.toJson(QJsonDocument::Compact));
}

} // namespace

RecentFilesMenu::RecentFilesMenu(QMenu& menu, QObject* p) : QObject(p)
{
  connect(&menu, SIGNAL(aboutToShow()), SLOT(aboutToShowMenu()));
}

RecentFilesMenu::~RecentFilesMenu() = default;

void RecentFilesMenu::pushDataReader(DataSource* dataSource)
{
  // Add non-proxy based readers separately.
  auto settings = loadSettings();
  auto readerList = settings["readers"].toArray();
  QJsonObject readerJson =
    QJsonObject::fromVariantMap(dataSource->readerProperties());
  auto fileNames = dataSource->fileNames();
  if (fileNames.size() < 1) {
    return;
  }

  readerJson["fileNames"] = QJsonArray::fromStringList(fileNames);

  // Remove the file if it is already in the list
  for (int i = readerList.size() - 1; i >= 0; --i) {<commit_msg>Remove duplicate store of recent files<commit_after>/******************************************************************************

  This source file is part of the tomviz project.

  Copyright Kitware, Inc.

  This source code is released under the New BSD License, (the "License").

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

******************************************************************************/
#include "RecentFilesMenu.h"

#include "ActiveObjects.h"
#include "DataSource.h"
#include "LoadDataReaction.h"
#include "SaveLoadStateReaction.h"
#include "Utilities.h"

#include <pqSettings.h>

#include <QDebug>
#include <QMenu>
#include <QMessageBox>

#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>

#include <string>

namespace tomviz {

static const int MAX_ITEMS = 10;

namespace {

QJsonObject loadSettings()
{
  // Load the recent files from our saved state.
  auto settings = pqApplicationCore::instance()->settings();
  // Remove a key that can interfere/double count.
  settings->remove("RecentlyUsedResourcesList");
  auto recent = settings->value("recentFiles").toByteArray();
  auto doc = QJsonDocument::fromJson(recent);
  if (!doc.isNull() && doc.isObject()) {
    return doc.object();
  } else {
    QJsonObject json;
    json["readers"] = QJsonArray();
    json["molecules"] = QJsonArray();
    json["states"] = QJsonArray();
    return QJsonObject();
  }
}

void saveSettings(QJsonObject json)
{
  // Save recent file list to our saved state, prune lists to be <= 10 entries.
  QJsonArray readers;
  if (json.contains("readers") && json["readers"].isArray()) {
    readers = json["readers"].toArray();
  }
  QJsonArray states;
  if (json.contains("states") && json["states"].isArray()) {
    states = json["states"].toArray();
  }
  QJsonArray molecules;
  if (json.contains("molecules") && json["molecules"].isArray()) {
    molecules = json["molecules"].toArray();
  }
  if (readers.size() > MAX_ITEMS) {
    // We need to prune the list to 10.
    while (readers.size() > MAX_ITEMS) {
      readers.removeLast();
    }
  }
  if (states.size() > MAX_ITEMS) {
    // We need to prune the list to 10.
    while (states.size() > MAX_ITEMS) {
      states.removeLast();
    }
  }
  if (molecules.size() > MAX_ITEMS) {
    // We need to prune the list to 10.
    while (molecules.size() > MAX_ITEMS) {
      molecules.removeLast();
    }
  }
  json["readers"] = readers;
  json["states"] = states;
  json["molecules"] = molecules;
  QJsonDocument doc(json);

  auto settings = pqApplicationCore::instance()->settings();
  settings->setValue("recentFiles", doc.toJson(QJsonDocument::Compact));
  // Remove a key that can interfere/double count.
  settings->remove("RecentlyUsedResourcesList");
}

} // namespace

RecentFilesMenu::RecentFilesMenu(QMenu& menu, QObject* p) : QObject(p)
{
  connect(&menu, SIGNAL(aboutToShow()), SLOT(aboutToShowMenu()));
}

RecentFilesMenu::~RecentFilesMenu() = default;

void RecentFilesMenu::pushDataReader(DataSource* dataSource)
{
  // Add non-proxy based readers separately.
  auto settings = loadSettings();
  auto readerList = settings["readers"].toArray();
  QJsonObject readerJson =
    QJsonObject::fromVariantMap(dataSource->readerProperties());
  auto fileNames = dataSource->fileNames();
  if (fileNames.size() < 1) {
    return;
  }

  readerJson["fileNames"] = QJsonArray::fromStringList(fileNames);

  // Remove the file if it is already in the list
  for (int i = readerList.size() - 1; i >= 0; --i) {
{'fraction_non_alphanumeric': 0.1747095010252905, 'fraction_numerical': 0.0035543403964456597, 'mean_word_length': 4.363636363636363, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 47, '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': '6171083', 'n_tokens_mistral': 2254, 'n_tokens_neox': 2159, 'n_words': 609}