This repository has been archived on 2024-03-20. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
kpasman/kpasman/kpasmanview.cpp
2014-11-26 08:33:20 -06:00

182 lines
4 KiB
C++

// include files for Qt
#include <qpainter.h>
// application specific includes
#include <kpasmanview.h>
#include "kpasmandoc.h"
#include "kpasman.h"
#include "kpasentrydlg.h"
KpasmanView::KpasmanView(QWidget *parent, const char* name) : QListView(parent, name){
setBackgroundMode( PaletteBase );
setAllColumnsShowFocus(true);
m_iPWColumn = 2;
m_bPWHidden = false;
m_iPWSize = 80;
addColumn(i18n("Location"), 120);
addColumn(i18n("Username"), 80);
addColumn(i18n("Password"), m_iPWSize);
addColumn(i18n("Comment"), 200);
setColumnWidthMode(m_iPWColumn, QListView::Manual);
m_bLocked = false;
m_bLockPasswords = false;
}
KpasmanView::~KpasmanView(){
}
KpasmanDoc* KpasmanView::getDocument() const
{
KpasmanApp* theApp=(KpasmanApp*)parentWidget();
return theApp->getDocument();
}
void KpasmanView::setDocDirty(bool dirty)
{
KpasmanDoc* theDoc = getDocument();
theDoc->setModified(dirty);
}
void KpasmanView::hidePassword(bool mode)
{
if (m_bPWHidden == false)
m_iPWSize = columnWidth(m_iPWColumn);
setColumnWidth(m_iPWColumn, mode == false ? m_iPWSize : 0);
triggerUpdate();
m_bPWHidden = mode;
}
void KpasmanView::lockWindow(bool mode)
{
if (mode && !m_bLocked) {
if (getDocument()->lock()) {
m_bLockPasswords = isPasswordHidden();
hidePassword(true);
m_bLocked = true;
}
} else if (!mode && m_bLocked) {
if (getDocument()->unlock()) {
hidePassword(m_bLockPasswords);
m_bLocked = false;
}
}
}
bool KpasmanView::addEntry()
{
if (m_bLocked)
lockWindow(false);
if (m_bLocked)
return false;
KpasentryDlg pasentryDlg;
pasentryDlg.setCaption(i18n("Add Entry"));
if (pasentryDlg.exec()) {
QString location, username, password, comment;
pasentryDlg.getData(location, username, password, comment);
if ( !location.isEmpty() && !username.isEmpty() && !password.isEmpty() ) {
QListViewItem* item = new QListViewItem(this, location, username, password, comment);
setSelected(item, true);
setDocDirty(true);
triggerUpdate();
return true;
}
}
return false;
}
bool KpasmanView::editEntry()
{
if (m_bLocked)
lockWindow(false);
if (m_bLocked)
return false;
QListViewItem* item = currentItem();
if (item != 0) {
KpasentryDlg pasentryDlg;
QString location, username, password, comment;
location = item->text(0);
username = item->text(1);
password = item->text(2);
comment = item->text(3);
pasentryDlg.setCaption(i18n("Edit Entry"));
pasentryDlg.setData(location, username, password, comment);
if (pasentryDlg.exec()) {
pasentryDlg.getData(location, username, password, comment);
if ( !location.isEmpty() && !username.isEmpty() && !password.isEmpty() ) {
item->setText(0, location);
item->setText(1, username);
item->setText(2, password);
item->setText(3, comment);
setSelected(item, true);
setDocDirty(true);
triggerUpdate();
return true;
}
}
}
return false;
}
bool KpasmanView::deleteEntry()
{
if (m_bLocked)
lockWindow(false);
if (m_bLocked)
return false;
QListViewItem* item = currentItem();
if (item != 0) {
delete item;
setDocDirty(true);
// hack alert!
QListViewItem* newitem = firstChild();
if (newitem != 0)
setSelected(newitem, true);
else
emit selectionChanged((QListViewItem*)0);
triggerUpdate();
return true;
}
return false;
}
void KpasmanView::mouseDoubleClickEvent(QMouseEvent* event)
{
if (itemAt(event->pos()) == currentItem())
editEntry();
}
void KpasmanView::mousePressEvent(QMouseEvent* event)
{
QListView::mousePressEvent(event);
if (event->button() == RightButton) {
QPopupMenu* rmbpopup = new QPopupMenu();
int iedt, idel;
rmbpopup->insertItem("&Add Entry...", this, SLOT(slotAddEntry()));
iedt = rmbpopup->insertItem("&Edit Entry...", this, SLOT(slotEditEntry()));
idel = rmbpopup->insertItem("&Delete Entry", this, SLOT(slotDeleteEntry()));
QListViewItem* item = currentItem();
if ( (item == 0) || (itemAt(event->pos()) != item) ) {
rmbpopup->setItemEnabled(iedt, false);
rmbpopup->setItemEnabled(idel, false);
}
rmbpopup->exec(QCursor::pos());
}
}