2011-10-29 12:24:12 +02:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2012-01-01 15:29:55 +01:00
|
|
|
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
|
2011-10-29 12:24:12 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2011-10-22 22:29:33 +02:00
|
|
|
#include "bookmarksimportdialog.h"
|
|
|
|
#include "ui_bookmarksimportdialog.h"
|
|
|
|
#include "firefoximporter.h"
|
|
|
|
#include "chromeimporter.h"
|
|
|
|
#include "operaimporter.h"
|
2011-12-08 21:52:03 +01:00
|
|
|
#include "htmlimporter.h"
|
2011-10-22 22:29:33 +02:00
|
|
|
#include "mainapplication.h"
|
2012-03-06 15:28:52 +01:00
|
|
|
#include "bookmarksimporticonfetcher.h"
|
2012-01-17 19:27:24 +01:00
|
|
|
#include "iconprovider.h"
|
2011-10-28 23:17:38 +02:00
|
|
|
#include "networkmanager.h"
|
2011-10-22 22:29:33 +02:00
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QWebSettings>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QFileDialog>
|
2012-03-06 15:28:52 +01:00
|
|
|
#include <QThread>
|
2012-02-29 18:33:50 +01:00
|
|
|
|
2011-10-22 22:29:33 +02:00
|
|
|
BookmarksImportDialog::BookmarksImportDialog(QWidget* parent)
|
|
|
|
: QDialog(parent)
|
|
|
|
, ui(new Ui::BookmarksImportDialog)
|
|
|
|
, m_currentPage(0)
|
2012-03-06 15:28:52 +01:00
|
|
|
, m_fetcher(0)
|
|
|
|
, m_fetcherThread(0)
|
2011-10-22 22:29:33 +02:00
|
|
|
{
|
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
|
ui->browserList->setCurrentRow(0);
|
|
|
|
|
|
|
|
connect(ui->nextButton, SIGNAL(clicked()), this, SLOT(nextPage()));
|
|
|
|
connect(ui->chooseFile, SIGNAL(clicked()), this, SLOT(setFile()));
|
|
|
|
connect(ui->cancelButton, SIGNAL(clicked()), this, SLOT(close()));
|
|
|
|
connect(ui->stopButton, SIGNAL(clicked()), this, SLOT(stopDownloading()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksImportDialog::nextPage()
|
|
|
|
{
|
|
|
|
switch (m_currentPage) {
|
|
|
|
case 0:
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!ui->browserList->currentItem()) {
|
2011-10-22 22:29:33 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-22 22:29:33 +02:00
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
m_browser = (Browser)(ui->browserList->currentRow());
|
2011-10-22 22:29:33 +02:00
|
|
|
setupBrowser(m_browser);
|
|
|
|
ui->iconLabel->setPixmap(m_browserPixmap);
|
|
|
|
ui->importingFromLabel->setText(tr("<b>Importing from %1</b>").arg(m_browserName));
|
|
|
|
ui->fileText1->setText(m_browserFileText);
|
|
|
|
ui->fileText2->setText(m_browserFileText2);
|
|
|
|
ui->standardDirLabel->setText("<i>" + m_standardDir + "</i>");
|
|
|
|
ui->nextButton->setEnabled(false);
|
|
|
|
|
|
|
|
m_currentPage++;
|
|
|
|
ui->stackedWidget->setCurrentIndex(m_currentPage);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2011-11-06 17:01:23 +01:00
|
|
|
if (ui->fileLine->text().isEmpty()) {
|
2011-10-22 22:29:33 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-22 22:29:33 +02:00
|
|
|
|
|
|
|
if (exportedOK()) {
|
|
|
|
m_currentPage++;
|
|
|
|
ui->stackedWidget->setCurrentIndex(m_currentPage);
|
|
|
|
startFetchingIcons();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
addExportedBookmarks();
|
|
|
|
close();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksImportDialog::startFetchingIcons()
|
|
|
|
{
|
|
|
|
ui->nextButton->setText(tr("Finish"));
|
|
|
|
ui->nextButton->setEnabled(false);
|
|
|
|
ui->progressBar->setValue(0);
|
|
|
|
ui->progressBar->setMaximum(m_exportedBookmarks.count());
|
|
|
|
|
2012-03-06 15:28:52 +01:00
|
|
|
m_fetcherThread = new QThread();
|
|
|
|
m_fetcher = new BookmarksImportIconFetcher();
|
|
|
|
m_fetcher->moveToThread(m_fetcherThread);
|
|
|
|
|
|
|
|
QIcon defaultIcon = QIcon(QWebSettings::globalSettings()->webGraphic(QWebSettings::DefaultFrameIconGraphic));
|
|
|
|
QIcon folderIcon = style()->standardIcon(QStyle::SP_DirIcon);
|
|
|
|
QHash<QString, QTreeWidgetItem*> hash;
|
|
|
|
|
2012-01-24 19:12:31 +01:00
|
|
|
foreach(const Bookmark & b, m_exportedBookmarks) {
|
2012-03-06 15:28:52 +01:00
|
|
|
QTreeWidgetItem* item;
|
|
|
|
QTreeWidgetItem* findParent = hash[b.folder];
|
|
|
|
if (findParent) {
|
|
|
|
item = new QTreeWidgetItem(findParent);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
QTreeWidgetItem* newParent = new QTreeWidgetItem(ui->treeWidget);
|
|
|
|
newParent->setText(0, b.folder);
|
|
|
|
newParent->setIcon(0, folderIcon);
|
|
|
|
ui->treeWidget->addTopLevelItem(newParent);
|
|
|
|
hash[b.folder] = newParent;
|
|
|
|
|
|
|
|
item = new QTreeWidgetItem(newParent);
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant bookmarkVariant = qVariantFromValue(b);
|
2011-10-22 22:29:33 +02:00
|
|
|
item->setText(0, b.title);
|
2012-03-06 15:28:52 +01:00
|
|
|
if (b.image.isNull()) {
|
|
|
|
item->setIcon(0, defaultIcon);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
item->setIcon(0, QIcon(QPixmap::fromImage(b.image)));
|
|
|
|
}
|
2011-10-22 22:29:33 +02:00
|
|
|
item->setText(1, b.url.toString());
|
2012-03-06 15:28:52 +01:00
|
|
|
item->setData(0, Qt::UserRole + 10, bookmarkVariant);
|
2011-10-22 22:29:33 +02:00
|
|
|
|
|
|
|
ui->treeWidget->addTopLevelItem(item);
|
2012-03-06 15:28:52 +01:00
|
|
|
|
|
|
|
m_fetcher->addEntry(b.url, item);
|
2011-10-22 22:29:33 +02:00
|
|
|
}
|
2012-03-06 15:28:52 +01:00
|
|
|
|
|
|
|
ui->treeWidget->expandAll();
|
|
|
|
|
|
|
|
connect(m_fetcher, SIGNAL(iconFetched(QImage, QTreeWidgetItem*)), this, SLOT(iconFetched(QImage, QTreeWidgetItem*)));
|
|
|
|
connect(m_fetcher, SIGNAL(oneFinished()), this, SLOT(loadFinished()));
|
|
|
|
|
|
|
|
m_fetcherThread->start();
|
|
|
|
m_fetcher->startFetching();
|
2011-10-22 22:29:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksImportDialog::stopDownloading()
|
|
|
|
{
|
|
|
|
ui->nextButton->setEnabled(true);
|
2011-10-28 23:17:38 +02:00
|
|
|
ui->stopButton->hide();
|
2011-10-22 22:29:33 +02:00
|
|
|
ui->progressBar->setValue(ui->progressBar->maximum());
|
2011-12-09 21:16:32 +01:00
|
|
|
ui->fetchingLabel->setText(tr("Please press Finish to complete importing process."));
|
2011-10-22 22:29:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksImportDialog::loadFinished()
|
|
|
|
{
|
|
|
|
ui->progressBar->setValue(ui->progressBar->value() + 1);
|
|
|
|
|
2011-10-28 23:17:38 +02:00
|
|
|
if (ui->progressBar->value() == ui->progressBar->maximum()) {
|
|
|
|
ui->stopButton->hide();
|
2011-10-22 22:29:33 +02:00
|
|
|
ui->nextButton->setEnabled(true);
|
2011-12-09 21:16:32 +01:00
|
|
|
ui->fetchingLabel->setText(tr("Please press Finish to complete importing process."));
|
2011-10-28 23:17:38 +02:00
|
|
|
}
|
2011-10-22 22:29:33 +02:00
|
|
|
}
|
|
|
|
|
2012-03-06 15:28:52 +01:00
|
|
|
void BookmarksImportDialog::iconFetched(const QImage &image, QTreeWidgetItem* item)
|
2011-10-22 22:29:33 +02:00
|
|
|
{
|
2012-03-06 15:28:52 +01:00
|
|
|
item->setIcon(0, QIcon(QPixmap::fromImage(image)));
|
2011-10-22 22:29:33 +02:00
|
|
|
|
2012-03-06 15:28:52 +01:00
|
|
|
Bookmark b = item->data(0, Qt::UserRole + 10).value<Bookmark>();
|
2011-10-22 22:29:33 +02:00
|
|
|
|
2012-03-07 12:19:54 +01:00
|
|
|
int index = m_exportedBookmarks.indexOf(b);
|
|
|
|
if (index != -1) {
|
|
|
|
m_exportedBookmarks[index].image = image;
|
|
|
|
}
|
2011-10-22 22:29:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BookmarksImportDialog::exportedOK()
|
|
|
|
{
|
|
|
|
if (m_browser == Firefox) {
|
|
|
|
FirefoxImporter firefox(this);
|
|
|
|
firefox.setFile(ui->fileLine->text());
|
2011-11-06 17:01:23 +01:00
|
|
|
if (firefox.openDatabase()) {
|
2011-10-22 22:29:33 +02:00
|
|
|
m_exportedBookmarks = firefox.exportBookmarks();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-22 22:29:33 +02:00
|
|
|
|
|
|
|
if (firefox.error()) {
|
|
|
|
QMessageBox::critical(this, tr("Error!"), firefox.errorString());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else if (m_browser == Chrome) {
|
2011-10-22 22:29:33 +02:00
|
|
|
ChromeImporter chrome(this);
|
|
|
|
chrome.setFile(ui->fileLine->text());
|
2011-11-06 17:01:23 +01:00
|
|
|
if (chrome.openFile()) {
|
2011-10-22 22:29:33 +02:00
|
|
|
m_exportedBookmarks = chrome.exportBookmarks();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-22 22:29:33 +02:00
|
|
|
|
|
|
|
if (chrome.error()) {
|
|
|
|
QMessageBox::critical(this, tr("Error!"), chrome.errorString());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else if (m_browser == Opera) {
|
2011-10-22 22:29:33 +02:00
|
|
|
OperaImporter opera(this);
|
|
|
|
opera.setFile(ui->fileLine->text());
|
2011-11-06 17:01:23 +01:00
|
|
|
if (opera.openFile()) {
|
2011-10-22 22:29:33 +02:00
|
|
|
m_exportedBookmarks = opera.exportBookmarks();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-22 22:29:33 +02:00
|
|
|
|
|
|
|
if (opera.error()) {
|
|
|
|
QMessageBox::critical(this, tr("Error!"), opera.errorString());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2011-12-08 21:52:03 +01:00
|
|
|
else if (m_browser == Html) {
|
|
|
|
HtmlImporter html(this);
|
|
|
|
html.setFile(ui->fileLine->text());
|
|
|
|
if (html.openFile()) {
|
|
|
|
m_exportedBookmarks = html.exportBookmarks();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (html.error()) {
|
|
|
|
QMessageBox::critical(this, tr("Error!"), html.errorString());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2011-10-22 22:29:33 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksImportDialog::setFile()
|
|
|
|
{
|
|
|
|
#ifdef Q_WS_WIN
|
|
|
|
if (m_browser == IE) {
|
|
|
|
QString path = QFileDialog::getExistingDirectory(this, tr("Choose directory..."));
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!path.isEmpty()) {
|
2011-10-22 22:29:33 +02:00
|
|
|
ui->fileLine->setText(path);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2011-10-22 22:29:33 +02:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
QString path = QFileDialog::getOpenFileName(this, tr("Choose file..."), QDir::homePath(), m_browserBookmarkFile);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!path.isEmpty()) {
|
2011-10-22 22:29:33 +02:00
|
|
|
ui->fileLine->setText(path);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-22 22:29:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ui->nextButton->setEnabled(!ui->fileLine->text().isEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksImportDialog::addExportedBookmarks()
|
|
|
|
{
|
|
|
|
qApp->setOverrideCursor(Qt::WaitCursor);
|
|
|
|
|
|
|
|
BookmarksModel* model = mApp->bookmarksModel();
|
|
|
|
|
2012-01-24 19:12:31 +01:00
|
|
|
foreach(const Bookmark & b, m_exportedBookmarks) {
|
2012-01-17 19:27:24 +01:00
|
|
|
model->saveBookmark(b.url, b.title, IconProvider::iconFromImage(b.image), b.folder);
|
|
|
|
}
|
2011-10-22 22:29:33 +02:00
|
|
|
|
|
|
|
qApp->restoreOverrideCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksImportDialog::setupBrowser(Browser browser)
|
|
|
|
{
|
|
|
|
switch (browser) {
|
|
|
|
case Firefox:
|
|
|
|
m_browserPixmap = QPixmap(":icons/browsers/firefox.png");
|
|
|
|
m_browserName = "Mozilla Firefox";
|
|
|
|
m_browserBookmarkFile = "places.sqlite";
|
|
|
|
m_browserFileText = tr("Mozilla Firefox stores its bookmarks in <b>places.sqlite</b> SQLite database. "
|
|
|
|
"This file is usually located in ");
|
|
|
|
m_browserFileText2 = tr("Please choose this file to begin importing bookmarks.");
|
|
|
|
m_standardDir =
|
|
|
|
#ifdef Q_WS_WIN
|
2011-11-06 17:01:23 +01:00
|
|
|
"%APPDATA%/Mozilla/";
|
2011-10-22 22:29:33 +02:00
|
|
|
#else
|
2011-11-06 17:01:23 +01:00
|
|
|
"/home/user/.mozilla/firefox/profilename/";
|
2011-10-22 22:29:33 +02:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Chrome:
|
|
|
|
m_browserPixmap = QPixmap(":icons/browsers/chrome.png");
|
|
|
|
m_browserName = "Google Chrome";
|
|
|
|
m_browserBookmarkFile = "Bookmarks";
|
|
|
|
m_browserFileText = tr("Google Chrome stores its bookmarks in <b>Bookmarks</b> text file. "
|
|
|
|
"This file is usually located in ");
|
|
|
|
m_browserFileText2 = tr("Please choose this file to begin importing bookmarks.");
|
|
|
|
m_standardDir =
|
|
|
|
#ifdef Q_WS_WIN
|
2012-01-04 13:44:43 +01:00
|
|
|
"%APPDATA%/Chrome/Default/";
|
2011-10-22 22:29:33 +02:00
|
|
|
#else
|
|
|
|
"/home/user/.config/chrome/Default/";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Opera:
|
|
|
|
m_browserPixmap = QPixmap(":icons/browsers/opera.png");
|
|
|
|
m_browserName = "Opera";
|
|
|
|
m_browserBookmarkFile = "bookmarks.adr";
|
|
|
|
m_browserFileText = tr("Opera stores its bookmarks in <b>bookmarks.adr</b> text file. "
|
|
|
|
"This file is usually located in ");
|
|
|
|
m_browserFileText2 = tr("Please choose this file to begin importing bookmarks.");
|
|
|
|
m_standardDir =
|
|
|
|
#ifdef Q_WS_WIN
|
2012-01-04 13:44:43 +01:00
|
|
|
"%APPDATA%/Opera/";
|
2011-10-22 22:29:33 +02:00
|
|
|
#else
|
2011-11-06 17:01:23 +01:00
|
|
|
"/home/user/.opera/";
|
2011-10-22 22:29:33 +02:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
2011-12-08 21:52:03 +01:00
|
|
|
case Html:
|
|
|
|
m_browserPixmap = QPixmap(":icons/browsers/html.png");
|
|
|
|
m_browserName = "Html Import";
|
|
|
|
m_browserBookmarkFile = "*.htm, *.html";
|
|
|
|
m_browserFileText = tr("You can import bookmarks from any browser that supports HTML exporting. "
|
|
|
|
"This file has usually these suffixes");
|
|
|
|
m_browserFileText2 = tr("Please choose this file to begin importing bookmarks.");
|
|
|
|
m_standardDir = ".html, .htm";
|
|
|
|
break;
|
|
|
|
|
2011-10-22 22:29:33 +02:00
|
|
|
case IE:
|
|
|
|
m_browserPixmap = QPixmap(":icons/browsers/ie.png");
|
|
|
|
m_browserName = "Internet Explorer";
|
|
|
|
m_browserFileText = tr("Internet Explorer stores its bookmarks in <b>Favorites</b> folder. "
|
|
|
|
"This folder is usually located in ");
|
|
|
|
m_browserFileText2 = tr("Please choose this folder to begin importing bookmarks.");
|
|
|
|
m_standardDir = "C:\\Users\\username\\Favorites\\";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BookmarksImportDialog::~BookmarksImportDialog()
|
|
|
|
{
|
|
|
|
delete ui;
|
2012-03-06 15:28:52 +01:00
|
|
|
|
|
|
|
if (m_fetcherThread) {
|
|
|
|
m_fetcherThread->exit();
|
|
|
|
m_fetcherThread->wait();
|
|
|
|
|
|
|
|
m_fetcherThread->deleteLater();
|
|
|
|
m_fetcher->deleteLater();
|
|
|
|
}
|
2011-10-22 22:29:33 +02:00
|
|
|
}
|