add more spacing
[personal-kdebase.git] / apps / kdepasswd / passwddlg.cpp
blob27af7ee3ec269061fd97514491e3e1a73d4a91ea
1 /* vi: ts=8 sts=4 sw=4
3 * This file is part of the KDE project, module kdesu.
4 * Copyright (C) 2000 Geert Jansen <jansen@kde.org>
6 Permission to use, copy, modify, and distribute this software
7 and its documentation for any purpose and without fee is hereby
8 granted, provided that the above copyright notice appear in all
9 copies and that both that the copyright notice and this
10 permission notice and warranty disclaimer appear in supporting
11 documentation, and that the name of the author not be used in
12 advertising or publicity pertaining to distribution of the
13 software without specific, written prior permission.
15 The author disclaim all warranties with regard to this
16 software, including all implied warranties of merchantability
17 and fitness. In no event shall the author be liable for any
18 special, indirect or consequential damages or any damages
19 whatsoever resulting from loss of use, data or profits, whether
20 in an action of contract, negligence or other tortious action,
21 arising out of or in connection with the use or performance of
22 this software.
25 #include "passwddlg.h"
26 #include "passwd.h"
28 #include <klocale.h>
29 #include <kmessagebox.h>
31 KDEpasswd1Dialog::KDEpasswd1Dialog()
32 : KPasswordDialog()
34 setCaption(i18n("Change Password"));
35 setPrompt(i18n("Please enter your current password:"));
39 KDEpasswd1Dialog::~KDEpasswd1Dialog()
43 void KDEpasswd1Dialog::accept()
45 PasswdProcess proc(0);
47 int ret = proc.checkCurrent(password().toLocal8Bit());
48 switch (ret)
50 case -1:
52 QString msg = QString::fromLocal8Bit(proc.error());
53 if (!msg.isEmpty())
54 msg = "<p>\"<i>" + msg + "</i>\"";
55 msg = "<qt>" + i18n("Conversation with 'passwd' failed.") + msg;
56 KMessageBox::error(this, msg);
57 done(Rejected);
58 return;
61 case 0:
62 return KPasswordDialog::accept();
64 case PasswdProcess::PasswdNotFound:
65 KMessageBox::error(this, i18n("Could not find the program 'passwd'."));
66 done(Rejected);
67 return;
69 case PasswdProcess::PasswordIncorrect:
70 KMessageBox::sorry(this, i18n("Incorrect password. Please try again."));
71 return;
73 default:
74 KMessageBox::error(this, i18n("Internal error: illegal return value "
75 "from PasswdProcess::checkCurrent."));
76 done(Rejected);
77 return;
82 // static
83 int KDEpasswd1Dialog::getPassword(QByteArray &password)
85 KDEpasswd1Dialog *dlg = new KDEpasswd1Dialog();
86 int res = dlg->exec();
87 if (res == Accepted)
88 password = dlg->password().toLocal8Bit();
89 delete dlg;
90 return res;
95 KDEpasswd2Dialog::KDEpasswd2Dialog(const char *oldpass, const QByteArray &user)
96 : KNewPasswordDialog()
98 m_Pass = oldpass;
99 m_User = user;
101 setCaption(i18n("Change Password"));
102 if (m_User.isEmpty())
103 setPrompt(i18n("Please enter your new password:"));
104 else
105 setPrompt(i18n("Please enter the new password for user <b>%1</b>:", QString::fromLocal8Bit(m_User)));
109 KDEpasswd2Dialog::~KDEpasswd2Dialog()
114 void KDEpasswd2Dialog::accept()
116 PasswdProcess proc(m_User);
118 QString p;
119 if(!checkAndGetPassword(&p)){
120 return;
123 if (p.length() > 8)
125 switch(KMessageBox::warningYesNoCancel(this,
126 m_User.isEmpty() ?
127 i18n("Your password is longer than 8 characters. On some "
128 "systems, this can cause problems. You can truncate "
129 "the password to 8 characters, or leave it as it is.") :
130 i18n("The password is longer than 8 characters. On some "
131 "systems, this can cause problems. You can truncate "
132 "the password to 8 characters, or leave it as it is.")
134 i18n("Password Too Long"),
135 KGuiItem(i18n("Truncate")),
136 KGuiItem(i18n("Use as Is")),
137 KStandardGuiItem::cancel(),
138 "truncatePassword"))
140 case KMessageBox::Yes :
141 p=p.left(8);
142 break;
143 case KMessageBox::No :
144 break;
145 default : return;
149 int ret = proc.exec(m_Pass, p.toLocal8Bit());
150 switch (ret)
152 case 0:
154 hide();
155 QString msg = QString::fromLocal8Bit(proc.error());
156 if (!msg.isEmpty())
157 msg = "<p>\"<i>" + msg + "</i>\"";
158 msg = "<qt>" + i18n("Your password has been changed.") + msg;
159 KMessageBox::information(0L, msg);
160 return KNewPasswordDialog::accept();
163 case PasswdProcess::PasswordNotGood:
165 QString msg = QString::fromLocal8Bit(proc.error());
166 if (!msg.isEmpty())
167 msg = "<p>\"<i>" + msg + "</i>\"";
168 msg = "<qt>" + i18n("Your password has not been changed.") + msg;
170 // The pw change did not succeed. Print the error.
171 KMessageBox::sorry(this, msg);
172 return;
175 default:
176 QString msg = QString::fromLocal8Bit(proc.error());
177 if (!msg.isEmpty())
178 msg = "<p>\"<i>" + msg + "</i>\"";
179 msg = "<qt>" + i18n("Conversation with 'passwd' failed.") + msg;
180 KMessageBox::sorry(this, msg);
181 done(Rejected);
182 return;
185 return KNewPasswordDialog::accept();
190 #include "passwddlg.moc"