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

Go to the SVN repository for this file.

1 /* $Id $
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: Yury Voronov
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 
35 
36 #include "winmask_files_dlg.hpp"
37 
39 
40 #include <corelib/ncbiapp.hpp>
41 
44 
46 #include <gui/utils/ftp_utils.hpp>
47 
48 #include <wx/filename.h>
49 #include <wx/dir.h>
50 
53 
57 
59  : m_RegPath()
60  , m_UseEnvPath(true)
61  , m_JobId(CAppJobDispatcher::eInvalidJobID)
62 {
63 }
64 
66 {
69 }
70 
72 {
73  static CRef<CWinMaskerFileStorage> s_Storage;
74  DEFINE_STATIC_MUTEX(s_StorageMutex);
75  if( !s_Storage ){
76  CMutexGuard LOCK(s_StorageMutex);
77  if( !s_Storage ){
78  s_Storage.Reset( new CWinMaskerFileStorage() );
79  }
80  }
81  return *s_Storage;
82 }
83 
85 {
86  CAppJobNotification* notn = dynamic_cast<CAppJobNotification*>(evt);
87  _ASSERT(notn);
88  if (notn->GetJobID() != m_JobId) return;
89 
90  switch (notn->GetState()) {
92  case IAppJob::eCanceled:
93  case IAppJob::eFailed:
95  break;
96  default:
97  return;
98  }
99 }
100 
101 void CWinMaskerFileStorage::SetRegistryPath( const string& path )
102 {
103  m_RegPath = path;
104 }
105 
106 static const char* kUseEnvPath = "UseEnvPath";
107 
109 {
110  _ASSERT(!m_RegPath.empty());
111 
112  if( !m_RegPath.empty() ){
114  CRegistryWriteView view = gui_reg.GetWriteView(m_RegPath);
116  }
117 }
118 
120 {
121  _ASSERT(!m_RegPath.empty());
122 
123  if( !m_RegPath.empty() ){
125  CRegistryReadView view = gui_reg.GetReadView(m_RegPath);
126  m_UseEnvPath = view.GetBool(kUseEnvPath, true);
127  }
128 }
129 
131 {
132  wxString path;
134  const string& env_path = env.Get("WINDOW_MASKER_PATH");
135  if (!env_path.empty())
136  path = wxString::FromUTF8(env_path.c_str());
137 
138  if (path.empty()) {
140  if (app){
141  const CNcbiRegistry& reg = app->GetConfig();
142  const string& wm_path = reg.Get("WINDOW_MASKER", "WINDOW_MASKER_PATH");
143  if (!wm_path.empty())
144  path = wxString::FromUTF8(wm_path.c_str());
145  }
146  }
147 
148  if (path.empty()) {
149  wxString internal_path = wxT("\\\\SNOWMAN\\gbench\\data\\window-masker");
150  if (wxFileName::DirExists(internal_path)){
151  path = internal_path;
152  }
153  }
154  // wxFileName doesn't work with win UNC names wich starts with "//"
155 #ifdef NCBI_OS_MSWIN
156  path.Replace(wxT("/"), wxT("\\"));
157 #endif
158  return path;
159 }
160 
162 {
164 }
165 
166 void CWinMaskerFileStorage::GetTaxIds(vector<int>& ids)
167 {
168  ids.clear();
169 
170  bool succeeded = false;
171 
172  try {
173  wxString dir_path = GetPath();
174  if (dir_path.empty())
175  return;
176 
177  wxDir dir(dir_path);
178  if (!dir.IsOpened())
179  return;
180 
181  wxString dirName;
182  bool cont = dir.GetFirst(&dirName, wxEmptyString, wxDIR_DIRS);
183  while (cont) {
184  long tax_id;
185  if (dirName.ToLong(&tax_id) && tax_id)
186  ids.push_back((int)tax_id);
187 
188  cont = dir.GetNext(&dirName);
189  }
190  succeeded = true;
191  } NCBI_CATCH("CWinMaskerFileStorage::GetWinMaskerTaxIdsSync()");
192 
193  if (!succeeded)
194  ids.clear();
195 }
196 
197 static const wxChar* kFtpUrl = wxT("ftp://ftp.ncbi.nlm.nih.gov/toolbox/gbench/data/window-masker2/");
198 
200 {
201  return kFtpUrl;
202 }
203 
205 {
206  return CSysPath::ResolvePath(wxT("<home>/window-masker"));
207 }
208 
209 static bool s_RmDir(const wxString& path)
210 {
211  wxDir dir(path);
212  if (!dir.IsOpened())
213  return true;
214 
215  bool success = true;
216 
217  wxString entry;
218  bool cont = dir.GetFirst(&entry);
219  while (cont) {
220  wxString fullPath = wxFileName(path, entry).GetFullPath();
221  if (wxFileName::DirExists(fullPath)) {
222  if (!s_RmDir(fullPath))
223  success = false;
224  }
225  else {
226  if (!wxRemoveFile(fullPath))
227  success = false;
228  }
229  cont = dir.GetNext(&entry);
230  }
231 
232  if (!wxFileName::Rmdir(path))
233  success = false;
234 
235  return success;
236 }
237 
239 {
240  wxString path = x_GetDownloadPath();
241  wxDir dir(path);
242  if (!dir.IsOpened())
243  return true;
244 
245  if (wxFileName::DirExists(path) && !s_RmDir(path)) {
246  LOG_POST(Error << "Failed to delte direciory for window masker files:" << path.ToUTF8());
247  return false;
248  }
249  return true;
250 }
251 
252 void CWinMaskerFileStorage::x_DownloadFileList(vector<string>& files)
253 {
254  CFtpUtils::GetDirContent(files, string(GeFtpUrl().ToUTF8()));
255 }
256 
257 void CWinMaskerFileStorage::x_DownloadFiles(const vector<string>& files)
258 {
259  wxString path = x_GetDownloadPath();
260  if (!wxFileName::DirExists(path) && !::wxMkdir(path)) {
261  LOG_POST(Error << "Failed to create direciory for window masker files:" << path.ToUTF8());
262  return;
263  }
264 
267  disp.DeleteJob(m_JobId);
269  }
270 
271  CRef<CDownloadJob> job(new CDownloadJob("Download", string(path.ToUTF8()), string(GeFtpUrl().ToUTF8()), files));
272  m_JobId = disp.StartJob(*job, "ThreadPool", *this, 0, true);
273 }
274 
276 {
277  return (CWinMaskFilesDlg(NULL).ShowModal() == wxID_OK);
278 }
279 
CAppJobDispatcher.
CAppJobNotification Notification send by CAppJobEventTranslator.
Job to download list of files By default it extracts the downloaded tarball archives.
CEventHandler.
CEvent - generic event implementation TODO TODO - Attachments.
Definition: event.hpp:86
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
static CNcbiApplication * Instance(void)
Singleton method.
Definition: ncbiapp.cpp:264
CNcbiEnvironment –.
Definition: ncbienv.hpp:110
CNcbiRegistry –.
Definition: ncbireg.hpp:913
CRef –.
Definition: ncbiobj.hpp:618
class CRegistryReadView provides a nested hierarchical view at a particular key.
Definition: reg_view.hpp:58
bool GetBool(const string &key, bool default_val=false) const
Definition: reg_view.cpp:241
void Set(const string &key, int val)
access a named key at this level, with no recursion
Definition: reg_view.cpp:533
static wxString ResolvePath(const wxString &path, const wxString &rel_name)
Utility function to hide the platform specifics of locating our standard directories and files.
Definition: sys_path.cpp:106
virtual ~CWinMaskerFileStorage()
bool x_ClearDownloads() const
wxString GetPath() const
void GetTaxIds(vector< int > &ids)
void x_OnJobNotification(CEvent *evt)
static CWinMaskerFileStorage & GetInstance()
access the application-wide singleton
void SetRegistryPath(const string &path)
CAppJobDispatcher::TJobID m_JobId
wxString GetEnvPath() const
wxString GeFtpUrl() const
friend class CWinMaskFilesDlg
void x_DownloadFiles(const vector< string > &files)
void x_DownloadFileList(vector< string > &files)
wxString x_GetDownloadPath() const
#define true
Definition: bool.h:35
static HENV env
Definition: transaction2.c:38
const CNcbiRegistry & GetConfig(void) const
Get the application's cached configuration parameters (read-only).
#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_CATCH(message)
Catch CExceptions as well This macro is deprecated - use *_X or *_XX variant instead of it.
Definition: ncbiexpt.hpp:580
static CAppJobDispatcher & GetInstance()
bool DeleteJob(TJobID job_id)
when a Job is deleted the listener is not notified
TJobID StartJob(IAppJob &job, const string &engine_name, IEngineParams *params=NULL)
Starts a Job on the specified engine in "passive mode" - no notifications or progress reports will be...
#define ON_EVENT(type, id, handler)
#define END_EVENT_MAP()
Ends definition of Command Map.
#define BEGIN_EVENT_MAP(thisClass, baseClass)
Begins definition of Command Map for CEventHandler-derived class.
TJobState GetState() const
static void GetDirContent(vector< string > &content, const string &ftp_url)
Definition: ftp_utils.cpp:40
@ eCanceled
Definition: app_job.hpp:91
@ eCompleted
Definition: app_job.hpp:89
@ eFailed
Definition: app_job.hpp:90
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
virtual const string & Get(const string &section, const string &name, TFlags flags=0) const
Get the parameter value.
Definition: ncbireg.cpp:262
#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 DEFINE_STATIC_MUTEX(id)
Define static mutex and initialize it.
Definition: ncbimtx.hpp:512
#define wxT(x)
Definition: muParser.cpp:41
Defines the CNcbiApplication and CAppException classes for creating NCBI applications.
#define _ASSERT
USING_SCOPE(objects)
static bool s_RmDir(const wxString &path)
static const wxChar * kFtpUrl
static const char * kUseEnvPath
Modified on Fri Sep 20 14:57:31 2024 by modify_doxy.py rev. 669887