Falkon Develop
Cross-platform Qt-based web browser
bookmarksimportdialog.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
19#include "ui_bookmarksimportdialog.h"
20#include "firefoximporter.h"
21#include "chromeimporter.h"
22#include "operaimporter.h"
23#include "htmlimporter.h"
24#include "ieimporter.h"
25#include "bookmarks.h"
26#include "bookmarkitem.h"
27#include "bookmarksmodel.h"
29#include "mainapplication.h"
30
31#include <QMessageBox>
32
34 : QDialog(parent)
35 , ui(new Ui::BookmarksImportDialog)
36 , m_currentPage(0)
37 , m_importer(nullptr)
38 , m_importedFolder(nullptr)
39 , m_model(nullptr)
40{
41 setAttribute(Qt::WA_DeleteOnClose);
42 ui->setupUi(this);
43
44 ui->browserList->setCurrentRow(0);
45 ui->treeView->setItemDelegate(new BookmarksItemDelegate(ui->treeView));
46
47 connect(ui->nextButton, &QAbstractButton::clicked, this, &BookmarksImportDialog::nextPage);
48 connect(ui->backButton, &QAbstractButton::clicked, this, &BookmarksImportDialog::previousPage);
49 connect(ui->chooseFile, &QAbstractButton::clicked, this, &BookmarksImportDialog::setFile);
50 connect(ui->cancelButton, &QDialogButtonBox::rejected, this, &QWidget::close);
51
52#ifndef Q_OS_WIN
53 ui->browserList->item(IE)->setHidden(true);
54#endif
55}
56
58{
59 ui->treeView->setModel(nullptr);
60 delete m_model;
61 delete m_importedFolder;
62 delete m_importer;
63 delete ui;
64}
65
66void BookmarksImportDialog::nextPage()
67{
68 switch (m_currentPage) {
69 case 0:
70 if (!ui->browserList->currentItem()) {
71 return;
72 }
73
74 switch (ui->browserList->currentRow()) {
75 case Firefox:
76 m_importer = new FirefoxImporter;
77 break;
78 case Chrome:
79 m_importer = new ChromeImporter;
80 break;
81 case Opera:
82 m_importer = new OperaImporter;
83 break;
84 case IE:
85 m_importer = new IeImporter;
86 break;
87 case Html:
88 m_importer = new HtmlImporter;
89 break;
90 default:
91 Q_ASSERT(!"Unreachable");
92 break;
93 }
94
95 ui->fileLine->clear();
96 showImporterPage();
97
98 ui->nextButton->setEnabled(false);
99 ui->backButton->setEnabled(true);
100 ui->stackedWidget->setCurrentIndex(++m_currentPage);
101 break;
102
103 case 1:
104 if (ui->fileLine->text().isEmpty()) {
105 return;
106 }
107
108 if (m_importer->prepareImport()) {
109 m_importedFolder = m_importer->importBookmarks();
110 }
111
112 if (m_importer->error()) {
113 QMessageBox::critical(this, tr("Error!"), m_importer->errorString());
114 return;
115 }
116
117 if (!m_importedFolder || m_importedFolder->children().isEmpty()) {
118 QMessageBox::warning(this, tr("Error!"), tr("No bookmarks were found."));
119 return;
120 }
121
122 Q_ASSERT(m_importedFolder->isFolder());
123
124 ui->stackedWidget->setCurrentIndex(++m_currentPage);
125 ui->nextButton->setText(tr("Finish"));
126 showExportedBookmarks();
127 break;
128
129 case 2:
130 addExportedBookmarks();
131 close();
132 break;
133
134 default:
135 Q_ASSERT(!"Unreachable");
136 }
137}
138
139void BookmarksImportDialog::previousPage()
140{
141 switch (m_currentPage) {
142 case 0:
143 break;
144
145 case 1:
146 ui->nextButton->setEnabled(true);
147 ui->backButton->setEnabled(false);
148 ui->stackedWidget->setCurrentIndex(--m_currentPage);
149
150 delete m_importer;
151 m_importer = nullptr;
152 break;
153
154 case 2:
155 showImporterPage();
156
157 ui->nextButton->setText(tr("Next >"));
158 ui->nextButton->setEnabled(true);
159 ui->backButton->setEnabled(true);
160 ui->stackedWidget->setCurrentIndex(--m_currentPage);
161
162 ui->treeView->setModel(nullptr);
163 delete m_model;
164 m_model = nullptr;
165
166 delete m_importedFolder;
167 m_importedFolder = nullptr;
168 break;
169
170 default:
171 Q_ASSERT(!"Unreachable");
172 }
173}
174
175void BookmarksImportDialog::setFile()
176{
177 Q_ASSERT(m_importer);
178
179 ui->fileLine->setText(m_importer->getPath(this));
180 ui->nextButton->setEnabled(!ui->fileLine->text().isEmpty());
181}
182
183void BookmarksImportDialog::showImporterPage()
184{
185 ui->iconLabel->setPixmap(ui->browserList->currentItem()->icon().pixmap(48));
186 ui->importingFromLabel->setText(tr("<b>Importing from %1</b>").arg(ui->browserList->currentItem()->text()));
187 ui->fileText1->setText(m_importer->description());
188 ui->standardDirLabel->setText(QSL("<i>%1</i>").arg(m_importer->standardPath()));
189}
190
191void BookmarksImportDialog::showExportedBookmarks()
192{
193 m_model = new BookmarksModel(m_importedFolder, nullptr, this);
194 ui->treeView->setModel(m_model);
195 ui->treeView->header()->resizeSection(0, ui->treeView->header()->width() / 2);
196 ui->treeView->expandAll();
197}
198
199void BookmarksImportDialog::addExportedBookmarks()
200{
201 mApp->bookmarks()->addBookmark(mApp->bookmarks()->unsortedFolder(), m_importedFolder);
202 m_importedFolder = nullptr;
203}
bool isFolder() const
QList< BookmarkItem * > children() const
BookmarksImportDialog(QWidget *parent=nullptr)
virtual QString description() const =0
virtual BookmarkItem * importBookmarks()=0
virtual QString standardPath() const =0
virtual QString getPath(QWidget *parent)=0
QString errorString() const
virtual bool prepareImport()=0
#define mApp
#define QSL(x)
Definition: qzcommon.h:40