~kubuntu-members/kgpg/4.11

1297 by Rolf Eike Beer
add copyright and license to files containing only my code
1
/* Copyright 2008  Rolf Eike Beer <kde@opensource.sf-tec.de>
2
 *
3
 * This program is free software; you can redistribute it and/or
4
 * modify it under the terms of the GNU General Public License as
5
 * published by the Free Software Foundation; either version 2 of
6
 * the License or (at your option) version 3 or any later version
7
 * accepted by the membership of KDE e.V. (or its successor approved
8
 * by the membership of KDE e.V.), which shall act as a proxy
9
 * defined in Section 14 of version 3 of the license.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
1183.1.41 by Rolf Eike Beer
Implement context menu in keysmanager
19
#include "keytreeview.h"
1183.1.113 by Rolf Eike Beer
First try to bring drag&drop support to KeyTreeView
20
1985 by Rolf Eike Beer
clean up includes
21
#include "model/keylistproxymodel.h"
22
#include "model/kgpgitemmodel.h"
23
#include "model/kgpgitemnode.h"
24
#include "transactions/kgpgexport.h"
25
#include "transactions/kgpgimport.h"
26
27
#include <KConfigGroup>
28
#include <KLocale>
29
#include <KMessageBox>
1183.1.113 by Rolf Eike Beer
First try to bring drag&drop support to KeyTreeView
30
#include <QDragMoveEvent>
31
#include <QDropEvent>
1484 by Laurent Montel
Finish porting.
32
#include <QHeaderView>
1183.1.113 by Rolf Eike Beer
First try to bring drag&drop support to KeyTreeView
33
1183.1.41 by Rolf Eike Beer
Implement context menu in keysmanager
34
KeyTreeView::KeyTreeView(QWidget *parent, KeyListProxyModel *model)
35
	: QTreeView(parent), m_proxy(model)
36
{
37
	setModel(model);
1183.1.113 by Rolf Eike Beer
First try to bring drag&drop support to KeyTreeView
38
	setDragEnabled(true);
39
	setDragDropMode(DragDrop);
40
	setAcceptDrops(true);
1950 by Atanas Gospodinov
Implemented group inplace editing.
41
	setEditTriggers(QTreeView::NoEditTriggers);
1183.1.41 by Rolf Eike Beer
Implement context menu in keysmanager
42
}
43
44
QList<KGpgNode *>
45
KeyTreeView::selectedNodes(bool *psame, KgpgCore::KgpgItemType *pt) const
46
{
47
	QModelIndexList selidx = selectedIndexes();
48
	QList<KGpgNode *> ndlist;
49
	KgpgItemType tp = 0;
50
	bool sametype = true;
51
52
	if (selidx.count() == 0) {
53
		if (pt != NULL)
54
			*pt = tp;
55
		if (psame != NULL)
56
			*psame = sametype;
57
		return ndlist;
58
	}
59
60
	tp = m_proxy->nodeForIndex(selidx[0])->getType();
61
62
	for (int i = 0; i < selidx.count(); i++) {
63
		if (selidx[i].column() != 0)
64
			continue;
65
		KGpgNode *nd = m_proxy->nodeForIndex(selidx[i]);
66
67
		if (nd->getType() != tp) {
68
			tp |= nd->getType();
69
			sametype = false;
70
		}
71
72
		ndlist << nd;
73
	}
74
75
	if (pt != NULL)
76
		*pt = tp;
77
	if (psame != NULL)
78
		*psame = sametype;
79
	return ndlist;
80
}
1183.1.43 by Rolf Eike Beer
Port much stuff over to use KeyTreeView instead of KeyListView
81
82
KGpgNode *
83
KeyTreeView::selectedNode() const
84
{
85
	QModelIndexList selidx = selectedIndexes();
86
87
	if (selidx.isEmpty())
88
		return NULL;
89
90
	return m_proxy->nodeForIndex(selidx[0]);
91
}
1183.1.57 by Rolf Eike Beer
port "go to default key"
92
93
void
94
KeyTreeView::selectNode(KGpgNode *nd)
95
{
1279 by Rolf Eike Beer
fix crash on double-click on signature from unknown key
96
	if (nd == NULL)
97
		return;
98
1183.1.57 by Rolf Eike Beer
port "go to default key"
99
	QModelIndex idx = m_proxy->nodeIndex(nd);
100
101
	selectionModel()->select(idx, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
102
	selectionModel()->setCurrentIndex(idx, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
103
}
1183.1.66 by Rolf Eike Beer
port restoreLayout() to KeyTreeView
104
105
void
1183.1.113 by Rolf Eike Beer
First try to bring drag&drop support to KeyTreeView
106
KeyTreeView::restoreLayout(KConfigGroup &cg)
1183.1.66 by Rolf Eike Beer
port restoreLayout() to KeyTreeView
107
{
1622 by Rolf Eike Beer
set some sane defaults for column width
108
	QStringList cols(cg.readEntry("ColumnWidths", QStringList()));
1183.1.66 by Rolf Eike Beer
port restoreLayout() to KeyTreeView
109
	int i = 0;
110
1622 by Rolf Eike Beer
set some sane defaults for column width
111
	QStringList::ConstIterator it(cols.constBegin());
112
	const QStringList::ConstIterator itEnd(cols.constEnd());
1183.1.66 by Rolf Eike Beer
port restoreLayout() to KeyTreeView
113
	for (; it != itEnd; ++it)
114
		setColumnWidth(i++, (*it).toInt());
115
1622 by Rolf Eike Beer
set some sane defaults for column width
116
	while (i < model()->columnCount(QModelIndex())) {
117
		int width = 100;
118
		switch (i) {
119
		case KEYCOLUMN_NAME:
120
			width = 250;
121
			break;
122
		case KEYCOLUMN_EMAIL:
123
			width = 150;
124
			break;
125
		case KEYCOLUMN_TRUST:
126
			// the trust column needs to be only that big as the header which is done automatically
127
			i++;
128
			continue;
129
		}
130
		setColumnWidth(i, width);
131
		i++;
132
	}
133
1183.1.66 by Rolf Eike Beer
port restoreLayout() to KeyTreeView
134
	if (cg.hasKey("SortColumn")) {
135
		Qt::SortOrder order = cg.readEntry("SortAscending", true) ? Qt::AscendingOrder : Qt::DescendingOrder;
136
		sortByColumn(cg.readEntry("SortColumn", 0), order);
137
	}
138
}
139
140
void
141
KeyTreeView::saveLayout(KConfigGroup &cg) const
142
{
143
	QStringList widths;
144
145
	const int colCount = model()->columnCount();
146
147
	for (int i = 0; i < colCount; ++i) {
148
		widths << QString::number(columnWidth(i));
149
	}
150
	cg.writeEntry("ColumnWidths", widths);
1484 by Laurent Montel
Finish porting.
151
        cg.writeEntry( "SortColumn", header ()->sortIndicatorSection () );
152
        cg.writeEntry( "SortAscending", ( header()->sortIndicatorOrder () == Qt::AscendingOrder ) );
1183.1.66 by Rolf Eike Beer
port restoreLayout() to KeyTreeView
153
}
1183.1.113 by Rolf Eike Beer
First try to bring drag&drop support to KeyTreeView
154
155
void
156
KeyTreeView::contentsDragMoveEvent(QDragMoveEvent *e)
157
{
158
	e->setAccepted(KUrl::List::canDecode(e->mimeData()));
159
}
160
161
void
162
KeyTreeView::contentsDropEvent(QDropEvent *o)
163
{
164
	KUrl::List uriList = KUrl::List::fromMimeData(o->mimeData());
165
	if (!uriList.isEmpty()) {
166
		if (KMessageBox::questionYesNo(this, i18n("<p>Do you want to import file <b>%1</b> into your key ring?</p>",
167
					uriList.first().path()), QString(), KGuiItem(i18n("Import")),
168
					KGuiItem(i18n("Do Not Import"))) != KMessageBox::Yes)
169
			return;
170
1530 by Rolf Eike Beer
Clean up key importing
171
		emit importDrop(uriList);
1183.1.113 by Rolf Eike Beer
First try to bring drag&drop support to KeyTreeView
172
	}
173
}
174
175
void
176
KeyTreeView::startDrag(Qt::DropActions supportedActions)
177
{
178
	QList<KGpgNode *> nodes = selectedNodes();
179
180
	if (nodes.isEmpty())
181
		return;
182
183
	KGpgNode *nd = nodes.first();
184
	QString keyid = nd->getId();
185
186
	if (!(nd->getType() & ITYPE_PUBLIC))
187
		return;
188
1599 by Rolf Eike Beer
make key exporting a transaction
189
	KGpgExport *exp = new KGpgExport(this, QStringList(keyid));
190
	exp->start();
191
192
	int result = exp->waitForFinished();
193
194
	if (result == KGpgTransaction::TS_OK) {
195
		QMimeData *m = new QMimeData();
1860 by Laurent Montel
Compile fine with "-DQT_NO_CAST_FROM_ASCII -DQT_NO_CAST_TO_ASCII"
196
		m->setText(QString::fromLatin1( exp->getOutputData() ));
1599 by Rolf Eike Beer
make key exporting a transaction
197
		QDrag *drag = new QDrag(this);
198
		drag->setMimeData(m);
199
		drag->exec(supportedActions, Qt::IgnoreAction);
200
		// do NOT delete drag.
201
	}
202
203
	delete exp;
1183.1.113 by Rolf Eike Beer
First try to bring drag&drop support to KeyTreeView
204
}
1254 by Rolf Eike Beer
if QTreeView does not have resizeColumnsToContent() write one
205
206
void
207
KeyTreeView::resizeColumnsToContents()
208
{
209
	for (int i = m_proxy->columnCount() - 1; i >= 0; i--)
210
		resizeColumnToContents(i);
211
}
1530 by Rolf Eike Beer
Clean up key importing
212
1897 by Rolf Eike Beer
fix some actions showing up for toolbars not having a text
213
void
214
KeyTreeView::keyPressEvent(QKeyEvent *event)
215
{
216
	if (event->key() == Qt::Key_Return) {
217
		if (!event->isAutoRepeat())
218
			emit returnPressed();
219
220
		return;
221
	}
222
	QTreeView::keyPressEvent(event);
223
}
224
1950 by Atanas Gospodinov
Implemented group inplace editing.
225
bool
226
KeyTreeView::isEditing() const
227
{
228
	return (state() == EditingState);
229
}
230
1530 by Rolf Eike Beer
Clean up key importing
231
#include "keytreeview.moc"