LeechCraft  0.6.70-15082-g543737046d
Modular cross-platform feature rich live environment.
passutils.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #include "passutils.h"
10 #include <QString>
11 #include <QObject>
12 #include <QInputDialog>
13 #include <util/xpc/util.h>
14 #include <interfaces/structures.h>
18 #include <util/sll/eithercont.h>
19 
20 namespace LC::Util
21 {
22  namespace
23  {
24  QString GetPasswordHelper (const QByteArray& key, const ICoreProxy_ptr& proxy)
25  {
26  const auto& result = Util::GetPersistentData (key, proxy);
27  if (!result.isValid ())
28  {
29  qWarning () << Q_FUNC_INFO
30  << "invalid result for key"
31  << key;
32  return {};
33  }
34 
35  switch (result.type ())
36  {
37  case QVariant::String:
38  return result.toString ();
39  case QVariant::List:
40  return result.toList ().value (0).toString ();
41  case QVariant::StringList:
42  return result.toStringList ().value (0);
43  default:
44  qWarning () << Q_FUNC_INFO
45  << "unknown result type"
46  << result.type ()
47  << result
48  << "for key"
49  << key;
50  return {};
51  }
52  }
53  }
54 
55  QString GetPassword (const QString& key, const QString& diaText,
56  const ICoreProxy_ptr& proxy, bool useStored)
57  {
58  if (useStored)
59  {
60  const auto& result = GetPasswordHelper (key.toUtf8 (), proxy);
61  if (!result.isNull ())
62  return result;
63  }
64 
65  const auto& result = QInputDialog::getText (nullptr,
66  QStringLiteral ("LeechCraft"),
67  diaText,
68  QLineEdit::Password);
69  if (!result.isNull ())
70  SavePassword (result, key, proxy);
71  return result;
72  }
73 
74  void GetPassword (const QString& key, const QString& diaText,
75  const ICoreProxy_ptr& proxy,
76  const EitherCont<void (), void (QString)>& cont,
77  QObject *depender,
78  bool useStored)
79  {
80  if (useStored)
81  {
82  const auto& result = GetPasswordHelper (key.toUtf8 (), proxy);
83  if (!result.isNull ())
84  {
85  cont.Right (result);
86  return;
87  }
88  }
89 
90  const auto dialog = new QInputDialog;
91  dialog->setInputMode (QInputDialog::TextInput);
92  dialog->setWindowTitle (QStringLiteral ("LeechCraft"));
93  dialog->setLabelText (diaText);
94  dialog->setTextEchoMode (QLineEdit::Password);
95  dialog->setAttribute (Qt::WA_DeleteOnClose);
96 
97  if (depender)
98  QObject::connect (depender,
99  &QObject::destroyed,
100  dialog,
101  &QObject::deleteLater);
102 
103  QObject::connect (dialog,
104  &QDialog::finished,
105  [dialog, cont] (int r)
106  {
107  const auto& value = dialog->textValue ();
108  if (r == QDialog::Rejected || value.isEmpty ())
109  cont.Left ();
110  else
111  cont.Right (value);
112  });
113 
114  dialog->show ();
115  }
116 
117  void SavePassword (const QString& password, const QString& key,
118  const ICoreProxy_ptr& proxy)
119  {
120  const auto& plugins = proxy->GetPluginsManager ()->GetAllCastableTo<IPersistentStoragePlugin*> ();
121  for (const auto plugin : plugins)
122  if (const auto& storage = plugin->RequestStorage ())
123  storage->Set (key.toUtf8 (), password);
124  }
125 }
ipluginsmanager.h
LC::Util
Definition: icoreproxy.h:33
structures.h
ipersistentstorageplugin.h
util.h
IPersistentStoragePlugin
Interface for plugins providing persistent (and possibly secure) storage.
Definition: ipersistentstorageplugin.h:78
LC::Util::SavePassword
void SavePassword(const QString &password, const QString &key, const ICoreProxy_ptr &proxy)
Saves the password to be retrieved later via GetPassword().
Definition: passutils.cpp:123
ICoreProxy_ptr
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition: icoreproxy.h:181
LC::Util::GetPassword
QString GetPassword(const QString &key, const QString &diaText, const ICoreProxy_ptr &proxy, bool useStored)
Returns password for the key, possibly asking the user.
Definition: passutils.cpp:61
eithercont.h
ientitymanager.h
passutils.h
LC::Util::GetPersistentData
QVariant GetPersistentData(const QByteArray &key, const ICoreProxy_ptr &proxy)
Returns persistent data stored under given key.
Definition: util.cpp:125