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

Go to the SVN repository for this file.

1 /* $Id: ui_data_source_service.cpp 44933 2020-04-21 18:56:36Z asztalos $
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 
33 
38 
42 
44 
46 
47 #include <corelib/ncbi_system.hpp>
48 
49 
52 
53 ///////////////////////////////////////////////////////////////////////////////
54 /// CUIDataSourceService
56 : m_SrvLocator(NULL)
57 {
58 }
59 
60 
62 {
63 }
64 
65 
67 {
68  LOG_POST(Info << "Initializing Data Source Service...");
69 
71 
72  LoadSettings();
73 
75 
77 
79 
80  // contribute Data Source page to Options dialog
84 
85  LOG_POST(Info << "Finished initializing Data Source Service");
86 }
87 
88 
90 {
91  /// get Data Source Types from Extension Registry
92  GetExtensionAsInterface("ui_data_source_type", m_DataSourceTypes);
93 
94  for( size_t i = 0; i < m_DataSourceTypes.size(); i++ ) {
96  string name = ds_type.GetDescr().GetLabel();
97  LOG_POST(Info << "Registered data source type \"" << name << "\"");
98  }
99 }
100 
101 
103 {
104  for( size_t i = 0; i < m_DataSourceTypes.size(); i++ ) {
106 
107  if(ds_type.AutoCreateDefaultDataSource()) {
108  // look if we have a Data Source of this type already
109  bool found = false;
110  for( size_t j = 0; ! found && j < m_DataSources.size(); j++ ) {
111  IUIDataSource& ds = *m_DataSources[j];
112  found = (&ds.GetType() == &ds_type);
113  }
114 
115  if( ! found) {
116  LOG_POST(Info << "Creating default Data Source for type \"" <<
117  ds_type.GetDescr().GetLabel() << "\" ...");
118 
120 
121  /// if this source implements IRegSettings - set its registry path
122  IRegSettings* rs = dynamic_cast<IRegSettings*>(&*ds);
123  if( rs ){
124  string help_id = ds->GetDescr().GetHelpId();
125  if( help_id.empty() ){
126  help_id = "DefaultUIDS";
127  }
128  string reg_path = m_RegPath + "." + help_id;
129  rs->SetRegistryPath( reg_path );
130  }
131 
133  AddDataSource(*ds);
134  }
135  }
136  }
137 }
138 
139 
141 {
142  /// TODO
143  // create System Load Managers
145 
146  CRef<CFileLoadManager> fileManager(new CFileLoadManager());
147  fileManager->LoadRegisteredManagers();
148  m_LoadManagers.push_back(TUIToolManagerRef(fileManager.GetPointer()));
150 
151  //TODO
152  // obtain managers coming from Data Sources
153  for( size_t i = 0; i < m_DataSources.size(); i++ ) {
155  CIRef<IUIToolManager> manager(ds->GetLoadManager());
156  if(manager) {
157  m_LoadManagers.push_back(manager);
158  }
159  }
160 
161  for( size_t i = 0; i < m_LoadManagers.size(); i++ ) {
162  string name = m_LoadManagers[i]->GetDescriptor().GetLabel();
163  LOG_POST(Info << "Registered data load manager \"" << name << "\"");
164  }
165 }
166 
167 
169 {
170  SaveSettings();
171 
172  if (m_OptionsDlgPage) {
174  registry->RemoveExtension(*m_OptionsDlgPage);
175  m_OptionsDlgPage = 0;
176  }
177 
179 
180  m_LoadManagers.clear();
181  m_DataSources.clear();
182  m_DataSourceTypes.clear();
183 }
184 
185 
187 {
188  m_SrvLocator = locator;
189 }
190 
191 
193 {
194  m_RegPath = path;
195 }
196 
198 {
199  _ASSERT(! m_RegPath.empty());
200 }
201 
202 
204 {
205  _ASSERT(! m_RegPath.empty());
206 }
207 
208 
210 {
211  CIRef<IUIDataSource> ds_ref(&data_source);
212  string ds_name = data_source.GetDescr().GetLabel();
213 
214  TUIDataSourceVec::iterator it =
215  std::find(m_DataSources.begin(), m_DataSources.end(), ds_ref);
216 
217  if(it != m_DataSources.end()) {
218  string msg = "Data Source " + ds_name + " is already registered";
220  } else {
221  m_DataSources.push_back(ds_ref);
222  LOG_POST(Info << "Registered data source \"" << ds_name << "\"");
223  }
224 }
225 
226 
228 {
229  type_vec = m_DataSourceTypes;
230 }
231 
232 
235 {
236  for( size_t i = 0; i < m_DataSourceTypes.size(); i++) {
238  if(ds_type->GetDescr().GetLabel() == label) {
239  return ds_type;
240  }
241  }
242  return TUIDataSourceTypeRef();
243 }
244 
245 
247 {
248  data_sources = m_DataSources;
249 }
250 
251 
254 {
255  for( size_t i = 0; i < m_DataSources.size(); i++) {
257  if(ds->GetDescr().GetLabel() == label) {
258  return ds;
259  }
260  }
261  return TUIDataSourceRef();
262 }
263 
264 
266 {
267  managers = m_LoadManagers;
268 }
269 
270 
271 static void sReportDSExceptionToLog(const string& prefix,
272  const string& ds_name, const string& what)
273 {
274  LOG_POST(Error << prefix << ds_name <<":\n" << what);
275 }
276 
277 static const char* kOpenErr = "Failed to open datasource ";
278 static const char* kCloseErr = "Failed to close datasource ";
279 
280 
282 {
283  LOG_POST(Info << "Opening Data Sources...");
284 
285  for( size_t i = 0; i < m_DataSources.size(); i++ ) {
286  IUIDataSource& ds = *m_DataSources[i];
287  string ds_name = ds.GetDescr().GetLabel();
288 
289  try {
290  _ASSERT(! ds.IsOpen());
291  ds.Open();
292  } catch(CException& e) {
293  sReportDSExceptionToLog(kOpenErr, ds_name, e.GetMsg());
294  }
295  catch(std::exception& e) {
296  sReportDSExceptionToLog(kOpenErr, ds_name, e.what());
297  }
298 #ifndef _DEBUG
299  catch (...) {
300  sReportDSExceptionToLog(kOpenErr, ds_name, "Unknown error.");
301  }
302 #endif
303  }
304  LOG_POST(Info << "Finished closing Data Sources");
305 }
306 
307 
309 {
310  LOG_POST(Info << "Closing Data Sources...");
311 
312  for( size_t i = m_DataSources.size(); i > 0 ; ) {
313  IUIDataSource& ds = *m_DataSources[--i];
314  string ds_name = ds.GetDescr().GetLabel();
315 
316  try {
317  if(ds.IsOpen()) {
318  ds.Close();
319  }
320  } catch(CException& e) {
322  }
323  catch(std::exception& e) {
324  sReportDSExceptionToLog(kCloseErr, ds_name, e.what());
325  }
326  }
327 
328  LOG_POST(Info << "Finished closing Data Sources");
329 }
330 
331 
CFileLoadManager CFileLoadManager - a Loader Manager implementing "File" option in "Open" dialog.
CGCAssemblyLoadManager - Load Manager that loads genome collection assembly.
CProjectLoadManager -Load Manager that loads both Projects and Workspaces.
CRef –.
Definition: ncbiobj.hpp:618
TDataSourceTypeVec m_DataSourceTypes
virtual void SaveSettings() const
vector< TUIToolManagerRef > TUIToolManagerVec
TUIToolManagerVec m_LoadManagers
virtual TUIDataSourceTypeRef GetUIDataSourceTypeByLabel(const string &label)
finds a Data Source Type by its UI label
virtual void AddDataSource(IUIDataSource &data_source)
registers a new data source
CIRef< IUIDataSource > TUIDataSourceRef
virtual void GetLoadManagers(TUIToolManagerVec &managers)
retrieve Load Managers (both system Managers the Data Sources Managers)
virtual void GetDataSources(TUIDataSourceVec &data_sources)
retrieve Data Sources registered in the application
virtual void GetDataSourceTypes(TDataSourceTypeVec &type_vec)
CIRef< IUIDataSourceType > TUIDataSourceTypeRef
vector< TUIDataSourceTypeRef > TDataSourceTypeVec
vector< TUIDataSourceRef > TUIDataSourceVec
virtual void SetRegistryPath(const string &path)
virtual TUIDataSourceRef GetUIDataSourceByLabel(const string &label)
finds a Data Source by its UI label
virtual void SetServiceLocator(IServiceLocator *locator)
CIRef< IUIToolManager > TUIToolManagerRef
CUIDataSourceService()
CUIDataSourceService.
IRegSettings An interface for objects that save / restore settings using CGuiRegistry.
virtual void SetRegistryPath(const string &path)=0
IServiceLocator - an abstract mechanism for locating services.
Definition: service.hpp:71
IUIDataSourceType - defines a type of a Data Source, can serve as a Data Source factory.
IUIDataSource - an interface representing a Data Source.
static CMemoryRegistry registry
Definition: cn3d_tools.cpp:81
#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
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
const string & GetMsg(void) const
Get message string.
Definition: ncbiexpt.cpp:461
virtual const char * what(void) const noexcept
Standard report (includes full backlog).
Definition: ncbiexpt.cpp:342
void Info(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1185
void Consumer_SetLocator(T *obj, IServiceLocator *locator)
Definition: service.hpp:111
virtual IUIDataSource * CreateDataSource()=0
factory method; create an Data Source instance
virtual bool AutoCreateDefaultDataSource()=0
returns "true" if this type needs to create a default instance of the Data Source at start-up
virtual bool IsOpen()=0
a Data source needs to be open before use and closed after.
virtual IUIDataSourceType & GetType() const =0
#define EXT_POINT__OPTIONS_DLG_EXTENSION
An extension point for dialog panels that are shown in the "Options" dialog.
virtual bool Open()=0
Prepares Data Source for use, any initialization that can be potentially time-consuming or may requir...
virtual const IUIObject & GetDescr()=0
returns UI description of the type (label, icon etc.)
virtual bool Close()=0
virtual const IUIObject & GetDescr()=0
returns UI description of the object (label, icon etc.)
void GetExtensionAsInterface(const string &ext_point_id, vector< CIRef< I > > &interfaces)
GetExtensionAsInterface() is a helper function that extracts all extensions implementing the specifie...
static CIRef< IExtensionRegistry > GetInstance()
provides access to the Singleton
virtual const string & GetLabel() const =0
TObjectType * GetPointer(void) THROWS_NONE
Get pointer,.
Definition: ncbiobj.hpp:998
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static const char label[]
int i
static const char * prefix[]
Definition: pcregrep.c:405
#define _ASSERT
USING_SCOPE(objects)
static const char * kCloseErr
static const char * kOpenErr
static void sReportDSExceptionToLog(const string &prefix, const string &ds_name, const string &what)
Modified on Wed Apr 24 14:10:41 2024 by modify_doxy.py rev. 669887