1 | /* This file is part of the KDE project |
2 | Copyright 2009 Stefan Nikolaus <stefan.nikolaus@kdemail.net> |
3 | |
4 | This library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Library General Public |
6 | License as published by the Free Software Foundation; either |
7 | version 2 of the License, or (at your option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Library General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to |
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
17 | Boston, MA 02110-1301, USA. |
18 | */ |
19 | |
20 | #include "ProtectableObject.h" |
21 | |
22 | #include "part/Digest.h" // FIXME detach from part |
23 | |
24 | #include <KoXmlNS.h> |
25 | |
26 | #include <kcodecs.h> |
27 | #include <kdebug.h> |
28 | #include <klocale.h> |
29 | #include <kmessagebox.h> |
30 | #include <knewpassworddialog.h> |
31 | #include <kpassworddialog.h> |
32 | |
33 | #include <QPointer> |
34 | |
35 | using namespace Calligra::Sheets; |
36 | |
37 | void ProtectableObject::password(QByteArray & password) const |
38 | { |
39 | password = m_password; |
40 | } |
41 | |
42 | bool ProtectableObject::isProtected() const |
43 | { |
44 | return !m_password.isNull(); |
45 | } |
46 | |
47 | void ProtectableObject::setProtected(QByteArray const & password) |
48 | { |
49 | m_password = password; |
50 | } |
51 | |
52 | bool ProtectableObject::checkPassword(QByteArray const & password) const |
53 | { |
54 | return (password == m_password); |
55 | } |
56 | |
57 | bool ProtectableObject::showPasswordDialog(QWidget* parent, Mode mode, const QString& title) |
58 | { |
59 | if (mode == Lock) { |
60 | QPointer<KNewPasswordDialog> dlg = new KNewPasswordDialog(parent); |
61 | dlg->setPrompt(i18n("Enter a password." )); |
62 | dlg->setWindowTitle(title); |
63 | if (dlg->exec() != KPasswordDialog::Accepted) { |
64 | return false; |
65 | } |
66 | |
67 | QByteArray hash; |
68 | QString password = dlg->password(); |
69 | if (password.length() > 0) { |
70 | SHA1::getHash(password, hash); |
71 | } |
72 | m_password = hash; |
73 | delete dlg; |
74 | } else { /* Unlock */ |
75 | QPointer<KPasswordDialog> dlg = new KPasswordDialog(parent); |
76 | dlg->setPrompt(i18n("Enter the password." )); |
77 | dlg->setWindowTitle(title); |
78 | if (dlg->exec() != KPasswordDialog::Accepted) { |
79 | return false; |
80 | } |
81 | |
82 | QByteArray hash("" ); |
83 | QString password(dlg->password()); |
84 | if (password.length() > 0) { |
85 | SHA1::getHash(password, hash); |
86 | } |
87 | if (!checkPassword(hash)) { |
88 | KMessageBox::error(parent, i18n("Password is incorrect." )); |
89 | return false; |
90 | } |
91 | m_password = QByteArray(); |
92 | delete dlg; |
93 | } |
94 | return true; |
95 | } |
96 | |
97 | void ProtectableObject::loadXmlProtection(const KoXmlElement& element) |
98 | { |
99 | if (element.hasAttribute("protected" )) { |
100 | const QString passwd = element.attribute("protected" ); |
101 | QByteArray str(passwd.toUtf8()); |
102 | m_password = KCodecs::base64Decode(str); |
103 | } |
104 | } |
105 | |
106 | void ProtectableObject::loadOdfProtection(const KoXmlElement& element) |
107 | { |
108 | if (element.hasAttributeNS(KoXmlNS::table, "protection-key" )) { |
109 | QString p = element.attributeNS(KoXmlNS::table, "protection-key" , QString()); |
110 | if (!p.isNull()) { |
111 | QByteArray str(p.toUtf8()); |
112 | kDebug(30518) <<"Decoding password:" << str; |
113 | m_password = KCodecs::base64Decode(str); |
114 | } |
115 | } |
116 | } |
117 | |