Skip to content
Snippets Groups Projects
ConfigService.cpp 69 KiB
Newer Older
 */
void ConfigServiceImpl::removeObserver(const Poco::AbstractObserver& observer) const
{
  m_notificationCenter.removeObserver(observer);
/*
Ammend paths to point to include the paraview core libraries.
@param path : path to add
*/
void ConfigServiceImpl::setParaviewLibraryPath(const std::string& path)
{
#ifdef _WIN32
  const std::string platformPathName = "PATH";
Owen Arnold's avatar
Owen Arnold committed
  Poco::Path existingPath;
  char separator = existingPath.pathSeparator();
  std::string strSeparator;
  strSeparator.push_back(separator);

  if(Poco::Environment::has(platformPathName))
  {
    existingPath = Poco::Environment::get(platformPathName);
    existingPath.append(strSeparator + path);
Owen Arnold's avatar
Owen Arnold committed
  }
  else
  {
    existingPath = path;
  }
  const std::string newPath = existingPath.toString();
  Poco::Environment::set(platformPathName, newPath);
  UNUSED_ARG(path)
  throw std::runtime_error("Cannot dynamically set the library path on Linux");
#elif defined __APPLE__
  throw std::runtime_error("Cannot dynamically set the library path on Mac");
#else
  throw std::runtime_error("ConfigServiceImpl::setParaviewLibraryPath cannot determine the running platform and therefore cannot set the path to the Paraview libraries.");
#endif 
/*
Extracts the string from a poco pipe and returns the numerical part.
@param pipe : input pipe.
@return the numerical part of the version string contained inside the pipe.
*/
const std::string extractVersionNumberFromPipe(const Poco::Pipe& pipe)
{
  std::string versionString = "undetermined";
  Poco::PipeInputStream pipeStream(pipe);
  std::stringstream stringStream;
  Poco::StreamCopier::copyStream(pipeStream, stringStream);
  const std::string givenVersion = stringStream.str();
  boost::smatch  match;
  boost::regex expression("(\\d+)\\.(\\d+)\\.?(\\d*)$"); // Gets the version number part.
  if(boost::regex_search(givenVersion, match, expression))
  {
    versionString = match[0];
  }
  return versionString;
}

Checks to see whether paraview usage is explicitly ignored in the property file then, 
quick check to determine if paraview is installed. We make the assumption 
that if the executable paraview binary is on the path that the paraview libraries 
will also be available on the library path, or equivalent.
@return True if paraview is available or not disabled.
*/
bool ConfigServiceImpl::quickParaViewCheck() const
{
  const std::string paraviewIgnoreProperty = "paraview.ignore";
  const bool ignoreParaview = hasProperty(paraviewIgnoreProperty) && atoi(getString(paraviewIgnoreProperty).c_str());
  if(ignoreParaview)
  {
    g_log.debug("Ignoring ParaView");
  g_log.debug("Checking for ParaView");
    //Try to run "paraview -V", which will succeed if ParaView is installed.
    std::string paraviewDir = getString("paraview.path");
    std::string cmd = "paraview";
    if(!paraviewDir.empty())
    {
      Poco::Path paraviewExe = Poco::Path(paraviewDir, "paraview");
      cmd = paraviewExe.toString();
    }
    std::vector<std::string> args;
    Poco::Pipe outPipe, errorPipe;
    Poco::ProcessHandle ph = Poco::Process::launch(cmd, args, 0, &outPipe, &errorPipe);
    const int rc = ph.wait();
    // Only if the paraview query returned successfully.
      // Check the actual version numbers against what we expect they should be.
      const std::string givenVersionNumber = extractVersionNumberFromPipe(errorPipe);
      const std::string targetVersionNumber = ParaViewVersion::targetVersion();
      if (givenVersionNumber == targetVersionNumber)
      {
        isAvailable = true;
        g_log.information("ParaView is available");
        // Now set the plugin path.
        this->setParaViewPluginPath();
      }
      else
      {
        std::stringstream messageStream;
        messageStream << "The compatible version of ParaView is " << targetVersionNumber << " but the installed version is " << givenVersionNumber;
        g_log.debug(messageStream.str());
        g_log.information("ParaView is not available");
      std::stringstream messageStream;
      messageStream << "ParaView version query failed with code: " << rc;
      g_log.debug(messageStream.str());
      g_log.information("ParaView is not available");
  catch(Poco::SystemException &e)
    g_log.debug(e.what());
    g_log.information("ParaView is not available");
/*
Quick check to determine if VATES is installed.
@return TRUE if available.
*/
bool ConfigServiceImpl::quickVatesCheck() const
{
  std::string path = this->getDirectoryOfExecutable();

  Poco::File dir(path);
  typedef std::vector<std::string> VecFiles;

  VecFiles files;
  dir.list(files);
  VecFiles::iterator it = files.begin();

  bool found = false;
  while(it != files.end())
  {
    std::string file = *it;
    boost::regex expression("^(VatesSimpleGui)", boost::regex::icase);
    if(boost::regex_search(file, expression))
/*
Gets the system proxy information
@url A url to match the proxy to
@return the proxy information.
*/
Kernel::ProxyInfo& ConfigServiceImpl::getProxy(const std::string& url)
{
  if (!m_isProxySet)
  {  
    //set the proxy
    //first check if the proxy is defined in the properties file
    std::string proxyHost;
    int proxyPort;
    if ((getValue("proxy.host",proxyHost) == 1) && (getValue("proxy.port",proxyPort) == 1))
    {
      //set it from the config values
      m_proxyInfo = ProxyInfo(proxyHost,proxyPort,true);
    }
    else
    {
      //get the system proxy
      Poco::URI uri(url);
      Mantid::Kernel::NetworkProxy proxyHelper;
      m_proxyInfo = proxyHelper.getHttpProxy(uri.toString());
    }      
    m_isProxySet = true;
  }
  return m_proxyInfo;
}

/**
 * Gets the path to ParaView.
 * @returns The ParaView path.
 */
const std::string ConfigServiceImpl::getParaViewPath() const
{
  return getString("paraview.path");
/// \cond TEMPLATE
template DLLExport int ConfigServiceImpl::getValue(const std::string&, double&);
template DLLExport int ConfigServiceImpl::getValue(const std::string&, std::string&);
template DLLExport int ConfigServiceImpl::getValue(const std::string&, int&);
Peterson, Peter's avatar
Peterson, Peter committed
template DLLExport int ConfigServiceImpl::getValue(const std::string&, std::size_t&);

} // namespace Kernel
} // namespace Mantid