File indexing completed on 2024-04-28 16:21:26
0001 /* This file is part of the KDE project 0002 Copyright 2009 Stefan Nikolaus <stefan.nikolaus@kdemail.net> 0003 0004 This library is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; either 0007 version 2 of the License, or (at your option) any later version. 0008 0009 This library is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this library; see the file COPYING.LIB. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "ProtectableObject.h" 0021 0022 #include "part/Digest.h" // FIXME detach from part 0023 #include "SheetsDebug.h" 0024 0025 #include <KoXmlNS.h> 0026 0027 #include <kcodecs.h> 0028 #include <KLocalizedString> 0029 #include <kmessagebox.h> 0030 #include <knewpassworddialog.h> 0031 #include <kpassworddialog.h> 0032 0033 #include <QPointer> 0034 0035 using namespace Calligra::Sheets; 0036 0037 void ProtectableObject::password(QByteArray & password) const 0038 { 0039 password = m_password; 0040 } 0041 0042 bool ProtectableObject::isProtected() const 0043 { 0044 return !m_password.isNull(); 0045 } 0046 0047 void ProtectableObject::setProtected(QByteArray const & password) 0048 { 0049 m_password = password; 0050 } 0051 0052 bool ProtectableObject::checkPassword(QByteArray const & password) const 0053 { 0054 return (password == m_password); 0055 } 0056 0057 bool ProtectableObject::showPasswordDialog(QWidget* parent, Mode mode, const QString& title) 0058 { 0059 if (mode == Lock) { 0060 QPointer<KNewPasswordDialog> dlg = new KNewPasswordDialog(parent); 0061 dlg->setPrompt(i18n("Enter a password.")); 0062 dlg->setWindowTitle(title); 0063 if (dlg->exec() != KPasswordDialog::Accepted) { 0064 return false; 0065 } 0066 0067 QByteArray hash; 0068 QString password = dlg->password(); 0069 if (password.length() > 0) { 0070 SHA1::getHash(password, hash); 0071 } 0072 m_password = hash; 0073 delete dlg; 0074 } else { /* Unlock */ 0075 QPointer<KPasswordDialog> dlg = new KPasswordDialog(parent); 0076 dlg->setPrompt(i18n("Enter the password.")); 0077 dlg->setWindowTitle(title); 0078 if (dlg->exec() != KPasswordDialog::Accepted) { 0079 return false; 0080 } 0081 0082 QByteArray hash(""); 0083 QString password(dlg->password()); 0084 if (password.length() > 0) { 0085 SHA1::getHash(password, hash); 0086 } 0087 if (!checkPassword(hash)) { 0088 KMessageBox::error(parent, i18n("Password is incorrect.")); 0089 return false; 0090 } 0091 m_password = QByteArray(); 0092 delete dlg; 0093 } 0094 return true; 0095 } 0096 0097 void ProtectableObject::loadXmlProtection(const KoXmlElement& element) 0098 { 0099 if (element.hasAttribute("protected")) { 0100 const QString passwd = element.attribute("protected"); 0101 QByteArray str(passwd.toUtf8()); 0102 m_password = KCodecs::base64Decode(str); 0103 } 0104 } 0105