Commit 94b411c1 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Start to implement export

parent 2b78bee3
......@@ -12,5 +12,6 @@ target_link_libraries(kaddressbook_importexportcsvplugin KF5::KaddressbookImport
KF5::I18n
KF5::XmlGui
KF5::Contacts
KF5::JobWidgets
kaddressbookimportexportlibprivate
)
......@@ -21,6 +21,15 @@
#include <KLocalizedString>
#include <KActionCollection>
#include <QAction>
#include <QFileInfo>
#include <QTemporaryFile>
#include <PimCommon/RenameFileDialog>
#include <KMessageBox>
#include <KJobWidgets>
#include <QTextStream>
#include <QFileDialog>
#include <QTextCodec>
#include <KIO/Job>
CSVImportExportPluginInterface::CSVImportExportPluginInterface(QObject *parent)
: KAddressBookImportExport::KAddressBookImportExportPluginInterface(parent)
......@@ -56,6 +65,7 @@ void CSVImportExportPluginInterface::exec()
case Import:
break;
case Export:
exportCSV();
break;
}
//TODO
......@@ -72,3 +82,114 @@ void CSVImportExportPluginInterface::slotExportCVS()
mImportExportAction = Export;
Q_EMIT emitPluginActivated(this);
}
void CSVImportExportPluginInterface::exportToFile(QFile *file, const KContacts::Addressee::List &contacts) const
{
#if 0 //FIXME
QTextStream stream(file);
stream.setCodec(QTextCodec::codecForLocale());
ContactFields::Fields fields = ContactFields::allFields();
fields.remove(ContactFields::Undefined);
bool first = true;
// First output the column headings
for (int i = 0; i < fields.count(); ++i) {
if (!first) {
stream << ",";
}
// add quoting as defined in RFC 4180
QString label = ContactFields::label(fields.at(i));
label.replace(QLatin1Char('"'), QStringLiteral("\"\""));
stream << "\"" << label << "\"";
first = false;
}
stream << "\n";
// Then all the contacts
for (int i = 0; i < contacts.count(); ++i) {
const KContacts::Addressee contact = contacts.at(i);
first = true;
for (int j = 0; j < fields.count(); ++j) {
if (!first) {
stream << ",";
}
QString content;
if (fields.at(j) == ContactFields::Birthday ||
fields.at(j) == ContactFields::Anniversary) {
const QDateTime dateTime =
QDateTime::fromString(ContactFields::value(fields.at(j), contact), Qt::ISODate);
if (dateTime.isValid()) {
content = dateTime.date().toString(Qt::ISODate);
}
} else {
content = ContactFields::value(fields.at(j), contact).replace(QLatin1Char('\n'), QStringLiteral("\\n"));
}
// add quoting as defined in RFC 4180
content.replace(QLatin1Char('"'), QStringLiteral("\"\""));
stream << '\"' << content << '\"';
first = false;
}
stream << "\n";
}
#endif
}
void CSVImportExportPluginInterface::exportCSV()
{
QUrl url = QFileDialog::getSaveFileUrl(parentWidget(), QString(), QUrl::fromLocalFile(QStringLiteral("addressbook.csv")));
if (url.isEmpty()) {
return;
}
if (QFileInfo(url.isLocalFile() ? url.toLocalFile() : url.path()).exists()) {
if (url.isLocalFile() && QFileInfo(url.toLocalFile()).exists()) {
PimCommon::RenameFileDialog::RenameFileDialogResult result = PimCommon::RenameFileDialog::RENAMEFILE_IGNORE;
PimCommon::RenameFileDialog *dialog = new PimCommon::RenameFileDialog(url, false, parentWidget());
result = static_cast<PimCommon::RenameFileDialog::RenameFileDialogResult>(dialog->exec());
if (result == PimCommon::RenameFileDialog::RENAMEFILE_RENAME) {
url = dialog->newName();
} else if (result == PimCommon::RenameFileDialog::RENAMEFILE_IGNORE) {
delete dialog;
return;
}
delete dialog;
}
}
if (!url.isLocalFile()) {
QTemporaryFile tmpFile;
if (!tmpFile.open()) {
const QString msg = i18n("<qt>Unable to open file <b>%1</b></qt>", url.url());
KMessageBox::error(parentWidget(), msg);
return;
}
#if 0 //FIXME
exportToFile(&tmpFile, contacts.addressList());
tmpFile.flush();
auto job = KIO::file_copy(QUrl::fromLocalFile(tmpFile.fileName()), url, -1, KIO::Overwrite);
KJobWidgets::setWindow(job, parentWidget());
job->exec();
#endif
} else {
QFile file(url.toLocalFile());
if (!file.open(QIODevice::WriteOnly)) {
const QString msg = i18n("<qt>Unable to open file <b>%1</b>.</qt>", url.toLocalFile());
KMessageBox::error(parentWidget(), msg);
return;
}
//FIXME exportToFile(&file, contacts.addressList());
file.close();
}
}
......@@ -21,6 +21,8 @@
#define CSVIMPORTEXPORTPLUGININTERFACE_H
#include <KAddressBookImportExport/KAddressBookImportExportPluginInterface>
#include <KContacts/Addressee>
class QFile;
class CSVImportExportPluginInterface : public KAddressBookImportExport::KAddressBookImportExportPluginInterface
{
......@@ -32,8 +34,10 @@ public:
void createAction(KActionCollection *ac) Q_DECL_OVERRIDE;
void exec() Q_DECL_OVERRIDE;
private:
void exportCSV();
void slotImportCVS();
void slotExportCVS();
void exportToFile(QFile *file, const KContacts::Addressee::List &contacts) const;
};
#endif // CSVIMPORTEXPORTPLUGININTERFACE_H
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment