/***************************************************************************
 *   Copyright (C) 2004 by Judd Baileys                                    *
 *   jbaileys@bigpond.net.au                                               *
 *                                                                         *
 *   This program 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.                                   *
 *                                                                         *
 *   This program 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 this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef CONFIGURATION_H
#define CONFIGURATION_H

#define MAX_ACCOUNTS 100
#define MAX_THEMES 100
#define MAX_DNS_ENTRIES 5
#define PPPD_TIMEOUT 30
#define PPP_STATS_INTERVAL 200

#include <unistd.h>
#include <sys/types.h>

#include <qstring.h>
#include <qstringlist.h>
#include <qcolor.h>
#include "kdslconfig.h"

class PrefDialog;
class DockWidget;
class KConfig;
class KGlobal;

// string lengths
#define PATH_SIZE 120
//#define MODEMSTR_SIZE 80
#define ACCNAME_SIZE 50
//#define PHONENUMBER_SIZE 60
#define COMMAND_SIZE 255
#define IPADDR_SIZE 15
#define DOMAIN_SIZE 50
#define TIMEOUT_SIZE 60

// keys for config file

#define GENERAL_GRP    "General"
#define ACCOUNT_GRP   "Accounts"
#define MODEM_GRP	"Modem"
#define THEME_GRP        "Themes"
#define TASK_GRP		"Tasks"
#define GRAPH_GRP          "Graph"
#define WINPOS_GRP         "WindowPosition"

// general
#define DEFAULTACCOUNT_KEY  "DefaultAccount"
#define PPPDVERSION_KEY    "pppdVersion"
#define PPPDTIMEOUT_KEY    "pppdTimeout"
#define CLOCK_KEY      "ShowClock"
#define SHOWLOGWIN_KEY     "ShowLogWindow"
#define AUTOREDIAL_KEY     "AutomaticRedial"
#define DISCONNECT_KEY     "DisconnectOnXServerExit"
#define QUITONDISCONNECT_KEY "QuitOnDisconnect"
#define NUMACCOUNTS_KEY    "NumberOfAccounts"
#define NUMTASKS_KEY		"NumberOfTasks"
#define ID_KEY		   "ID"
#define PLUGIN_KEY "PPPOE Plugin"
#define USE_PLUGIN_KEY "Use Plugin"
#define HIDE_IP_KEY "Hide IP"
#define AUTOSTART_KEY "Autostart"
#define CONNECT_KEY "Connect on Startup"
#define USB_KEY "USB modem Support"
#define BROWSER_KEY "External Browser"
#define USE_BROWSER_KEY "Use External Browser"
#define USE_SCHEDULER_KEY "Scheduler Enabled"
#define AUTO_START_KEY "Autostart Connection"
#define AUTO_START_TIME_KEY "Autostart at"
#define AUTO_STOP_KEY "Autostop Connection"
#define AUTO_STOP_TIME_KEY "Autostop at"

// account
#define NAME_KEY           "Name"
#define AUTH_KEY           "Authentication"
#define STORED_PASSWORD_KEY "Password"
#define STORED_USERNAME_KEY "Username"
#define STORE_PASSWORD_KEY "StorePassword"
//#define DEFAULTROUTE_KEY   "DefaultRoute"
#define STORED_USAGE_KEY "Usage URL"
#define STORE_USAGE_KEY "Enable Usage Meter"
#define DNS_KEY            "DNS"
#define AUTODNS_KEY        "AutoDNS"
#define IPADDR_KEY         "IPAddr"
#define AUTONAME_KEY       "AutoName"
#define ICONIFY_ON_CONNECT_KEY "iconifyOnConnect"
// #define DOCKING_KEY        "DockIntoPanel"
#define SOUND_KEY "Disable Sounds"
#define NOTIFY_KEY "Disable Notification"
#define PROTOCOL_KEY "Protocol to Use"
#define ACCOUNT_ICON_KEY "Account Icon"
#define PHONE_NUMBER_KEY "Phone Number"
#define STORED_ETH_DEVICE_KEY "ETH"
#define STORED_SPEEDCONTROL_KEY "Modem Speed"
#define LOCK_MODEM_KEY "Modem Locked"
#define FLOWCONTROL_KEY "Flow Control"
#define STORED_ACC_TYPE_KEY "AccountType"
#define PPPDARG_KEY        "pppdArguments"
#define BEFORE_CONNECT_KEY  "BeforeConnect"
#define COMMAND_KEY        "Command"
#define DISCONNECT_COMMAND_KEY "DisconnectCommand"
#define BEFORE_DISCONNECT_KEY  "BeforeDisconnect"
#define MODEMDEV_KEY       "Device"
#define MODEMVOL_KEY       "Modem Volume"
#define TIMEOUT_KEY        "Timeout"
#define IDLE_KEY  "Disconnect when idle for"
#define POS_X_KEY "X Co-ordinate"
#define POS_Y_KEY "Y Co-ordinate"
#define DEFAULTROUTE_KEY "Use default route"
#define ALLOW_ROOT_KEY "Allow Non Root"

// themename
#define DEFAULTTHEME_KEY  "DefaultTheme"
#define NUMTHEMES_KEY      "NumberofThemes"
#define ICON_NAME_KEY     "Name"
#define USE_COLORS_KEY   "Custom Notify Colors"
#define BACKGROUND_COLOR_KEY "Notify Background Color"
#define TEXT_COLOR_KEY   "Notify Text Color"

// Tasks
#define NUMTASKS_KEY		"NumberOfTasks"
#define TASK_NAME_KEY		"TaskName"
#define TASK_START_TIME_KEY "Start Time"
#define TASK_START_DATE_KEY "Start Date"
#define TASK_END_TIME_KEY  "End TIme"
#define TASK_END_DATE_KEY "End Date"
#define TASK_FREQUENCY_KEY "Frequency"
#define WINPOS_STATWIN_X   "WindowPositionStatWinX"
#define WINPOS_STATWIN_Y   "WindowPositionStatWinY"
// pppd errors
#define E_IF_TIMEOUT       1
#define E_PPPD_DIED        2

#if defined(__linux__) || defined(BSD)
#define PPP_PID_DIR "/var/run/"
#else
#define PPP_PID_DIR "/etc/ppp/"
#endif

//class PrefDialog;
class Configuration {
public:
  Configuration();
  ~Configuration() {};

   enum { NumInitStrings = 2 };

  // general functions
  bool open();
  void save();
  void cancel();
  int  access() const;       // read/write access

    // function to read/write date to configuration file
  QString readConfig(const QString &, const QString &, const QString &);
  QColor readColorConfig(const QString &, const QString &, const QColor *);
  int readNumConfig(const QString &, const QString &, int);
   int readNumConfig(const QString &, const QString &, int, int, int); 
  bool readListConfig(const QString &, const QString &,QStringList &, char sep = ',');
  void writeConfig(const QString &, const QString &, const QString &);
  void writeConfig(const QString &, const QString &, const QColor &);
  void writeConfig(const QString &, const QString &, int);
   void writeConfig(const QString &, const QString &, int, int, int); 
  void copyConfig(const QString &);
  void writeListConfig(const QString &, const QString &,
                       QStringList &, char sep = ',');


  // return the current account group
  QString currentGroup() { return cgroup; }
  QString currentThemeGroup() { return tgroup; }
  QString currnetTaskGroup() { return tagroup; }

  // functions to set/get general kdsl info
  QString password() const;
  void setPassword(const QString &);

  const QString defaultAccount();
  void setDefaultAccount(const QString &);

 // functions to set/get account information
  int count() const;
  bool setAccount(const QString &);
  bool setAccountbyIndex(int);

  bool isUniqueAccname(const QString &);

  bool deleteAccount();
  bool deleteAccount(const QString &);

  int newaccount();
  int copyaccount(int i);

// task stuff
  int taskcount() const;
  
  bool setTask(const QString &);
  bool setTaskbyIndex(int);  
  
// theme stuff
  int themecount() const;
  const QString defaultTheme();
  void setDefaultTheme(const QString &);

  bool setTheme(const QString &);
  bool setThemebyIndex(int);
  
  const QString storedModemVolume();
  void setStoredModemVolume(const QString &); 
  
  bool storeModemVolume();
  bool setStoreModemVolume(bool);

  bool isUniqueThemename(const QString &);

  bool deleteTheme();
  bool deleteTheme(const QString &);
  
  bool deleteTask();
  bool deleteTask(const QString &);
  
  int newtask();

  int newtheme();
  
  int pos_x();
  void setPos_x(int);
  
  int pos_y();
  void setPos_y(int);
  
  const QString usageURL();
  void setUsageURL(const QString &);
  
  const QString protocol();
  void setProtocol(const QString &);
 
   const QString phoneNumber();
  void setPhoneNumber(const QString &); 
   
  bool storeUsage();
  void setStoreUsage(bool);
  
  bool storeScheduler();
  void setStoreScheduler(bool);
  
  bool storeAutoStartScheduler();
  void setStoreAutoStartScheduler(bool);
  
  bool storeAutoStopScheduler();
  void setStoreAutoStopScheduler(bool);

  const QString autoStartTime();
  void setAutoStartTime(const QString &);
  
  const QString autoStopTime();
  void setAutoStopTime(const QString &);
  
  const QString browserURL();
  void setBrowserURL(const QString &);
  
  bool storeBrowserUsage();
  void setStoreBrowserUsage(bool);
  
  bool storeNotifyColors();
  void setStoreNotifyColors(bool);
  
  const  QString notifyBgColor();
  void setNotifyBgColor(QColor &);
  
  QColor notifyTextColor();
  void setNotifyTextColor(QColor &); 
  
  bool lockModem();
  void setLockModem(bool);
  
  const QString pluginname();
  void setPluginName(const QString &);

  bool storeUsePlugin();
  void setStoreUsePlugin(bool);
  
  bool storeConnect();
  void setStoreConnect(bool);  
 
  bool storeClock();
  void setStoreClock(bool);   
  
  bool storeDefaultRoute();
  void setStoreDefaultRoute(bool);
   
  bool storeEnableSound();
  void setStoreEnableSound(bool);   

  bool storeEnableNotify();
  void setStoreEnableNotify(bool);    
  
  bool storeEnableRoot();
  void setStoreEnableRoot(bool);
  
  const QString taskname();
  void setTaskname(const QString &);
    
  const QString themename();
  void setThemename(const QString &);
  
  const QString accountIconName();
  void setAccountIconName(const QString &); 

  const QString accname();
  void setAccname(const QString &);

  const QString storedUsername();
  void setStoredUsername(const QString &);
  
  const QString storedPassword();
  void setStoredPassword(const QString &);

  bool storePassword();
  void setStorePassword(bool);
  
  bool storeHideIP();
  void setStoreHideIP(bool);

  bool storeAutostart();
  void setStoreAutostart(bool);
  
  const QString command_before_connect();
  void setCommand_before_connect(const QString &);

  const QString command_on_connect();
  void setCommand_on_connect(const QString &);

  const QString command_on_disconnect();
  void setCommand_on_disconnect(const QString &);

  const QString command_before_disconnect();
  void setCommand_before_disconnect(const QString &);

  void set_quit_on_disconnect(bool);
  bool quit_on_disconnect();

  void set_show_log_window(bool set);
  bool get_show_log_window();
  
  void set_usb(bool);
  bool get_usb();

  const QString storedAccType();
  void setStoredAccType(const QString &);

  const QString storedTaskStartTime();
  void setStoredTaskStartTime(const QString &);
  
  const QString storedTaskStartDate();
  void setStoredTaskStartDate(const QString &);
  
  const QString storedTaskEndTime();
  void setStoredTaskEndTime(const QString &);
  
  const QString storedTaskEndDate();
  void setStoredTaskEndDate(const QString &);
  
  const QString storedTaskFrequency();
  void setStoredTaskFrequency(const QString &);
  
   
  const QString storedEthDevice();
  void setStoredEthDevice(const QString &);

  bool storeEthDevice();
  void setStoreEthDevice(bool);

  const QString ipaddr();
  void setIpaddr(const QString &);

  const QString modemDevice();
  void setModemDevice(const QString &);
  
  const QString flowControl();
  void setFlowControl(const QString &);
  
  const QString speedControl();
  void setSpeedControl(const QString &);

  bool defaultroute();
  void setDefaultroute(bool set);

  int modemTimeout();
  void setModemTimeout(int);
  
  int idleTimeout();
  void setIIdleTimeout(int); 

  int authMethod();
  void setAuthMethod(int);

  bool autoDNS();
  void setAutoDNS(bool set);

  QStringList &dns();
  void setDns(QStringList &);

  //functions to change/set the child pppd process info
  bool pppdRunning() const;
  void setpppdRunning(bool set);

  int pppdError() const;
  void setpppdError(int err);
  
  int pppdErrorMsg();
  void setpppdErrorMsg(int err);

  QStringList &pppdArgument();
  void setpppdArgumentDefaults();
  void setpppdArgument(QStringList &);

  QString pppdVersion();
  bool pppdVersionMin(int ver, int mod, int patch);

  int pppdTimeout();
  void setpppdTimeout(int);

  void winPosStatWin(int &, int &);
  void setWinPosStatWin(int, int);
  
DockWidget* dock_widget;

  private:
  QString passwd;
  KConfig* config;
  int highcount;
  int themehighcount;
  int taskhighcount;
  QString taskgroup;
  int cthemeaccount;
  int caccount;
  int ctaskname;
  QString cgroup;
  QString tgroup;
  QString tagroup;
  pid_t suidprocessid;                   // process ID of setuid child
  bool pppdisrunning;                    // pppd process
                                         
int pppderrormsg;			// daemon
  int pppderror;                         // error encounterd running pppd
  int pppdVer, pppdMod, pppdPatch;       // pppd version

};

extern Configuration gpppdata;




#endif  // CONFIGURATION_H
