File indexing completed on 2024-12-22 04:55:33
0001 /* 0002 This file is part of KAddressBook. 0003 SPDX-FileCopyrightText: 2002 Jost Schenck <jost@schenck.de> 0004 SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org> 0005 0006 SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0 0007 */ 0008 0009 #include "ringbinderstyle.h" 0010 #include "printingwizard.h" 0011 #include "printprogress.h" 0012 #include "ui_rbs_appearance.h" 0013 0014 #include <KConfig> 0015 #include <KConfigGroup> 0016 #include <KLocalizedString> 0017 0018 #include <KSharedConfig> 0019 #include <QLocale> 0020 #include <QPrinter> 0021 #include <QTextDocument> 0022 0023 using namespace KABPrinting; 0024 0025 static const char RingBinderConfigSectionName[] = "RingBinderPrintStyle"; 0026 static const char ShowPhoneNumbers[] = "ShowPhoneNumbers"; 0027 static const char ShowEmailAddresses[] = "ShowEmailAddresses"; 0028 static const char ShowStreetAddresses[] = "ShowStreetAddresses"; 0029 static const char ShowOrganization[] = "ShowOrganization"; 0030 static const char ShowBirthday[] = "ShowBirthday"; 0031 static const char ShowNote[] = "ShowNote"; 0032 0033 enum PrintField { PhoneNumbers = 1, Emails = 2, Addresses = 4, Organization = 8, Birthday = 16, Note = 32 }; 0034 0035 static QString contactsToHtml(const KContacts::Addressee::List &contacts, int fields) 0036 { 0037 QString content = QStringLiteral("<html>\n"); 0038 content += QLatin1StringView(" <body>\n"); 0039 content += QLatin1StringView( 0040 " <table style=\"border-width: 1px; border-style: solid; " 0041 "border-color: gray;\" width=\"100%\" cellspacing=\"0\">\n"); 0042 for (const KContacts::Addressee &contact : contacts) { 0043 QString nameString = contact.familyName() + QLatin1StringView(", ") + contact.givenName(); 0044 0045 if (fields & Organization) { 0046 if (!contact.organization().isEmpty()) { 0047 nameString += QLatin1StringView(" (") + contact.organization() + QLatin1Char(')'); 0048 } 0049 } 0050 0051 if (fields & Birthday) { 0052 if (contact.birthday().isValid()) { 0053 nameString += QLatin1StringView(" *") + QLocale().toString(contact.birthday().date(), QLocale::ShortFormat); 0054 } 0055 } 0056 0057 QStringList leftBlock; 0058 QStringList rightBlock; 0059 if (fields & PhoneNumbers) { 0060 const KContacts::PhoneNumber::List numbers = contact.phoneNumbers(); 0061 for (const KContacts::PhoneNumber &number : numbers) { 0062 rightBlock.append(number.typeLabel() + QLatin1StringView(": ") + number.number()); 0063 } 0064 } 0065 if (fields & Emails) { 0066 const QStringList emails = contact.emails(); 0067 for (const QString &email : emails) { 0068 rightBlock.append(email); 0069 } 0070 } 0071 if (fields & Note) { 0072 if (!contact.note().isEmpty()) { 0073 const QString note = i18n("Note: ") + contact.note().replace(QLatin1Char('\n'), QStringLiteral("<br/>")); 0074 0075 rightBlock.append(note); 0076 } 0077 } 0078 if (fields & Addresses) { 0079 const KContacts::Address::List addresses = contact.addresses(); 0080 for (const KContacts::Address &address : addresses) { 0081 const QString data = address.formatted(KContacts::AddressFormatStyle::Postal) 0082 .replace(QLatin1StringView("\n\n"), QStringLiteral("\n")) 0083 .replace(QLatin1Char('\n'), QStringLiteral("<br/>")); 0084 const QString subBlock = QLatin1StringView("<p style=\"margin-top: 0px; margin-left: 20px\">") + data + QLatin1StringView("</p>"); 0085 0086 leftBlock.append(subBlock); 0087 } 0088 } 0089 0090 content += QLatin1StringView(" <tr>\n"); 0091 content += QLatin1StringView( 0092 " <td style=\"padding-left: 3px; padding-top: 3px; padding-right: 3px; " 0093 "padding-bottom: 3px;\">") 0094 + nameString + leftBlock.join(QString()) + QLatin1StringView("</td>\n"); 0095 content += QLatin1StringView( 0096 " <td style=\"padding-left: 3px; padding-top: 3px; padding-right: 3px; " 0097 "padding-bottom: 3px;\">") 0098 + rightBlock.join(QLatin1StringView("<br/>")) + QLatin1StringView("</td>\n"); 0099 content += QLatin1StringView(" </tr>\n"); 0100 } 0101 content += QLatin1StringView(" </table>\n"); 0102 content += QLatin1StringView(" </body>\n"); 0103 content += QLatin1StringView("</html>\n"); 0104 0105 return content; 0106 } 0107 0108 namespace KABPrinting 0109 { 0110 class RingBinderStyleAppearanceForm : public QWidget, public Ui::RingBinderStyleAppearanceForm_Base 0111 { 0112 public: 0113 explicit RingBinderStyleAppearanceForm(QWidget *parent) 0114 : QWidget(parent) 0115 { 0116 setObjectName(QLatin1StringView("AppearancePage")); 0117 setupUi(this); 0118 } 0119 }; 0120 } 0121 0122 RingBinderPrintStyle::RingBinderPrintStyle(PrintingWizard *parent) 0123 : PrintStyle(parent) 0124 , mPageAppearance(new RingBinderStyleAppearanceForm(parent)) 0125 { 0126 setPreview(QStringLiteral("ringbinder-style.png")); 0127 setPreferredSortOptions(KAddressBookImportExport::ContactFields::FamilyName, Qt::AscendingOrder); 0128 0129 addPage(mPageAppearance, i18n("Ring Binder Printing Style - Appearance")); 0130 0131 // applying previous settings 0132 KConfigGroup config(KSharedConfig::openConfig(), QLatin1StringView(RingBinderConfigSectionName)); 0133 mPageAppearance->cbPhoneNumbers->setChecked(config.readEntry(ShowPhoneNumbers, true)); 0134 mPageAppearance->cbEmails->setChecked(config.readEntry(ShowEmailAddresses, true)); 0135 mPageAppearance->cbStreetAddresses->setChecked(config.readEntry(ShowStreetAddresses, true)); 0136 mPageAppearance->cbOrganization->setChecked(config.readEntry(ShowOrganization, true)); 0137 mPageAppearance->cbBirthday->setChecked(config.readEntry(ShowBirthday, false)); 0138 mPageAppearance->cbNote->setChecked(config.readEntry(ShowNote, false)); 0139 } 0140 0141 RingBinderPrintStyle::~RingBinderPrintStyle() = default; 0142 0143 void RingBinderPrintStyle::print(const KContacts::Addressee::List &contacts, PrintProgress *progress) 0144 { 0145 progress->addMessage(i18n("Setting up fields")); 0146 progress->setProgress(0); 0147 0148 // first write current config settings 0149 KConfigGroup config(KSharedConfig::openConfig(), QLatin1StringView(RingBinderConfigSectionName)); 0150 config.writeEntry(ShowPhoneNumbers, mPageAppearance->cbPhoneNumbers->isChecked()); 0151 config.writeEntry(ShowEmailAddresses, mPageAppearance->cbEmails->isChecked()); 0152 config.writeEntry(ShowStreetAddresses, mPageAppearance->cbStreetAddresses->isChecked()); 0153 config.writeEntry(ShowOrganization, mPageAppearance->cbOrganization->isChecked()); 0154 config.writeEntry(ShowBirthday, mPageAppearance->cbBirthday->isChecked()); 0155 config.writeEntry(ShowNote, mPageAppearance->cbNote->isChecked()); 0156 config.sync(); 0157 0158 QPrinter *printer = wizard()->printer(); 0159 printer->setPageMargins(QMarginsF(50, 20, 0, 50), QPageLayout::Point); 0160 0161 progress->addMessage(i18n("Setting up document")); 0162 0163 int fields = 0; 0164 0165 if (mPageAppearance->cbPhoneNumbers->isChecked()) { 0166 fields |= PhoneNumbers; 0167 } 0168 0169 if (mPageAppearance->cbEmails->isChecked()) { 0170 fields |= Emails; 0171 } 0172 0173 if (mPageAppearance->cbStreetAddresses->isChecked()) { 0174 fields |= Addresses; 0175 } 0176 0177 if (mPageAppearance->cbOrganization->isChecked()) { 0178 fields |= Organization; 0179 } 0180 0181 if (mPageAppearance->cbBirthday->isChecked()) { 0182 fields |= Birthday; 0183 } 0184 0185 if (mPageAppearance->cbNote->isChecked()) { 0186 fields |= Note; 0187 } 0188 0189 const QString html = contactsToHtml(contacts, fields); 0190 0191 QTextDocument document; 0192 document.setHtml(html); 0193 0194 progress->addMessage(i18n("Printing")); 0195 0196 document.print(printer); 0197 0198 progress->addMessage(i18nc("Finished printing", "Done")); 0199 } 0200 0201 RingBinderPrintStyleFactory::RingBinderPrintStyleFactory(PrintingWizard *parent) 0202 : PrintStyleFactory(parent) 0203 { 0204 } 0205 0206 PrintStyle *RingBinderPrintStyleFactory::create() const 0207 { 0208 return new RingBinderPrintStyle(mParent); 0209 } 0210 0211 QString RingBinderPrintStyleFactory::description() const 0212 { 0213 return i18n("Printout for Ring Binders"); 0214 }