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

Go to the SVN repository for this file.

1 /* $Id: app_popup.cpp 27993 2013-05-06 20:30:36Z rafanovi $
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: Peter Meric
27  *
28  * File Description:
29  * CAppPopup -- launch a url in a web browser
30  */
31 
32 #include <ncbi_pch.hpp>
33 #include <gui/utils/app_popup.hpp>
34 #include <gui/utils/exec.hpp>
35 #include <corelib/ncbiexec.hpp>
36 
37 #ifdef NCBI_OS_MSWIN
38 #include <windows.h>
39 #include <ShellAPI.h>
40 #include <atlbase.h>
41 #include <atlconv.h>
42 #endif
43 
44 #if defined(NCBI_OS_MAC) || defined(NCBI_OS_DARWIN)
45 # ifdef NCBI_COMPILER_METROWERKS
46 # define __NOEXTENSIONS__
47 # endif
48 # include <ApplicationServices/ApplicationServices.h>
49 #endif
50 
51 
52 
54 
55 
56 //
57 // function prototypes
58 //
60 
61 
62 //
63 // CAppPopup:: static member definition
64 //
66 
67 
68 CAppInfo::CAppInfo(const string& path)
69  : m_Exepath(path)
70 {
71 }
72 
73 
75 {
76 }
77 
78 
79 const string& CAppInfo::GetExePath(void) const
80 {
81  return m_Exepath;
82 }
83 
84 
86  filetype::TFileType& ftype)
87 {
88  switch (ftype) {
89  case filetype::ePdf:
90  strm << "PDF";
91  break;
92  case filetype::eUnknown:
93  strm << "Unknown";
94  break;
95  default:
96  strm << "Unrecognized file type";
97  break;
98  }
99  return strm;
100 }
101 
102 
104 {
105  CAppRegistry ar;
106 
107  ar[filetype::ePdf] = CRef<CAppInfo>(new CAppInfo("acroread"));
108 
109  return ar;
110 }
111 
112 
114 {
115 }
116 
117 
119 {
120 }
121 
122 
124 {
125  return m_AppReg[type];
126 }
127 
128 
130 {
131  TRegistry::const_iterator it = m_AppReg.find(filetype);
132  if (it == m_AppReg.end()) {
134  << "CAppRegistry::Find(): no application associated with type "
135  << filetype
136  );
137  }
138 
139  return it->second;
140 }
141 
142 
143 
144 
146 {
147  return m_Registry;
148 }
149 
150 
151 bool CAppPopup::PopupFile(const string& file, TFileType filetype)
152 {
153  if (file.empty()) {
154  return false;
155  }
156 
157 #ifdef NCBI_OS_MSWIN
158 
159  return PopupURL(file);
160 
161 #elif defined(NCBI_OS_DARWIN)
162 
163  FSRef fileRef;
164  OSErr err = FSPathMakeRef((const UInt8 *) file.c_str(), &fileRef, NULL);
165  if (err == noErr) {
166  err = LSOpenFSRef(&fileRef, NULL);
167  }
168  return (err == noErr);
169 
170 #else
171 
172  const CRef<CAppInfo>& ai = m_Registry.Find(filetype);
173  const string& prog = ai->GetExePath();
174  const string app(prog + " " + file);
175  return CExec::System(app.c_str()) != 0;
176 
177 #endif
178 }
179 
180 
181 bool CAppPopup::PopupURL(const string& url)
182 {
183  _TRACE(Info << "CAppPopup::PopupURL: opening URL: " << url);
184  if (url.empty()) {
185  return false;
186  }
187 
188 #ifdef NCBI_OS_MSWIN
189 
190  USES_CONVERSION;
191 
192  int res =
193  reinterpret_cast<int>(ShellExecute(NULL,
194  _T("open"),
195  CA2T(url.c_str()),
196  NULL,
197  NULL,
198  SW_SHOWNORMAL));
199  return res > 32;
200 
201 #elif defined(NCBI_OS_DARWIN)
202 
203  /* open the url with the default browser */
204 
205  CFURLRef urlRef = CFURLCreateWithBytes(kCFAllocatorDefault,
206  (const UInt8 *) url.c_str(),
207  url.size(),
208  kCFStringEncodingMacRoman,
209  NULL
210  );
211  const OSErr err = LSOpenCFURLRef(urlRef, NULL);
212  return (err == noErr);
213 
214 #elif defined(NCBI_OS_UNIX)
215 
216  // Use netscape/mozilla -remote mechanism is netscape is running
217  // Otherwise, launch browser anew
218 
219  string std_in, std_out, std_err;
220  vector<string> args;
221  int exit_status;
222 
223  vector<string> programs;
224  programs.push_back("firefox");
225  programs.push_back("mozilla");
226  programs.push_back("netscape");
227 
228  // try pop up a new window using netscape/mozilla -remote
229  args.push_back("-remote");
230  string arg = "openURL(" + url + ",new-window)";
231  args.push_back(arg);
232  ITERATE (vector<string>, program, programs) {
233  exit_status = CExecute::Exec(*program, args,
234  std_in, std_out, std_err);
235  if (!exit_status && std_err.empty()) {
236  return true;
237  }
238  }
239  // start a new browser
240  ITERATE (vector<string>, program, programs) {
241  try {
242  CExec::SpawnLP(CExec::eDetach, program->c_str(), url.c_str(), NULL);
243  }
244  catch(const std::exception&)
245  {
246  continue;
247  }
248  return true;
249  }
250  return false;
251 
252 #else
253 
254  return false;
255 
256 #endif
257 }
258 
259 
CAppRegistry s_CreateDefaultAppRegistry(void)
Definition: app_popup.cpp:103
class CAppInfo defines an interface for application registration information
Definition: app_popup.hpp:54
class CAppRegistry defines the registry of application information for automated application link-out...
Definition: app_popup.hpp:87
CRef –.
Definition: ncbiobj.hpp:618
const_iterator end() const
Definition: map.hpp:152
const_iterator find(const key_type &key) const
Definition: map.hpp:153
CNcbiOstream & operator<<(CNcbiOstream &out, const CEquivRange &range)
Definition: equiv_range.cpp:96
static int type
Definition: getdata.c:31
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define NULL
Definition: ncbistd.hpp:225
#define _TRACE(message)
Definition: ncbidbg.hpp:122
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
void Warning(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1191
void Info(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1185
static TExitCode System(const char *cmdline)
Execute the specified command.
Definition: ncbiexec.cpp:505
static CResult SpawnLP(EMode mode, const char *cmdname, const char *argv,...)
Spawn a new process with variable number of command-line arguments and find file to execute from the ...
Definition: ncbiexec.cpp:573
@ eDetach
Like eNoWait, continues to execute calling process; new process is run in background with no access t...
Definition: ncbiexec.hpp:107
TAppInfoRef & operator[](const TFileType &type)
Definition: app_popup.cpp:123
string m_Exepath
Definition: app_popup.hpp:61
TRegistry m_AppReg
Definition: app_popup.hpp:100
filetype::TFileType TFileType
Definition: app_popup.hpp:111
filetype::TFileType TFileType
Definition: app_popup.hpp:90
enum EFileType TFileType
Definition: app_popup.hpp:75
static int Exec(const string &cmd, const vector< string > &args, const string &std_in, string &std_out, string &std_err, STimeout *timeout=0)
Run an executable using strings for std* .
Definition: exec.cpp:42
const string & GetExePath(void) const
Definition: app_popup.cpp:79
static CAppRegistry m_Registry
Definition: app_popup.hpp:124
CAppInfo(const string &path=kEmptyStr)
Definition: app_popup.cpp:68
virtual ~CAppRegistry()
Definition: app_popup.cpp:118
virtual ~CAppInfo()
Definition: app_popup.cpp:74
static bool PopupFile(const string &file, TFileType filetype=filetype::eUnknown)
launch an application to handle a file
Definition: app_popup.cpp:151
const TAppInfoRef & Find(TFileType filetype) const
Definition: app_popup.cpp:129
static bool PopupURL(const string &url)
launch an application to handle a URL
Definition: app_popup.cpp:181
static CAppRegistry & GetRegistry(void)
retrieve the application registry
Definition: app_popup.cpp:145
@ ePdf
Definition: app_popup.hpp:73
@ eUnknown
Definition: app_popup.hpp:72
#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::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
FILE * file
static char * prog
Definition: mdb_load.c:33
Defines a portable execute class.
Definition: type.c:6
Modified on Mon Jun 17 05:07:17 2024 by modify_doxy.py rev. 669887