LeechCraft  0.6.70-3565-g2d86529
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
40 namespace LeechCraft
41 {
42 namespace Util
43 {
44  namespace
45  {
46  QString GetPasswordHelper (const QByteArray& key, const ICoreProxy_ptr& proxy)
47  {
48  const auto& result = Util::GetPersistentData (key, proxy);
49  if (!result.isValid ())
50  {
51  qWarning () << Q_FUNC_INFO
52  << "invalid result for key"
53  << key;
54  return {};
55  }
56 
57  switch (result.type ())
58  {
59  case QVariant::String:
60  return result.toString ();
61  case QVariant::List:
62  return result.toList ().value (0).toString ();
63  case QVariant::StringList:
64  return result.toStringList ().value (0);
65  default:
66  qWarning () << Q_FUNC_INFO
67  << "unknown result type"
68  << result.type ()
69  << result
70  << "for key"
71  << key;
72  return {};
73  }
74  }
75  }
76 
77  QString GetPassword (const QString& key, const QString& diaText,
78  const ICoreProxy_ptr& proxy, bool useStored)
79  {
80  if (useStored)
81  {
82  const auto& result = GetPasswordHelper (key.toUtf8 (), proxy);
83  if (!result.isNull ())
84  return result;
85  }
86 
87  QString result = QInputDialog::getText (0,
88  "LeechCraft",
89  diaText,
90  QLineEdit::Password);
91  if (!result.isNull ())
92  SavePassword (result, key, proxy);
93  return result;
94  }
95 
96  void SavePassword (const QString& password, const QString& key,
97  const ICoreProxy_ptr& proxy)
98  {
99  const auto& plugins = proxy->GetPluginsManager ()->
100  GetAllCastableTo<IPersistentStoragePlugin*> ();
101  for (const auto plugin : plugins)
102  if (const auto& storage = plugin->RequestStorage ())
103  storage->Set (key.toUtf8 (), password);
104  }
105 }
106 }
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition: icoreproxy.h:225
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:77
QVariant GetPersistentData(const QByteArray &key, const ICoreProxy_ptr &proxy)
Returns persistent data stored under given key.
Definition: util.cpp:148
void SavePassword(const QString &password, const QString &key, const ICoreProxy_ptr &proxy)
Saves the password to be retrieved later via GetPassword().
Definition: passutils.cpp:96