25 #include <KDesktopFile>
26 #include <KFileDialog>
27 #include <KFontChooser>
28 #include <KInputDialog>
30 #include <KMessageBox>
32 #include <KStandardDirs>
35 #include <QTextStream>
36 #include <QVBoxLayout>
39 using namespace KgpgCore;
44 m_config(new KConfig( QLatin1String(
"kgpgrc" ), KConfig::SimpleConfig)),
46 m_page2(new Decryption()),
47 m_page3(new UIConf()),
48 m_page4(new GPGConf()),
49 m_page6(new ServerConf()),
50 m_page7(new MiscConf()),
51 m_fontchooser(new KFontChooser(m_page3->tabWidget3->widget(1), KFontChooser::NoDisplayFlags, QStringList())),
60 m_combomodel->sort(0);
63 defaultKeyServer = QLatin1String(
"hkp://wwwkeys.pgp.net");
64 defaultServerList << defaultKeyServer
65 << QLatin1String(
"hkp://search.keyserver.net")
66 << QLatin1String(
"hkp://pgp.dtype.org")
67 << QLatin1String(
"hkp://subkeys.pgp.net");
77 serverList.replaceInStrings(QRegExp( QLatin1String(
" .*") ), QLatin1String(
"" ) );
79 defaultConfigPath = KUrl::fromPath(gpgConfigPath).fileName();
80 defaultHomePath = KUrl::fromPath(gpgConfigPath).directory(KUrl::AppendTrailingSlash);
87 QVBoxLayout *fontlayout =
new QVBoxLayout(m_page3->tabWidget3->widget(1));
88 fontlayout->setSpacing(spacingHint());
90 m_fontchooser->setObjectName( QLatin1String(
"kcfg_Font" ));
91 fontlayout->addWidget(m_fontchooser);
93 m_page3->kcfg_EmailSorting->addItem(i18n(
"Left to right, account first"));
94 m_page3->kcfg_EmailSorting->addItem(i18n(
"Right to left, TLD first"));
95 m_page3->kcfg_EmailSorting->addItem(i18n(
"Right to left, domain first"));
96 m_page3->kcfg_EmailSorting->addItem(i18n(
"Right to left, FQDN first"));
101 addPage(m_page1, i18n(
"Encryption"), QLatin1String(
"document-encrypt" ));
102 addPage(m_page2, i18n(
"Decryption"), QLatin1String(
"document-decrypt") );
103 addPage(m_page3, i18n(
"Appearance"), QLatin1String(
"preferences-desktop-theme" ));
104 addPage(m_page4, i18n(
"GnuPG Settings"), QLatin1String(
"kgpg" ));
105 addPage(m_page6, i18n(
"Key Servers"), QLatin1String(
"network-wired" ));
106 addPage(m_page7, i18n(
"Misc"), QLatin1String(
"preferences-other" ));
109 connect(m_page1->encrypt_to_always, SIGNAL(toggled(
bool)),
this, SLOT(slotChangeEncryptTo()));
110 connect(m_page4->changeHome, SIGNAL(clicked()),
this, SLOT(slotChangeHome()));
111 connect(m_page6->server_add, SIGNAL(clicked()),
this, SLOT(slotAddKeyServer()));
112 connect(m_page6->server_del, SIGNAL(clicked()),
this, SLOT(slotDelKeyServer()));
113 connect(m_page6->server_edit, SIGNAL(clicked()),
this, SLOT(slotEditKeyServer()));
114 connect(m_page6->server_default, SIGNAL(clicked()),
this, SLOT(slotDefaultKeyServer()));
115 connect(m_page6->ServerBox, SIGNAL(itemSelectionChanged()),
this, SLOT(slotChangeKeyServerButtonEnable()));
116 connect(m_page6->ServerBox, SIGNAL(executed(QListWidgetItem*)),
this, SLOT(slotEditKeyServer(QListWidgetItem*)));
117 connect(m_page7->kcfg_ShowSystray, SIGNAL(clicked()), SLOT(slotSystrayEnable()));
139 void kgpgOptions::slotChangeHome()
141 QString gpgHome = KFileDialog::getExistingDirectory(m_page4->gpg_home_path->text(),
this, i18n(
"New GnuPG Home Location"));
142 if (gpgHome.isEmpty())
145 if (!gpgHome.endsWith(QLatin1Char(
'/' )))
146 gpgHome.append(QLatin1Char(
'/' ));
148 QString confPath = QLatin1String(
"options" );
149 if (!QFile(gpgHome + confPath).exists()) {
150 confPath = QLatin1String(
"gpg.conf" );
151 if (!QFile(gpgHome + confPath).exists()) {
153 if (KMessageBox::Yes == KMessageBox::questionYesNo(
this,
154 i18n(
"No configuration file was found in the selected location.\nDo you want to create it now?\n\nWithout a configuration file, neither KGpg nor GnuPG will work properly."),
155 i18n(
"No Configuration File Found"),
156 KGuiItem(i18n(
"Create")),
157 KGuiItem(i18n(
"Ignore")))) {
159 QString gpgbin = m_page4->kcfg_GpgBinaryPath->text();
160 if (!QFile::exists(gpgbin))
161 gpgbin = QLatin1String(
"gpg" );
164 p << gpgbin << QLatin1String(
"--homedir" ) << gpgHome << QLatin1String(
"--no-tty" ) << QLatin1String(
"--list-secret-keys" );
168 confPath = QLatin1String(
"gpg.conf" );
169 QFile confFile(gpgHome + confPath);
170 if (!confFile.open(QIODevice::WriteOnly)) {
171 KMessageBox::sorry(
this, i18n(
"Cannot create configuration file. Please check if destination media is mounted and if you have write access."));
174 QTextStream stream(&confFile);
175 stream <<
"# Config file created by KGpg\n\n";
184 m_page4->gpg_conf_path->setText(confPath);
185 m_page4->gpg_home_path->setText(gpgHome);
188 bool kgpgOptions::isValidKeyserver(
const QString &server)
190 if (server.isEmpty())
193 if (server.contains(QLatin1Char(
' ' ))) {
194 KMessageBox::sorry(
this, i18n(
"Key server URLs may not contain whitespace."));
198 if (serverList.contains(server)) {
199 KMessageBox::sorry(
this, i18n(
"Key server already in the list."));
206 void kgpgOptions::slotAddKeyServer()
208 const QString newServer(KInputDialog::getText(i18n(
"Add New Key Server"), i18n(
"Server URL:")));
210 if (!isValidKeyserver(newServer))
213 serverList << newServer;
214 QListWidgetItem *item =
new QListWidgetItem(newServer, m_page6->ServerBox);
215 m_page6->ServerBox->setCurrentItem(item);
217 enableButtonApply(
true);
220 void kgpgOptions::slotChangeEncryptTo()
222 bool enable = (m_page1->encrypt_to_always->isChecked() != m_encrypttoalways);
223 enableButtonApply(enable);
226 void kgpgOptions::slotDelKeyServer()
228 QListWidgetItem *cur = m_page6->ServerBox->takeItem(m_page6->ServerBox->currentRow());
229 bool defaultDeleted = cur->text().contains(QLatin1Char(
' ' ));
232 cur = m_page6->ServerBox->currentItem();
236 cur->setSelected(
true);
238 slotDefaultKeyServer();
239 enableButtonApply(
true);
242 void kgpgOptions::slotEditKeyServer()
244 QListWidgetItem *cur = m_page6->ServerBox->currentItem();
245 slotEditKeyServer(cur);
248 void kgpgOptions::slotEditKeyServer(QListWidgetItem *cur)
253 QString oldServer(cur->text());
255 if (oldServer.contains(QLatin1Char(
' ' ))) {
257 oldServer = oldServer.section(QLatin1Char(
' ' ), 0, 0);
260 QString newServer(KInputDialog::getText(i18n(
"Edit Key Server"), i18n(
"Server URL:"), oldServer).simplified());
261 if (!isValidKeyserver(newServer))
264 newServer = i18nc(
"Mark default keyserver in GUI",
"%1 (Default)", newServer);
265 cur->setText(newServer);
267 enableButtonApply(
true);
270 void kgpgOptions::slotDefaultKeyServer()
272 QListWidgetItem *curr = m_page6->ServerBox->currentItem();
273 if (!curr->text().contains(QLatin1Char(
' ' ))) {
279 if (m_page6->ServerBox->findItems(keyServer, Qt::MatchContains).size() > 0) {
280 QListWidgetItem *prev = m_page6->ServerBox->findItems(keyServer, Qt::MatchContains).first();
281 prev->setText(prev->text().remove(QLatin1Char(
' ' ) + i18nc(
"Mark default keyserver in GUI",
"(Default)")));
284 keyServer = curr->text();
285 curr->setText(i18nc(
"Mark default keyserver in GUI",
"%1 (Default)", curr->text()));
287 enableButtonApply(
true);
291 void kgpgOptions::slotChangeKeyServerButtonEnable()
293 const bool empty = m_page6->ServerBox->selectedItems().isEmpty();
295 m_page6->server_del->setEnabled(!empty);
296 m_page6->server_default->setEnabled(!empty);
299 void kgpgOptions::updateWidgets()
305 m_encrypttoalways = !alwaysKeyID.isEmpty();
306 m_defaultencrypttoalways =
false;
308 m_page1->encrypt_to_always->setChecked(m_encrypttoalways);
314 if (!fileEncryptionKey.isEmpty()) {
315 int idpos = m_page1->file_key->findText(fileEncryptionKey);
317 idpos = fileEncryptionKey.indexOf(QRegExp( QLatin1String(
"([0-9A-Fa-F]{8})+" )));
319 QString fileId = fileEncryptionKey.mid(idpos);
320 idpos = fileId.indexOf(QRegExp( QLatin1String(
"[^a-fA-F0-9]" )));
322 fileId = fileId.left(idpos);
323 fileId.chop(fileId.length() % 8);
329 idpos = m_combomodel->
nodeIndex(anode).row();
332 m_page1->file_key->setCurrentIndex(idpos);
335 if (!alwaysKeyID.isEmpty()) {
338 const QModelIndex midx(m_combomodel->
nodeIndex(anode));
339 m_page1->always_key->setCurrentIndex(midx.row());
344 m_page4->gpg_conf_path->setText(KUrl::fromPath(gpgConfigPath).fileName());
345 m_page4->gpg_home_path->setText(KUrl::fromPath(gpgConfigPath).directory(KUrl::AppendTrailingSlash));
348 m_defaultuseagent =
false;
350 m_page4->use_agent->setChecked(m_useagent);
353 m_page3->kcfg_EmailSorting->setCurrentIndex(m_emailSortingIndex);
355 m_page6->ServerBox->clear();
356 QStringList servers(serverList);
358 if (!servers.isEmpty()) {
359 const QString defaultServer(servers.takeFirst());
360 servers.prepend(i18nc(
"Mark default keyserver in GUI",
"%1 (Default)", defaultServer));
361 m_page6->ServerBox->addItems(servers);
364 kDebug(2100) <<
"Finishing options" ;
367 void kgpgOptions::updateWidgetsDefault()
369 m_page7->EmailTemplateEdit->setPlainText(m_emailTemplate);
371 m_page1->encrypt_to_always->setChecked(m_defaultencrypttoalways);
372 m_page4->use_agent->setChecked(m_defaultuseagent);
374 m_page4->gpg_conf_path->setText(defaultConfigPath);
375 m_page4->gpg_home_path->setText(defaultHomePath);
377 m_page6->ServerBox->clear();
378 m_page6->ServerBox->addItem(i18nc(
"Mark default keyserver in GUI",
"%1 (Default)", defaultKeyServer));
379 m_page6->ServerBox->addItems(defaultServerList);
383 kDebug(2100) <<
"Finishing default options" ;
386 void kgpgOptions::updateSettings()
389 const QString newConfigFile = m_page4->gpg_home_path->text() + m_page4->gpg_conf_path->text();
390 if (newConfigFile != gpgConfigPath) {
394 gpgConfigPath = newConfigFile;
398 if (m_page1->kcfg_EncryptFilesTo->isChecked())
399 fileEncryptionKey = m_page1->file_key->itemData(m_page1->file_key->currentIndex(), Qt::ToolTipRole).
toString();
401 fileEncryptionKey.clear();
406 m_encrypttoalways = m_page1->encrypt_to_always->isChecked();
408 if (m_encrypttoalways)
409 alwaysKeyID = m_page1->always_key->itemData(m_page1->always_key->currentIndex(), Qt::ToolTipRole).
toString();
419 slotInstallSign(QLatin1String(
"application/octet-stream" ));
421 slotRemoveMenu(QLatin1String(
"signfile.desktop" ));
424 slotInstallDecrypt(QLatin1String(
"application/octet-stream" ));
427 slotInstallDecrypt(QLatin1String(
"application/pgp-encrypted,application/pgp-signature,application/pgp-keys" ));
429 slotRemoveMenu(QLatin1String(
"decryptfile.desktop" ));
431 m_useagent = m_page4->use_agent->isChecked();
445 for (
int i = 0; i < m_page6->ServerBox->count(); i++) {
446 QString server(m_page6->ServerBox->item(i)->text());
449 if (!server.contains(QLatin1Char(
' ' ))) {
450 serverList.append(server);
452 server.remove(QRegExp( QLatin1String(
" .*")) );
453 serverList.prepend(server);
458 if (keyUltimate != m_page3->kcfg_ColorUltimate->color())
461 if (keyGood != m_page3->kcfg_ColorGood->color())
464 if (keyExpired != m_page3->kcfg_ColorExpired->color())
467 if (keyMarginal != m_page3->kcfg_ColorMarginal->color())
470 if (keyBad != m_page3->kcfg_ColorBad->color()) {
475 if (keyUnknown != m_page3->kcfg_ColorUnknown->color()) {
481 if (keyRev != m_page3->kcfg_ColorRev->color())
484 m_showsystray = m_page7->kcfg_ShowSystray->isChecked();
487 m_trayaction = m_page7->kcfg_LeftClick->currentIndex();
490 m_mailUats = m_page7->kcfg_MailUats->currentIndex();
493 m_emailTemplate = m_page7->EmailTemplateEdit->toPlainText();
496 m_emailSortingIndex = m_page3->kcfg_EmailSorting->currentIndex();
505 void kgpgOptions::listKeys()
507 if (m_model->
rowCount(QModelIndex()) == 0) {
508 ids += QLatin1String(
"0");
509 m_page1->file_key->addItem(i18nc(
"no key available",
"none"));
510 m_page1->file_key->setModel(NULL);
511 m_page1->always_key->addItem(i18nc(
"no key available",
"none"));
512 m_page1->always_key->setModel(NULL);
514 m_page1->file_key->setModel(m_combomodel);
515 m_page1->always_key->setModel(m_combomodel);
519 void kgpgOptions::slotInstallDecrypt(
const QString &mimetype)
521 const QString path(KStandardDirs::locateLocal(
"data", QLatin1String(
"konqueror/servicemenus/decryptfile.desktop" )));
522 KDesktopFile configl2(path);
523 if (!configl2.isImmutable()) {
524 KConfigGroup gr(configl2.group(
"Desktop Entry"));
526 gr.writeXdgListEntry(
"MimeType", QStringList(mimetype));
527 gr.writeEntry(
"X-KDE-ServiceTypes",
"KonqPopupMenu/Plugin");
528 gr.writeEntry(
"Actions",
"decrypt");
530 gr = configl2.group(
"Desktop Action decrypt");
531 gr.writeEntry(
"Name", i18n(
"Decrypt File"));
533 gr.writeEntry(
"Exec",
"kgpg %U");
537 void kgpgOptions::slotInstallSign(
const QString &mimetype)
539 QString path(KStandardDirs::locateLocal(
"services", QLatin1String(
"ServiceMenus/signfile.desktop" )));
540 KDesktopFile configl2(path);
541 if (!configl2.isImmutable()) {
542 KConfigGroup gr = configl2.group(
"Desktop Entry");
543 gr.writeXdgListEntry(
"MimeType", QStringList(mimetype));
544 gr.writeEntry(
"X-KDE-ServiceTypes",
"KonqPopupMenu/Plugin");
545 gr.writeEntry(
"Actions",
"sign");
547 gr = configl2.group(
"Desktop Action sign");
548 gr.writeEntry(
"Name", i18n(
"Sign File"));
550 gr.writeEntry(
"Exec",
"kgpg -S %F");
554 void kgpgOptions::slotRemoveMenu(
const QString &menu)
556 QString path = KStandardDirs::locateLocal(
"services", QLatin1String(
"ServiceMenus/" ) + menu);
564 if (m_page1->kcfg_EncryptFilesTo->isChecked() && (m_page1->file_key->currentText() != fileEncryptionKey))
567 if (m_page1->encrypt_to_always->isChecked() != m_encrypttoalways)
570 if (m_page1->encrypt_to_always->isChecked() &&
571 (m_page1->always_key->itemData(m_page1->always_key->currentIndex(), Qt::ToolTipRole).
toString()) != alwaysKeyID)
574 if (m_page4->gpg_conf_path->text() != KUrl::fromPath(gpgConfigPath).fileName())
577 if (m_page4->gpg_home_path->text() != KUrl::fromPath(gpgConfigPath).directory(KUrl::AppendTrailingSlash))
580 if (m_page4->use_agent->isChecked() != m_useagent)
584 if (m_page6->ServerBox->count() != serverList.size())
588 for (
int i = 0; i < m_page6->ServerBox->count(); i++) {
589 QString server(m_page6->ServerBox->item(i)->text());
591 if (server.contains(QLatin1Char(
' ' ))) {
593 server.remove(QLatin1Char(
' ' ) + i18nc(
"Remove default marker from GUI if it is there",
"(Default)"));
594 if (keyServer != server)
596 }
else if (!serverList.contains(server)) {
601 if (m_page7->kcfg_ShowSystray->isChecked() != m_showsystray)
604 if (m_page7->kcfg_LeftClick->currentIndex() != m_trayaction)
607 if (m_page7->kcfg_MailUats->currentIndex() != m_mailUats)
610 if (m_page3->kcfg_EmailSorting->currentIndex() != m_emailSortingIndex)
618 if (m_page1->encrypt_to_always->isChecked() != m_defaultencrypttoalways)
621 if (m_page4->gpg_conf_path->text() != defaultConfigPath)
624 if (m_page4->gpg_home_path->text() != defaultHomePath)
627 if (m_page4->use_agent->isChecked() != m_defaultuseagent)
630 for (
int i = 0; i < m_page6->ServerBox->count(); i++) {
631 QString server(m_page6->ServerBox->item(i)->text());
633 if (server.contains(QLatin1Char(
' ' ))) {
635 server.remove(QLatin1Char(
' ' ) + i18nc(
"Remove default marker from GUI if it is there",
"(Default)"));
636 if (defaultKeyServer != server)
638 }
else if (!defaultServerList.contains(server)) {
643 if (m_page7->kcfg_ShowSystray->isChecked() != m_showsystray)
658 void kgpgOptions::slotSystrayEnable()
660 m_page7->kcfg_LeftClick->setEnabled(m_page7->kcfg_ShowSystray->isChecked());
663 #include "kgpgoptions.moc"
static int leftClick()
Get Choose default left-click behavior.
there is a minimal level of trust
static int mailUats()
Get Mail_Uats.
static QColor colorExpired()
Get Color used for expired keys.
static QString gpgConfigPath()
Get The path of the gpg configuration file.
static void setEmailSorting(int v)
Set EmailSorting.
void setGpgBoolSetting(const QString &name, const bool enable, const QString &url)
static void setShowSystray(bool v)
Set Show the systray icon.
static QColor colorUnknown()
Get Color used for unknown keys.
bool getGpgBoolSetting(const QString &name, const QString &configfile)
static bool showSystray()
Get Show the systray icon.
void setEncryptionKeyFilter(bool b)
show only keys capable of encryption
static QColor colorRev()
Get Color used for revoked keys.
static void setKeyServers(const QStringList &v)
Set Key servers used by KGpg.
void setKeyModel(KGpgItemModel *)
void setGpgSetting(const QString &name, const QString &value, const QString &url)
A public key with or without corresponding secret key.
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
static int emailSorting()
Get EmailSorting.
KGpgKeyNode * findKeyNode(const QString &id) const
void setTrustFilter(const KgpgCore::KgpgKeyTrustFlag t)
set the minimum trust level to be shown
void refreshTrust(KgpgCore::KgpgKeyTrust, QColor)
static QString gpgBinaryPath()
Get The path of the gpg binary used by KGpg.
key is disabled by user (not owner)
static QString emailTemplate()
Get This is the text of the email sent by the "Sign and Mail User ID" action.
static QColor colorUltimate()
Get Color used for ultimately trusted keys.
static QString fileEncryptionKey()
Get File encryption key.
static void setEmailTemplate(const QString &v)
Set This is the text of the email sent by the "Sign and Mail User ID" action.
kgpgOptions(QWidget *parent=0, KGpgItemModel *model=0)
QString toString(const KgpgKeyAlgo algorithm)
static void setFileEncryptionKey(const QString &v)
Set File encryption key.
you can fully trust this key
static void setLeftClick(int v)
Set Choose default left-click behavior.
static void setMailUats(int v)
Set Mail_Uats.
static void setGpgConfigPath(const QString &v)
Set The path of the gpg configuration file.
key is beyond it's expiry date
trust value undefined (i.e. you did not set a trust level)
static QColor colorGood()
Get Color used for trusted keys.
static KGpgSettings * self()
static QStringList keyServers()
Get Key servers used by KGpg.
KGpgItemModel * getModel() const
static QColor colorMarginal()
Get Color used for marginally trusted keys.
QModelIndex nodeIndex(KGpgNode *node)
virtual QModelIndex parent(const QModelIndex &child) const
trust value unknown (i.e. no entry in gpg's trust database)
virtual bool hasChanged()
this key has highest possible level of trust (e.g. your own secret keys)
there is no trusted path to this key
QString getGpgSetting(const QString &name, const QString &configfile)
static QColor colorBad()
Get Color used for untrusted keys.