/*
*  Naamari - an AES/SHA-based manual file crypto-synchronizer for cloud transfers
*  Copyright (c) 2015-2017 Sunkware
*  Key FP = 6B6D C8E9 3438 6E9C 3D97  56E5 2CE9 A476 99EF 28F6
*
*  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 3 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, see <http://www.gnu.org/licenses/>.
*
*  ----------------------------------------------------------------
*  WWW:       sunkware.org
*  E-mail:    sunkware@gmail.com
*  ----------------------------------------------------------------
*/

#include "stateglob.h"

namespace cryptrav
{


StateGlob::StateGlob(ContextGlob* pContext)
{
 contextP = pContext;

 counter = 0; // not much entropy...

 pathInHallway = "/";
 pathInNominis = "/";
}

QString        StateGlob::getPathInHallway(void)
{
 return        pathInHallway;
}

void           StateGlob::setPathInHallway(QString path)
{
 while (path.endsWith("//"))
  path.chop(1);

 pathInHallway = path;
}

QString        StateGlob::getPathInNominis(void)
{
 return        pathInNominis;
}

void           StateGlob::setPathInNominis(QString path)
{
 while (path.endsWith("//"))
  path.chop(1);

 pathInNominis = path;
}


bool           StateGlob::chdirInHallway(QString dirName)
{
 Pather        pather(contextP);

 if (dirName == ".")
  return true;


 if (dirName != "..")
  {
   QString newPath;

   if (dirName.startsWith("/"))
    newPath = dirName;
     else
    newPath = pathInHallway + dirName;

   newPath += "/";


   if ( !QDir(pather.getHallwayAbs(newPath)).exists() )
    return false;


   QDir newDir(pather.getHallwayAbs(newPath));
   setPathInHallway(pather.getHallwayRel(newDir.absolutePath()) + "/"); // to shorten paths like "dir/../dir" -> "dir"
  }
   else // going up 1 level... if we don't jump out of hallway folder
  {
   QDir newDir(pather.getHallwayAbs(pathInHallway));

   newDir.cdUp();

   if ( !newDir.absolutePath().startsWith(pather.getHallwayAbs("")) )
    return false;

   setPathInHallway(pather.getHallwayRel(newDir.absolutePath()) + "/");
  }

 return true;
}

bool           StateGlob::chdirInNominis(QString dirName)
{
 Pather        pather(contextP);

 if (dirName == ".")
  return true;


 if (dirName != "..")
  {
   QString newPath;

   if (dirName.startsWith("/"))
    newPath = dirName;
     else
    newPath = pathInNominis + dirName;

   newPath += "/";


   if ( !QDir(pather.getNominisAbs(newPath)).exists() )
    return false;


   QDir newDir(pather.getNominisAbs(newPath));
   setPathInNominis(pather.getNominisRel(newDir.absolutePath()) + "/"); // to shorten paths like "dir/../dir" -> "dir"
  }
   else // going up 1 level... if we don't jump out of nominis folder
  {
   QDir newDir(pather.getNominisAbs(pathInNominis));

   newDir.cdUp();

   if ( !newDir.absolutePath().startsWith(pather.getNominisAbs("")) )
    return false;

   setPathInNominis(pather.getNominisRel(newDir.absolutePath()) + "/");
  }

 return true;
}


QDate          StateGlob::getDate(void)
{
 return        QDate::currentDate();
}

QTime          StateGlob::getTime(void)
{
 return        QTime::currentTime();
}

qint64         StateGlob::getEpochTime(void)
{
 return        QDateTime::currentMSecsSinceEpoch();
}


qint64         StateGlob::getCounter(void)
{
 return        (counter++);
}


qint64         StateGlob::getAvailMemory(void)
{
 return        SYSTEM::getAvailPhysMemory();
}


StateGlob::~StateGlob()
{
 pathInHallway = "/";
 pathInNominis = "/";

 counter = 0;

 contextP = NULL;
}


} // namespace "cryptrav"
