// src/global_preferences.hh - Copyright 2005, 2006, University
//               of Padova, dept. of Pure and Applied
//               Mathematics
//
// This file is part of SGPEMv2.
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SGPEMv2 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

#ifndef GLOBAL_PREFERENCES_HH
#define GLOBAL_PREFERENCES_HH 1

#include "config.h"

#include <glibmm/ustring.h>
#include <iostream>
#include <vector>

// Do not include complete template definition here:
#include <sgpemv2/templates/singleton.hh>
#include <sgpemv2/schedulable.hh>
#include <sgpemv2/request.hh>

namespace sgpem
{
  class GlobalPreferences;
  class KeyFile;
}

namespace sgpem
{
  /**
    \brief Contains all global application preferences

   */
  class SG_DLLEXPORT GlobalPreferences : public Singleton<GlobalPreferences>
  {
    friend class Singleton<GlobalPreferences>;

  public:

    typedef std::vector<Glib::ustring>::const_iterator dir_iterator;

    dir_iterator modules_dir_begin() const;
    dir_iterator modules_dir_end() const;

    dir_iterator policies_dir_begin() const;
    dir_iterator policies_dir_end() const;



    /**
      \return GlobalPreferences configuration filename
    */
    const Glib::ustring get_config_filename();

    /**
      \brief Adds a directory search entry to modules dirs
     */
    void add_modules_dir(const Glib::ustring& moddir);

    /**
      \brief Adds a directory search entry to policies dirs
     */
    void add_policies_dir(const Glib::ustring& poldir);

    /**
      \brief Returns configured Policy directories

      \return ::_policy_dirs
    */
    std::vector<Glib::ustring>& get_policy_dirs();

    /**
      \brief Returns configured Plugin directories

      \return ::_plugin_dirs
    */
    std::vector<Glib::ustring>& get_plugin_dirs();

    /**
      \brief Returns the simumulation speed
      
      \return _speed
     */
    int get_speed();

    /**
      \brief Returns the simumulation speed and return old speed
      
      \return _speed
     */
    int set_speed(int new_speed);

    /**
      \return A HTML color name or hexadecimal code
     */
    Glib::ustring get_schedulable_color(Schedulable::state st) const;

    /**
      \brief Also works with SubRequest::state

      \return A HTML color name or hexadecimal code
     */
    Glib::ustring get_request_color(Request::state st) const;
    
    /**
      \brief Writes preferences to disk
      
      In order, this method should:
      #- Call get_preferences_dir()
      #- Open the file "config" inside this directory, in write mode
      #- Serialize options to disk with the format described in the associated ProductDefinition document.
      That is: simulation speed and configured directories.
      We advice using a key=value text format.
      #- Close the configuration file.
     */
    void write_configrc();
    /**
      \brief Load global preferences from disk

      This should be done once during program startup.
      Attempt opening the get_preferences_dir() + "/config" file.
      
      \throw std::io_error
     */
    void load_configrc();

    void write_configrc(std::ostream &os);
    void load_configrc(std::istream &is);
  private:
    GlobalPreferences();
    GlobalPreferences(const GlobalPreferences&);
    GlobalPreferences& operator=(const GlobalPreferences&);

    void key_file_read(KeyFile& kf);
    void key_file_write(KeyFile& kf);

    /**
      \brief Returns directories to search for plugins
     */
    std::vector<Glib::ustring>  _mod_dirs;
    /**
      \brief Returns directories to search for policies
     */
    std::vector<Glib::ustring>  _pol_dirs;
    /*
      These directories can be added to SGPEM in the following ways:
        -# Hardcoded (usually done for the default installation directory on *n?x-systems, but NOT for Microsoft Windows)
        -# Retrieved from a Windows registry key (only on Micro$oft platforms) set at installation time
        -# Via command line arguments
        -# By a backend::Plugin::on_init() method called when loading an external DSO. This is perfectly normal and permitted.
     */

    int _speed;
  };
}

#endif
