/**************************************************************************** * * * Copyright (C) 2005 Michael Buesch * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License version 2 * * as published by the Free Software Foundation. * * * * 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. Additionally a copy of the * * GNU General Public License is available here: * * http://passwordmanager.sourceforge.net/gplv2.txt * * http://www.gnu.org/licenses/gpl.txt * * * ****************************************************************************/ #include "regtrack.h" #include "advanced.h" #include #include #include #define CONTEXT_COLUMN 0 /*************************************************************************** * class ContextItem ***************************************************************************/ ContextItem::ContextItem(ContextList *_parent, const QString &name, int _pos) : QListViewItem (_parent, name) , parent (_parent) , pos (_pos) { setRenameEnabled(CONTEXT_COLUMN, true); } ContextItem::~ContextItem() { } /*************************************************************************** * class ContextList ***************************************************************************/ ContextList::ContextList(AdvancedWidget *_adv, const char *name, WFlags f) : QListView (_adv, name, f) , adv (_adv) { addColumn("Context"); setDefaultRenameAction(Accept); connect(this, SIGNAL(doubleClicked(QListViewItem *, const QPoint &, int)), this, SLOT(itemDoubleClicked(QListViewItem *, const QPoint &, int))); connect(this, SIGNAL(mouseButtonClicked(int, QListViewItem *, const QPoint &, int)), this, SLOT(itemClicked(int, QListViewItem *, const QPoint &, int))); connect(this, SIGNAL(itemRenamed(QListViewItem *, int, const QString &)), this, SLOT(itemRenamed(QListViewItem *, int, const QString &))); } ContextList::~ContextList() { } void ContextList::itemDoubleClicked(QListViewItem *_item, const QPoint &/*point*/, int /*column*/) { ContextItem *item = dynamic_cast(_item); if (!item) return; item->startRename(CONTEXT_COLUMN); } void ContextList::itemClicked(int button, QListViewItem *_item, const QPoint &/*pos*/, int /*column*/) { ContextItem *item = dynamic_cast(_item); if (!item) return; if (button == Qt::MidButton) adv->rt->clipboardPut(item->text(CONTEXT_COLUMN)); } void ContextList::itemRenamed(QListViewItem *_item, int /*column*/, const QString &text) { ContextItem *item = dynamic_cast(_item); if (!item) return; adv->rt->getContext()->renameContext(item->getPos(), text); adv->updateContextList(); } /*************************************************************************** * class AdvancedWidget ***************************************************************************/ AdvancedWidget::AdvancedWidget(RegTrack *_rt, const char *name, WFlags f) : QWidget (_rt, name, f) , rt (_rt) { QHBoxLayout *h; QVBoxLayout *v; layout = new QVBoxLayout(this); // Context layout->addSpacing(4); h = new QHBoxLayout(layout); context = new ContextList(this); h->addWidget(context); v = new QVBoxLayout(h); contextClone = new QPushButton("Clone", this); v->addWidget(contextClone); contextDel = new QPushButton("Delete", this); v->addWidget(contextDel); // Buttons layout->addSpacing(10); h = new QHBoxLayout(layout); loadButton = new QPushButton("Load from file...", this); h->addWidget(loadButton); saveButton = new QPushButton("Save to file...", this); h->addWidget(saveButton); // Connections connect(loadButton, SIGNAL(clicked()), this, SLOT(loadClicked())); connect(saveButton, SIGNAL(clicked()), this, SLOT(saveClicked())); connect(contextClone, SIGNAL(clicked()), this, SLOT(contextCloneClicked())); connect(contextDel, SIGNAL(clicked()), this, SLOT(contextDelClicked())); connect(context, SIGNAL(currentChanged(QListViewItem *)), this, SLOT(contextSelected(QListViewItem *))); } AdvancedWidget::~AdvancedWidget() { } void AdvancedWidget::show() { updateContextList(); QWidget::show(); } void AdvancedWidget::saveClicked() { rt->saveFile(); } void AdvancedWidget::loadClicked() { rt->loadFile(); updateContextList(); } void AdvancedWidget::contextCloneClicked() { ContextItem *item = dynamic_cast(context->currentItem()); if (!item) return; int index = item->getPos(); QString name = rt->context->name(index); name += " (clone)"; rt->context->cloneContext(index, name); updateContextList(); } void AdvancedWidget::contextDelClicked() { ContextItem *item = dynamic_cast(context->currentItem()); if (!item) return; if (rt->context->count() == 1) return; int res; res = QMessageBox::question(this, "Delete Context?", QString( "Do you really want to delete the \"%1\" Context?") .arg(rt->context->name(item->getPos())), QMessageBox::Yes, QMessageBox::No); if (res != QMessageBox::Yes) return; rt->context->deleteContext(item->getPos()); updateContextList(); } void AdvancedWidget::updateContextList() { int i, cnt; ContextItem *item, *selected = 0; context->clear(); cnt = rt->context->count(); for (i = 0; i < cnt; i++) { item = new ContextItem(context, rt->context->name(i), i); if (i == rt->context->current()) selected = item; } context->setSelected(selected, true); } void AdvancedWidget::contextSelected(QListViewItem *_item) { ContextItem *item = dynamic_cast(_item); if (!item) return; rt->context->switchContext(item->getPos()); }