예제 #1
0
void Options::addKey()
{
	AddKeyDlg dlg(this);
	if (dlg.exec() == QDialog::Rejected) {
		return;
	}

	QString key;
	QString type, stype, length, name, comment, email, expiration, pass;
	switch (dlg.type()) {
	case 0: type = stype = "RSA"; break;
	case 1: type = "DSA"; stype = "ELG-E"; break;
	case 2: type = "DSA"; break;
	case 3: type = "RSA"; break;
	}

	length = QString::number(dlg.length());
	name = dlg.name();
	comment = dlg.comment();
	email = dlg.email();
	expiration = dlg.expiration().isValid() ? dlg.expiration().toString(Qt::ISODate) : "0";
	pass = dlg.pass();

	key += QString("Key-Type: %1\n").arg(type);
	key += QString("Key-Length: %2\n").arg(length);
	if (!stype.isEmpty()) {
		key += QString("Subkey-Type: %1\n").arg(stype);
		key += QString("Subkey-Length: %2\n").arg(length);
	}

	if (!name.isEmpty()) {
		key += QString("Name-Real: %1\n").arg(name);
	}

	if (!comment.isEmpty()) {
		key += QString("Name-Comment: %1\n").arg(comment);
	}

	if (!email.isEmpty()) {
		key += QString("Name-Email: %1\n").arg(email);
	}

	key += QString("Expire-Date: %1\n").arg(expiration);

	if (!pass.isEmpty()) {
		key += QString("Passphrase: %1\n").arg(pass);
	}

	key += "%commit\n";

	QProgressDialog waitingDlg("", trUtf8("Cancel"), 0, 0, this);

	QLabel progressTextLabel(trUtf8(
"<b>Please wait!</b><br/>"
"We need to generate a lot of random bytes. It is a good idea to perform "
"some other action (type on the keyboard, move the mouse, utilize the "
"disks) during the prime generation; this gives the random number "
"generator a better chance to gain enough entropy."), &waitingDlg);
	progressTextLabel.setAlignment(Qt::AlignHCenter);
	progressTextLabel.setWordWrap(true);

	waitingDlg.setLabel(&progressTextLabel);

	QProgressBar progressBar(&waitingDlg);
	progressBar.setAlignment(Qt::AlignHCenter);
	progressBar.setMinimum(0);
	progressBar.setMaximum(0);

	waitingDlg.setBar(&progressBar);

	waitingDlg.setWindowModality(Qt::WindowModal);
	waitingDlg.setWindowTitle(trUtf8("Key pair generating"));
	waitingDlg.show();

	GpgProcess gpg;
	QStringList arguments;
	arguments << "--batch"
			  << "--gen-key";

	gpg.start(arguments);
	gpg.waitForStarted();
	gpg.write(key.toUtf8());
	gpg.closeWriteChannel();
	while (gpg.state() == QProcess::Running) {
		gpg.waitForFinished(1);
		if (waitingDlg.wasCanceled()) {
			gpg.terminate();
			break;
		}
		qApp->processEvents();
	}

	updateKeys();
}