File indexing completed on 2024-12-22 04:40:57
0001 /* ============================================================ 0002 * Falkon - Qt web browser 0003 * Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com> 0004 * 0005 * This program is free software: you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation, either version 3 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This program is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License 0016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0017 * ============================================================ */ 0018 #include "bookmarksimportdialog.h" 0019 #include "ui_bookmarksimportdialog.h" 0020 #include "firefoximporter.h" 0021 #include "chromeimporter.h" 0022 #include "operaimporter.h" 0023 #include "htmlimporter.h" 0024 #include "ieimporter.h" 0025 #include "bookmarks.h" 0026 #include "bookmarkitem.h" 0027 #include "bookmarksmodel.h" 0028 #include "bookmarksitemdelegate.h" 0029 #include "mainapplication.h" 0030 0031 #include <QMessageBox> 0032 0033 BookmarksImportDialog::BookmarksImportDialog(QWidget* parent) 0034 : QDialog(parent) 0035 , ui(new Ui::BookmarksImportDialog) 0036 , m_currentPage(0) 0037 , m_importer(nullptr) 0038 , m_importedFolder(nullptr) 0039 , m_model(nullptr) 0040 { 0041 setAttribute(Qt::WA_DeleteOnClose); 0042 ui->setupUi(this); 0043 0044 ui->browserList->setCurrentRow(0); 0045 ui->treeView->setItemDelegate(new BookmarksItemDelegate(ui->treeView)); 0046 0047 connect(ui->nextButton, &QAbstractButton::clicked, this, &BookmarksImportDialog::nextPage); 0048 connect(ui->backButton, &QAbstractButton::clicked, this, &BookmarksImportDialog::previousPage); 0049 connect(ui->chooseFile, &QAbstractButton::clicked, this, &BookmarksImportDialog::setFile); 0050 connect(ui->cancelButton, &QDialogButtonBox::rejected, this, &QWidget::close); 0051 0052 #ifndef Q_OS_WIN 0053 ui->browserList->item(IE)->setHidden(true); 0054 #endif 0055 } 0056 0057 BookmarksImportDialog::~BookmarksImportDialog() 0058 { 0059 ui->treeView->setModel(nullptr); 0060 delete m_model; 0061 delete m_importedFolder; 0062 delete m_importer; 0063 delete ui; 0064 } 0065 0066 void BookmarksImportDialog::nextPage() 0067 { 0068 switch (m_currentPage) { 0069 case 0: 0070 if (!ui->browserList->currentItem()) { 0071 return; 0072 } 0073 0074 switch (ui->browserList->currentRow()) { 0075 case Firefox: 0076 m_importer = new FirefoxImporter; 0077 break; 0078 case Chrome: 0079 m_importer = new ChromeImporter; 0080 break; 0081 case Opera: 0082 m_importer = new OperaImporter; 0083 break; 0084 case IE: 0085 m_importer = new IeImporter; 0086 break; 0087 case Html: 0088 m_importer = new HtmlImporter; 0089 break; 0090 default: 0091 Q_ASSERT(!"Unreachable"); 0092 break; 0093 } 0094 0095 ui->fileLine->clear(); 0096 showImporterPage(); 0097 0098 ui->nextButton->setEnabled(false); 0099 ui->backButton->setEnabled(true); 0100 ui->stackedWidget->setCurrentIndex(++m_currentPage); 0101 break; 0102 0103 case 1: 0104 if (ui->fileLine->text().isEmpty()) { 0105 return; 0106 } 0107 0108 if (m_importer->prepareImport()) { 0109 m_importedFolder = m_importer->importBookmarks(); 0110 } 0111 0112 if (m_importer->error()) { 0113 QMessageBox::critical(this, tr("Error!"), m_importer->errorString()); 0114 return; 0115 } 0116 0117 if (!m_importedFolder || m_importedFolder->children().isEmpty()) { 0118 QMessageBox::warning(this, tr("Error!"), tr("No bookmarks were found.")); 0119 return; 0120 } 0121 0122 Q_ASSERT(m_importedFolder->isFolder()); 0123 0124 ui->stackedWidget->setCurrentIndex(++m_currentPage); 0125 ui->nextButton->setText(tr("Finish")); 0126 showExportedBookmarks(); 0127 break; 0128 0129 case 2: 0130 addExportedBookmarks(); 0131 close(); 0132 break; 0133 0134 default: 0135 Q_ASSERT(!"Unreachable"); 0136 } 0137 } 0138 0139 void BookmarksImportDialog::previousPage() 0140 { 0141 switch (m_currentPage) { 0142 case 0: 0143 break; 0144 0145 case 1: 0146 ui->nextButton->setEnabled(true); 0147 ui->backButton->setEnabled(false); 0148 ui->stackedWidget->setCurrentIndex(--m_currentPage); 0149 0150 delete m_importer; 0151 m_importer = nullptr; 0152 break; 0153 0154 case 2: 0155 showImporterPage(); 0156 0157 ui->nextButton->setText(tr("Next >")); 0158 ui->nextButton->setEnabled(true); 0159 ui->backButton->setEnabled(true); 0160 ui->stackedWidget->setCurrentIndex(--m_currentPage); 0161 0162 ui->treeView->setModel(nullptr); 0163 delete m_model; 0164 m_model = nullptr; 0165 0166 delete m_importedFolder; 0167 m_importedFolder = nullptr; 0168 break; 0169 0170 default: 0171 Q_ASSERT(!"Unreachable"); 0172 } 0173 } 0174 0175 void BookmarksImportDialog::setFile() 0176 { 0177 Q_ASSERT(m_importer); 0178 0179 ui->fileLine->setText(m_importer->getPath(this)); 0180 ui->nextButton->setEnabled(!ui->fileLine->text().isEmpty()); 0181 } 0182 0183 void BookmarksImportDialog::showImporterPage() 0184 { 0185 ui->iconLabel->setPixmap(ui->browserList->currentItem()->icon().pixmap(48)); 0186 ui->importingFromLabel->setText(tr("<b>Importing from %1</b>").arg(ui->browserList->currentItem()->text())); 0187 ui->fileText1->setText(m_importer->description()); 0188 ui->standardDirLabel->setText(QSL("<i>%1</i>").arg(m_importer->standardPath())); 0189 } 0190 0191 void BookmarksImportDialog::showExportedBookmarks() 0192 { 0193 m_model = new BookmarksModel(m_importedFolder, nullptr, this); 0194 ui->treeView->setModel(m_model); 0195 ui->treeView->header()->resizeSection(0, ui->treeView->header()->width() / 2); 0196 ui->treeView->expandAll(); 0197 } 0198 0199 void BookmarksImportDialog::addExportedBookmarks() 0200 { 0201 mApp->bookmarks()->addBookmark(mApp->bookmarks()->unsortedFolder(), m_importedFolder); 0202 m_importedFolder = nullptr; 0203 }