/****************************************************************************
** Dooble - The Secure Internet Web Browser
**
** Copyright (c) 2008, 2009, 2010 Alexis Megas,
** Gunther van Dooble, and the Dooble Team.
** All rights reserved.
**
** License: GPL2 only:
** 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; version 2 of the License only.
**
** 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
** or see here: http://www.gnu.org/licenses/gpl.html
**
** For the WebKit library, please see: http://webkit.org.
**
** THE CODE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY
** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
** ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
** GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
** IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
** ARISING IN ANY WAY OUT OF THE USE OF THIS APPLICATION, EVEN IF ADVISED
** OF THE POSSIBILITY OF SUCH DAMAGE.
**
** Please report all praise, requests, bugs, and problems to the project
** team and administrators: http://sf.net/projects/dooble.
**
** You can find us listed at our project page. New team members are welcome.
** The name of the authors should not be used to endorse or promote products
** derived from Dooble without specific prior written permission.
** If you use this code for other projects, please let us know.
**
** Web sites:
**   http://sf.net/projects/dooble
**   http://dooble.sf.net
****************************************************************************/

#ifndef _dwebhistory_h_
#define _dwebhistory_h_

#include <QtDebug>

#include "dwebhistoryitem.h"

class dwebhistory
{
 public:
  dwebhistory(void)
  {
    current = 0;
  }

  ~dwebhistory()
  {
    container.clear();
  }

  bool canGoBack(void) const
  {
    if(container.isEmpty())
      return false;
    else
      return current < container.size() - 1;
  }

  bool canGoForward(void) const
  {
    if(container.isEmpty())
      return false;
    else
      return current > 0;
  }

  void add(const QUrl &url)
  {
    add(dwebhistoryitem(url));
  }

  void add(const dwebhistoryitem &item)
  {
    QString scheme(item.url().scheme().toLower());

    if(!(scheme == "file" || scheme == "ftp" ||
	 scheme == "http" || scheme == "https"))
      return;

    if(container.isEmpty())
      container.append(item);
    else
      {
	if(!(item == currentItem()))
	  container.insert(current, item);
      }
  }

  void clear(void)
  {
    QUrl url;

    /*
    ** Retain the current URL.
    */

    if(!container.isEmpty())
      url = container.at(current).url();

    current = 0;
    container.clear();

    if(!url.isEmpty())
      container.append(dwebhistoryitem(url));
  }

  dwebhistoryitem back(void)
  {
    if(canGoBack())
      {
	current += 1;

	if(current >= 0 && current < container.size())
	  return container.at(current);
	else
	  return dwebhistoryitem();
      }
    else
      return dwebhistoryitem();
  }

  dwebhistoryitem forward(void)
  {
    if(canGoForward())
      {
	current -= 1;

	if(current >= 0 && current < container.size())
	  return container.at(current);
	else
	  return dwebhistoryitem();
      }
    else
      return dwebhistoryitem();
  }

  dwebhistoryitem currentItem(void) const
  {
    if(current >= 0 && current < container.size())
      return container.at(current);
    else
      return dwebhistoryitem();
  }

  QList<dwebhistoryitem> backItems(const int n) const
  {
    if(n <= 0)
      return QList<dwebhistoryitem> ();
    else if((current + 1) < container.size())
      /*
      ** Skip (current + 1) the current item.
      */

      return container.mid(current + 1, qMin(n, container.size()));
    else
      return QList<dwebhistoryitem> ();
  }

  QList<dwebhistoryitem> forwardItems(const int n) const
  {
    if(n <= 0)
      return QList<dwebhistoryitem> ();
    else if(container.size() > 0)
      return container.mid(0, qMin(n, current));
    else
      return QList<dwebhistoryitem> ();
  }

 private:
  int current;
  QList<dwebhistoryitem> container;
};

#endif
