LeechCraft 0.6.70-17609-g3dde4097dd
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
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>
18#include <util/sll/eithercont.h>
19
20namespace LC::Util
21{
22 namespace
23 {
24 QString GetPasswordHelper (const QByteArray& key, const ICoreProxy_ptr& proxy)
25 {
26 const auto& result = GetPersistentData (key, proxy);
27 if (!result.isValid ())
28 {
29 qWarning () << "invalid result for key" << key;
30 return {};
31 }
32
33 switch (result.typeId ())
34 {
35 case QMetaType::QString:
36 return result.toString ();
37 case QMetaType::QVariantList:
38 return result.toList ().value (0).toString ();
39 case QMetaType::QStringList:
40 return result.toStringList ().value (0);
41 default:
42 qWarning () << "unknown result type" << result.metaType () << result << "for key" << key;
43 return {};
44 }
45 }
46 }
47
48 QString GetPassword (const QString& key, const QString& diaText,
49 const ICoreProxy_ptr& proxy, bool useStored)
50 {
51 if (useStored)
52 {
53 const auto& result = GetPasswordHelper (key.toUtf8 (), proxy);
54 if (!result.isNull ())
55 return result;
56 }
57
58 const auto& result = QInputDialog::getText (nullptr,
59 QStringLiteral ("LeechCraft"),
60 diaText,
61 QLineEdit::Password);
62 if (!result.isNull ())
63 SavePassword (result, key, proxy);
64 return result;
65 }
66
67 void GetPassword (const QString& key, const QString& diaText,
68 const ICoreProxy_ptr& proxy,
69 const EitherCont<void (), void (QString)>& cont,
70 QObject *depender,
71 bool useStored)
72 {
73 if (useStored)
74 {
75 const auto& result = GetPasswordHelper (key.toUtf8 (), proxy);
76 if (!result.isNull ())
77 {
78 cont.Right (result);
79 return;
80 }
81 }
82
83 const auto dialog = new QInputDialog;
84 dialog->setInputMode (QInputDialog::TextInput);
85 dialog->setWindowTitle (QStringLiteral ("LeechCraft"));
86 dialog->setLabelText (diaText);
87 dialog->setTextEchoMode (QLineEdit::Password);
88 dialog->setAttribute (Qt::WA_DeleteOnClose);
89
90 if (depender)
91 QObject::connect (depender,
92 &QObject::destroyed,
93 dialog,
94 &QObject::deleteLater);
95
96 QObject::connect (dialog,
97 &QDialog::finished,
98 [dialog, cont] (int r)
99 {
100 const auto& value = dialog->textValue ();
101 if (r == QDialog::Rejected || value.isEmpty ())
102 cont.Left ();
103 else
104 cont.Right (value);
105 });
106
107 dialog->show ();
108 }
109
110 void SavePassword (const QString& password, const QString& key,
111 const ICoreProxy_ptr& proxy)
112 {
113 const auto& plugins = proxy->GetPluginsManager ()->GetAllCastableTo<IPersistentStoragePlugin*> ();
114 for (const auto plugin : plugins)
115 if (const auto& storage = plugin->RequestStorage ())
116 storage->Set (key.toUtf8 (), password);
117 }
118}
virtual IPluginsManager * GetPluginsManager() const =0
Returns the application's plugin manager.
Interface for plugins providing persistent (and possibly secure) storage.
QList< T > GetAllCastableTo() const
Similar to GetAlLCastableRoots() and provided for convenience.
A peir of two functions, typically a continuation and an error handler.
Definition eithercont.h:31
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition icoreproxy.h:181
QVariant GetPersistentData(const QByteArray &key, const ICoreProxy_ptr &proxy)
Returns persistent data stored under given key.
Definition util.cpp:125
void SavePassword(const QString &password, const QString &key, const ICoreProxy_ptr &proxy)
Saves the password to be retrieved later via 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:48