NCBI C++ ToolKit
file_load_wizard.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: file_load_wizard.cpp 47479 2023-05-02 13:24:02Z ucko $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Andrey Yazhuk
27  *
28  */
29 
30 #include <ncbi_pch.hpp>
31 
32 #include "file_load_panel.hpp"
36 
39 
41 
44 
45 #include <wx/msgdlg.h>
46 
48 
49 ///////////////////////////////////////////////////////////////////////////////
50 /// CFileLoadWizard
52 : m_ParentWindow(),
53  m_StartPage(),
54  m_CurrFormat(-1),
55  m_FileSelectPanel(NULL),
56  m_FileSelectPage(*this)
57 {
58 }
59 
61 {
63 }
64 
65 //
66 // IOpenObjectsPanelClient implementation
67 //
68 
70 {
71  return "File Import";
72 }
73 
75 {
76  IExecuteUnit* execute_unit = 0;
77  if (m_CurrFormat >= 0) {
78  execute_unit = m_FormatManagers[m_CurrFormat]->GetExecuteUnit();
79  }
80  return execute_unit;
81 }
82 
83 //
84 // IToolWizard implementation
85 //
86 
87 void CFileLoadWizard::SetParentWindow(wxWindow* parent)
88 {
89  m_ParentWindow = parent;
90 
91  for (size_t i = 0; i < m_FormatManagers.size(); i++)
92  dynamic_cast<IToolWizard&>(*m_FormatManagers[i]).SetParentWindow(parent);
93 }
94 
96 {
98  return &m_FileSelectPage;
99 }
100 
102 {
103  if (m_CurrFormat >= 0) {
104  return dynamic_cast<IToolWizard&>(*m_FormatManagers[m_CurrFormat]).GetOptionsPage();
105  }
106  return 0;
107 }
108 
110 {
111  for (size_t i = 0; i < m_FormatManagers.size(); i++)
112  dynamic_cast<IToolWizard&>(*m_FormatManagers[i]).SetPrevPage(prevPage);
113 }
114 
116 {
117  for (size_t i = 0; i < m_FormatManagers.size(); i++)
118  dynamic_cast<IToolWizard&>(*m_FormatManagers[i]).SetNextPage(nextPage);
119 }
120 
121 void CFileLoadWizard::LoadFormats(const vector<string>& format_ids)
122 {
123  set<string> ids;
124  ids.insert(format_ids.begin(), format_ids.end());
125  vector< CIRef<IFileLoadPanelClientFactory> > fileFormats;
126  GetExtensionAsInterface("file_load_panel_client", fileFormats);
127 
128  for (size_t i = 0; i < fileFormats.size(); ++i) {
129  if (ids.find(fileFormats[i]->GetFileLoaderId()) != ids.end())
130  m_FormatManagers.push_back(CIRef<IFileLoadPanelClient>(fileFormats[i]->CreateInstance()));
131  }
132 }
133 
134 void CFileLoadWizard::SetWorkDir(const wxString& workDir)
135 {
136  m_WorkDir = workDir;
137 
138  for (size_t i = 0; i < m_FormatManagers.size(); ++i) {
139  IExecuteUnit* execute_unit = m_FormatManagers[i]->GetExecuteUnit();
140  CReportLoaderErrors* reporter = dynamic_cast<CReportLoaderErrors*>(execute_unit);
141  if (!reporter)
142  continue;
143  reporter->SetWorkDir(workDir);
144  }
145 }
146 
148 {
150 
151  if(m_FileSelectPanel) {
153  }
154 }
155 
157 {
160  m_ParentWindow->ProcessWindowEvent(evt);
161 }
162 
164 {
165  vector<wxString> filenames;
167 
168  if(filenames.empty()) {
169  NcbiErrorBox("Please select at least one file!");
170  return false;
171  }
172 
174  if (!x_CheckFormatConflict(filenames)) { // it might modify m_CurrFormat
175  return false;
176  }
177 
178  // select Format Manager for the format
180  if (!manager.ValidateFilenames(filenames)) {
181  NcbiErrorBox("The file(s) cannot be opened with this file format manager!");
182  return false;
183  }
184 
185  manager.SetFilenames(filenames);
186  string id = manager.GetFileLoaderId();
187  wxString label = ToWxString(manager.GetLabel());
188 
189  ITERATE(vector<wxString>, it, filenames)
191 
193 
194  return true;
195 }
196 
197 
199 {
201  try {
202  CNcbiIfstream istr(filenames[0].fn_str(), ios_base::binary);
203  CFormatGuess guesser(istr);
204  detected_fmt = guesser.GuessFormat();
205  }
206  catch (const CException& e) {
207  LOG_POST(Error << "Format of " << filenames[0].ToUTF8() << " could not be recognized");
208  LOG_POST(Error << e.GetMsg());
209  return false;
210  }
211  string detected_fmt_name = CFormatGuess::GetFormatName(detected_fmt);
212  if (detected_fmt_name.empty())
213  detected_fmt_name = "No name";
214 
215  auto& manager = m_FormatManagers[m_CurrFormat];
216  if (!manager->RecognizeFormat(detected_fmt)) {
217  bool found_mgr = false;
218  for (size_t i = 0; i < m_FormatManagers.size() && !found_mgr; ++i) {
219  if (i != m_CurrFormat) {
220  auto& current_manager = m_FormatManagers[i];
221  if (current_manager->RecognizeFormat(detected_fmt)) {
222  const wxFormatString kFormatConflictMsg = "Detected data format '%s' doesn't appear to match the format you selected.\n"
223  "Are you sure you want to load your data as '%s'? Click 'Yes' to proceed or click 'No' to process the data as '%s'";
224  int resp = NcbiMessageBoxW(wxString::Format(kFormatConflictMsg, detected_fmt_name, manager->GetLabel(), detected_fmt_name),
226  if (resp == eCancel) {
227  return false;
228  } else {
229  if (resp == eNo) {
230  m_CurrFormat = static_cast<int>(i);
231  }
232  found_mgr = true;
233  }
234  }
235  }
236  }
237  }
238  return true;
239 }
240 
242 {
245  IToolWizard& wizard = dynamic_cast<IToolWizard&>(manager);
246  return wizard.GetFirstPage();
247 }
248 
250 {
253  IToolWizard& wizard = dynamic_cast<IToolWizard&>(manager);
254  return wizard.GetOptionsPage();
255 }
256 
258 {
259  if (!m_FileSelectPanel) {
263 
264  if(m_Filenames.empty()) {
265  // select default format
266  m_CurrFormat = std::max(0, m_CurrFormat); // do not allow -1
268  } else {
269  // have files - autoguess
271  }
273  }
274  return m_FileSelectPanel;
275 }
276 
277 
278 void CFileLoadWizard::SetRegistryPath(const string& path)
279 {
280  m_RegPath = path; // store for later use
281  m_RegPath += "2"; // changed contents of MRU section JIRA: GB-1658
282 
283  for( size_t i = 0; i < m_FormatManagers.size(); i++ ) {
285  IRegSettings* rgs = dynamic_cast<IRegSettings*>(&manager);
286  if (rgs) {
287  string mgr_path = m_RegPath + "." + manager.GetFileLoaderId();
288  rgs->SetRegistryPath(mgr_path);
289  }
290  }
291 }
292 
293 static const char* kSelectedFormatTag = "SelectedFormat";
294 static const char* kMRUTag = "MRUFiles";
295 
297 {
298  if (m_RegPath.empty())
299  return;
300 
302  CRegistryWriteView view = gui_reg.GetWriteView(m_RegPath);
303 
304  string selectedLabel;
305  if (m_CurrFormat != -1) {
307  selectedLabel = manager.GetLabel();
308  }
309  view.Set(kSelectedFormatTag, selectedLabel);
310 
311  // save MRU Files
312  vector<string> values;
314  view.Set(kMRUTag, values);
315 
316  // save Managers' settigns
317  for (size_t i = 0; i < m_FormatManagers.size(); i++) {
318  const IRegSettings* rgs = dynamic_cast<const IRegSettings*>(m_FormatManagers[i].GetPointer());
319  if (rgs)
320  rgs->SaveSettings();
321  }
322 }
323 
325 {
326  if (m_RegPath.empty())
327  return;
328 
330  CRegistryReadView view = gui_reg.GetReadView(m_RegPath);
331 
332  // load the default option
333  string sel_fmt_label = view.GetString(kSelectedFormatTag, "empty");
334 
335  // load MRU Files
336  vector<string> values;
337  view.GetStringVec(kMRUTag, values);
339 
340  for (size_t i = 0; i < m_FormatManagers.size(); i++) {
342  // load settings (if supported)
343  IRegSettings* rgs = dynamic_cast<IRegSettings*>(&manager);
344  if(rgs)
345  rgs->LoadSettings();
346 
347  // check whether this is the selected manager
348  string mgr_label = manager.GetLabel();
349  if (mgr_label == sel_fmt_label)
350  m_CurrFormat = (int)i;
351  }
352 }
353 
354 
void LoadFromStrings(const vector< string > &values)
void SaveToStrings(vector< string > &values) const
void SetWorkDir(const wxString &workDir)
void SetManager(CFileLoadWizard *manager)
void SelectFormat(int format)
void GetFilenames(vector< wxString > &filenames) const
void SetFilenames(const vector< wxString > &filenames)
virtual wxPanel * GetPanel()
CFileLoadWizard()
CFileLoadWizard.
vector< wxString > m_Filenames
CFilePage m_FileSelectPage
CFileLoadPanel * m_FileSelectPanel
CFileLoadMRUList m_FileMRUList
vector< CIRef< IFileLoadPanelClient > > m_FormatManagers
virtual void SaveSettings() const
wxWindow * m_ParentWindow
virtual void SetParentWindow(wxWindow *parent)
bool x_CheckFormatConflict(const vector< wxString > &filenames)
virtual void LoadSettings()
void SetWorkDir(const wxString &workDir)
IWizardPage * x_GetOptionsPage()
void OnFormatChanged(int format)
virtual void SetNextPage(IWizardPage *nexPage)
Sets a wizard page which should show after the last page of the tool.
void LoadFormats(const vector< string > &format_ids)
virtual IWizardPage * GetOptionsPage()
Returns page for tool options (long wizard path via "Options" button)
void SetFilenames(vector< wxString > &filenames)
virtual void SetPrevPage(IWizardPage *prevPage)
Sets a wizard page which should show before the first page of the tool.
CFileLoadPanel * x_GetFileSelectPanel()
virtual void SetRegistryPath(const string &path)
IWizardPage * x_GetNextPage()
virtual string GetLabel() const
virtual IWizardPage * GetFirstPage()
Returns first options page (if any) of the tool to be shown in wizard dialog.
virtual IExecuteUnit * GetExecuteUnit()
Class implements different ad-hoc unreliable file format identifications.
EFormat
The formats are checked in the same order as declared here.
@ eUnknown
unknown format
EFormat GuessFormat(EMode)
static const char * GetFormatName(EFormat format)
CRegistryWriteView GetWriteView(const string &section)
get a read-write view at a particular level.
Definition: registry.cpp:462
static CGuiRegistry & GetInstance()
access the application-wide singleton
Definition: registry.cpp:400
CRegistryReadView GetReadView(const string &section) const
get a read-only view at a particular level.
Definition: registry.cpp:428
class CRegistryReadView provides a nested hierarchical view at a particular key.
Definition: reg_view.hpp:58
string GetString(const string &key, const string &default_val=kEmptyStr) const
Definition: reg_view.cpp:246
void GetStringVec(const string &key, vector< string > &val) const
Definition: reg_view.cpp:263
void Set(const string &key, int val)
access a named key at this level, with no recursion
Definition: reg_view.cpp:533
CReportLoaderErrors.
void SetWorkDir(const wxString &workDir)
void Add(T elem, time_t time=0)
Definition: mru_list.hpp:176
@ eCmdUpdateButtons
Definition: wizard_dlg.hpp:86
virtual bool ValidateFilenames(const vector< wxString > &filenames)=0
checks given filenames and returns true if the manager can handle them, otherwise - shows an error me...
virtual void SetFilenames(const vector< wxString > &filenames)=0
initilize the manager with the given filenames
virtual string GetLabel() const =0
virtual string GetFileLoaderId() const =0
IRegSettings An interface for objects that save / restore settings using CGuiRegistry.
virtual void SaveSettings() const =0
virtual void LoadSettings()=0
virtual void SetRegistryPath(const string &path)=0
virtual void SetNextPage(IWizardPage *)
Sets a wizard page which should show after the last page of the tool.
Definition: tool_wizard.hpp:63
virtual IWizardPage * GetOptionsPage()
Returns page for tool options (long wizard path via "Options" button)
Definition: tool_wizard.hpp:59
virtual void SetPrevPage(IWizardPage *)
Sets a wizard page which should show before the first page of the tool.
Definition: tool_wizard.hpp:61
virtual void SetParentWindow(wxWindow *parent)=0
virtual IWizardPage * GetFirstPage()
Returns first options page (if any) of the tool to be shown in wizard dialog.
Definition: tool_wizard.hpp:57
iterator_bool insert(const value_type &val)
Definition: set.hpp:149
const_iterator find(const key_type &key) const
Definition: set.hpp:137
const_iterator end() const
Definition: set.hpp:136
static const char * kSelectedFormatTag
static const char * kMRUTag
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define NULL
Definition: ncbistd.hpp:225
#define LOG_POST(message)
This macro is deprecated and it's strongly recomended to move in all projects (except tests) to macro...
Definition: ncbidiag.hpp:226
void Error(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1197
const string & GetMsg(void) const
Get message string.
Definition: ncbiexpt.cpp:461
void GetExtensionAsInterface(const string &ext_point_id, vector< CIRef< I > > &interfaces)
GetExtensionAsInterface() is a helper function that extracts all extensions implementing the specifie...
void NcbiErrorBox(const string &message, const string &title="Error")
specialized Message Box function for reporting critical errors
EDialogReturnValue NcbiMessageBoxW(const wxString &message, TDialogType type=eDialog_Ok, EDialogIcon icon=eIcon_Exclamation, const wxString &title=wxT("Error"), EDialogTextMode text_mode=eRaw)
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ifstream CNcbiIfstream
Portable alias for ifstream.
Definition: ncbistre.hpp:439
static const char label[]
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
@ eIcon_Exclamation
Definition: types.hpp:65
@ eCancel
Definition: types.hpp:72
@ eDialog_YesNoCancel
Definition: types.hpp:50
int i
#define wxT(x)
Definition: muParser.cpp:41
T max(T x_, T y_)
static Format format
Definition: njn_ioutil.cpp:53
Format
Definition: njn_ioutil.hpp:52
static int filenames
Definition: pcregrep.c:172
wxEVT_COMMAND_MENU_SELECTED
wxString ToWxString(const string &s)
Definition: wx_utils.hpp:173
Modified on Wed Jul 31 17:19:34 2024 by modify_doxy.py rev. 669887