LeechCraft  0.6.70-10870-g558588d6ec
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  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "passutils.h"
31 #include <QString>
32 #include <QObject>
33 #include <QInputDialog>
34 #include <util/xpc/util.h>
35 #include <interfaces/structures.h>
39 #include <util/sll/eithercont.h>
40 #include <util/sll/slotclosure.h>
41 
42 namespace LeechCraft
43 {
44 namespace Util
45 {
46  namespace
47  {
48  QString GetPasswordHelper (const QByteArray& key, const ICoreProxy_ptr& proxy)
49  {
50  const auto& result = Util::GetPersistentData (key, proxy);
51  if (!result.isValid ())
52  {
53  qWarning () << Q_FUNC_INFO
54  << "invalid result for key"
55  << key;
56  return {};
57  }
58 
59  switch (result.type ())
60  {
61  case QVariant::String:
62  return result.toString ();
63  case QVariant::List:
64  return result.toList ().value (0).toString ();
65  case QVariant::StringList:
66  return result.toStringList ().value (0);
67  default:
68  qWarning () << Q_FUNC_INFO
69  << "unknown result type"
70  << result.type ()
71  << result
72  << "for key"
73  << key;
74  return {};
75  }
76  }
77  }
78 
79  QString GetPassword (const QString& key, const QString& diaText,
80  const ICoreProxy_ptr& proxy, bool useStored)
81  {
82  if (useStored)
83  {
84  const auto& result = GetPasswordHelper (key.toUtf8 (), proxy);
85  if (!result.isNull ())
86  return result;
87  }
88 
89  const auto& result = QInputDialog::getText (nullptr,
90  "LeechCraft",
91  diaText,
92  QLineEdit::Password);
93  if (!result.isNull ())
94  SavePassword (result, key, proxy);
95  return result;
96  }
97 
98  void GetPassword (const QString& key, const QString& diaText,
99  const ICoreProxy_ptr& proxy,
100  const EitherCont<void (), void (QString)>& cont,
101  QObject *depender,
102  bool useStored)
103  {
104  if (useStored)
105  {
106  const auto& result = GetPasswordHelper (key.toUtf8 (), proxy);
107  if (!result.isNull ())
108  {
109  cont.Right (result);
110  return;
111  }
112  }
113 
114  const auto dialog = new QInputDialog;
115  dialog->setInputMode (QInputDialog::TextInput);
116  dialog->setWindowTitle ("LeechCraft");
117  dialog->setLabelText (diaText);
118  dialog->setTextEchoMode (QLineEdit::Password);
119  dialog->setAttribute (Qt::WA_DeleteOnClose);
120 
121  if (depender)
122  QObject::connect (depender,
123  SIGNAL (destroyed ()),
124  dialog,
125  SLOT (deleteLater ()));
126 
128  {
129  [dialog, cont]
130  {
131  const auto& value = dialog->textValue ();
132  if (value.isEmpty ())
133  cont.Left ();
134  else
135  cont.Right (value);
136  },
137  dialog,
138  SIGNAL (accepted ()),
139  dialog
140  };
141 
143  {
144  [cont] { cont.Left (); },
145  dialog,
146  SIGNAL (rejected ()),
147  dialog
148  };
149 
150  dialog->show ();
151  }
152 
153  void SavePassword (const QString& password, const QString& key,
154  const ICoreProxy_ptr& proxy)
155  {
156  const auto& plugins = proxy->GetPluginsManager ()->
157  GetAllCastableTo<IPersistentStoragePlugin*> ();
158  for (const auto plugin : plugins)
159  if (const auto& storage = plugin->RequestStorage ())
160  storage->Set (key.toUtf8 (), password);
161  }
162 }
163 }
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:79
QVariant GetPersistentData(const QByteArray &key, const ICoreProxy_ptr &proxy)
Returns persistent data stored under given key.
Definition: util.cpp:148
Executes a given functor upon a signal (or a list of signals).
Definition: slotclosure.h:100
A peir of two functions, typically a continuation and an error handler.
Definition: eithercont.h:51
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition: icoreproxy.h:225
void SavePassword(const QString &password, const QString &key, const ICoreProxy_ptr &proxy)
Saves the password to be retrieved later via GetPassword().
Definition: passutils.cpp:153