/guitone-1.0rc5/src/model/Keys.cpp
C++ | 248 lines | 188 code | 41 blank | 19 comment | 50 complexity | fc17efbdb403ac87590b4dc6f20d93a2 MD5 | raw file
Possible License(s): GPL-3.0
- /***************************************************************************
- * Copyright (C) 2006 by Thomas Keller *
- * me@thomaskeller.biz *
- * *
- * 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 3 of the License, or *
- * (at your option) any later version. *
- * *
- * This program is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
- * GNU General Public License for more details. *
- * *
- * You should have received a copy of the GNU General Public License *
- * along with this program. If not, see <http://www.gnu.org/licenses/>. *
- ***************************************************************************/
- #include "Keys.h"
- #include "BasicIOParser.h"
- #include "Settings.h"
- const int Key::Database = 1;
- const int Key::Keystore = 2;
- Keys::Keys(QObject * parent, const MonotoneHandlePtr & handle)
- : QAbstractItemModel(parent), AutomateCommand(0), monotoneHandle(handle)
- {}
- Keys::~Keys()
- {
- qDeleteAll(keys);
- }
- void Keys::readKeys()
- {
- MonotoneTaskPtr task(new MonotoneTask(QStringList() << "keys"));
- AutomateCommand::enqueueTask(monotoneHandle, task);
- }
- void Keys::processTaskResult(const MonotoneTaskPtr & task)
- {
- if (task->getReturnCode() != 0)
- {
- C(QString("Command returned with a non-zero return code (%1)")
- .arg(task->getDecodedOutput()));
- return;
- }
- reset();
- keys.clear();
- BasicIOParser parser(task->getDecodedOutput());
- I(parser.parse());
- StanzaList list = parser.getStanzas();
- for (int i=0, size = list.size(); i < size; ++i)
- {
- Stanza stanza = list.at(i);
- Key * key = new Key();
- bool isItem = false;
- for (int j=0, size2 = stanza.size(); j < size2; j++)
- {
- StanzaEntry entry = stanza.at(j);
- // we're now only interested in stanzas starting with a "name" entry
- if (j == 0 && entry.sym != "hash") break;
- isItem = true;
- if (entry.sym == "hash")
- {
- I(entry.vals.size() == 1);
- key->hash = entry.vals.at(0);
- continue;
- }
- if (entry.sym == "given_name")
- {
- I(entry.vals.size() == 1);
- key->given_name = entry.vals.at(0);
- continue;
- }
- if (entry.sym == "local_name")
- {
- I(entry.vals.size() == 1);
- key->local_name = entry.vals.at(0);
- continue;
- }
- if (entry.sym == "public_location")
- {
- for (int k=0, size3 = entry.vals.size(); k < size3; k++)
- {
- if (entry.vals.at(k) == "database")
- {
- key->public_locations |= Key::Database;
- continue;
- }
- if (entry.vals.at(k) == "keystore")
- {
- key->public_locations |= Key::Keystore;
- continue;
- }
- W(QString("Unknown key location '%1'.").arg(entry.vals.at(k)));
- }
- continue;
- }
- if (entry.sym == "private_location")
- {
- for (int k=0, size3 = entry.vals.size(); k < size3; k++)
- {
- if (entry.vals.at(k) == "database")
- {
- key->private_locations |= Key::Database;
- continue;
- }
- if (entry.vals.at(k) == "keystore")
- {
- key->private_locations |= Key::Keystore;
- continue;
- }
- W(QString("Unknown key location '%1'.").arg(entry.vals.at(k)));
- }
- continue;
- }
- W(QString("Unknown symbol %1.").arg(entry.sym));
- }
- // check if we really processed an item entry
- if (!isItem) continue;
- keys.append(key);
- }
- reset();
- emit keysRead();
- }
- int Keys::columnCount(const QModelIndex & parent) const
- {
- Q_UNUSED(parent);
- return 6;
- }
- QVariant Keys::data(const QModelIndex & index, int role) const
- {
- if (!index.isValid())
- {
- return QVariant();
- }
- if (role != Qt::DisplayRole)
- {
- return QVariant();
- }
- int row = index.row();
- if (row >= keys.size()) return QVariant();
- Key * key = keys.at(row);
- switch (index.column())
- {
- case 0: return QVariant(key->hash);
- case 1: return QVariant(key->given_name);
- case 2: return QVariant(key->local_name);
- case 3: return QVariant(getLocationString(key->public_locations));
- case 4: return QVariant(getLocationString(key->private_locations));
- case 5: return QVariant(getPasswordSaveStatus(key));
- }
- return QVariant();
- }
- QString Keys::getLocationString(int loc) const
- {
- QStringList str;
- if ((loc & Key::Database) == Key::Database)
- str << tr("Database");
- if ((loc & Key::Keystore) == Key::Keystore)
- str << tr("Keystore");
- return str.join(", ");
- }
- QString Keys::getPasswordSaveStatus(Key * key) const
- {
- if (!key->isPrivate())
- return tr("-");
- if (Settings::getItemFromMap("KeyPasswords", key->hash).isNull())
- return tr("No");
- return tr("Yes");
- }
- Qt::ItemFlags Keys::flags(const QModelIndex & index) const
- {
- Q_UNUSED(index);
- return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
- }
- QVariant Keys::headerData(int section, Qt::Orientation orientation, int role) const
- {
- if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
- {
- switch (section)
- {
- case 0: return QVariant(tr("Hash"));
- case 1: return QVariant(tr("Given Name"));
- case 2: return QVariant(tr("Local Name"));
- case 3: return QVariant(tr("Public Locations"));
- case 4: return QVariant(tr("Private Locations"));
- case 5: return QVariant(tr("Password saved?"));
- }
- }
- return QVariant();
- }
- int Keys::rowCount(const QModelIndex & parent) const
- {
- Q_UNUSED(parent);
- return keys.size();
- }
- QModelIndex Keys::index(int row, int column, const QModelIndex & parent) const
- {
- if (!hasIndex(row, column, parent))
- {
- return QModelIndex();
- }
- if (row >= keys.size()) return QModelIndex();
- return createIndex(row, column, keys.at(row));
- }
- QModelIndex Keys::parent(const QModelIndex & index) const
- {
- Q_UNUSED(index);
- return QModelIndex();
- }