app_popup.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:8k
- /*
- * ===========================================================================
- * PRODUCTION $Log: app_popup.cpp,v $
- * PRODUCTION Revision 1000.2 2004/06/01 21:04:40 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.18
- * PRODUCTION
- * ===========================================================================
- */
- /* $Id: app_popup.cpp,v 1000.2 2004/06/01 21:04:40 gouriano Exp $
- * ===========================================================================
- *
- * PUBLIC DOMAIN NOTICE
- * National Center for Biotechnology Information
- *
- * This software/database is a "United States Government Work" under the
- * terms of the United States Copyright Act. It was written as part of
- * the author's official duties as a United States Government employee and
- * thus cannot be copyrighted. This software/database is freely available
- * to the public for use. The National Library of Medicine and the U.S.
- * Government have not placed any restriction on its use or reproduction.
- *
- * Although all reasonable efforts have been taken to ensure the accuracy
- * and reliability of the software and data, the NLM and the U.S.
- * Government do not and cannot warrant the performance or results that
- * may be obtained by using this software or data. The NLM and the U.S.
- * Government disclaim all warranties, express or implied, including
- * warranties of performance, merchantability or fitness for any particular
- * purpose.
- *
- * Please cite the author in any work or product based on this material.
- *
- * ===========================================================================
- *
- * Authors: Peter Meric
- *
- * File Description:
- * CAppPopup -- launch a url in a web browser
- */
- #include <ncbi_pch.hpp>
- #include <gui/utils/app_popup.hpp>
- #include <gui/utils/exec.hpp>
- #include <corelib/ncbiexec.hpp>
- #ifdef NCBI_OS_MSWIN
- #include <windows.h>
- #include <ShellAPI.h>
- #endif
- #if defined(NCBI_OS_MAC) || defined(NCBI_OS_DARWIN)
- #define __NOEXTENSIONS__
- #include <ApplicationServices/ApplicationServices.h>
- #endif
- BEGIN_NCBI_SCOPE
- //
- // function prototypes
- //
- CAppRegistry s_CreateDefaultAppRegistry(void);
- //
- // CAppPopup:: static member definition
- //
- CAppRegistry CAppPopup::m_Registry(s_CreateDefaultAppRegistry());
- CAppInfo::CAppInfo(const string& path)
- : m_Exepath(path)
- {
- }
- CAppInfo::~CAppInfo()
- {
- }
- const string& CAppInfo::GetExePath(void) const
- {
- return m_Exepath;
- }
- CNcbiOstream& filetype::operator<< (CNcbiOstream& strm,
- filetype::TFileType& ftype)
- {
- switch (ftype) {
- case filetype::ePdf:
- strm << "PDF";
- break;
- case filetype::eUnknown:
- strm << "Unknown";
- break;
- default:
- strm << "Unrecognized file type";
- break;
- }
- return strm;
- }
- CAppRegistry s_CreateDefaultAppRegistry(void)
- {
- CAppRegistry ar;
- ar[filetype::ePdf] = CRef<CAppInfo>(new CAppInfo("acroread"));
- return ar;
- }
- CAppRegistry::CAppRegistry()
- {
- }
- CAppRegistry::~CAppRegistry()
- {
- }
- CAppRegistry::TAppInfoRef& CAppRegistry::operator[](const TFileType& type)
- {
- return m_AppReg[type];
- }
- const CAppRegistry::TAppInfoRef& CAppRegistry::Find(TFileType filetype) const
- {
- TRegistry::const_iterator it = m_AppReg.find(filetype);
- if (it == m_AppReg.end()) {
- ERR_POST(Warning
- << "CAppRegistry::Find(): no application associated with type "
- << filetype
- );
- }
- return it->second;
- }
- CAppRegistry& CAppPopup::GetRegistry(void)
- {
- return m_Registry;
- }
- bool CAppPopup::PopupFile(const string& file, TFileType filetype)
- {
- if (file.empty()) {
- return false;
- }
- #ifdef NCBI_OS_MSWIN
- return PopupURL(file);
- #elif defined(NCBI_OS_DARWIN)
-
- FSRef fileRef;
- OSErr err = FSPathMakeRef((const UInt8 *) file.c_str(), &fileRef, NULL);
- if (err == noErr) {
- err = LSOpenFSRef(&fileRef, NULL);
- }
- return (err == noErr);
- #else
- const CRef<CAppInfo>& ai = m_Registry.Find(filetype);
- const string& prog = ai->GetExePath();
- const string app(prog + " " + file);
- return CExec::System(app.c_str()) != 0;
- #endif
- }
- bool CAppPopup::PopupURL(const string& url)
- {
- _TRACE(Info << "CAppPopup::PopupURL: opening URL: " << url);
- if (url.empty()) {
- return false;
- }
- #ifdef NCBI_OS_MSWIN
- int res =
- reinterpret_cast<int>(ShellExecute(NULL,
- "open",
- url.c_str(),
- NULL,
- NULL,
- SW_SHOWNORMAL));
- return res > 32;
- #elif defined(NCBI_OS_DARWIN)
- /* open the url with the default browser */
- CFURLRef urlRef = CFURLCreateWithBytes(kCFAllocatorDefault,
- (const UInt8 *) url.c_str(),
- url.size(),
- kCFStringEncodingMacRoman,
- NULL
- );
- const OSErr err = LSOpenCFURLRef(urlRef, NULL);
- return (err == noErr);
- #elif defined(NCBI_OS_UNIX)
- // Use netscape -remote mechanism is netscape is running
- // Otherwise, launch netscape anew
- string std_in, std_out, std_err;
- vector<string> args;
- int exit_status;
- // try pop up a new window using netscape -remote
- args.push_back("-remote");
- string arg = "openURL(" + url + ",new-window)";
- args.push_back(arg);
- exit_status = CExecute::Exec("netscape", args, std_in, std_out, std_err);
- if (!exit_status && std_err.empty()) {
- return true;
- } else {
- // start a new netscape
- exit_status = CExec::SpawnLP(CExec::eDetach, "netscape",
- url.c_str(), 0);
- return exit_status != -1;
- }
- #else
- return false;
- #endif
- }
- END_NCBI_SCOPE
- /*
- * ===========================================================================
- * $Log: app_popup.cpp,v $
- * Revision 1000.2 2004/06/01 21:04:40 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.18
- *
- * Revision 1.18 2004/05/21 22:27:50 gorelenk
- * Added PCH ncbi_pch.hpp
- *
- * Revision 1.17 2004/05/17 13:54:51 dicuccio
- * Added include for <widnows.h> in Win32-specific section
- *
- * Revision 1.16 2004/03/22 18:07:41 gorelenk
- * Changed implementations of CAppPopup::PopupURL and CAppPopup::PopupFile,
- * removed include of urlmon.h to get rid of usage urlmon.dll on Win32 .
- *
- * Revision 1.15 2003/09/26 15:47:22 jcherry
- * One less exec for linkouts under unix
- *
- * Revision 1.14 2003/09/25 23:04:26 jcherry
- * Made linkouts work on unix
- *
- * Revision 1.13 2003/09/24 20:10:35 dicuccio
- * Restore Shellapi.h for Win32
- *
- * Revision 1.12 2003/09/24 18:38:26 dicuccio
- * +ncbiexec.hpp for CExec
- *
- * Revision 1.11 2003/09/24 18:28:02 dicuccio
- * Dropped internal app spawn function in favor of CExec::System()
- *
- * Revision 1.10 2003/08/21 12:04:00 dicuccio
- * Code formatting changes. Changed typedefs to be standards-compliant
- *
- * Revision 1.9 2003/06/24 19:46:34 rsmith
- * Get the right Mac header
- *
- * Revision 1.8 2003/06/23 19:07:20 meric
- * Added tests for empty strings
- *
- * Revision 1.7 2003/06/23 18:47:46 rsmith
- * app_popup.cpp
- *
- * Revision 1.6 2003/06/19 16:39:33 meric
- * Renamed AppInfo to CAppInfo
- *
- * Revision 1.5 2003/06/19 16:32:57 meric
- * Added CAppRegistry, a simple application registry for file types
- * Added unix support for opening files associated with applications
- *
- * Revision 1.4 2003/06/17 19:50:25 meric
- * Replace Popup() with PopupFile() and PopupURL()
- *
- * Revision 1.3 2003/06/16 16:56:58 dicuccio
- * Remove unneeded header for widening strings. Fix resize of dlg box
- *
- * Revision 1.2 2003/06/16 16:01:38 dicuccio
- * Moved generic print code code from opengl/print to utils.
- *
- * Revision 1.1 2003/06/13 18:53:05 meric
- * Initial revision
- *
- *
- * ===========================================================================
- */