LeechCraft 0.6.70-18450-gabe19ee3b0
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#include <util/sll/qtutil.h>
20
21namespace LC::Util
22{
23 namespace
24 {
25 QString GetPasswordHelper (const QByteArray& key, const ICoreProxy_ptr& proxy)
26 {
27 const auto& result = GetPersistentData (key, proxy);
28 if (!result.isValid ())
29 {
30 qWarning () << "invalid result for key" << key;
31 return {};
32 }
33
34 return HandleQVariant (result,
35 [] (const QString& str) { return str; },
36 [] (const QVariantList& list) { return list.value (0).toString (); },
37 [] (const QStringList& list) { return list.value (0); },
38 [&]
39 {
40 qWarning () << "unknown result type" << result.metaType () << result << "for key" << key;
41 return QString {};
42 });
43 }
44 }
45
46 QString GetPassword (const QString& key, const QString& diaText,
47 const ICoreProxy_ptr& proxy, bool useStored)
48 {
49 if (useStored)
50 {
51 const auto& result = GetPasswordHelper (key.toUtf8 (), proxy);
52 if (!result.isNull ())
53 return result;
54 }
55
56 const auto& result = QInputDialog::getText (nullptr,
57 QStringLiteral ("LeechCraft"),
58 diaText,
59 QLineEdit::Password);
60 if (!result.isNull ())
61 SavePassword (result, key, proxy);
62 return result;
63 }
64
65 void GetPassword (const QString& key, const QString& diaText,
66 const ICoreProxy_ptr& proxy,
67 const EitherCont<void (), void (QString)>& cont,
68 QObject *depender,
69 bool useStored)
70 {
71 if (useStored)
72 {
73 const auto& result = GetPasswordHelper (key.toUtf8 (), proxy);
74 if (!result.isNull ())
75 {
76 cont.Right (result);
77 return;
78 }
79 }
80
81 const auto dialog = new QInputDialog;
82 dialog->setInputMode (QInputDialog::TextInput);
83 dialog->setWindowTitle (QStringLiteral ("LeechCraft"));
84 dialog->setLabelText (diaText);
85 dialog->setTextEchoMode (QLineEdit::Password);
86 dialog->setAttribute (Qt::WA_DeleteOnClose);
87
88 if (depender)
89 QObject::connect (depender,
90 &QObject::destroyed,
91 dialog,
92 &QObject::deleteLater);
93
94 QObject::connect (dialog,
95 &QDialog::finished,
96 [dialog, cont] (int r)
97 {
98 const auto& value = dialog->textValue ();
99 if (r == QDialog::Rejected || value.isEmpty ())
100 cont.Left ();
101 else
102 cont.Right (value);
103 });
104
105 dialog->show ();
106 }
107
108 void SavePassword (const QString& password, const QString& key,
109 const ICoreProxy_ptr& proxy)
110 {
111 const auto& plugins = proxy->GetPluginsManager ()->GetAllCastableTo<IPersistentStoragePlugin*> ();
112 for (const auto plugin : plugins)
113 if (const auto& storage = plugin->RequestStorage ())
114 storage->Set (key.toUtf8 (), password);
115 }
116}
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:177
QVariant GetPersistentData(const QByteArray &key, const ICoreProxy_ptr &proxy)
Returns persistent data stored under given key.
Definition util.cpp:125
auto HandleQVariant(const QVariant &variant, Fs &&... handlers)
Definition qtutil.h:143
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:46