
#include "sysfsblpath.h"

sysfsBlPath::sysfsBlPath (const QString sysblp)
{
  sysfsblpath = sysblp;
  sysfsdir = sysblp;
  validblpath = false;
  brightness = maxbrightness = -1;
  find_sysfsBlPath();
}

sysfsBlPath::~sysfsBlPath()
{
}

bool sysfsBlPath::find_sysfsBlPath()
{
  /* check sysfs backlight dir exists */
  if (sysfsdir.exists() == false) {
    return validblpath = false;
  }
  QString tmp (sysfsblpath);
  const QFileInfoList *list = sysfsdir.entryInfoList();
  QFileInfoListIterator it (*list);
  QFileInfo *fi;

  /* iterate over directory contents */
  while ((fi = it.current()) != 0) {
    it += 1;
    /* skip dot-files */
    if (fi->fileName() == "." || fi->fileName() == "..") {
      continue;
    }
    /* set backlight directory base from sysfsblpath */
    blpath = sysfsblpath;
    /* add current directory element to backlight path */
    add_path_element (blpath, fi->fileName());
    /* initialize QDir bldir */
    bldir = blpath;
    /* validate directory exists */
    if (bldir.exists()) {
      bool ok;
      /* initialize backlight brightness and maxbrightness paths */
      blbrightness = blpath;
      blmaxbrightness = blpath;
      /* add the final element to the paths */
      add_path_element (blbrightness, SYSFSIFACEFILE);
      add_path_element (blmaxbrightness, SYSFSIFACEMAX);
      /* create QFile instances from baths */
      QFile qfbrightness (blbrightness),
            qfmaxbrightness (blmaxbrightness);
      /* validate both backlight interface files exist, read/convert values:
       *
       * read current brightness from interface
       */
      brightness = qfile_read_int (qfbrightness, ok);
      if (!ok) {
        qWarning ("error : read or conversion failed '%s'",
                  blbrightness.latin1());
        continue;
      }
      /* read max brightness value for interface */
      maxbrightness = qfile_read_int (qfmaxbrightness, ok);
      if (!ok) {
        qWarning ("error : read or conversion failed '%s'",
                  blmaxbrightness.latin1());
        continue;
      }
      /* set the backlight interface name used */
      bliface = fi->fileName();

      /* set validblpath true and break loop */
      validblpath = true;
      break;
    }
  }
#ifdef DEBUG
  if (validblpath) {
    qDebug ("found      : %s", blpath.latin1());
    qDebug ("brightness : %d", brightness);
    qDebug ("max value  : %d", maxbrightness);
    qDebug ("ifacename  : %s", bliface.latin1());
  }
#endif
  return validblpath;
}

const QString sysfsBlPath::get_sysfsBlPath()
{
  return sysfsblpath;
}

const QString sysfsBlPath::get_blPath()
{
  return blpath;
}

const QString sysfsBlPath::get_blIface()
{
  return bliface;
}

bool sysfsBlPath::valid_blPath()
{
  return validblpath;
}

QString sysfsBlPath::add_path_element (QString &s, const QString e)
{
  if (s.isEmpty() == false && s[s.length() - 1] != '/') {
    s.append ("/");
  }
  s.append (e);

  return s;
}

int sysfsBlPath::qfile_read_int (QFile &f, bool &ok)
{
  QString tmp;
  int value = -1;

  /* open and read string from file */
  if (f.open (IO_ReadOnly) && f.readLine (tmp, 32) >= 0) {
    /* convert value to int, setting result in ok */
    value = tmp.toInt (&ok, 10);
  }

  return value;     /* -1 on failure and ok = false */
}

int sysfsBlPath::get_brightness()
{
  return brightness;
}

int sysfsBlPath::get_maxbrightness()
{
  return maxbrightness;
}

