/*
 * BookmarkMenu.cpp
 *
 * (c) 2002 by Thorsten Scheuermann <tscheuermann@gmx.net>
 *
 * 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.
 */

#include "BookmarkMenu.h"
#include "Bookmark.h"
#include "ViewerSettings.h"
#include <qlist.h>
#include <qpoint.h>
#include <qpe/resource.h>

BookmarkMenu::BookmarkMenu(QWidget *parent, const char *name)
: QPopupMenu(parent, name)
{
  update();
}

BookmarkMenu::~BookmarkMenu()
{
}

void BookmarkMenu::popupMenu(const QPoint& p)
{
  int id;
  Bookmark *bm;

  id = exec(p);
  if(id == -1)
    return;
  else if(id == 0)
    emit documentsSelected();
  else
  {
    bm = ViewerSettings::getInstance()->bookmarks().at(id-1);
    if(!bm)
      return;
    Bookmark tempBm(*bm);

    emit bookmarkSelected(tempBm);
  }
}

void BookmarkMenu::update()
{
  QPixmap storageIcon[3];
  int id = 0;

  storageIcon[Bookmark::STORAGE_CF] = Resource::loadPixmap("mooview/CFCard");
  storageIcon[Bookmark::STORAGE_SD] = Resource::loadPixmap("mooview/SDCard");

  clear();

  insertItem(Resource::loadPixmap("mooview/Documents"), "Documents", id);

  QListIterator<Bookmark> bmIt(ViewerSettings::getInstance()->bookmarks());
  if(!bmIt.isEmpty())
    insertSeparator();

  for(id=1 ; bmIt.current(); ++bmIt, id++)
  {
    insertItem(storageIcon[(*bmIt)->storage()], (*bmIt)->name(), id);
    setItemEnabled(id, (*bmIt)->available());
  }
}
