summaryrefslogtreecommitdiffabout
path: root/pwmanager/pwmanager/subtbleditimpl.cpp
blob: cfc66ddba5edebf7239faba09584c24ef0d1fcb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/***************************************************************************
 *                                                                         *
 *   copyright (C) 2004 by Michael Buesch                                  *
 *   email: mbuesch@freenet.de                                             *
 *                                                                         *
 *   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.                         *
 *                                                                         *
 ***************************************************************************/

/***************************************************************************
 * copyright (C) 2004 by Ulf Schenk
 * This file is originaly based on version 1.0.1 of pwmanager
 * and was modified to run on embedded devices that run microkde
 *
 * $Id$
 **************************************************************************/  

#include "subtbleditimpl.h"
#include "pwmexception.h"

#include <qmessagebox.h>

#include <klocale.h>


SubTblEditImpl::SubTblEditImpl(QWidget* parent,
				 const char* name,
				 WFlags fl)
 : subTblEdit(parent, name, fl)
{
	prevSelection = -1;
	connect(entryListBox, SIGNAL(currentChanged(QListBoxItem *)),
		this, SLOT(selectedEntry_slot()));
}

SubTblEditImpl::~SubTblEditImpl()
{
}

void SubTblEditImpl::okButton_slot()
{
	if (getTitle().isEmpty()) {
		QMessageBox::information(this,
					 i18n("no title"),
					 i18n("Please enter a title."));
		return;
	}
	if (!entries.size()) {
		QMessageBox::information(this,
					 i18n("no entries"),
					 i18n("Please add some entries."));
		return;
	}
	int index = curIndex();
	if (index != -1) {
		entries[index].first = nameLineEdit->text();
		entries[index].second = valueLineEdit->text();
	}
	done(0);
}

void SubTblEditImpl::cancelButton_slot()
{
	done(1);
}

void SubTblEditImpl::addButton_slot()
{
	QString name(nameLineEdit->text());
	if (name.isEmpty())
		return;
	QString value(valueLineEdit->text());
	prevSelection = -1;
	nameLineEdit->clear();
	valueLineEdit->clear();
#ifndef PWM_EMBEDDED
	entryListBox->setSelected(entryListBox->index(
				  entryListBox->selectedItem()),
				  false);
#else
	entryListBox->setSelected(entryListBox->currentItem(),
				  false);
#endif
	pair<QString, QString> p;
	p.first = name;
	p.second = value;
	entries.push_back(p);
	entryListBox->insertItem(name);
}

void SubTblEditImpl::delButton_slot()
{
	int index = curIndex();
	if (index == -1)
		return;
	entries.erase(entries.begin() + index);
	entryListBox->removeItem(index);
}

void SubTblEditImpl::selectedEntry_slot()
{
	int index = curIndex();
	if (index == -1)
		return;
	disconnect(entryListBox, SIGNAL(currentChanged(QListBoxItem *)),
		   this, SLOT(selectedEntry_slot()));
	if ((prevSelection != -1) && (prevSelection != index)) {
		entries[prevSelection].first = nameLineEdit->text();
		entries[prevSelection].second = valueLineEdit->text();
		entryListBox->changeItem(nameLineEdit->text(), prevSelection);
	}
	pair<QString, QString> p(entries[index]);
	nameLineEdit->setText(p.first);
	valueLineEdit->setText(p.second);
	prevSelection = index;
	entryListBox->setSelected(index, true);
	connect(entryListBox, SIGNAL(currentChanged(QListBoxItem *)),
		this, SLOT(selectedEntry_slot()));
}

void SubTblEditImpl::setContent(const QString &title,
				const vector< pair<QString, QString> > *_entries)
{
	entries = *_entries;
	titleLineEdit->setText(title);
	vector< pair<QString, QString> >::iterator i = entries.begin(),
						   end = entries.end();
	while (i != end) {
		entryListBox->insertItem(i->first);
		++i;
	}
}

#ifndef PWM_EMBEDDED
#include "subtbleditimpl.moc"
#endif