33 #include <KActionCollection>
35 #include <KEncodingFileDialog>
37 #include <KFindDialog>
41 #include <KMessageBox>
42 #include <KRecentFilesAction>
44 #include <KStandardAction>
45 #include <KTemporaryFile>
46 #include <KToggleAction>
50 #include <QTextStream>
51 #include <QVBoxLayout>
53 #include <QtDBus/QtDBus>
54 #include <QtGui/QPrintDialog>
55 #include <QtGui/QPrinter>
56 #include <kio/netaccess.h>
57 #include <kio/renamedialog.h>
59 class KgpgView :
public QWidget {
67 QVBoxLayout *vb =
new QVBoxLayout(
this);
69 vb->addWidget(editor);
70 vb->addWidget(toolbar);
88 connect(
m_editor, SIGNAL(resetEncoding(
bool)), SLOT(slotResetEncoding(
bool)));
91 setCaption(i18n(
"Untitled"),
false);
92 m_editredo->setEnabled(
false);
93 m_editundo->setEnabled(
false);
94 m_editcopy->setEnabled(
false);
95 m_editcut->setEnabled(
false);
97 setObjectName( QLatin1String(
"editor" ));
99 setupGUI((ToolBar | Keys | StatusBar | Save | Create), QLatin1String(
"kgpgeditor.rc" ));
100 setAutoSaveSettings(QLatin1String(
"Editor" ),
true);
102 connect(
m_editor, SIGNAL(textChanged()), SLOT(modified()));
103 connect(
m_editor, SIGNAL(newText()), SLOT(newText()));
104 connect(
m_editor, SIGNAL(undoAvailable(
bool)), SLOT(slotUndoAvailable(
bool)));
105 connect(
m_editor, SIGNAL(redoAvailable(
bool)), SLOT(slotRedoAvailable(
bool)));
106 connect(
m_editor, SIGNAL(copyAvailable(
bool)), SLOT(slotCopyAvailable(
bool)));
111 m_recentfiles->saveEntries( KConfigGroup(KGlobal::config(),
"Recent Files" ) );
116 QString tempopenfile;
117 if(KIO::NetAccess::download(url, tempopenfile,
this))
119 QFile qfile(tempopenfile);
120 if (qfile.open(QIODevice::ReadOnly))
122 QTextStream t(&qfile);
123 t.setCodec(encoding.toAscii());
124 m_editor->setPlainText(t.readAll());
127 m_textchanged =
false;
129 setCaption(url.fileName(),
false);
132 KIO::NetAccess::removeTempFile(tempopenfile);
148 m_recentfiles->saveEntries( KConfigGroup(KGlobal::config(),
"Recent Files" ) );
160 KStandardAction::openNew(
this, SLOT(slotFileNew()), actionCollection());
161 KStandardAction::open(
this, SLOT(slotFileOpen()), actionCollection());
162 KStandardAction::save(
this, SLOT(slotFileSave()), actionCollection());
163 KStandardAction::saveAs(
this, SLOT(slotFileSaveAs()), actionCollection());
164 KStandardAction::close(
this, SLOT(slotFileClose()), actionCollection());
165 KStandardAction::paste(
this, SLOT(slotEditPaste()), actionCollection());
166 KStandardAction::print(
this, SLOT(slotFilePrint()), actionCollection());
167 KStandardAction::selectAll(
this, SLOT(slotSelectAll()), actionCollection());
168 KStandardAction::find(
this, SLOT(slotFind()), actionCollection());
169 KStandardAction::findNext(
this, SLOT(slotFindNext()), actionCollection());
170 KStandardAction::findPrev(
this, SLOT(slotFindPrev()), actionCollection());
171 actionCollection()->addAction(KStandardAction::Preferences, QLatin1String(
"options_configure" ),
172 this, SLOT(slotOptions()));
174 m_editundo = KStandardAction::undo(
this, SLOT(slotundo()), actionCollection());
175 m_editredo = KStandardAction::redo(
this, SLOT(slotredo()), actionCollection());
176 m_editcopy = KStandardAction::copy(
this, SLOT(slotEditCopy()), actionCollection());
177 m_editcut = KStandardAction::cut(
this, SLOT(slotEditCut()), actionCollection());
182 m_recentfiles->loadEntries( KConfigGroup(KGlobal::config(),
"Recent Files" ) );
185 KAction *action = actionCollection()->addAction(QLatin1String(
"file_encrypt"),
this, SLOT(slotFilePreEnc()));
186 action->setIcon(KIcon( QLatin1String(
"document-encrypt" )));
187 action->setText(i18n(
"&Encrypt File..."));
189 action = actionCollection()->addAction(QLatin1String(
"file_decrypt"),
this, SLOT(slotFilePreDec()));
190 action->setIcon(KIcon( QLatin1String(
"document-decrypt" )));
191 action->setText(i18n(
"&Decrypt File..."));
193 action = actionCollection()->addAction(QLatin1String(
"key_manage"),
this, SLOT(slotKeyManager()));
194 action->setIcon(KIcon( QLatin1String(
"kgpg" )));
195 action->setText(i18n(
"&Open Key Manager"));
197 action = actionCollection()->addAction(QLatin1String(
"sign_generate"),
this, SLOT(slotPreSignFile()));
198 action->setText(i18n(
"&Generate Signature..."));
199 action->setIcon(KIcon( QLatin1String(
"document-sign-key" )));
201 action = actionCollection()->addAction(QLatin1String(
"sign_verify"),
this, SLOT(slotPreVerifyFile()));
202 action->setText(i18n(
"&Verify Signature..."));
204 action = actionCollection()->addAction(QLatin1String(
"sign_check"),
this, SLOT(slotCheckMd5()));
205 action->setText(i18n(
"&Check MD5 Sum..."));
207 m_encodingaction = actionCollection()->add<KToggleAction>(QLatin1String(
"charsets"),
this, SLOT(slotSetCharset()));
208 m_encodingaction->setText(i18n(
"&Unicode (utf-8) Encoding"));
212 action = actionCollection()->addAction(QLatin1String(
"text_encrypt"),
m_editor, SLOT(slotEncode()));
213 action->setIcon(KIcon( QLatin1String(
"document-encrypt" )));
214 action->setText(i18n(
"En&crypt"));
216 action = actionCollection()->addAction(QLatin1String(
"text_decrypt"),
m_editor, SLOT(slotDecode()));
217 action->setIcon(KIcon( QLatin1String(
"document-decrypt" )));
218 action->setText(i18n(
"&Decrypt"));
220 action = actionCollection()->addAction(QLatin1String(
"text_sign_verify"),
m_editor, SLOT(slotSignVerify()));
221 action->setIcon(KIcon( QLatin1String(
"document-sign-key" )));
222 action->setText(i18n(
"S&ign/Verify"));
240 if (m_docname.fileName().isEmpty())
241 fname = i18n(
"Untitled");
243 fname = m_docname.fileName();
245 QString msg = i18n(
"The document \"%1\" has changed.\nDo you want to save it?", fname);
246 QString caption = i18n(
"Close the document");
247 int res = KMessageBox::warningYesNoCancel(
this, msg, caption, KStandardGuiItem::save(), KStandardGuiItem::discard());
248 if (res == KMessageBox::Yes)
249 return slotFileSave();
251 if (res == KMessageBox::No)
260 void KgpgEditor::slotFileNew()
269 void KgpgEditor::slotFileOpen()
273 KEncodingFileDialog::Result loadResult;
274 loadResult = KEncodingFileDialog::getOpenUrlAndEncoding(QString(), QString(), QString(),
this);
275 KUrl url = loadResult.URLs.first();
276 m_textencoding = loadResult.encoding;
283 bool KgpgEditor::slotFileSave()
285 QString filn = m_docname.path();
287 return slotFileSaveAs();
289 QTextCodec *cod = QTextCodec::codecForName(m_textencoding.toAscii());
292 KMessageBox::sorry(
this, i18n(
"The document could not been saved, as the selected codec is not supported."));
296 if (!checkEncoding(cod))
298 KMessageBox::sorry(
this, i18n(
"The document could not been saved, as the selected encoding cannot encode every unicode character in it."));
302 if (m_docname.isLocalFile())
305 if (!f.open(QIODevice::WriteOnly))
307 KMessageBox::sorry(
this, i18n(
"The document could not be saved, please check your permissions and disk space."));
318 KTemporaryFile tmpfile;
320 QTextStream stream(&tmpfile);
321 stream.setCodec(cod);
324 if(!KIO::NetAccess::upload(tmpfile.fileName(), m_docname,
this))
326 KMessageBox::sorry(
this, i18n(
"The document could not be saved, please check your permissions and disk space."));
331 m_textchanged =
false;
333 setCaption(m_docname.fileName(),
false);
337 bool KgpgEditor::slotFileSaveAs()
339 KEncodingFileDialog::Result saveResult;
340 saveResult = KEncodingFileDialog::getSaveUrlAndEncoding(QString(), QString(), QString(),
this);
343 if (!saveResult.URLs.empty())
344 url = saveResult.URLs.first();
347 const QString selectedEncoding = saveResult.encoding;
348 if (url.isLocalFile()) {
349 QString filn = url.path();
352 const QString message = i18n(
"Overwrite existing file %1?", url.fileName());
353 int result = KMessageBox::warningContinueCancel(
this, message, QString(), KStandardGuiItem::overwrite());
354 if (result == KMessageBox::Cancel)
358 }
else if (KIO::NetAccess::exists(url, KIO::NetAccess::DestinationSide,
this)) {
359 const QString message = i18n(
"Overwrite existing file %1?", url.fileName());
360 int result = KMessageBox::warningContinueCancel(
this, message, QString(), KStandardGuiItem::overwrite());
361 if (result == KMessageBox::Cancel)
366 m_textencoding = selectedEncoding;
374 void KgpgEditor::slotFilePrint()
377 QPointer<QPrintDialog> printDialog =
new QPrintDialog(&prt,
this);
378 if (printDialog->exec() == QDialog::Accepted) {
379 int width = prt.width();
380 int height = prt.height();
381 QPainter painter(&prt);
382 painter.drawText(0, 0, width, height, Qt::AlignLeft | Qt::AlignTop | Qt::TextDontClip,
m_editor->toPlainText());
387 void KgpgEditor::slotFind()
389 QPointer<KFindDialog> fd =
new KFindDialog(
this);
392 fd->setOptions(m_find->options());
393 fd->setPattern(m_find->pattern());
396 if (fd->exec() != QDialog::Accepted) {
402 m_find =
new KFind(fd->pattern(), fd->options(),
this);
404 if (m_find->options() & KFind::FromCursor)
405 m_find->setData(
m_editor->toPlainText(),
m_editor->textCursor().selectionStart());
407 m_find->setData(
m_editor->toPlainText());
408 connect(m_find, SIGNAL(highlight(QString,
int,
int)),
m_editor, SLOT(slotHighlightText(QString,
int,
int)));
409 connect(m_find, SIGNAL(findNext()),
this, SLOT(slotFindText()));
411 m_find->setPattern(fd->pattern());
412 m_find->setOptions(fd->options());
413 m_find->resetCounts();
420 void KgpgEditor::slotFindNext()
425 void KgpgEditor::slotFindPrev()
432 long oldopt = m_find->options();
433 long newopt = oldopt ^ KFind::FindBackwards;
434 m_find->setOptions(newopt);
436 m_find->setOptions(oldopt);
439 void KgpgEditor::slotFindText()
447 if (m_find->find() == KFind::NoMatch)
449 if (m_find->numMatches() == 0)
451 m_find->displayFinalDialog();
457 if (m_find->shouldRestart(
true,
false))
459 m_find->setData(
m_editor->toPlainText());
463 m_find->closeFindNextDialog();
468 void KgpgEditor::slotFilePreEnc()
470 KUrl::List urls = KFileDialog::getOpenUrls(KUrl(), i18n(
"*|All Files"),
this, i18n(
"Open File to Encode"));
477 void KgpgEditor::slotFilePreDec()
479 KUrl url = KFileDialog::getOpenUrl(KUrl(), i18n(
"*|All Files"),
this, i18n(
"Open File to Decode"));
483 QString oldname = url.fileName();
486 if (oldname.endsWith(QLatin1String(
".gpg")) || oldname.endsWith(QLatin1String(
".asc")) || oldname.endsWith(QLatin1String(
".pgp")))
489 oldname.append(QLatin1String(
".clear" ));
490 oldname.prepend(url.directory(KUrl::AppendTrailingSlash));
492 QPointer<KDialog> popn =
new KDialog(
this);
493 popn->setCaption( i18n(
"Decrypt File To") );
494 popn->setButtons( KDialog::Ok | KDialog::Cancel );
495 popn->setDefaultButton( KDialog::Ok );
496 popn->setModal(
true );
498 SrcSelect *page =
new SrcSelect();
499 popn->setMainWidget(page);
500 page->newFilename->setUrl(oldname);
501 page->newFilename->setMode(KFile::File);
502 page->newFilename->setWindowTitle(i18n(
"Save File"));
504 page->checkClipboard->setText(i18n(
"Editor"));
505 page->resize(page->minimumSize());
506 popn->resize(popn->minimumSize());
507 if (popn->exec() == QDialog::Accepted)
509 if (page->checkFile->isChecked())
510 newname = page->newFilename->url().path();
519 if (!newname.isEmpty())
523 QPointer<KIO::RenameDialog> over =
new KIO::RenameDialog(
this, i18n(
"File Already Exists"), KUrl(), KUrl::fromPath(newname), KIO::M_OVERWRITE);
525 if (over->exec() != QDialog::Accepted) {
529 newname = over->newDestUrl().path();
534 connect(decr, SIGNAL(done(
int)), SLOT(slotLibraryDone()));
542 KgpgEditor::slotLibraryDone()
544 sender()->deleteLater();
547 void KgpgEditor::slotKeyManager()
553 void KgpgEditor::slotFileClose()
559 void KgpgEditor::slotundo()
564 void KgpgEditor::slotredo()
569 void KgpgEditor::slotEditCut()
574 void KgpgEditor::slotEditCopy()
579 void KgpgEditor::slotEditPaste()
584 void KgpgEditor::slotSelectAll()
589 void KgpgEditor::slotSetCharset()
591 if (!m_encodingaction->isChecked())
592 m_editor->setPlainText(QString::fromUtf8(
m_editor->toPlainText().toAscii()));
595 if (checkEncoding(QTextCodec::codecForLocale()))
601 bool KgpgEditor::checkEncoding(QTextCodec *codec)
603 return codec->canEncode(
m_editor->toPlainText());
606 void KgpgEditor::slotResetEncoding(
bool enc)
608 m_encodingaction->setChecked(enc);
611 void KgpgEditor::slotPreSignFile()
614 KUrl url = KFileDialog::getOpenUrl(KUrl(), i18n(
"*|All Files"),
this, i18n(
"Open File to Sign"));
619 void KgpgEditor::slotSignFile(
const KUrl &url)
626 if (opts->exec() == QDialog::Accepted) {
627 signKeyID = opts->getKeyID();
640 connect(signt, SIGNAL(done(
int)), SLOT(slotSignFileFin()));
645 void KgpgEditor::slotSignFileFin()
647 sender()->deleteLater();
650 void KgpgEditor::slotPreVerifyFile()
652 KUrl url = KFileDialog::getOpenUrl(KUrl(), i18n(
"*|All Files"),
this, i18n(
"Open File to Verify"));
656 void KgpgEditor::slotVerifyFile(
const KUrl &url)
661 if (!url.fileName().endsWith(QLatin1String(
".sig")))
663 sigfile = url.path() + QLatin1String(
".sig" );
667 sigfile = url.path() + QLatin1String(
".asc" );
676 if (sigfile.isEmpty())
679 chkfiles << KUrl::fromPath(sigfile);
682 connect(verify, SIGNAL(done(
int)),
m_editor, SLOT(slotVerifyDone(
int)));
687 void KgpgEditor::slotCheckMd5()
690 KUrl url = KFileDialog::getOpenUrl(KUrl(), i18n(
"*|All Files"),
this, i18n(
"Open File to Verify"));
691 if (!url.isEmpty()) {
692 QPointer<Md5Widget> mdwidget =
new Md5Widget(
this, url);
698 void KgpgEditor::importSignatureKey(
const QString &
id,
const QString &fileName)
700 sender()->deleteLater();
702 if (KMessageBox::questionYesNo(0,
703 i18n(
"<qt><b>Missing signature:</b><br />Key id: %1<br /><br />Do you want to import this key from a keyserver?</qt>",
id),
704 fileName, KGuiItem(i18n(
"Import")), KGuiItem(i18n(
"Do Not Import"))) != KMessageBox::Yes)
709 connect(ks, SIGNAL(importFinished(QStringList)), SLOT(slotDownloadKeysFinished(QStringList)));
710 connect(ks, SIGNAL(importFailed()), ks, SLOT(deleteLater()));
712 ks->
startImport(QStringList(
id), QString(),QLatin1String( qgetenv(
"http_proxy") ));
715 void KgpgEditor::slotDownloadKeysFinished(QStringList ids)
719 sender()->deleteLater();
723 KgpgEditor::slotVerifyFinished(
const QString &
id,
const QString &message)
725 sender()->deleteLater();
730 showId = i18n(
"No signature found.");
735 i18nc(
"Caption of message box",
"Verification Finished"));
738 void KgpgEditor::slotOptions()
743 void KgpgEditor::modified()
745 QString capt = m_docname.fileName();
747 m_textchanged = !
m_editor->toPlainText().isEmpty();
749 capt = i18n(
"Untitled");
750 }
else if (!m_textchanged) {
751 m_textchanged =
true;
753 setCaption(capt, m_textchanged);
754 m_editor->document()->setModified(m_textchanged);
757 void KgpgEditor::slotUndoAvailable(
const bool &v)
759 m_editundo->setEnabled(v);
762 void KgpgEditor::slotRedoAvailable(
const bool &v)
764 m_editredo->setEnabled(v);
767 void KgpgEditor::slotCopyAvailable(
const bool &v)
769 m_editcopy->setEnabled(v);
770 m_editcut->setEnabled(v);
773 void KgpgEditor::newText()
775 m_textchanged =
false;
778 setCaption(i18n(
"Untitled"),
false);
779 slotResetEncoding(
false);
782 #include "kgpgeditor.moc"
KgpgTextEdit *const m_editor
output the data as printable ASCII as opposed to binary data
decrypt the given text or files
verify the signature of the given text or files
void start()
Start the operation.
KRecentFilesAction * m_recentfiles
static QFont font()
Get Font.
void refreshKeys(const QStringList &ids)
static bool asciiArmor()
Get Use ASCII armored encryption.
static void encryptFiles(KeysManager *parent, const KUrl::List &urls)
create a new object, encrypt the given files, and destroy the object
void openEncryptedDocumentFile(const KUrl &url)
void startImport(const QStringList &keys, QString server=QString(), const QString &proxy=QString())
import the given keys
static void setFirstRun(bool v)
Set Is the first time the application runs.
sign the given text or files
static KGpgSettings * self()
static uint recentFiles()
Get The number of recently opened files shown in the editor menu.
void openDocumentFile(const KUrl &url, const QString &encoding=QString())
void slotDroppedFile(const KUrl &url)
void slotSetFont(QFont myFont)
save the signature in a separate file