~ubuntu-branches/ubuntu/quantal/kgpg/quantal-updates

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
139
140
/*
 * Copyright (C) 2002 Jean-Baptiste Mardelle <bj@altern.org>
 * Copyright (C) 2007,2008 Rolf Eike Beer <kde@opensource.sf-tec.de>
 */

/***************************************************************************
 *                                                                         *
 *   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 "groupedit.h"
#include <QHeaderView>
#include <QSortFilterProxyModel>
#include <KAction>
#include <KActionCollection>
#include "groupeditproxymodel.h"
#include "kgpgitemmodel.h"
#include "kgpgkey.h"
#include "kgpgsettings.h"

groupEdit::groupEdit(QWidget *parent, QList<KGpgNode *> *ids)
	: QWidget(parent),
	m_outFilter(new QSortFilterProxyModel(this)),
	members(ids)
{
	setupUi( this );
	KgpgCore::KgpgKeyTrust mintrust;
	if (KGpgSettings::allowUntrustedGroupMembers()) {
		mintrust = KgpgCore::TRUST_UNDEFINED;
		textLabelAvailable->setText(i18n("Available Keys"));
	} else {
		mintrust = KgpgCore::TRUST_FULL;
		textLabelAvailable->setText(i18n("Available Trusted Keys"));
	}

	m_in = new GroupEditProxyModel(this, false, members, mintrust);
	m_out = new GroupEditProxyModel(this, true, members, mintrust);

	m_outFilter->setSourceModel(m_out);
	m_outFilter->setFilterCaseSensitivity(Qt::CaseInsensitive);
	m_outFilter->setFilterKeyColumn(-1);

	connect(filterEdit, SIGNAL(textChanged(QString)), m_outFilter, SLOT(setFilterFixedString(QString)));

	availableKeys->setModel(m_outFilter);
	groupKeys->setModel(m_in);
	buttonAdd->setIcon(KIcon( QLatin1String( "go-down" )));
	buttonRemove->setIcon(KIcon( QLatin1String( "go-up" )));

	availableKeys->setColumnWidth(0, 200);
	availableKeys->setColumnWidth(1, 200);
	availableKeys->setColumnWidth(2, 100);
	availableKeys->verticalHeader()->hide();

	groupKeys->setColumnWidth(0, 200);
	groupKeys->setColumnWidth(1, 200);
	groupKeys->setColumnWidth(2, 100);
	groupKeys->verticalHeader()->hide();

	setMinimumSize(sizeHint());

	connect(buttonAdd, SIGNAL(clicked()), this, SLOT(groupAdd()));
	connect(buttonRemove, SIGNAL(clicked()), this, SLOT(groupRemove()));
	connect(availableKeys, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(groupAdd(QModelIndex)));
	connect(groupKeys, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(groupRemove(QModelIndex)));
}

groupEdit::~groupEdit()
{
	delete m_in;
	delete m_out;
}

void
groupEdit::setModel(KGpgItemModel *md)
{
	m_model = md;
	m_in->setKeyModel(md);
	m_out->setKeyModel(md);
}

void
groupEdit::groupAdd()
{
	QModelIndexList sel = availableKeys->selectionModel()->selectedIndexes();
	for (int i = 0; i < sel.count(); i++) {
		if (sel.at(i).column() != 0)
			continue;
		KGpgNode *nd = m_out->nodeForIndex(m_outFilter->mapToSource(sel.at(i)));
		members->append(nd);
	}
	m_in->invalidate();
	m_out->invalidate();
}

void
groupEdit::groupRemove()
{
	Q_ASSERT(!members->isEmpty());
	QModelIndexList sel = groupKeys->selectionModel()->selectedIndexes();
	for (int i = 0; i < sel.count(); i++) {
		if (sel.at(i).column() != 0)
			continue;
		KGpgNode *nd = m_in->nodeForIndex(sel.at(i));
		for (int j = 0; j < members->count(); j++)
			if (members->at(j)->getId() == nd->getId()) {
				members->removeAt(j);
				break;
			}
	}
	m_in->invalidate();
	m_out->invalidate();
}

void
groupEdit::groupAdd(const QModelIndex &index)
{
	KGpgNode *nd = m_out->nodeForIndex(m_outFilter->mapToSource(index));
	members->append(nd);
	m_in->invalidate();
	m_out->invalidate();
}

void
groupEdit::groupRemove(const QModelIndex &index)
{
	Q_ASSERT(!members->isEmpty());
	KGpgNode *nd = m_in->nodeForIndex(index);
	for (int i = 0; i < members->count(); i++)
		if (members->at(i)->getId() == nd->getId()) {
			members->removeAt(i);
			break;
		}
	m_in->invalidate();
	m_out->invalidate();
}