00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "kgpglibrary.h"
00019
00020 #include <QDesktopWidget>
00021 #include <QApplication>
00022 #include <QTextStream>
00023 #include <QFile>
00024
00025 #include <KJob>
00026 #include <KFileDialog>
00027 #include <KPassivePopup>
00028 #include <kio/renamedialog.h>
00029 #include <KMessageBox>
00030 #include <KLocale>
00031 #include <KConfig>
00032 #include <kio/deletejob.h>
00033 #include <kio/jobuidelegate.h>
00034
00035 #include "images.h"
00036 #include "selectpublickeydialog.h"
00037 #include "kgpgsettings.h"
00038 #include "kgpginterface.h"
00039
00040 using namespace KgpgCore;
00041
00042 KgpgLibrary::KgpgLibrary(QWidget *parent, const bool &pgpExtension)
00043 {
00044 if (pgpExtension)
00045 m_extension = ".pgp";
00046 else
00047 m_extension = ".gpg";
00048
00049 m_popisactive = false;
00050 m_panel = parent;
00051 }
00052
00053 void KgpgLibrary::slotFileEnc(const KUrl::List &urls, const QStringList &opts, const QStringList &defaultKey, const KShortcut &goDefaultKey)
00054 {
00055 if (!urls.empty())
00056 {
00057 m_urlselecteds = urls;
00058 if (defaultKey.isEmpty())
00059 {
00060 QString fileNames = urls.first().fileName();
00061 if (urls.count() > 1)
00062 fileNames += ",...";
00063
00064 KgpgSelectPublicKeyDlg *dialog = new KgpgSelectPublicKeyDlg(0, fileNames, true, goDefaultKey);
00065 if (dialog->exec() == KDialog::Accepted)
00066 {
00067 QStringList options;
00068 if (dialog->getUntrusted()) options << "--always-trust";
00069 if (dialog->getArmor()) options << "--armor";
00070 if (dialog->getHideId()) options << "--throw-keyid";
00071
00072 if (!dialog->getCustomOptions().isEmpty())
00073 if (KGpgSettings::allowCustomEncryptionOptions())
00074 options << dialog->getCustomOptions().split(" ");
00075
00076 startEncode(dialog->selectedKeys(), options, dialog->getSymmetric());
00077 }
00078
00079 delete dialog;
00080 }
00081 else
00082 startEncode(defaultKey, opts, false);
00083 }
00084 }
00085
00086 void KgpgLibrary::startEncode(const QStringList &encryptkeys, const QStringList &encryptoptions, const bool &symetric)
00087 {
00088 m_popisactive = false;
00089
00090
00091 m_encryptkeys = encryptkeys;
00092 m_encryptoptions = encryptoptions;
00093 m_symetric = symetric;
00094 fastEncode(m_urlselecteds.first(), encryptkeys, encryptoptions, symetric);
00095 }
00096
00097 void KgpgLibrary::fastEncode(const KUrl &filetocrypt, const QStringList &encryptkeys, const QStringList &encryptoptions, const bool &symetric)
00098 {
00099 if ((encryptkeys.isEmpty()) && (!symetric))
00100 {
00101 KMessageBox::sorry(0, i18n("You have not chosen an encryption key."));
00102 return;
00103 }
00104
00105 m_urlselected = filetocrypt;
00106
00107 KUrl dest;
00108 if (encryptoptions.contains("--armor"))
00109 dest.setPath(m_urlselected.path() + ".asc");
00110 else
00111 dest.setPath(m_urlselected.path() + m_extension);
00112
00113 QFile fgpg(dest.path());
00114 if (fgpg.exists())
00115 {
00116 KIO::RenameDialog over(0, i18n("File Already Exists"), KUrl(), dest, KIO::M_OVERWRITE);
00117 if (over.exec() == QDialog::Rejected)
00118 {
00119 emit systemMessage(QString(), true);
00120 return;
00121 }
00122
00123 dest = over.newDestUrl();
00124 }
00125
00126 int filesToEncode = m_urlselecteds.count();
00127 if (filesToEncode > 1)
00128 emit systemMessage(i18n("<p><b>%1 Files left.</b><br /><b>Encrypting </b>%2</p>", filesToEncode, m_urlselecteds.first().path()));
00129 else
00130 emit systemMessage(i18n("<b>Encrypting </b>%1", m_urlselecteds.first().path()));
00131
00132 m_pop = new KPassivePopup(m_panel);
00133
00134 KgpgInterface *cryptFileProcess = new KgpgInterface();
00135 connect(cryptFileProcess, SIGNAL(fileEncryptionFinished(KUrl, KgpgInterface*)), this, SLOT(processEnc(KUrl, KgpgInterface*)));
00136 connect(cryptFileProcess, SIGNAL(errorMessage(QString, KgpgInterface*)), this, SLOT(processEncError(QString, KgpgInterface*)));
00137 cryptFileProcess->encryptFile(encryptkeys, m_urlselected, dest, encryptoptions, symetric);
00138
00139 if (!m_popisactive)
00140 {
00141
00142 m_popisactive = true;
00143 }
00144
00145 }
00146
00147 void KgpgLibrary::processEnc(const KUrl &, KgpgInterface *i)
00148 {
00149 delete i;
00150 emit systemMessage(QString());
00151
00152 m_urlselecteds.pop_front();
00153
00154 if (m_urlselecteds.count() > 0)
00155 fastEncode(m_urlselecteds.first(), m_encryptkeys, m_encryptoptions, m_symetric);
00156 else
00157 emit encryptionOver();
00158 }
00159
00160 void KgpgLibrary::processEncError(const QString &mssge, KgpgInterface *i)
00161 {
00162 delete i;
00163 m_popisactive = false;
00164 emit systemMessage(QString(), true);
00165 KMessageBox::detailedSorry(m_panel, i18n("<p><b>Process halted</b>.<br />Not all files were encrypted.</p>"), mssge);
00166 }
00167
00168 void KgpgLibrary::slotFileDec(const KUrl &src, const KUrl &dest, const QStringList &customDecryptOption)
00169 {
00170
00171 m_pop = new KPassivePopup();
00172 m_urlselected = src;
00173
00174 KgpgInterface *decryptFileProcess = new KgpgInterface();
00175 decryptFileProcess->decryptFile(src, dest, customDecryptOption);
00176 connect(decryptFileProcess, SIGNAL(decryptFileStarted(KUrl)), this, SLOT(processEncPopup(KUrl)));
00177 connect(decryptFileProcess, SIGNAL(decryptFileFinished(int, KgpgInterface*)), this, SLOT(processDecOver()));
00178 }
00179
00180 void KgpgLibrary::processDecOver()
00181 {
00182 emit systemMessage(QString());
00183 delete m_pop;
00184 emit decryptionOver();
00185 }
00186
00187 void KgpgLibrary::processDecError(const QString &mssge)
00188 {
00189 delete m_pop;
00190 emit systemMessage(QString());
00191
00192
00193 QFile qfile(QFile::encodeName(m_urlselected.path()));
00194 if (qfile.open(QIODevice::ReadOnly))
00195 {
00196 QTextStream t(&qfile);
00197 QString result = t.readAll();
00198 qfile.close();
00199
00200
00201 if (result.startsWith("-----BEGIN PGP PUBLIC KEY BLOCK"))
00202 {
00203
00204 int result = KMessageBox::warningContinueCancel(0, i18n("<p>The file <b>%1</b> is a public key.<br />Do you want to import it ?</p>", m_urlselected.path()));
00205 if (result == KMessageBox::Cancel)
00206 return;
00207 else
00208 {
00209 KgpgInterface *importKeyProcess = new KgpgInterface();
00210 importKeyProcess->importKey(m_urlselected);
00211 connect(importKeyProcess, SIGNAL(importfinished(QStringList)), this, SIGNAL(importOver(QStringList)));
00212 return;
00213 }
00214 }
00215 else
00216 if(result.startsWith("-----BEGIN PGP PRIVATE KEY BLOCK"))
00217 {
00218
00219 qfile.close();
00220 KMessageBox::information(0, i18n("<p>The file <b>%1</b> is a private key block. Please use KGpg key manager to import it.</p>", m_urlselected.path()));
00221 return;
00222 }
00223 }
00224
00225 KMessageBox::detailedSorry(0, i18n("Decryption failed."), mssge);
00226 }
00227
00228 void KgpgLibrary::processEncPopup(const KUrl &url)
00229 {
00230 emit systemMessage(i18n("Decrypting %1", url.pathOrUrl()));
00231
00232 m_pop->setTimeout(0);
00233 m_pop->setView(i18n("Processing decryption"), i18n("Please wait..."), Images::kgpg());
00234 m_pop->show();
00235
00236 QRect qRect(QApplication::desktop()->screenGeometry());
00237 int iXpos = qRect.width() / 2 - m_pop->width() / 2;
00238 int iYpos = qRect.height() / 2 - m_pop->height() / 2;
00239 m_pop->move(iXpos, iYpos);
00240 }
00241
00242 void KgpgLibrary::processPopup2(const QString &fileName)
00243 {
00244
00245 m_pop->setView(i18n("Processing encryption (%1)", fileName),i18n("Please wait..."), Images::kgpg());
00246 m_pop->show();
00247
00248
00249
00250
00251 }
00252
00253 void KgpgLibrary::addPhoto(const QString &keyid)
00254 {
00255 QString mess = i18n("The image must be a JPEG file. Remember that the image is stored within your public key."
00256 "If you use a very large picture, your key will become very large as well! Keeping the image "
00257 "close to 240x288 is a good size to use.");
00258
00259 if (KMessageBox::warningContinueCancel(0, mess) != KMessageBox::Continue)
00260 return;
00261
00262 QString imagepath = KFileDialog::getOpenFileName(KUrl(), "image/jpeg", 0);
00263 if (imagepath.isEmpty())
00264 return;
00265
00266 KgpgInterface *interface = new KgpgInterface();
00267 connect(interface, SIGNAL(addPhotoFinished(int, KgpgInterface*)), this, SLOT(slotAddPhotoFinished(int, KgpgInterface*)));
00268 interface->addPhoto(keyid, imagepath);
00269 }
00270
00271 void KgpgLibrary::slotAddPhotoFinished(int res, KgpgInterface *interface)
00272 {
00273 delete interface;
00274
00275
00276
00277 if (res == 2)
00278 emit photoAdded();
00279 }
00280
00281 #include "kgpglibrary.moc"