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

Go to the SVN repository for this file.

1 /* $Id: restore_local_file.cpp 40044 2017-12-13 15:38:02Z katargir $
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: Andrea Asztalos
27  *
28  */
29 
30 #include <ncbi_pch.hpp>
31 #include <objmgr/scope.hpp>
34 
35 #include <gui/core/document.hpp>
40 
41 #include <gui/objutils/objects.hpp>
46 
48 
51 
52 
54  : m_Workbench(workbench)
55 {
57 }
58 
60 {
62  if (!view_srv) return 0;
63 
66  view_srv->GetViews(views);
67 
69  CRef<CGBWorkspace> ws = srv->GetGBWorkspace();
70 
71 
73  IProjectView* project_view = dynamic_cast<IProjectView*>((*it).GetPointer());
74  if (project_view &&
75  project_view->GetLabel(IProjectView::eType) == "Text View" &&
76  NStr::Find(project_view->GetLabel(IProjectView::eProject), "SMART Client") != NPOS) {
77  // limit the functionality to Smart project item
78 
79  return project_view;
80  }
81  }
82 
83  return 0;
84 }
85 
87 {
88  CRef<CFileLoadWizard> fileManager(new CFileLoadWizard());
89  vector<string> format_ids;
90  format_ids.push_back("file_loader_asn");
91  fileManager->LoadFormats(format_ids);
92 
93  vector<CIRef<IOpenObjectsPanelClient> > loadManagers;
94  loadManagers.push_back(CIRef<IOpenObjectsPanelClient>(fileManager.GetPointer()));
95 
96  COpenObjectsDlg dlg(NULL);
97  dlg.SetSize(710, 480);
98 
99  dlg.SetRegistryPath("Dialogs.Edit.OpenTables");
100  dlg.SetManagers(loadManagers);
101 
102  if (dlg.ShowModal() == wxID_OK) {
103  CIRef<IObjectLoader> object_loader(dlg.GetObjectLoader());
104  if (!object_loader) {
105  return false;
106  }
107  else {
108  IExecuteUnit* execute_unit = dynamic_cast<IExecuteUnit*>(object_loader.GetPointer());
109  _ASSERT(execute_unit);
110  if (execute_unit) {
111  if (!execute_unit->PreExecute())
112  return false;
113 
114  if (!GUI_AsyncExecUnit(*execute_unit, wxT("Reading file(s)...")))
115  return false; // Canceled
116 
117  if (!execute_unit->PostExecute())
118  return false;
119  }
120 
121  NON_CONST_ITERATE(IObjectLoader::TObjects, obj_it, object_loader->GetObjects()) {
122  CSerialObject* so = dynamic_cast<CSerialObject*>(obj_it->GetObjectPtr());
123  if (so) {
124  m_NewSerialObj.Reset(so);
125  return true;
126  }
127  }
128  }
129  }
130  return false;
131 }
132 
134 {
135  const CGBDocument::TViews& doc_views = doc->GetViews();
136  if (!doc_views.empty()) {
137  set<IProjectView*> viewToClose;
138  for (auto it : doc_views) {
139  viewToClose.insert(it);
140  }
141  ITERATE(set<IProjectView*>, it, viewToClose) {
142  m_PrjService->RemoveProjectView(**it);
143  }
144  }
145 }
146 
148 {
149  if (!item)
150  return false;
151 
152  const CSerialObject* so = item->GetObject();
154  CSeq_entry_Handle seh;
155 
157  const CSeq_entry* entry = dynamic_cast<const CSeq_entry *>(so);
158  if (entry) {
159  seh = scope.GetSeq_entryHandle(*entry);
160  }
161  }
162  else if (type == CProjectItem::TItem::e_Submit) {
163  const CSeq_submit* submit = dynamic_cast<const CSeq_submit *>(so);
164  if (submit && submit->IsSetData() && submit->IsEntrys()) {
165  seh = scope.GetSeq_entryHandle(*submit->GetData().GetEntrys().front());
166  }
167  }
168 
169  if (seh) {
170  try {
171  scope.RemoveTopLevelSeqEntry(const_cast<CTSE_Handle&>(seh.GetTSE_Handle()));
172  }
173  catch (CException& ex){
174  LOG_POST(Error << "DetachOrigObject(): error removing seq-entry: " << ex.GetMsg());
175  return false;
176  }
177  }
178 
179  return true;
180 }
181 
183 {
185  if (!item) {
186  return false;
187  }
188  CRef<CSeq_entry> new_entry;
189 
190  CSeq_entry* seq_entry = dynamic_cast<CSeq_entry*>(m_NewSerialObj.GetNCPointer());
191  CSeq_submit* seq_submit = dynamic_cast<CSeq_submit*>(m_NewSerialObj.GetNCPointer());
192 
193  if (seq_entry) {
194  new_entry = Ref(seq_entry);
195  } else if (seq_submit) {
196  if (convert) {
197  new_entry = edit::SeqEntryFromSeqSubmit(*seq_submit);
198  } else {
199  if (seq_submit && seq_submit->IsSetData() && seq_submit->IsEntrys()) {
200  new_entry = seq_submit->GetData().GetEntrys().front();
201  }
202  }
203  }
204 
205  if (!new_entry) {
206  NcbiMessageBox("New file should contain either a seq-entry or a seq-submit.");
207  return false;
208  }
209 
210  try {
212  _ASSERT(seh);
213  if (convert) {
214  item->SetObject(new_entry.GetNCObject());
215  } else {
217  }
218  }
219  catch (CException& ex){
220  LOG_POST(Error << "Failed to attach seq-entry: " << ex.GetMsg());
221  return false;
222  }
223 
224  return true;
225 }
226 
228 {
229  IProjectView* project_view = FindSMARTPrjTextView();
230  if (!project_view) {
231  NcbiMessageBox("Failed to find Text View associated with SMART project.");
232  return;
233  }
234 
236  CRef<CGBWorkspace> ws = srv->GetGBWorkspace();
237 
238  IProjectView::TProjectId id = project_view->GetProjectId();
239  CGBDocument* doc = dynamic_cast<CGBDocument*>(ws->GetProjectFromId(id));
240 
241  // get relevant ProjectItem
243  project_view->GetMainObject(objects);
244  _ASSERT(!objects.empty());
245 
246  const CProjectItem* const_item = m_PrjService->GetProjectItem(objects[0].object.GetObject(), objects[0].scope.GetNCObject());
247  CProjectItem* project_item = const_cast<CProjectItem*>(const_item);
248 
249  if (!ReadObjectFromFile()) {
250  return;
251  }
252 
253  CloseOpenViews(doc);
254 
255  CScope* scope = doc->GetScope();
256  if (scope &&
257  x_DetachOrigObject(Ref(project_item), *scope) &&
258  x_AttachNewObject(Ref(project_item), *scope, convert)) {
259 
260  // clean the history of commands
261  CUndoManager& undo_mgr = doc->GetUndoManager();
262  undo_mgr.Reset();
263 
264  // should we call smart processing on the new entry - no (that step was already called when the file was originally sent from SMART)
265  // reopen the Text view
266  const CSerialObject* so = project_item->GetObject();
267  if (so) {
269  SConstScopedObject object(so, scope);
270  COpenViewTask* task = new COpenViewTask(m_Workbench, "Text View", object, 0, true);
271  taskService->AddTask(*task);
272  }
273  }
274 }
275 
User-defined methods of the data storage class.
bool GUI_AsyncExecUnit(IExecuteUnit &exec_unit, const wxString &msg)
Definition: async_call.cpp:53
CAppTaskService - Application Task Service.
virtual void SetRegistryPath(const string &path)
Definition: dialog.cpp:59
CGBDocument.
Definition: document.hpp:113
virtual const TViews & GetViews(void) const
Retrieve the existing views for this class.
Definition: document.cpp:388
CUndoManager & GetUndoManager()
Definition: document.hpp:158
vector< CIRef< IProjectView > > TViews
Definition: document.hpp:131
CLocalFileRestorer(IWorkbench *workbench)
bool x_DetachOrigObject(CRef< objects::CProjectItem > item, objects::CScope &scope)
IProjectView * FindSMARTPrjTextView() const
CRef< CSerialObject > m_NewSerialObj
CIRef< CProjectService > m_PrjService
void RestoreFile(bool convert=false)
bool x_AttachNewObject(CRef< objects::CProjectItem > item, objects::CScope &scope, bool convert)
void CloseOpenViews(CGBDocument *doc)
IObjectLoader * GetObjectLoader()
void SetManagers(vector< CIRef< IOpenObjectsPanelClient > > &managers)
const CSerialObject * GetObject() const
retrieve the object pointed to as a CObject*
void SetObject(CSerialObject &object)
wrapper for setting the object pointed to by this item
CProjectService - a service providing API for operations with Workspaces and Projects.
CRef –.
Definition: ncbiobj.hpp:618
CScope –.
Definition: scope.hpp:92
CSeq_entry_Handle –.
Definition: Seq_entry.hpp:56
bool IsEntrys(void) const
Definition: Seq_submit.cpp:54
Base class for all serializable objects.
Definition: serialbase.hpp:150
virtual bool PreExecute()=0
virtual bool PostExecute()=0
vector< SObject > TObjects
class IProjectView defines the abstract interface for views observing projects.
objects::CGBProjectHandle::TId TProjectId
virtual TProjectId GetProjectId() const =0
virtual string GetLabel(ELabelType type) const =0
returns name of the plug-in created this view (view class name)
virtual void GetMainObject(TConstScopedObjects &objects) const =0
Adds the main data objects represented by the client to "objects".
IViewManagerService IViewManagerService manages views in Workbench.
IWorkbench is the central interface in the application framework.
Definition: workbench.hpp:113
Definition: set.hpp:45
iterator_bool insert(const value_type &val)
Definition: set.hpp:149
static TDSRET convert(TDSSOCKET *tds, TDSICONV *conv, TDS_ICONV_DIRECTION direction, const char *from, size_t from_len, char *dest, size_t *dest_len)
Definition: charconv.c:57
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define NON_CONST_ITERATE(Type, Var, Cont)
Non constant version of ITERATE macro.
Definition: ncbimisc.hpp:822
#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
CIRef< T > GetServiceByType()
retrieves a typed reference to a service, the name of C++ type is used as the name of the service.
Definition: service.hpp:91
void AddTask(IAppTask &task)
Add a task to the queue.
vector< CIRef< IView > > TViews
virtual void GetViews(TViews &views)=0
get all registered views
EDialogReturnValue NcbiMessageBox(const string &message, TDialogType type=eDialog_Ok, EDialogIcon icon=eIcon_Exclamation, const string &title="Error", EDialogTextMode text_mode=eRaw)
Definition: message_box.cpp:48
vector< SConstScopedObject > TConstScopedObjects
Definition: objects.hpp:65
CSeq_entry_Handle AddTopLevelSeqEntry(CSeq_entry &top_entry, TPriority pri=kPriority_Default, EExist action=eExist_Default)
Add seq_entry, default priority is higher than for defaults or loaders Add object to the score with p...
Definition: scope.cpp:522
CSeq_entry_Handle GetSeq_entryHandle(CDataLoader *loader, const TBlobId &blob_id, EMissing action=eMissing_Default)
Get Seq-entry handle by its blob-id, with possible loading.
Definition: scope.cpp:113
void RemoveTopLevelSeqEntry(const CTSE_Handle &entry)
Revoke TSE previously added using AddTopLevelSeqEntry() or AddBioseq().
Definition: scope.cpp:376
@ eExist_Get
Definition: scope.hpp:260
@ kPriority_Default
Use default priority for added data.
Definition: scope.hpp:100
const CTSE_Handle & GetTSE_Handle(void) const
TObjectType * GetNCPointer(void) const THROWS_NONE
Get pointer,.
Definition: ncbiobj.hpp:1174
CRef< C > Ref(C *object)
Helper functions to get CRef<> and CConstRef<> objects.
Definition: ncbiobj.hpp:2015
TObjectType * GetPointer(void) THROWS_NONE
Get pointer,.
Definition: ncbiobj.hpp:998
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
TObjectType & GetNCObject(void) const
Get object.
Definition: ncbiobj.hpp:1187
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define NPOS
Definition: ncbistr.hpp:133
static SIZE_TYPE Find(const CTempString str, const CTempString pattern, ECase use_case=eCase, EDirection direction=eForwardSearch, SIZE_TYPE occurrence=0)
Find the pattern in the string.
Definition: ncbistr.cpp:2891
E_Choice Which(void) const
Which variant is currently selected.
const TItem & GetItem(void) const
Get the Item member data.
@ e_Entry
for projects that contain something else
bool IsSetData(void) const
Check if a value has been assigned to Data data member.
const TEntrys & GetEntrys(void) const
Get the variant data.
const TData & GetData(void) const
Get the Data member data.
#define wxT(x)
Definition: muParser.cpp:41
USING_SCOPE(objects)
CRef< CSeq_entry > SeqEntryFromSeqSubmit(const CSeq_submit &submit)
Create a Seq-entry from a Seq-submit.
Definition: type.c:6
#define _ASSERT
Modified on Tue Jun 18 13:41:08 2024 by modify_doxy.py rev. 669887