/*
  KPF - Public fileserver for KDE

  Copyright 2001 Rik Hemsley (rikkus) <rik@kde.org>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to
  deal in the Software without restriction, including without limitation the
  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  sell copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <ctime>
#include <clocale>

#include <tqfile.h>
#include <tqregexp.h>

#include <tdelocale.h>

#include "Defines.h"
#include "Utils.h"

namespace KPF
{
  static bool dateInitDone = false;

  static TQStringList monthList;

  static const char rfc1123Format[] = "%a, %d %b %Y %H:%M:%S GMT";

  static time_t qDateTimeToTimeT(const TQDateTime & t)
  {
    struct tm tempTm;

    tempTm.tm_year  = t.date().year();
    tempTm.tm_mon   = t.date().month();
    tempTm.tm_mday  = t.date().day();
    tempTm.tm_hour  = t.time().hour();
    tempTm.tm_min   = t.time().minute();
    tempTm.tm_sec   = t.time().second();
    tempTm.tm_isdst = -1;

    // Fix up differences between TQDateTime and tm.

    tempTm.tm_year -= 1900;

    --tempTm.tm_mon;

    return ::mktime(&tempTm);
  }

    TQDateTime
  toGMT(const TQDateTime & dt)
  {
    time_t dtAsTimeT = qDateTimeToTimeT(dt);

    struct tm * dtAsGmTm = ::gmtime(&dtAsTimeT);

    if (0 == dtAsGmTm)
      return TQDateTime();

    time_t dtAsGmTimeT = ::mktime(dtAsGmTm);

    TQDateTime ret;

    ret.setTime_t(dtAsGmTimeT);

    return ret;
  }

  void dateInit()
  {
    if (dateInitDone)
      return;

    dateInitDone = true;

    monthList
      << "Jan"
      << "Feb"
      << "Mar"
      << "Apr"
      << "May"
      << "Jun"
      << "Jul"
      << "Aug"
      << "Sep"
      << "Oct"
      << "Nov"
      << "Dec"
      ;
  }

  TQString dateString()
  {
    return dateString(TQDateTime::currentDateTime());
  }


  TQString dateString(const TQDateTime & t)
  {
    time_t asTimeT = qDateTimeToTimeT(t);

    struct tm * asTm = ::gmtime(&asTimeT);

    if (0 == asTm)
    {
      kpfDebug << "::gmtime() failed" << endl;
      return TQString();
    }

    asTm->tm_isdst = -1;

    const int len = 128;

    char buf[len];

    // Ensure we use locale "C" for strftime.

    TQCString oldLC_ALL  = ::strdup(::setlocale(LC_TIME, "C"));
    TQCString oldLC_TIME = ::strdup(::setlocale(LC_ALL, "C"));
    {
      ::strftime(static_cast<char *>(buf), len, rfc1123Format, asTm);
    }
    ::setlocale(LC_TIME,  oldLC_TIME.data());
    ::setlocale(LC_ALL,   oldLC_ALL.data());

    return TQString::fromUtf8(buf);
  }

    bool
  parseDate(const TQString & s, TQDateTime & dt)
  {
    dateInit();

//    kpfDebug << "Parsing date `" << s << "'" << endl;

    TQStringList l(TQStringList::split(' ', s));

    switch (l.count())
    {
      case 4:
//        kpfDebug << "Date type is RFC 850" << endl;
        return parseDateRFC850(l, dt);
        break;
      case 5:
//        kpfDebug << "Date type is asctime" << endl;
        return parseDateAscTime(l, dt);
        break;
      case 6:
//        kpfDebug << "Date type is RFC1123" << endl;
        return parseDateRFC1123(l, dt);
        break;
      default:
//        kpfDebug << "Date type is unknown" << endl;
        return false;
        break;
    }
    
    return false;
  }

    bool
  parseDateRFC1123(const TQStringList & l, TQDateTime & dt)
  {
    if ("GMT" != l[5])
      return false;

    uint day(l[1].toUInt());

    bool haveMonth = false;
    uint month = 0;

    TQStringList::ConstIterator it;

    for (it = monthList.begin(); it != monthList.end(); ++it)
    {
      if (*it == l[2])
      {
        haveMonth = true;
        break;
      }

      ++month;
    }

    if (!haveMonth)
      return false;

    uint year(l[3].toUInt());

    TQStringList timeTokenList(TQStringList::split(':', l[4]));

    if (3 != timeTokenList.count())
      return false;

    uint hours    (timeTokenList[0].toUInt());
    uint minutes  (timeTokenList[1].toUInt());
    uint seconds  (timeTokenList[2].toUInt());

    dt.setDate(TQDate(year, month + 1, day));
    dt.setTime(TQTime(hours, minutes, seconds));

    return dt.isValid();
  }

    bool
  parseDateRFC850(const TQStringList & l, TQDateTime & dt)
  {
    if ("GMT" != l[3])
      return false;

    TQStringList dateTokenList(TQStringList::split('-', l[1]));

    if (3 != dateTokenList.count())
      return false;

    uint day(dateTokenList[0].toUInt());

    bool haveMonth = false;
    uint month = 0;

    TQStringList::ConstIterator it;

    for (it = monthList.begin(); it != monthList.end(); ++it)
    {
      if (*it == dateTokenList[1])
      {
        haveMonth = true;
        break;
      }

      ++month;
    }

    if (!haveMonth)
      return false;

    uint year(dateTokenList[2].toUInt());

    if (year < 50)
      year += 2000;
    else if (year < 100)
      year += 1900;

    TQStringList timeTokenList(TQStringList::split(':', l[2]));

    if (3 != timeTokenList.count())
      return false;

    uint hours    (timeTokenList[0].toUInt());
    uint minutes  (timeTokenList[1].toUInt());
    uint seconds  (timeTokenList[2].toUInt());

    dt.setDate(TQDate(year, month + 1, day));
    dt.setTime(TQTime(hours, minutes, seconds));

    return dt.isValid();
  }

     bool
  parseDateAscTime(const TQStringList & l, TQDateTime & dt)
  {
    bool haveMonth = false;
    uint month = 0;

    TQStringList::ConstIterator it;

    for (it = monthList.begin(); it != monthList.end(); ++it)
    {
      if (*it == l[1])
      {
        haveMonth = true;
        break;
      }

      ++month;
    }

    if (!haveMonth)
      return false;

    uint day(l[2].toUInt());

    TQStringList timeTokenList(TQStringList::split(':', l[3]));

    if (3 != timeTokenList.count())
      return false;

    uint hours    (timeTokenList[0].toUInt());
    uint minutes  (timeTokenList[1].toUInt());
    uint seconds  (timeTokenList[2].toUInt());

    uint year(l[4].toUInt());

    dt.setDate(TQDate(year, month + 1, day));
    dt.setTime(TQTime(hours, minutes, seconds));

    return dt.isValid();
  }

     TQString
  translatedResponseName(uint code)
  {
    TQString s;

    switch (code)
    {
      case 200:
        s = i18n("OK");
        break;
      case 206:
        s = i18n("Partial content");
        break;
      case 304:
        s = i18n("Not modified");
        break;
      case 400:
        s = i18n("Bad request");
        break;
      case 403:
        s = i18n("Forbidden");
        break;
      case 404:
        s = i18n("Not found");
        break;
      case 412:
        s = i18n("Precondition failed");
        break;
      case 416:
        s = i18n("Bad range");
        break;
      case 500:
        s = i18n("Internal error");
        break;
      case 501:
        s = i18n("Not implemented");
        break;
      case 505:
        s = i18n("HTTP version not supported");
        break;
      default:
        s = i18n("Unknown");
        break;
    }

    return s;
  }

     TQString
  responseName(uint code)
  {
    TQString s;

    switch (code)
    {
      case 200:
        s = "OK";
        break;
      case 206:
        s = "Partial content";
        break;
      case 304:
        s = "Not modified";
        break;
      case 400:
        s = "Bad request";
        break;
      case 403:
        s = "Forbidden";
        break;
      case 404:
        s = "Not found";
        break;
      case 412:
        s = "Precondition failed";
        break;
      case 416:
        s = "Bad range";
        break;
      case 500:
        s = "Internal error";
        break;
      case 501:
        s = "Not implemented";
        break;
      case 505:
        s = "HTTP version not supported";
        break;
      default:
        s = "Unknown";
        break;
    }

    return s;
  }


} // End namespace KPF
