• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

kgpg

  • sources
  • kde-4.14
  • kdeutils
  • kgpg
  • core
KGpgGroupNode.cpp
Go to the documentation of this file.
1 /* Copyright 2008,2009,2010,2012 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  */
19 #include "KGpgGroupNode.h"
20 
21 #include "KGpgGroupMemberNode.h"
22 #include "KGpgRootNode.h"
23 #include "kgpgsettings.h"
24 
25 #include <KDebug>
26 #include <KLocale>
27 #include <QFile>
28 #include <QStringList>
29 #include <QTextStream>
30 
31 class KGpgGroupNodePrivate {
32 public:
33  KGpgGroupNodePrivate(const QString &name);
34 
35  QString m_name;
36 
47  int findGroupEntry(QFile &conffile, QTextStream &stream, QStringList &lines);
48 
49  static const QRegExp &groupPattern();
50  static const QString &groupTag();
51 };
52 
53 KGpgGroupNodePrivate::KGpgGroupNodePrivate(const QString &name)
54  : m_name(name)
55 {
56 }
57 
58 int
59 KGpgGroupNodePrivate::findGroupEntry(QFile &conffile, QTextStream &stream, QStringList &lines)
60 {
61  conffile.setFileName(KGpgSettings::gpgConfigPath());
62 
63  if (!conffile.exists())
64  return -1;
65 
66  if (!conffile.open(QIODevice::ReadWrite))
67  return -1;
68 
69  stream.setDevice(&conffile);
70  int index = -1;
71  int i = -1;
72 
73  while (!stream.atEnd()) {
74  const QString rawLine = stream.readLine();
75  i++;
76  QString parsedLine = rawLine.simplified().section(QLatin1Char('#'), 0, 0);
77 
78  if (groupPattern().exactMatch(parsedLine)) {
79  // remove "group "
80  parsedLine = parsedLine.remove(0, 6);
81  if (parsedLine.startsWith(m_name)) {
82  if (parsedLine.mid(m_name.length()).simplified().startsWith(QLatin1Char('='))) {
83  if (index >= 0) {
84  // multiple definitions of the same group, drop the second one
85  continue;
86  } else {
87  index = i;
88  }
89  }
90  }
91  }
92 
93  lines << rawLine;
94  }
95 
96  stream.seek(0);
97 
98  return index;
99 }
100 
101 const QRegExp &
102 KGpgGroupNodePrivate::groupPattern()
103 {
104  static const QRegExp groupre(QLatin1String("^group [^ ]+ ?= ?([0-9a-fA-F]{8,} ?)*$"));
105 
106  return groupre;
107 }
108 
109 const QString &
110 KGpgGroupNodePrivate::groupTag()
111 {
112  static const QString grouptag(QLatin1String("group "));
113 
114  return grouptag;
115 }
116 
117 KGpgGroupNode::KGpgGroupNode(KGpgRootNode *parent, const QString &name, const QStringList &members)
118  : KGpgExpandableNode(parent),
119  d_ptr(new KGpgGroupNodePrivate(name))
120 {
121  foreach (const QString &id, members)
122  new KGpgGroupMemberNode(this, id);
123 
124  parent->m_groups++;
125 }
126 
127 KGpgGroupNode::KGpgGroupNode(KGpgRootNode *parent, const QString &name, const KGpgKeyNode::List &members)
128  : KGpgExpandableNode(parent),
129  d_ptr(new KGpgGroupNodePrivate(name))
130 {
131  Q_ASSERT(members.count() > 0);
132 
133  foreach (KGpgKeyNode *nd, members)
134  new KGpgGroupMemberNode(this, nd);
135 
136  parent->m_groups++;
137 }
138 
139 KGpgGroupNode::~KGpgGroupNode()
140 {
141  KGpgRootNode *root = m_parent->toRootNode();
142 
143  if (root != NULL)
144  root->m_groups--;
145 }
146 
147 KgpgCore::KgpgItemType
148 KGpgGroupNode::getType() const
149 {
150  return ITYPE_GROUP;
151 }
152 
153 QString
154 KGpgGroupNode::getName() const
155 {
156  const Q_D(KGpgGroupNode);
157 
158  return d->m_name;
159 }
160 
161 QString
162 KGpgGroupNode::getSize() const
163 {
164  return i18np("1 key", "%1 keys", children.count());
165 }
166 
167 void
168 KGpgGroupNode::readChildren()
169 {
170 }
171 
172 void
173 KGpgGroupNode::rename(const QString &newName)
174 {
175  Q_D(KGpgGroupNode);
176 
177  QFile conffile;
178  QTextStream t;
179  QStringList lines;
180  int index = d->findGroupEntry(conffile, t, lines);
181 
182  // check if file opening failed
183  if (!t.device())
184  return;
185 
186  if (index < 0) {
187  kDebug(2100) << "Group " << d->m_name << " not renamed, group does not exist";
188  return;
189  }
190 
191  // 6 = groupTag().length()
192  const QString values = lines[index].simplified().mid(6 + d->m_name.length());
193  lines[index] = d->groupTag() + newName + QLatin1Char(' ') + values;
194 
195  conffile.resize(0);
196  t << lines.join(QLatin1String("\n")) + QLatin1Char('\n');
197 
198  d->m_name = newName;
199 }
200 
201 void
202 KGpgGroupNode::saveMembers()
203 {
204  Q_D(KGpgGroupNode);
205 
206  QFile conffile;
207  QTextStream t;
208  QStringList lines;
209  int index = d->findGroupEntry(conffile, t, lines);
210 
211  // check if file opening failed
212  if (!t.device())
213  return;
214 
215  QStringList memberIds;
216 
217  for (int j = getChildCount() - 1; j >= 0; j--)
218  memberIds << getChild(j)->toGroupMemberNode()->getId();
219 
220  const QString groupEntry = d->groupTag() + d->m_name + QLatin1String(" = ") +
221  memberIds.join(QLatin1String(" "));
222 
223  if (index >= 0)
224  lines[index] = groupEntry;
225  else
226  lines << groupEntry;
227 
228  conffile.resize(0);
229  t << lines.join(QLatin1String("\n")) + QLatin1Char('\n');
230 }
231 
232 void
233 KGpgGroupNode::remove()
234 {
235  Q_D(KGpgGroupNode);
236 
237  QFile conffile;
238  QTextStream t;
239  QStringList lines;
240  int index = d->findGroupEntry(conffile, t, lines);
241 
242  // check if file opening failed
243  if (!t.device())
244  return;
245 
246  if (index < 0)
247  return;
248 
249  lines.removeAt(index);
250  conffile.resize(0);
251  t << lines.join(QLatin1String("\n")) + QLatin1Char('\n');
252 }
253 
254 QStringList
255 KGpgGroupNode::readGroups()
256 {
257  QStringList groups;
258  QFile qfile(KGpgSettings::gpgConfigPath());
259 
260  if (!qfile.exists() || !qfile.open(QIODevice::ReadOnly))
261  return groups;
262 
263  QTextStream t(&qfile);
264 
265  while (!t.atEnd()) {
266  QString line = t.readLine().simplified().section(QLatin1Char('#'), 0, 0);
267  if (!KGpgGroupNodePrivate::groupPattern().exactMatch(line))
268  continue;
269 
270  // remove the "group " at the start
271  line.remove(0, 6);
272  // transform it in a simple space separated list
273  groups.append(line.replace(QLatin1Char('='), QLatin1Char(' ')).simplified());
274  }
275 
276  return groups;
277 }
KGpgNode::toRootNode
KGpgRootNode * toRootNode()
Definition: KGpgNode.cpp:112
QTextStream::device
QIODevice * device() const
KGpgSettings::gpgConfigPath
static QString gpgConfigPath()
Get The path of the gpg configuration file.
Definition: kgpgsettings.h:288
QTextStream::readLine
QString readLine(qint64 maxlen)
QFile::resize
bool resize(qint64 sz)
KGpgGroupNode
A GnuPG group of public keys.
Definition: KGpgGroupNode.h:33
KGpgGroupMemberNode.h
QObject::children
const QObjectList & children() const
QList::removeAt
void removeAt(int i)
QString::simplified
QString simplified() const
QFile::setFileName
void setFileName(const QString &name)
KGpgGroupNode::getName
virtual QString getName() const
Definition: KGpgGroupNode.cpp:154
QStringList::join
QString join(const QString &separator) const
QFile::exists
bool exists() const
QString::remove
QString & remove(int position, int n)
KGpgExpandableNode::getChild
virtual KGpgNode * getChild(const int index) const
Returns the child node at the given index.
Definition: KGpgExpandableNode.cpp:41
QFile
KGpgRootNode.h
QTextStream
KGpgGroupNode::KGpgGroupNode
KGpgGroupNode(KGpgRootNode *parent, const QString &name, const QStringList &members)
Definition: KGpgGroupNode.cpp:117
KGpgKeyNode
A public key with or without corresponding secret key.
Definition: KGpgKeyNode.h:33
QRegExp
QList::count
int count(const T &value) const
KGpgRootNode
The parent of all key data objects.
Definition: KGpgRootNode.h:38
QList::append
void append(const T &value)
QTextStream::atEnd
bool atEnd() const
KGpgGroupNode::rename
void rename(const QString &newName)
Rename this group node.
Definition: KGpgGroupNode.cpp:173
QTextStream::setDevice
void setDevice(QIODevice *device)
KGpgNode::toGroupMemberNode
KGpgGroupMemberNode * toGroupMemberNode()
Definition: KGpgNode.cpp:208
KGpgGroupNode::~KGpgGroupNode
virtual ~KGpgGroupNode()
Definition: KGpgGroupNode.cpp:139
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
KGpgExpandableNode::getChildCount
virtual int getChildCount()
Return how many child nodes exist.
Definition: KGpgExpandableNode.cpp:49
KGpgGroupNode::getSize
virtual QString getSize() const
Return size of group.
Definition: KGpgGroupNode.cpp:162
QString
QList
KGpgGroupNode::readGroups
static QStringList readGroups()
get all groups from GnuPG config file
Definition: KGpgGroupNode.cpp:255
KgpgCore::ITYPE_GROUP
the element is a GnuPG key group
Definition: kgpgkey.h:91
QFile::open
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
QStringList
KGpgGroupNode::getType
virtual KgpgCore::KgpgItemType getType() const
Returns the item type of this object.
Definition: KGpgGroupNode.cpp:148
QLatin1Char
kgpgsettings.h
KGpgExpandableNode
The abstract base class for all classes that may have child objects.
Definition: KGpgExpandableNode.h:34
QString::replace
QString & replace(int position, int n, QChar after)
QString::mid
QString mid(int position, int n) const
QLatin1String
KGpgRefNode::getId
virtual QString getId() const
Definition: KGpgRefNode.cpp:115
KGpgGroupMemberNode
A member of a GnuPG group.
Definition: KGpgGroupMemberNode.h:35
KGpgNode::m_parent
KGpgExpandableNode * m_parent
Definition: KGpgNode.h:50
QList::mid
QList< T > mid(int pos, int length) const
KGpgGroupNode.h
QString::section
QString section(QChar sep, int start, int end, QFlags< QString::SectionFlag > flags) const
KGpgGroupNode::saveMembers
void saveMembers()
Write the current members to GnuPG config file.
Definition: KGpgGroupNode.cpp:202
KGpgGroupNode::remove
void remove()
Remove this group from the GnuPG config file.
Definition: KGpgGroupNode.cpp:233
KGpgGroupNode::readChildren
virtual void readChildren()
reimplemented in every base class to read in the child data
Definition: KGpgGroupNode.cpp:168
QTextStream::seek
bool seek(qint64 pos)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:42:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kgpg

Skip menu "kgpg"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • ktimer
  • kwallet
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal