LeechCraft  %{LEECHCRAFT_VERSION}
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
passutils.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2013 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.h"
35 #include "interfaces/structures.h"
36 
37 namespace LeechCraft
38 {
39 namespace Util
40 {
41  namespace
42  {
43  QString GetPasswordHelper (const QString& key, QObject *emitter)
44  {
45  QList<QVariant> keys;
46  keys << key;
47  const auto& result = Util::GetPersistentData (keys, emitter);
48  if (result.size () != 1)
49  {
50  qWarning () << Q_FUNC_INFO
51  << "incorrect result size for key"
52  << key
53  << "; result:"
54  << result;
55  return QString ();
56  }
57 
58  const auto& strVarList = result.at (0).toList ();
59  if (strVarList.isEmpty () ||
60  !strVarList.at (0).canConvert<QString> ())
61  {
62  qWarning () << Q_FUNC_INFO
63  << "invalid string variant list"
64  << strVarList
65  << "for key"
66  << key;
67  return QString ();
68  }
69 
70  return strVarList.at (0).toString ();
71  }
72  }
73 
74  QString GetPassword (const QString& key, const QString& diaText,
75  QObject *emitter, bool useStored)
76  {
77  if (useStored)
78  {
79  const QString& result = GetPasswordHelper (key, emitter);
80  if (!result.isNull ())
81  return result;
82  }
83 
84  QString result = QInputDialog::getText (0,
85  "LeechCraft",
86  diaText,
87  QLineEdit::Password);
88  if (!result.isNull ())
89  SavePassword (result, key, emitter);
90  return result;
91  }
92 
93  void SavePassword (const QString& password, const QString& key,
94  QObject *emitter)
95  {
96  QList<QVariant> keys;
97  keys << key;
98 
99  QList<QVariant> passwordVar;
100  passwordVar << password;
101  QList<QVariant> values;
102  values << QVariant (passwordVar);
103 
104  Entity e = Util::MakeEntity (keys,
105  QString (),
106  Internal,
107  "x-leechcraft/data-persistent-save");
108  e.Additional_ ["Values"] = values;
109  e.Additional_ ["Overwrite"] = true;
110 
111  QMetaObject::invokeMethod (emitter, "gotEntity", Q_ARG (LeechCraft::Entity, e));
112  }
113 }
114 }
UTIL_API QVariantList GetPersistentData(const QList< QVariant > &keys, QObject *object)
Definition: util.cpp:347
void SavePassword(const QString &password, const QString &key, QObject *emitter)
Saves the password to be retrieved later via GetPassword().
Definition: passutils.cpp:93
QString GetPassword(const QString &key, const QString &diaText, QObject *emitter, bool useStored)
Returns password for the key, possibly asking the user.
Definition: passutils.cpp:74
QMap< QString, QVariant > Additional_
Additional parameters.
Definition: structures.h:228
UTIL_API Entity MakeEntity(const QVariant &entity, const QString &location, LeechCraft::TaskParameters tp, const QString &mime=QString())
An utility function to make a Entity.
Definition: util.cpp:279
Describes parameters of an entity.
Definition: structures.h:157