File indexing completed on 2024-05-05 04:22:01
0001 /* SPDX-FileCopyrightText: 2003-2010 Jesper K. Pedersen <blackie@kde.org> 0002 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 #include "Import.h" 0006 0007 #include "ImportDialog.h" 0008 #include "ImportHandler.h" 0009 #include "KimFileReader.h" 0010 0011 #include <MainWindow/Window.h> 0012 0013 #include <KIO/Job> 0014 #include <KIO/JobUiDelegate> 0015 #include <KLocalizedString> 0016 #include <KMessageBox> 0017 #include <QFileDialog> 0018 #include <QTemporaryFile> 0019 0020 using namespace ImportExport; 0021 0022 void Import::imageImport() 0023 { 0024 QUrl url = QFileDialog::getOpenFileUrl( 0025 nullptr, /*parent*/ 0026 i18n("KPhotoAlbum Export Files"), /*caption*/ 0027 QUrl(), /* directory */ 0028 i18n("KPhotoAlbum import files") + QString::fromLatin1("(*.kim)") /*filter*/ 0029 ); 0030 if (url.isEmpty()) 0031 return; 0032 0033 imageImport(url); 0034 // This instance will delete itself when done. 0035 } 0036 0037 void Import::imageImport(const QUrl &url) 0038 { 0039 Import *import = new Import; 0040 import->m_kimFileUrl = url; 0041 if (!url.isLocalFile()) 0042 import->downloadUrl(url); 0043 else 0044 import->exec(url.path()); 0045 // This instance will delete itself when done. 0046 } 0047 0048 ImportExport::Import::Import() 0049 : m_tmp(nullptr) 0050 { 0051 } 0052 0053 void ImportExport::Import::downloadUrl(const QUrl &url) 0054 { 0055 m_tmp = new QTemporaryFile; 0056 m_tmp->setFileTemplate(QString::fromLatin1("XXXXXX.kim")); 0057 if (!m_tmp->open()) { 0058 KMessageBox::error(MainWindow::Window::theMainWindow(), i18n("Unable to create temporary file")); 0059 delete this; 0060 return; 0061 } 0062 KIO::TransferJob *job = KIO::get(url); 0063 connect(job, &KIO::TransferJob::result, this, &Import::downloadKimJobCompleted); 0064 connect(job, &KIO::TransferJob::data, this, &Import::data); 0065 } 0066 0067 void ImportExport::Import::downloadKimJobCompleted(KJob *job) 0068 { 0069 if (job->error()) { 0070 job->uiDelegate()->showErrorMessage(); 0071 delete this; 0072 } else { 0073 QString path = m_tmp->fileName(); 0074 m_tmp->close(); 0075 exec(path); 0076 } 0077 } 0078 0079 void ImportExport::Import::exec(const QString &fileName) 0080 { 0081 ImportDialog dialog(MainWindow::Window::theMainWindow()); 0082 KimFileReader kimFileReader; 0083 if (!kimFileReader.open(fileName)) { 0084 delete this; 0085 return; 0086 } 0087 0088 bool ok = dialog.exec(&kimFileReader, m_kimFileUrl); 0089 0090 if (ok) { 0091 ImportHandler handler; 0092 handler.exec(dialog.settings(), &kimFileReader); 0093 } 0094 0095 delete this; 0096 } 0097 0098 void ImportExport::Import::data(KIO::Job *, const QByteArray &data) 0099 { 0100 m_tmp->write(data); 0101 } 0102 0103 ImportExport::Import::~Import() 0104 { 0105 delete m_tmp; 0106 } 0107 0108 // vi:expandtab:tabstop=4 shiftwidth=4: 0109 0110 #include "moc_Import.cpp"