Debian Codegraph @

src/libs/widgets/KoDocumentInfoDlg.cpp

raw
1 /* This file is part of the KDE project
2 SPDX-FileCopyrightText: 2000 Simon Hausmann <[email protected]>
3 2006 Martin Pfeiffer <[email protected]>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8 // clazy:excludeall=qstring-arg
9 #include "KoDocumentInfoDlg.h"
10 #include "WidgetsDebug.h"
11
12 #include "ui_koDocumentInfoAboutWidget.h"
13 #include "ui_koDocumentInfoAuthorWidget.h"
14
15 #include "KoDocumentInfo.h"
16 #include "KoDocumentBase.h"
17 #include "KoGlobal.h"
18 #ifdef QCA2
19 #include <KoEncryptionChecker.h>
20 #endif
21 #include "KoPageWidgetItem.h"
22 //#include <KoDocumentRdfBase.h>
23 #include <KoIcon.h>
24
25 #include <KLocalizedString>
26 #include <KIconLoader>
27
28 #include <KMessageBox>
29 #include <KMainWindow>
30 #include <KoDialog.h>
31
32 #include <QUrl>
33 #include <QLineEdit>
34 #include <QDateTime>
35 #include <QMimeDatabase>
36 #include <QMimeType>
37
38
39 // see KoIcon.h
40 #define koSmallPixmap(name) KIconLoader::global()->loadIcon(QStringLiteral(name), KIconLoader::Small)
41
42 class KoPageWidgetItemAdapter : public KPageWidgetItem
43 {
44 Q_OBJECT
45 public:
46 KoPageWidgetItemAdapter(KoPageWidgetItem *item)
47 : KPageWidgetItem(item->widget(), item->name())
48 , m_item(item)
49 {
50 setHeader(item->name());
51 setIcon(QIcon::fromTheme(item->iconName()));
52 }
53 ~KoPageWidgetItemAdapter() override { delete m_item; }
54
55 bool shouldDialogCloseBeVetoed() { return m_item->shouldDialogCloseBeVetoed(); }
56 void apply() { m_item->apply(); }
57
58 private:
59 KoPageWidgetItem * const m_item;
60 };
61
62
63 class KoDocumentInfoDlg::KoDocumentInfoDlgPrivate
64 {
65 public:
66 KoDocumentInfoDlgPrivate() :
67 toggleEncryption(false),
68 applyToggleEncryption(false),
69 documentSaved(false) {}
70 ~KoDocumentInfoDlgPrivate() {}
71
72 KoDocumentInfo* info;
73 QList<KPageWidgetItem*> pages;
74 Ui::KoDocumentInfoAboutWidget* aboutUi;
75 Ui::KoDocumentInfoAuthorWidget* authorUi;
76
77 bool toggleEncryption;
78 bool applyToggleEncryption;
79 bool documentSaved;
80 };
81
82
83 KoDocumentInfoDlg::KoDocumentInfoDlg(QWidget* parent, KoDocumentInfo* docInfo)
84 : KPageDialog(parent)
85 , d(new KoDocumentInfoDlgPrivate)
86 {
87 d->info = docInfo;
88
89 setWindowTitle(i18n("Document Information"));
90 // setInitialSize(QSize(500, 500));
91 setFaceType(KPageDialog::List);
92 setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
93 button(QDialogButtonBox::Ok)->setDefault(true);
94
95 d->aboutUi = new Ui::KoDocumentInfoAboutWidget();
96 QWidget *infodlg = new QWidget();
97 d->aboutUi->setupUi(infodlg);
98 #ifdef QCA2
99 if (!KoEncryptionChecker::isEncryptionSupported()) {
100 #endif
101 d->aboutUi->lblEncryptedDesc->setVisible(false);
102 d->aboutUi->lblEncrypted->setVisible(false);
103 d->aboutUi->pbEncrypt->setVisible(false);
104 d->aboutUi->lblEncryptedPic->setVisible(false);
105 #ifdef QCA2
106 }
107 #endif
108 d->aboutUi->cbLanguage->addItems(KoGlobal::listOfLanguages());
109 d->aboutUi->cbLanguage->setCurrentIndex(-1);
110
111 KPageWidgetItem *page = new KPageWidgetItem(infodlg, i18n("General"));
112 page->setHeader(i18n("General"));
113
114 // Ugly hack, the mimetype should be a parameter, instead
115 KoDocumentBase* doc = dynamic_cast< KoDocumentBase* >(d->info->parent());
116 if (doc) {
117 QMimeDatabase db;
118 QMimeType mime = db.mimeTypeForName(QString::fromLatin1(doc->mimeType()));
119 if (mime.isValid()) {
120 page->setIcon(QIcon::fromTheme(mime.iconName()));
121 }
122 } else {
123 // hide all entries not used in pages for KoDocumentInfoPropsPage
124 d->aboutUi->filePathInfoLabel->setVisible(false);
125 d->aboutUi->filePathLabel->setVisible(false);
126 d->aboutUi->filePathSeparatorLine->setVisible(false);
127 d->aboutUi->lblTypeDesc->setVisible(false);
128 d->aboutUi->lblType->setVisible(false);
129 }
130 addPage(page);
131 d->pages.append(page);
132
133 initAboutTab();
134
135 d->authorUi = new Ui::KoDocumentInfoAuthorWidget();
136 QWidget *authordlg = new QWidget();
137 d->authorUi->setupUi(authordlg);
138 page = new KPageWidgetItem(authordlg, i18n("Author"));
139 page->setHeader(i18n("Last saved by"));
140 page->setIcon(koIcon("user-identity"));
141 addPage(page);
142 d->pages.append(page);
143
144 initAuthorTab();
145 }
146
147 KoDocumentInfoDlg::~KoDocumentInfoDlg()
148 {
149 delete d->authorUi;
150 delete d->aboutUi;
151 delete d;
152 }
153
154 void KoDocumentInfoDlg::accept()
155 {
156 // check if any pages veto the close
157 for (KPageWidgetItem* item : std::as_const(d->pages)) {
158 KoPageWidgetItemAdapter *page = dynamic_cast<KoPageWidgetItemAdapter*>(item);
159 if (page) {
160 if (page->shouldDialogCloseBeVetoed()) {
161 return;
162 }
163 }
164 }
165
166 // all fine, go and apply
167 saveAboutData();
168 for (KPageWidgetItem* item : std::as_const(d->pages)) {
169 KoPageWidgetItemAdapter *page = dynamic_cast<KoPageWidgetItemAdapter*>(item);
170 if (page) {
171 page->apply();
172 }
173 }
174
175 KPageDialog::accept();
176 }
177
178 bool KoDocumentInfoDlg::isDocumentSaved()
179 {
180 return d->documentSaved;
181 }
182
183 void KoDocumentInfoDlg::initAboutTab()
184 {
185 KoDocumentBase* doc = dynamic_cast< KoDocumentBase* >(d->info->parent());
186
187 if (doc) {
188 d->aboutUi->filePathLabel->setText(doc->localFilePath());
189 }
190
191 d->aboutUi->leTitle->setText(d->info->aboutInfo("title"));
192 d->aboutUi->leSubject->setText(d->info->aboutInfo("subject"));
193 QString language = KoGlobal::languageFromTag(d->info->aboutInfo("language"));
194 d->aboutUi->cbLanguage->setCurrentIndex(d->aboutUi->cbLanguage->findText(language));
195
196 d->aboutUi->leKeywords->setToolTip(i18n("Use ';' (Example: Office;KDE;Calligra)"));
197 if (!d->info->aboutInfo("keyword").isEmpty())
198 d->aboutUi->leKeywords->setText(d->info->aboutInfo("keyword"));
199
200 d->aboutUi->meComments->setPlainText(d->info->aboutInfo("description"));
201 if (doc && !doc->mimeType().isEmpty()) {
202 QMimeDatabase db;
203 QMimeType docmime = db.mimeTypeForName(QLatin1String(doc->mimeType()));
204 if (docmime.isValid())
205 d->aboutUi->lblType->setText(docmime.comment());
206 }
207 if (!d->info->aboutInfo("creation-date").isEmpty()) {
208 QDateTime t = QDateTime::fromString(d->info->aboutInfo("creation-date"),
209 Qt::ISODate);
210 QString s = QLocale().toString(t);
211 d->aboutUi->lblCreated->setText(s + QStringLiteral(", ") +
212 d->info->aboutInfo("initial-creator"));
213 }
214
215 if (!d->info->aboutInfo("date").isEmpty()) {
216 QDateTime t = QDateTime::fromString(d->info->aboutInfo("date"), Qt::ISODate);
217 QString s = QLocale().toString(t);
218 d->aboutUi->lblModified->setText(s + QStringLiteral(", ") + d->info->authorInfo("creator"));
219 }
220
221 d->aboutUi->lblRevision->setText(d->info->aboutInfo("editing-cycles"));
222
223 if (doc && (doc->supportedSpecialFormats() & KoDocumentBase::SaveEncrypted)) {
224 if (doc->specialOutputFlag() == KoDocumentBase::SaveEncrypted) {
225 if (d->toggleEncryption) {
226 d->aboutUi->lblEncrypted->setText(i18n("This document will be decrypted"));
227 d->aboutUi->lblEncryptedPic->setPixmap(koSmallPixmap("object-unlocked"));
228 d->aboutUi->pbEncrypt->setText(i18n("Do not decrypt"));
229 } else {
230 d->aboutUi->lblEncrypted->setText(i18n("This document is encrypted"));
231 d->aboutUi->lblEncryptedPic->setPixmap(koSmallPixmap("object-locked"));
232 d->aboutUi->pbEncrypt->setText(i18n("D&ecrypt"));
233 }
234 } else {
235 if (d->toggleEncryption) {
236 d->aboutUi->lblEncrypted->setText(i18n("This document will be encrypted."));
237 d->aboutUi->lblEncryptedPic->setPixmap(koSmallPixmap("object-locked"));
238 d->aboutUi->pbEncrypt->setText(i18n("Do not encrypt"));
239 } else {
240 d->aboutUi->lblEncrypted->setText(i18n("This document is not encrypted"));
241 d->aboutUi->lblEncryptedPic->setPixmap(koSmallPixmap("object-unlocked"));
242 d->aboutUi->pbEncrypt->setText(i18n("&Encrypt"));
243 }
244 }
245 } else {
246 d->aboutUi->lblEncrypted->setText(i18n("This document does not support encryption"));
247 d->aboutUi->pbEncrypt->setEnabled(false);
248 }
249 connect(d->aboutUi->pbReset, &QAbstractButton::clicked,
250 this, &KoDocumentInfoDlg::slotResetMetaData);
251 connect(d->aboutUi->pbEncrypt, &QAbstractButton::clicked,
252 this, &KoDocumentInfoDlg::slotToggleEncryption);
253 }
254
255 void KoDocumentInfoDlg::initAuthorTab()
256 {
257 d->authorUi->fullName->setText(d->info->authorInfo("creator"));
258 d->authorUi->initials->setText(d->info->authorInfo("initial"));
259 d->authorUi->title->setText(d->info->authorInfo("author-title"));
260 d->authorUi->company->setText(d->info->authorInfo("company"));
261 d->authorUi->email->setText(d->info->authorInfo("email"));
262 d->authorUi->phoneWork->setText(d->info->authorInfo("telephone-work"));
263 d->authorUi->phoneHome->setText(d->info->authorInfo("telephone"));
264 d->authorUi->fax->setText(d->info->authorInfo("fax"));
265 d->authorUi->country->setText(d->info->authorInfo("country"));
266 d->authorUi->postal->setText(d->info->authorInfo("postal-code"));
267 d->authorUi->city->setText(d->info->authorInfo("city"));
268 d->authorUi->street->setText(d->info->authorInfo("street"));
269 d->authorUi->position->setText(d->info->authorInfo("position"));
270 }
271
272 void KoDocumentInfoDlg::saveAboutData()
273 {
274 d->info->setAboutInfo("keyword", d->aboutUi->leKeywords->text());
275 d->info->setAboutInfo("title", d->aboutUi->leTitle->text());
276 d->info->setAboutInfo("subject", d->aboutUi->leSubject->text());
277 d->info->setAboutInfo("description", d->aboutUi->meComments->toPlainText());
278 d->info->setAboutInfo("language", KoGlobal::tagOfLanguage(d->aboutUi->cbLanguage->currentText()));
279 d->applyToggleEncryption = d->toggleEncryption;
280 }
281
282 void KoDocumentInfoDlg::hideEvent(QHideEvent *event)
283 {
284 Q_UNUSED(event);
285
286 // Saving encryption implies saving the document, this is done after closing the dialog
287 // TODO: shouldn't this be skipped if cancel is pressed?
288 saveEncryption();
289 }
290
291 void KoDocumentInfoDlg::slotResetMetaData()
292 {
293 d->info->resetMetaData();
294
295 if (!d->info->aboutInfo("creation-date").isEmpty()) {
296 QDateTime t = QDateTime::fromString(d->info->aboutInfo("creation-date"),
297 Qt::ISODate);
298 QString s = QLocale().toString(t);
299 d->aboutUi->lblCreated->setText(s + QStringLiteral(", ") +
300 d->info->aboutInfo("initial-creator"));
301 }
302
303 if (!d->info->aboutInfo("date").isEmpty()) {
304 QDateTime t = QDateTime::fromString(d->info->aboutInfo("date"), Qt::ISODate);
305 QString s = QLocale().toString(t);
306 d->aboutUi->lblModified->setText(s + QStringLiteral(", ") + d->info->authorInfo("creator"));
307 }
308
309 d->aboutUi->lblRevision->setText(d->info->aboutInfo("editing-cycles"));
310 }
311
312 void KoDocumentInfoDlg::slotToggleEncryption()
313 {
314 KoDocumentBase* doc = dynamic_cast< KoDocumentBase* >(d->info->parent());
315 if (!doc)
316 return;
317
318 d->toggleEncryption = !d->toggleEncryption;
319
320 if (doc->specialOutputFlag() == KoDocumentBase::SaveEncrypted) {
321 if (d->toggleEncryption) {
322 d->aboutUi->lblEncrypted->setText(i18n("This document will be decrypted"));
323 d->aboutUi->lblEncryptedPic->setPixmap(koSmallPixmap("object-unlocked"));
324 d->aboutUi->pbEncrypt->setText(i18n("Do not decrypt"));
325 } else {
326 d->aboutUi->lblEncrypted->setText(i18n("This document is encrypted"));
327 d->aboutUi->lblEncryptedPic->setPixmap(koSmallPixmap("object-locked"));
328 d->aboutUi->pbEncrypt->setText(i18n("D&ecrypt"));
329 }
330 } else {
331 if (d->toggleEncryption) {
332 d->aboutUi->lblEncrypted->setText(i18n("This document will be encrypted."));
333 d->aboutUi->lblEncryptedPic->setPixmap(koSmallPixmap("object-locked"));
334 d->aboutUi->pbEncrypt->setText(i18n("Do not encrypt"));
335 } else {
336 d->aboutUi->lblEncrypted->setText(i18n("This document is not encrypted"));
337 d->aboutUi->lblEncryptedPic->setPixmap(koSmallPixmap("object-unlocked"));
338 d->aboutUi->pbEncrypt->setText(i18n("&Encrypt"));
339 }
340 }
341 }
342
343 void KoDocumentInfoDlg::saveEncryption()
344 {
345 if (!d->applyToggleEncryption)
346 return;
347
348 KoDocumentBase* doc = dynamic_cast< KoDocumentBase* >(d->info->parent());
349 if (!doc)
350 return;
351
352 KMainWindow* mainWindow = dynamic_cast< KMainWindow* >(parent());
353 bool saveas = false;
354 if (doc->specialOutputFlag() == KoDocumentBase::SaveEncrypted) {
355 // Decrypt
356 if (KMessageBox::warningContinueCancel(
357 this,
358 i18n("<qt>Decrypting the document will remove the password protection from it."
359 "<p>Do you still want to decrypt the file?</qt>"),
360 i18n("Confirm Decrypt"),
361 KGuiItem(i18n("Decrypt")),
362 KStandardGuiItem::cancel(),
363 QStringLiteral("DecryptConfirmation")
364 ) != KMessageBox::Continue) {
365 return;
366 }
367 bool modified = doc->isModified();
368 doc->setOutputMimeType(doc->outputMimeType(), doc->specialOutputFlag() & ~KoDocumentBase::SaveEncrypted);
369 if (!mainWindow) {
370 KMessageBox::information(
371 this,
372 i18n("<qt>Your document could not be saved automatically."
373 "<p>To complete the decryption, please save the document.</qt>"),
374 i18n("Save Document"),
375 QStringLiteral("DecryptSaveMessage"));
376 return;
377 }
378 if (modified && KMessageBox::questionTwoActions(
379 this,
380 i18n("<qt>The document has been changed since it was opened. To complete the decryption the document needs to be saved."
381 "<p>Do you want to save the document now?</qt>"),
382 i18n("Save Document"),
383 KStandardGuiItem::save(),
384 KStandardGuiItem::dontSave(),
385 QStringLiteral("DecryptSaveConfirmation")
386 ) != KMessageBox::PrimaryAction) {
387 return;
388 }
389 } else if (doc->mimeType().startsWith("application/vnd.oasis.opendocument.")) {
390 // Encrypt oasis document
391 bool modified = doc->isModified();
392 if (!doc->url().isEmpty() && doc->specialOutputFlag() == 0) {
393 QMimeDatabase db;
394 QMimeType mime = db.mimeTypeForName(QString::fromLatin1(doc->mimeType()));
395 QString comment = mime.isValid() ? mime.comment() : i18n("%1 (unknown file type)", QString::fromLatin1(doc->mimeType()));
396 if (KMessageBox::warningContinueCancel(
397 this,
398 i18n("<qt>The document is currently saved as %1. The document needs to be changed to <b>OASIS OpenDocument</b> to be encrypted."
399 "<p>Do you want to change the file to OASIS OpenDocument?</qt>", QStringLiteral("<b>%1</b>").arg(comment)),
400 i18n("Change Filetype"),
401 KGuiItem(i18n("Change")),
402 KStandardGuiItem::cancel(),
403 QStringLiteral("EncryptChangeFiletypeConfirmation")
404 ) != KMessageBox::Continue) {
405 return;
406 }
407 doc->resetURL();
408 }
409 doc->setMimeType(doc->nativeOasisMimeType());
410 doc->setOutputMimeType(doc->nativeOasisMimeType(), KoDocumentBase::SaveEncrypted);
411 if (!mainWindow) {
412 KMessageBox::information(
413 this,
414 i18n("<qt>Your document could not be saved automatically."
415 "<p>To complete the encryption, please save the document.</qt>"),
416 i18n("Save Document"),
417 QStringLiteral("EncryptSaveMessage"));
418 return;
419 }
420 if (modified && KMessageBox::questionTwoActions(
421 this,
422 i18n("<qt>The document has been changed since it was opened. To complete the encryption the document needs to be saved."
423 "<p>Do you want to save the document now?</qt>"),
424 i18n("Save Document"),
425 KStandardGuiItem::save(),
426 KStandardGuiItem::dontSave(),
427 QStringLiteral("EncryptSaveConfirmation")
428 ) != KMessageBox::PrimaryAction) {
429 return;
430 }
431 } else {
432 // Encrypt non-oasis document
433 bool modified = doc->isModified();
434 saveas = doc->outputMimeType().isEmpty();
435 if (!doc->url().isEmpty() && doc->specialOutputFlag() == 0) {
436 QMimeDatabase db;
437 QMimeType mime = db.mimeTypeForName(QString::fromLatin1(doc->mimeType()));
438 if (!doc->mimeType().isEmpty() && doc->mimeType() != doc->nativeFormatMimeType()) {
439 QString comment = mime.isValid() ? mime.comment() : i18n("%1 (unknown file type)", QString::fromLatin1(doc->mimeType()));
440 QString native = db.mimeTypeForName(QString::fromLatin1(doc->nativeFormatMimeType())).comment();
441 if (KMessageBox::warningContinueCancel(
442 this,
443 i18n("<qt>The document is currently saved as %1. The document needs to be changed to <b>%2</b> to be encrypted."
444 "<p>Do you want to change the file to %2?</qt>", QStringLiteral("<b>%1</b>").arg(comment), native),
445 i18n("Change Filetype"),
446 KGuiItem(i18n("Change")),
447 KStandardGuiItem::cancel(),
448 QStringLiteral("EncryptChangeFiletypeConfirmation")
449 ) != KMessageBox::Continue) {
450 return;
451 }
452 // Replace the current extension with our native one.
453 // If the current extension is an unknown mimetype,
454 // we just add our native extension. Maybe not ideal
455 // but should not happen in normal cases,
456 // and the user can always correct it in the save dialog.
457 auto patterns = mime.globPatterns();
458 auto url = doc->url().url();
459 for (auto p : patterns) {
460 p.remove(QLatin1Char('*'));
461 if (url.endsWith(p)) {
462 url = url.left(url.lastIndexOf(p));
463 }
464 }
465 if (!url.isEmpty()) {
466 const auto nativePattern = db.mimeTypeForName(QString::fromLatin1(doc->nativeFormatMimeType())).globPatterns().value(0).remove(QLatin1Char('*'));
467 url.append(nativePattern);
468 }
469 doc->setUrl(QUrl(url));
470 saveas = true;
471 debugWidgets<<"document mimetype changed:"<<doc->url()<<patterns;
472 }
473 }
474 doc->setMimeType(doc->nativeFormatMimeType());
475 doc->setOutputMimeType(doc->nativeFormatMimeType(), KoDocumentBase::SaveEncrypted);
476 if (!mainWindow) {
477 KMessageBox::information(
478 this,
479 i18n("<qt>Your document could not be saved automatically."
480 "<p>To complete the encryption, please save the document.</qt>"),
481 i18n("Save Document"),
482 QStringLiteral("EncryptSaveMessage"));
483 return;
484 }
485 if (modified && KMessageBox::questionTwoActions(
486 this,
487 i18n("<qt>The document has been changed since it was opened. To complete the encryption the document needs to be saved."
488 "<p>Do you want to save the document now?</qt>"),
489 i18n("Save Document"),
490 KStandardGuiItem::save(),
491 KStandardGuiItem::dontSave(),
492 QStringLiteral("EncryptSaveConfirmation")
493 ) != KMessageBox::PrimaryAction) {
494 return;
495 }
496 }
497 // Let KoMainWindow handle save
498 Q_EMIT saveRequested(saveas, false, doc->specialOutputFlag());
499 d->toggleEncryption = false;
500 d->applyToggleEncryption = false;
501 // Detects when the user cancelled saving of an unsaved document
502 // FIXME: Should this be tracked in the document?
503 d->documentSaved = !doc->url().scheme().isEmpty();
504 }
505
506 QList<KPageWidgetItem*> KoDocumentInfoDlg::pages() const
507 {
508 return d->pages;
509 }
510
511 void KoDocumentInfoDlg::setReadOnly(bool ro)
512 {
513 d->aboutUi->meComments->setReadOnly(ro);
514
515 for (KPageWidgetItem* page : std::as_const(d->pages)) {
516 const QList<QLineEdit*> lineEdits = page->widget()->findChildren<QLineEdit *>();
517 for (QLineEdit* le : lineEdits) {
518 le->setReadOnly(ro);
519 }
520 const QList<QPushButton*> buttons = page->widget()->findChildren<QPushButton *>();
521 for (QPushButton* le : buttons ) {
522 le->setDisabled(ro);
523 }
524 }
525 }
526
527 void KoDocumentInfoDlg::addPageItem(KoPageWidgetItem *item)
528 {
529 KPageWidgetItem * page = new KoPageWidgetItemAdapter(item);
530
531 addPage(page);
532 d->pages.append(page);
533 }
534 #include "KoDocumentInfoDlg.moc"