• 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
  • model
gpgservermodel.cpp
Go to the documentation of this file.
1 /* Copyright 2014 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 
20 #include "gpgservermodel.h"
21 
22 #include <KLocale>
23 #include <QStringList>
24 
25 GpgServerModel::GpgServerModel(QObject *parent)
26  : QStringListModel(parent),
27  m_defaultRow(-1)
28 {
29  connect(this, SIGNAL(rowsRemoved(QModelIndex,int,int)), SLOT(slotRowsRemoved(QModelIndex,int,int)));
30 }
31 
32 GpgServerModel::~GpgServerModel()
33 {
34 }
35 
36 void
37 GpgServerModel::setDefault(const int row)
38 {
39  Q_ASSERT(row < stringList().count());
40 
41  if (m_defaultRow == row)
42  return;
43 
44  const int oldRow = m_defaultRow;
45  m_defaultRow = row;
46  if (oldRow >= 0)
47  emit dataChanged(index(oldRow, 0), index(oldRow, 0));
48  if (row >= 0)
49  emit dataChanged(index(row, 0), index(row, 0));
50 }
51 
52 void
53 GpgServerModel::setDefault(const QString &server)
54 {
55  if (server.isEmpty()) {
56  setDefault(-1);
57  } else {
58  const int row = stringList().indexOf(server);
59  Q_ASSERT(row >= 0);
60  setDefault(row);
61  }
62 }
63 
64 int
65 GpgServerModel::defaultRow() const
66 {
67  // only in case there is not set any default yet promote the first entry of the list
68  if ((m_defaultRow >= 0) || stringList().empty())
69  return m_defaultRow;
70  else
71  return 0;
72 }
73 
74 QString
75 GpgServerModel::defaultServer() const
76 {
77  if (stringList().isEmpty())
78  return QString();
79  if (m_defaultRow < 0)
80  return stringList().first();
81  else
82  return stringList().at(m_defaultRow);
83 }
84 
85 QVariant
86 GpgServerModel::data(const QModelIndex &index, int role) const
87 {
88  QVariant res = QStringListModel::data(index, role);
89 
90  if ((role == Qt::DisplayRole) && (index.row() == m_defaultRow))
91  res = i18nc("Mark default keyserver in GUI", "%1 (Default)", res.toString());
92 
93  return res;
94 }
95 
96 void
97 GpgServerModel::slotRowsRemoved(const QModelIndex &, int start, int end)
98 {
99  if (end < m_defaultRow) {
100  // removed before default, i.e. default is moved up
101  setDefault(m_defaultRow - (end - start) - 1);
102  } else if ((start <= m_defaultRow) && (end >= m_defaultRow)) {
103  // the default was deleted
104  if (m_defaultRow >= rowCount())
105  m_defaultRow = -1; // avoid sending dataChanged() for the already deleted row
106  if (rowCount() > 0)
107  setDefault(0);
108  }
109 }
QModelIndex
GpgServerModel::defaultServer
QString defaultServer() const
returns the URL of the default server
Definition: gpgservermodel.cpp:75
QList::at
const T & at(int i) const
GpgServerModel::~GpgServerModel
virtual ~GpgServerModel()
Definition: gpgservermodel.cpp:32
QStringListModel::rowCount
virtual int rowCount(const QModelIndex &parent) const
QStringListModel::stringList
QStringList stringList() const
QStringListModel::data
virtual QVariant data(const QModelIndex &index, int role) const
QAbstractItemModel::dataChanged
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
GpgServerModel::setDefault
void setDefault(const QString &server)
Definition: gpgservermodel.cpp:53
QObject
QAbstractListModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
QString::isEmpty
bool isEmpty() const
GpgServerModel::GpgServerModel
GpgServerModel(QObject *parent=NULL)
Definition: gpgservermodel.cpp:25
QModelIndex::row
int row() const
QList::first
T & first()
QString
QAbstractItemModel::rowsRemoved
void rowsRemoved(const QModelIndex &parent, int start, int end)
QStringListModel
GpgServerModel::defaultRow
int defaultRow() const
returns the row of the current default keyserver
Definition: gpgservermodel.cpp:65
GpgServerModel::data
virtual QVariant data(const QModelIndex &index, int role) const
Definition: gpgservermodel.cpp:86
QStringList::indexOf
int indexOf(const QRegExp &rx, int from) const
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
gpgservermodel.h
QVariant::toString
QString toString() const
QVariant
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