PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  1. /***************************************************************************
  2. * Copyright (C) 2006 by Thomas Keller *
  3. * me@thomaskeller.biz *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation, either version 3 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  17. ***************************************************************************/
  18. #include "Keys.h"
  19. #include "BasicIOParser.h"
  20. #include "Settings.h"
  21. const int Key::Database = 1;
  22. const int Key::Keystore = 2;
  23. Keys::Keys(QObject * parent, const MonotoneHandlePtr & handle)
  24. : QAbstractItemModel(parent), AutomateCommand(0), monotoneHandle(handle)
  25. {}
  26. Keys::~Keys()
  27. {
  28. qDeleteAll(keys);
  29. }
  30. void Keys::readKeys()
  31. {
  32. MonotoneTaskPtr task(new MonotoneTask(QStringList() << "keys"));
  33. AutomateCommand::enqueueTask(monotoneHandle, task);
  34. }
  35. void Keys::processTaskResult(const MonotoneTaskPtr & task)
  36. {
  37. if (task->getReturnCode() != 0)
  38. {
  39. C(QString("Command returned with a non-zero return code (%1)")
  40. .arg(task->getDecodedOutput()));
  41. return;
  42. }
  43. reset();
  44. keys.clear();
  45. BasicIOParser parser(task->getDecodedOutput());
  46. I(parser.parse());
  47. StanzaList list = parser.getStanzas();
  48. for (int i=0, size = list.size(); i < size; ++i)
  49. {
  50. Stanza stanza = list.at(i);
  51. Key * key = new Key();
  52. bool isItem = false;
  53. for (int j=0, size2 = stanza.size(); j < size2; j++)
  54. {
  55. StanzaEntry entry = stanza.at(j);
  56. // we're now only interested in stanzas starting with a "name" entry
  57. if (j == 0 && entry.sym != "hash") break;
  58. isItem = true;
  59. if (entry.sym == "hash")
  60. {
  61. I(entry.vals.size() == 1);
  62. key->hash = entry.vals.at(0);
  63. continue;
  64. }
  65. if (entry.sym == "given_name")
  66. {
  67. I(entry.vals.size() == 1);
  68. key->given_name = entry.vals.at(0);
  69. continue;
  70. }
  71. if (entry.sym == "local_name")
  72. {
  73. I(entry.vals.size() == 1);
  74. key->local_name = entry.vals.at(0);
  75. continue;
  76. }
  77. if (entry.sym == "public_location")
  78. {
  79. for (int k=0, size3 = entry.vals.size(); k < size3; k++)
  80. {
  81. if (entry.vals.at(k) == "database")
  82. {
  83. key->public_locations |= Key::Database;
  84. continue;
  85. }
  86. if (entry.vals.at(k) == "keystore")
  87. {
  88. key->public_locations |= Key::Keystore;
  89. continue;
  90. }
  91. W(QString("Unknown key location '%1'.").arg(entry.vals.at(k)));
  92. }
  93. continue;
  94. }
  95. if (entry.sym == "private_location")
  96. {
  97. for (int k=0, size3 = entry.vals.size(); k < size3; k++)
  98. {
  99. if (entry.vals.at(k) == "database")
  100. {
  101. key->private_locations |= Key::Database;
  102. continue;
  103. }
  104. if (entry.vals.at(k) == "keystore")
  105. {
  106. key->private_locations |= Key::Keystore;
  107. continue;
  108. }
  109. W(QString("Unknown key location '%1'.").arg(entry.vals.at(k)));
  110. }
  111. continue;
  112. }
  113. W(QString("Unknown symbol %1.").arg(entry.sym));
  114. }
  115. // check if we really processed an item entry
  116. if (!isItem) continue;
  117. keys.append(key);
  118. }
  119. reset();
  120. emit keysRead();
  121. }
  122. int Keys::columnCount(const QModelIndex & parent) const
  123. {
  124. Q_UNUSED(parent);
  125. return 6;
  126. }
  127. QVariant Keys::data(const QModelIndex & index, int role) const
  128. {
  129. if (!index.isValid())
  130. {
  131. return QVariant();
  132. }
  133. if (role != Qt::DisplayRole)
  134. {
  135. return QVariant();
  136. }
  137. int row = index.row();
  138. if (row >= keys.size()) return QVariant();
  139. Key * key = keys.at(row);
  140. switch (index.column())
  141. {
  142. case 0: return QVariant(key->hash);
  143. case 1: return QVariant(key->given_name);
  144. case 2: return QVariant(key->local_name);
  145. case 3: return QVariant(getLocationString(key->public_locations));
  146. case 4: return QVariant(getLocationString(key->private_locations));
  147. case 5: return QVariant(getPasswordSaveStatus(key));
  148. }
  149. return QVariant();
  150. }
  151. QString Keys::getLocationString(int loc) const
  152. {
  153. QStringList str;
  154. if ((loc & Key::Database) == Key::Database)
  155. str << tr("Database");
  156. if ((loc & Key::Keystore) == Key::Keystore)
  157. str << tr("Keystore");
  158. return str.join(", ");
  159. }
  160. QString Keys::getPasswordSaveStatus(Key * key) const
  161. {
  162. if (!key->isPrivate())
  163. return tr("-");
  164. if (Settings::getItemFromMap("KeyPasswords", key->hash).isNull())
  165. return tr("No");
  166. return tr("Yes");
  167. }
  168. Qt::ItemFlags Keys::flags(const QModelIndex & index) const
  169. {
  170. Q_UNUSED(index);
  171. return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
  172. }
  173. QVariant Keys::headerData(int section, Qt::Orientation orientation, int role) const
  174. {
  175. if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
  176. {
  177. switch (section)
  178. {
  179. case 0: return QVariant(tr("Hash"));
  180. case 1: return QVariant(tr("Given Name"));
  181. case 2: return QVariant(tr("Local Name"));
  182. case 3: return QVariant(tr("Public Locations"));
  183. case 4: return QVariant(tr("Private Locations"));
  184. case 5: return QVariant(tr("Password saved?"));
  185. }
  186. }
  187. return QVariant();
  188. }
  189. int Keys::rowCount(const QModelIndex & parent) const
  190. {
  191. Q_UNUSED(parent);
  192. return keys.size();
  193. }
  194. QModelIndex Keys::index(int row, int column, const QModelIndex & parent) const
  195. {
  196. if (!hasIndex(row, column, parent))
  197. {
  198. return QModelIndex();
  199. }
  200. if (row >= keys.size()) return QModelIndex();
  201. return createIndex(row, column, keys.at(row));
  202. }
  203. QModelIndex Keys::parent(const QModelIndex & index) const
  204. {
  205. Q_UNUSED(index);
  206. return QModelIndex();
  207. }