File indexing completed on 2025-01-19 04:46:31
0001 /* 0002 SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "ldifimportexportplugininterface.h" 0008 #include <KActionCollection> 0009 #include <KContacts/LDIFConverter> 0010 #include <KJobWidgets> 0011 #include <KLocalizedString> 0012 #include <KMessageBox> 0013 #include <PimCommon/RenameFileDialog> 0014 #include <QAction> 0015 #include <QFile> 0016 #include <QFileDialog> 0017 #include <QPointer> 0018 #include <QTemporaryFile> 0019 #include <QTextStream> 0020 #include <kio/filecopyjob.h> 0021 0022 #include <KAddressBookImportExport/ContactList> 0023 #include <KAddressBookImportExport/ContactSelectionDialog> 0024 #include <KAddressBookImportExport/ImportExportEngine> 0025 0026 LDifImportExportPluginInterface::LDifImportExportPluginInterface(QObject *parent) 0027 : KAddressBookImportExport::PluginInterface(parent) 0028 { 0029 } 0030 0031 LDifImportExportPluginInterface::~LDifImportExportPluginInterface() = default; 0032 0033 void LDifImportExportPluginInterface::createAction(KActionCollection *ac) 0034 { 0035 QAction *action = ac->addAction(QStringLiteral("file_import_ldif")); 0036 action->setText(i18n("Import LDIF file...")); 0037 action->setWhatsThis(i18n("Import contacts from an LDIF file.")); 0038 connect(action, &QAction::triggered, this, &LDifImportExportPluginInterface::slotImportLdif); 0039 setImportActions(QList<QAction *>() << action); 0040 0041 action = ac->addAction(QStringLiteral("file_export_ldif")); 0042 action->setText(i18n("Export LDIF file...")); 0043 action->setWhatsThis(i18n("Export contacts to an LDIF file.")); 0044 connect(action, &QAction::triggered, this, &LDifImportExportPluginInterface::slotExportLdif); 0045 setExportActions(QList<QAction *>() << action); 0046 } 0047 0048 void LDifImportExportPluginInterface::exec() 0049 { 0050 switch (mImportExportAction) { 0051 case Import: 0052 importLdif(); 0053 break; 0054 case Export: 0055 exportLdif(); 0056 break; 0057 } 0058 } 0059 0060 void LDifImportExportPluginInterface::slotImportLdif() 0061 { 0062 mImportExportAction = Import; 0063 Q_EMIT emitPluginActivated(this); 0064 } 0065 0066 void LDifImportExportPluginInterface::slotExportLdif() 0067 { 0068 mImportExportAction = Export; 0069 Q_EMIT emitPluginActivated(this); 0070 } 0071 0072 void LDifImportExportPluginInterface::importLdifFile(const QString &fileName) 0073 { 0074 if (fileName.isEmpty()) { 0075 return; 0076 } 0077 0078 QFile file(fileName); 0079 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { 0080 const QString msg = i18n("<qt>Unable to open <b>%1</b> for reading.</qt>", fileName); 0081 KMessageBox::error(parentWidget(), msg); 0082 return; 0083 } 0084 0085 QTextStream stream(&file); 0086 stream.setEncoding(QStringConverter::Latin1); 0087 0088 const QString wholeFile = stream.readAll(); 0089 const QDateTime dtDefault = QFileInfo(file).lastModified(); 0090 file.close(); 0091 KContacts::ContactGroup::List lstGroup; 0092 KContacts::Addressee::List lstAddresses; 0093 KContacts::LDIFConverter::LDIFToAddressee(wholeFile, lstAddresses, lstGroup, dtDefault); 0094 KAddressBookImportExport::ContactList contactList; 0095 contactList.setAddressList(lstAddresses); 0096 contactList.setContactGroupList(lstGroup); 0097 0098 auto engine = new KAddressBookImportExport::ImportExportEngine(this); 0099 engine->setContactList(contactList); 0100 engine->setDefaultAddressBook(defaultCollection()); 0101 engine->importContacts(); 0102 } 0103 0104 void LDifImportExportPluginInterface::importLdif() 0105 { 0106 const QString fileName = QFileDialog::getOpenFileName(parentWidget(), QString(), QDir::homePath(), QStringLiteral("%1 (*.ldif)").arg(i18n("LDif Files"))); 0107 importLdifFile(fileName); 0108 } 0109 0110 void doExport(QFile *file, const KAddressBookImportExport::ContactList &list) 0111 { 0112 QString data; 0113 KContacts::LDIFConverter::addresseeAndContactGroupToLDIF(list.addressList(), list.contactGroupList(), data); 0114 0115 QTextStream stream(file); 0116 stream << data; 0117 } 0118 0119 void LDifImportExportPluginInterface::exportLdif() 0120 { 0121 QPointer<KAddressBookImportExport::ContactSelectionDialog> dlg = 0122 new KAddressBookImportExport::ContactSelectionDialog(itemSelectionModel(), false, parentWidget()); 0123 dlg->setMessageText(i18n("Which contact do you want to export?")); 0124 dlg->setDefaultAddressBook(defaultCollection()); 0125 if (!dlg->exec()) { 0126 delete dlg; 0127 return; 0128 } 0129 const KContacts::AddresseeList contacts = dlg->selectedContacts().addressList(); 0130 delete dlg; 0131 0132 if (contacts.isEmpty()) { 0133 KMessageBox::error(nullptr, i18n("You have not selected any contacts to export.")); 0134 return; 0135 } 0136 0137 KAddressBookImportExport::ContactList contactLists; 0138 contactLists.setAddressList(contacts); 0139 0140 QFileDialog::Options options = QFileDialog::DontConfirmOverwrite; 0141 const QUrl url = QFileDialog::getSaveFileUrl(parentWidget(), 0142 QString(), 0143 QUrl::fromLocalFile(QDir::homePath() + QStringLiteral("/addressbook.ldif")), 0144 i18n("LDif Files (*.ldif)"), 0145 nullptr, 0146 options); 0147 if (url.isEmpty()) { 0148 return; 0149 } 0150 0151 if (!url.isLocalFile()) { 0152 QTemporaryFile tmpFile; 0153 if (!tmpFile.open()) { 0154 const QString msg = i18n("<qt>Unable to open file <b>%1</b></qt>", url.url()); 0155 KMessageBox::error(parentWidget(), msg); 0156 return; 0157 } 0158 0159 doExport(&tmpFile, contactLists); 0160 tmpFile.flush(); 0161 auto job = KIO::file_copy(QUrl::fromLocalFile(tmpFile.fileName()), url, -1, KIO::Overwrite); 0162 KJobWidgets::setWindow(job, parentWidget()); 0163 job->exec(); 0164 } else { 0165 QString fileName = url.toLocalFile(); 0166 0167 if (QFileInfo::exists(fileName)) { 0168 if (url.isLocalFile() && QFileInfo::exists(url.toLocalFile())) { 0169 auto dialog = new PimCommon::RenameFileDialog(url, false, parentWidget()); 0170 auto result = static_cast<PimCommon::RenameFileDialog::RenameFileDialogResult>(dialog->exec()); 0171 if (result == PimCommon::RenameFileDialog::RENAMEFILE_RENAME) { 0172 fileName = dialog->newName().toLocalFile(); 0173 } else if (result == PimCommon::RenameFileDialog::RENAMEFILE_IGNORE) { 0174 delete dialog; 0175 return; 0176 } 0177 delete dialog; 0178 } 0179 } 0180 0181 QFile file(fileName); 0182 0183 if (!file.open(QIODevice::WriteOnly)) { 0184 const QString txt = i18n("<qt>Unable to open file <b>%1</b>.</qt>", fileName); 0185 KMessageBox::error(parentWidget(), txt); 0186 return; 0187 } 0188 QString data; 0189 KContacts::LDIFConverter::addresseeAndContactGroupToLDIF(contactLists.addressList(), contactLists.contactGroupList(), data); 0190 0191 QTextStream stream(&file); 0192 stream << data; 0193 file.close(); 0194 } 0195 } 0196 0197 bool LDifImportExportPluginInterface::canImportFileType(const QUrl &url) 0198 { 0199 return url.path().endsWith(QLatin1StringView(".ldif")); 0200 } 0201 0202 void LDifImportExportPluginInterface::importFile(const QUrl &url) 0203 { 0204 if (url.isLocalFile()) { 0205 importLdifFile(url.path()); 0206 } 0207 } 0208 0209 #include "moc_ldifimportexportplugininterface.cpp"