<commit_before>
/*
 * Copyright (c) 2003-2011 <NAME> <<EMAIL>>.
 * All rights reserved. Use of the code is allowed under the
 * Artistic License 2.0 terms, as specified in the LICENSE file
 * distributed with this code, or available from
 * http://www.opensource.org/licenses/artistic-license-2.0.php
 */

/** \file guiinfo.cpp
* 
*/

#include <wx/wxprec.h>

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#include "./guiinfo.h"
#include "./passwordsafeframe.h"
#include "./PWStree.h"
#include "./PWSgrid.h"
#include <functional>

void GUIInfo::Save(PasswordSafeFrame* frame)
{
  SaveTreeViewInfo(frame->m_tree);
  SaveGridViewInfo(frame->m_grid);
}

void GUIInfo::Restore(PasswordSafeFrame* frame)
{
  RestoreTreeViewInfo(frame->m_tree);
  RestoreGridViewInfo(frame->m_grid);
}

void GUIInfo::SaveTreeViewInfo(PWSTreeCtrl* tree)
{
  //save the first visible item
  wxTreeItemId treeItem = tree->GetFirstVisibleItem();
  if (treeItem.IsOk()) {
    CItemData* item = tree->GetItem(treeItem);
    if (item) {
      m_treeTop = item->GetUUID();
    }
    else if (tree->ItemHasChildren(treeItem)) {
      m_treeTop = tree->GetItemText(treeItem);
    }
    else {
      m_treeTop.Clear();
      wxFAIL_MSG(wxString(wxT("Tree item \'")) << tree->GetItemText(treeItem) << wxT("\' found with no children and no CItemData"));
    }
  }
  else {
    m_treeTop.Clear();
  }
  
  m_expanded.Empty();

  //find out all the expanded groups in a depth-first manner
  wxTreeItemIdValue dummy;
  const wxTreeItemId root = tree->GetRootItem();
  if (root.IsOk()) {
    for ( wxTreeItemId id = tree->GetFirstChild(root, dummy); id.IsOk(); ) {
      if (tree->ItemHasChildren(id) && tree->IsExpanded(id)) {
        m_expanded.Add(tree->GetItemText(id));
        id = tree->GetFirstChild(id, dummy);
      }
      else {
        wxTreeItemId parent = tree->GetItemParent(id);
        for (id = tree->GetNextSibling(id); !id.IsOk() && parent != root; 
                   id = tree->GetNextSibling(parent), parent = tree->GetItemParent(parent))
        {
        }
      }
    }
  }

  //save the selected item
  wxTreeItemId selection = tree->GetSelection();
  if (selection.IsOk() && selection != tree->GetRootItem()) {
    if(tree->HasChildren(selection)) {
      m_treeSelection = tree->GetItemText(selection);
      const wxString selectionStr = m_treeSelection;
      wxASSERT(!selectionStr.IsEmpty());
    }
    else {
      CItemData* item = tree->GetItem(selection);
      if (item) {
        m_treeSelection = item->GetUUID();
      }
      else {
        m_treeSelection.Clear();
        wxFAIL_MSG(wxString(wxT("tree item \'")) << tree->GetItemText(selection) << wxT("\' found with no CItemData attached"));
      }
    }
  }
  else {
    m_treeSelection.Clear();
  }
}

void GUIInfo::SaveGridViewInfo(PWSGrid* grid)
{
  const int row = grid->YToRow(0);
  if (row != wxNOT_FOUND) {
    CItemData* item = grid->GetItem(row);
    if (item) {
      m_gridTop = item->GetUUID();
    }
    else {
      wxFAIL_MSG(wxString(wxT("Top grid row ")) << row << wxT(" has no CItemData attached"));
      m_gridTop = pws_os::CUUID::NullUUID();
    }
  }
  else {
    m_gridTop = pws_os::CUUID::NullUUID();
  }

  const int selection = grid->GetGridCursorRow();
  if (selection != wxNOT_FOUND) {
    CItemData* item = grid->GetItem(selection);
    if (item) {
      m_gridSelection = item->GetUUID();
    }
    else {
      wxFAIL_MSG(wxString(wxT("Selected grid row ")) << selection << wxT(" has no CItemData attached"));
      m_gridSelection = pws_os::CUUID::NullUUID();
    }<commit_msg>Avoid a spurious assert by checking if the grid has anything to save before querying it for db entries.<commit_after>
/*
 * Copyright (c) 2003-2011 <NAME> <<EMAIL>>.
 * All rights reserved. Use of the code is allowed under the
 * Artistic License 2.0 terms, as specified in the LICENSE file
 * distributed with this code, or available from
 * http://www.opensource.org/licenses/artistic-license-2.0.php
 */

/** \file guiinfo.cpp
* 
*/

#include <wx/wxprec.h>

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#include "./guiinfo.h"
#include "./passwordsafeframe.h"
#include "./PWStree.h"
#include "./PWSgrid.h"
#include <functional>

void GUIInfo::Save(PasswordSafeFrame* frame)
{
  SaveTreeViewInfo(frame->m_tree);
  SaveGridViewInfo(frame->m_grid);
}

void GUIInfo::Restore(PasswordSafeFrame* frame)
{
  RestoreTreeViewInfo(frame->m_tree);
  RestoreGridViewInfo(frame->m_grid);
}

void GUIInfo::SaveTreeViewInfo(PWSTreeCtrl* tree)
{
  //save the first visible item
  wxTreeItemId treeItem = tree->GetFirstVisibleItem();
  if (treeItem.IsOk()) {
    CItemData* item = tree->GetItem(treeItem);
    if (item) {
      m_treeTop = item->GetUUID();
    }
    else if (tree->ItemHasChildren(treeItem)) {
      m_treeTop = tree->GetItemText(treeItem);
    }
    else {
      m_treeTop.Clear();
      wxFAIL_MSG(wxString(wxT("Tree item \'")) << tree->GetItemText(treeItem) << wxT("\' found with no children and no CItemData"));
    }
  }
  else {
    m_treeTop.Clear();
  }
  
  m_expanded.Empty();

  //find out all the expanded groups in a depth-first manner
  wxTreeItemIdValue dummy;
  const wxTreeItemId root = tree->GetRootItem();
  if (root.IsOk()) {
    for ( wxTreeItemId id = tree->GetFirstChild(root, dummy); id.IsOk(); ) {
      if (tree->ItemHasChildren(id) && tree->IsExpanded(id)) {
        m_expanded.Add(tree->GetItemText(id));
        id = tree->GetFirstChild(id, dummy);
      }
      else {
        wxTreeItemId parent = tree->GetItemParent(id);
        for (id = tree->GetNextSibling(id); !id.IsOk() && parent != root; 
                   id = tree->GetNextSibling(parent), parent = tree->GetItemParent(parent))
        {
        }
      }
    }
  }

  //save the selected item
  wxTreeItemId selection = tree->GetSelection();
  if (selection.IsOk() && selection != tree->GetRootItem()) {
    if(tree->HasChildren(selection)) {
      m_treeSelection = tree->GetItemText(selection);
      const wxString selectionStr = m_treeSelection;
      wxASSERT(!selectionStr.IsEmpty());
    }
    else {
      CItemData* item = tree->GetItem(selection);
      if (item) {
        m_treeSelection = item->GetUUID();
      }
      else {
        m_treeSelection.Clear();
        wxFAIL_MSG(wxString(wxT("tree item \'")) << tree->GetItemText(selection) << wxT("\' found with no CItemData attached"));
      }
    }
  }
  else {
    m_treeSelection.Clear();
  }
}

void GUIInfo::SaveGridViewInfo(PWSGrid* grid)
{
  //has the grid been initialized?
  if (grid->GetNumItems() == 0)
    return;

  const int row = grid->YToRow(0);
  if (row != wxNOT_FOUND) {
    CItemData* item = grid->GetItem(row);
    if (item) {
      m_gridTop = item->GetUUID();
    }
    else {
      wxFAIL_MSG(wxString(wxT("Top grid row ")) << row << wxT(" has no CItemData attached"));
      m_gridTop = pws_os::CUUID::NullUUID();
    }
  }
  else {
    m_gridTop = pws_os::CUUID::NullUUID();
  }

  const int selection = grid->GetGridCursorRow();
  if (selection != wxNOT_FOUND) {
    CItemData* item = grid->GetItem(selection);
    if (item) {
      m_gridSelection = item->GetUUID();
    }
    else {
      wxFAIL_MSG(wxString(wxT("Selected grid row ")) << selection << wxT(" has no CItemData attached"));
      m_gridSelection = pws_os::CUUID::NullUUID();
    }
{'fraction_non_alphanumeric': 0.14968501780334156, 'fraction_numerical': 0.0036976170912078883, 'mean_word_length': 3.173142857142857, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 80, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6948996', 'n_tokens_mistral': 2516, 'n_tokens_neox': 2473, 'n_words': 523}