2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2012-01-01 15:29:55 +01:00
|
|
|
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
|
2011-03-03 18:29:20 +01: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-03-02 16:57:41 +01:00
|
|
|
#include "bookmarksmodel.h"
|
2012-01-21 20:27:45 +01:00
|
|
|
#include "tabbedwebview.h"
|
2011-07-30 17:57:14 +02:00
|
|
|
#include "iconprovider.h"
|
2011-12-08 21:52:03 +01:00
|
|
|
#include "databasewriter.h"
|
2012-02-29 18:33:50 +01:00
|
|
|
#include "mainapplication.h"
|
|
|
|
#include "settings.h"
|
|
|
|
|
|
|
|
#include <QBuffer>
|
2011-03-02 16:57:41 +01:00
|
|
|
|
|
|
|
// SQLite DB -> table bookmarks + folders
|
|
|
|
// Unique in bookmarks table is id
|
|
|
|
// However from bookmark icon, it is not possible to add more than one bookmark
|
|
|
|
// Only from Ctrl+D dialog it is possible
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
BookmarksModel::BookmarksModel(QObject* parent)
|
2011-04-15 20:45:22 +02:00
|
|
|
: QObject(parent)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
|
|
|
loadSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksModel::loadSettings()
|
|
|
|
{
|
2012-01-11 21:58:25 +01:00
|
|
|
Settings settings;
|
2012-02-04 22:47:55 +01:00
|
|
|
settings.beginGroup("Bookmarks");
|
2011-11-06 17:01:23 +01:00
|
|
|
m_showMostVisited = settings.value("showMostVisited", true).toBool();
|
2012-02-17 19:54:58 +01:00
|
|
|
m_showOnlyIconsInToolbar = settings.value("showOnlyIconsInToolbar", false).toBool();
|
2012-02-04 22:47:55 +01:00
|
|
|
m_lastFolder = settings.value("LastFolder", "unsorted").toString();
|
2011-03-02 16:57:41 +01:00
|
|
|
settings.endGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksModel::setShowingMostVisited(bool state)
|
|
|
|
{
|
2012-01-11 21:58:25 +01:00
|
|
|
Settings settings;
|
2012-02-04 22:47:55 +01:00
|
|
|
settings.beginGroup("Bookmarks");
|
2011-11-06 17:01:23 +01:00
|
|
|
settings.setValue("showMostVisited", state);
|
2011-03-02 16:57:41 +01:00
|
|
|
settings.endGroup();
|
|
|
|
m_showMostVisited = state;
|
|
|
|
}
|
|
|
|
|
2012-02-17 19:54:58 +01:00
|
|
|
void BookmarksModel::setShowingOnlyIconsInToolbar(bool state)
|
|
|
|
{
|
|
|
|
Settings settings;
|
|
|
|
settings.beginGroup("Bookmarks");
|
|
|
|
settings.setValue("showOnlyIconsInToolbar", state);
|
|
|
|
settings.endGroup();
|
|
|
|
m_showOnlyIconsInToolbar = state;
|
|
|
|
}
|
|
|
|
|
2012-03-06 15:28:52 +01:00
|
|
|
bool BookmarksModel::isFolder(const QString &name)
|
|
|
|
{
|
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare("SELECT name FROM folders WHERE name = ?");
|
|
|
|
query.bindValue(0, name);
|
|
|
|
query.exec();
|
|
|
|
|
|
|
|
return query.next();
|
|
|
|
}
|
|
|
|
|
2012-02-04 22:47:55 +01:00
|
|
|
void BookmarksModel::setLastFolder(const QString &folder)
|
|
|
|
{
|
|
|
|
Settings settings;
|
|
|
|
settings.beginGroup("Bookmarks");
|
|
|
|
settings.setValue("lastFolder", folder);
|
|
|
|
settings.endGroup();
|
|
|
|
m_lastFolder = folder;
|
|
|
|
}
|
|
|
|
|
2011-04-15 20:45:22 +02:00
|
|
|
bool BookmarksModel::isBookmarked(const QUrl &url)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare("SELECT count(id) FROM bookmarks WHERE url=?");
|
|
|
|
query.bindValue(0, url.toString());
|
|
|
|
query.exec();
|
|
|
|
query.next();
|
2011-11-06 17:01:23 +01:00
|
|
|
return query.value(0).toInt() > 0;
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bookmark search priority:
|
|
|
|
// Bookmarks in menu > bookmarks in toolbar -> user folders and unsorted
|
2011-04-15 20:45:22 +02:00
|
|
|
int BookmarksModel::bookmarkId(const QUrl &url)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare("SELECT id FROM bookmarks WHERE url=? AND folder='bookmarksMenu' ");
|
|
|
|
query.bindValue(0, url.toString());
|
|
|
|
query.exec();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (query.next()) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return query.value(0).toInt();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
|
|
|
query.prepare("SELECT id FROM bookmarks WHERE url=? AND folder='bookmarksToolbar' ");
|
|
|
|
query.bindValue(0, url.toString());
|
|
|
|
query.exec();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (query.next()) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return query.value(0).toInt();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
|
|
|
query.prepare("SELECT id FROM bookmarks WHERE url=? ");
|
|
|
|
query.bindValue(0, url.toString());
|
|
|
|
query.exec();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (query.next()) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return query.value(0).toInt();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-04-15 20:45:22 +02:00
|
|
|
int BookmarksModel::bookmarkId(const QUrl &url, const QString &title, const QString &folder)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare("SELECT id FROM bookmarks WHERE url=? AND title=? AND folder=? ");
|
|
|
|
query.bindValue(0, url.toString());
|
|
|
|
query.bindValue(1, title);
|
|
|
|
query.bindValue(2, folder);
|
|
|
|
query.exec();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (query.next()) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return query.value(0).toInt();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-04-15 20:45:22 +02:00
|
|
|
BookmarksModel::Bookmark BookmarksModel::getBookmark(int id)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2011-04-15 20:45:22 +02:00
|
|
|
Bookmark bookmark;
|
2011-03-02 16:57:41 +01:00
|
|
|
QSqlQuery query;
|
2011-07-30 17:57:14 +02:00
|
|
|
query.prepare("SELECT url, title, folder, icon FROM bookmarks WHERE id=?");
|
2011-03-02 16:57:41 +01:00
|
|
|
query.bindValue(0, id);
|
|
|
|
query.exec();
|
2011-04-15 20:45:22 +02:00
|
|
|
if (query.next()) {
|
|
|
|
bookmark.id = id;
|
|
|
|
bookmark.url = query.value(0).toUrl();
|
|
|
|
bookmark.title = query.value(1).toString();
|
|
|
|
bookmark.folder = query.value(2).toString();
|
2012-01-17 19:27:24 +01:00
|
|
|
bookmark.image = QImage::fromData(query.value(3).toByteArray());
|
2011-10-28 17:52:42 +02:00
|
|
|
bookmark.inSubfolder = isSubfolder(bookmark.folder);
|
2011-04-15 20:45:22 +02:00
|
|
|
}
|
|
|
|
return bookmark;
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2011-07-30 17:57:14 +02:00
|
|
|
bool BookmarksModel::saveBookmark(const QUrl &url, const QString &title, const QIcon &icon, const QString &folder)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (url.isEmpty() || title.isEmpty() || folder.isEmpty()) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return false;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-01-17 19:27:24 +01:00
|
|
|
QImage image = icon.pixmap(16, 16).toImage();
|
2012-01-28 12:02:37 +01:00
|
|
|
if (image.isNull()) {
|
2012-04-22 20:51:28 +02:00
|
|
|
image = qIconProvider->emptyWebImage();
|
2012-01-28 12:02:37 +01:00
|
|
|
}
|
2012-01-17 19:27:24 +01:00
|
|
|
|
2012-03-06 15:28:52 +01:00
|
|
|
if (!isFolder(folder)) {
|
|
|
|
createFolder(folder);
|
|
|
|
}
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
QSqlQuery query;
|
2011-07-30 17:57:14 +02:00
|
|
|
query.prepare("INSERT INTO bookmarks (url, title, folder, icon) VALUES (?,?,?,?)");
|
2011-03-02 16:57:41 +01:00
|
|
|
query.bindValue(0, url.toString());
|
|
|
|
query.bindValue(1, title);
|
|
|
|
query.bindValue(2, folder);
|
2012-01-17 19:27:24 +01:00
|
|
|
QByteArray ba;
|
|
|
|
QBuffer buffer(&ba);
|
|
|
|
buffer.open(QIODevice::WriteOnly);
|
|
|
|
image.save(&buffer, "PNG");
|
|
|
|
query.bindValue(3, buffer.data());
|
2011-12-17 14:30:54 +01:00
|
|
|
query.exec();
|
2011-04-15 20:45:22 +02:00
|
|
|
|
|
|
|
Bookmark bookmark;
|
|
|
|
bookmark.id = query.lastInsertId().toInt();
|
|
|
|
bookmark.url = url;
|
|
|
|
bookmark.title = title;
|
|
|
|
bookmark.folder = folder;
|
2012-01-17 19:27:24 +01:00
|
|
|
bookmark.image = image;
|
2011-10-28 17:52:42 +02:00
|
|
|
bookmark.inSubfolder = isSubfolder(bookmark.folder);
|
|
|
|
|
2012-02-04 22:47:55 +01:00
|
|
|
setLastFolder(folder);
|
|
|
|
|
2011-04-15 20:45:22 +02:00
|
|
|
emit bookmarkAdded(bookmark);
|
2012-01-21 23:19:38 +01:00
|
|
|
mApp->sendMessages(Qz::AM_BookmarksChanged, true);
|
2011-04-15 20:45:22 +02:00
|
|
|
return true;
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-02-04 22:47:55 +01:00
|
|
|
bool BookmarksModel::saveBookmark(WebView* view, QString folder)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-02-04 22:47:55 +01:00
|
|
|
if (folder.isEmpty()) {
|
|
|
|
folder = m_lastFolder;
|
|
|
|
}
|
|
|
|
|
2012-01-21 20:27:45 +01:00
|
|
|
return saveBookmark(view->url(), view->title(), view->icon(), folder);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-04-05 10:27:35 +02:00
|
|
|
void BookmarksModel::removeBookmark(int id)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-04-05 10:27:35 +02:00
|
|
|
QList<int> list;
|
|
|
|
list.append(id);
|
2011-04-15 20:45:22 +02:00
|
|
|
|
2012-04-05 10:27:35 +02:00
|
|
|
return removeBookmark(list);
|
|
|
|
}
|
2011-04-15 20:45:22 +02:00
|
|
|
|
2012-04-05 10:27:35 +02:00
|
|
|
void BookmarksModel::removeBookmark(const QList<int> list)
|
|
|
|
{
|
|
|
|
QSqlDatabase db = QSqlDatabase::database();
|
|
|
|
db.transaction();
|
|
|
|
|
2012-04-08 21:45:40 +02:00
|
|
|
foreach(int id, list) {
|
2012-04-05 10:27:35 +02:00
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare("SELECT url, title, folder FROM bookmarks WHERE id = ?");
|
|
|
|
query.bindValue(0, id);
|
|
|
|
query.exec();
|
|
|
|
if (!query.next()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Bookmark bookmark;
|
|
|
|
bookmark.id = id;
|
|
|
|
bookmark.url = query.value(0).toUrl();
|
|
|
|
bookmark.title = query.value(1).toString();
|
|
|
|
bookmark.folder = query.value(2).toString();
|
|
|
|
bookmark.image = QImage::fromData(query.value(3).toByteArray());
|
|
|
|
bookmark.inSubfolder = isSubfolder(bookmark.folder);
|
|
|
|
|
|
|
|
if (!query.exec("DELETE FROM bookmarks WHERE id = " + QString::number(id))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit bookmarkDeleted(bookmark);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-04-15 20:45:22 +02:00
|
|
|
|
2012-04-05 10:27:35 +02:00
|
|
|
db.commit();
|
2012-01-21 23:19:38 +01:00
|
|
|
mApp->sendMessages(Qz::AM_BookmarksChanged, true);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-04-05 10:27:35 +02:00
|
|
|
void BookmarksModel::removeBookmark(const QUrl &url)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-04-05 10:27:35 +02:00
|
|
|
removeBookmark(bookmarkId(url));
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-04-05 10:27:35 +02:00
|
|
|
void BookmarksModel::removeBookmark(WebView* view)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-04-05 10:27:35 +02:00
|
|
|
removeBookmark(bookmarkId(view->url()));
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2011-04-15 20:45:22 +02:00
|
|
|
//bool BookmarksModel::editBookmark(int id, const QString &title, const QString &folder)
|
|
|
|
//{
|
|
|
|
// QSqlQuery query;
|
|
|
|
// query.prepare("UPDATE bookmarks SET title=?, folder=? WHERE id=?");
|
|
|
|
// query.bindValue(0, title);
|
|
|
|
// query.bindValue(1, folder);
|
|
|
|
// query.bindValue(2, id);
|
|
|
|
// return query.exec();
|
|
|
|
//}
|
|
|
|
|
|
|
|
//bool BookmarksModel::editBookmark(int id, const QUrl &url, const QString &title)
|
|
|
|
//{
|
|
|
|
// QSqlQuery query;
|
|
|
|
// query.prepare("UPDATE bookmarks SET title=?, url=? WHERE id=?");
|
|
|
|
// query.bindValue(0, title);
|
|
|
|
// query.bindValue(1, url.toString());
|
|
|
|
// query.bindValue(2, id);
|
|
|
|
// return query.exec();
|
|
|
|
//}
|
|
|
|
|
|
|
|
bool BookmarksModel::editBookmark(int id, const QString &title, const QUrl &url, const QString &folder)
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (title.isEmpty() && url.isEmpty() && folder.isEmpty()) {
|
2011-04-15 20:45:22 +02:00
|
|
|
return false;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-05-22 11:46:22 +02:00
|
|
|
|
2011-04-15 20:45:22 +02:00
|
|
|
QSqlQuery query;
|
2012-05-22 11:46:22 +02:00
|
|
|
query.exec("SELECT title, url, folder, icon FROM bookmarks WHERE id = " + QString::number(id));
|
|
|
|
if (!query.next()) {
|
2011-04-15 20:45:22 +02:00
|
|
|
return false;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-04-15 20:45:22 +02:00
|
|
|
|
|
|
|
Bookmark before;
|
|
|
|
before.id = id;
|
|
|
|
before.title = query.value(0).toString();
|
|
|
|
before.url = query.value(1).toUrl();
|
|
|
|
before.folder = query.value(2).toString();
|
2012-01-17 19:27:24 +01:00
|
|
|
before.image = QImage::fromData(query.value(3).toByteArray());
|
2011-10-28 17:52:42 +02:00
|
|
|
before.inSubfolder = isSubfolder(before.folder);
|
2011-04-15 20:45:22 +02:00
|
|
|
|
|
|
|
Bookmark after;
|
|
|
|
after.id = id;
|
|
|
|
after.title = title.isEmpty() ? before.title : title;
|
|
|
|
after.url = url.isEmpty() ? before.url : url;
|
|
|
|
after.folder = folder.isEmpty() ? before.folder : folder;
|
2012-01-17 19:27:24 +01:00
|
|
|
after.image = before.image;
|
2011-10-28 17:52:42 +02:00
|
|
|
after.inSubfolder = isSubfolder(after.folder);
|
2011-04-15 20:45:22 +02:00
|
|
|
|
|
|
|
query.prepare("UPDATE bookmarks SET title=?, url=?, folder=? WHERE id = ?");
|
|
|
|
query.bindValue(0, after.title);
|
|
|
|
query.bindValue(1, after.url.toString());
|
|
|
|
query.bindValue(2, after.folder);
|
|
|
|
query.bindValue(3, id);
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!query.exec()) {
|
2011-04-15 20:45:22 +02:00
|
|
|
return false;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-04-15 20:45:22 +02:00
|
|
|
|
|
|
|
emit bookmarkEdited(before, after);
|
2012-01-21 23:19:38 +01:00
|
|
|
mApp->sendMessages(Qz::AM_BookmarksChanged, true);
|
2011-04-15 20:45:22 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-22 11:46:22 +02:00
|
|
|
bool BookmarksModel::changeIcon(int id, const QIcon &icon)
|
|
|
|
{
|
|
|
|
QSqlQuery query;
|
|
|
|
query.exec("SELECT title, url, folder, icon FROM bookmarks WHERE id = " + QString::number(id));
|
|
|
|
if (!query.next()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Bookmark before;
|
|
|
|
before.id = id;
|
|
|
|
before.title = query.value(0).toString();
|
|
|
|
before.url = query.value(1).toUrl();
|
|
|
|
before.folder = query.value(2).toString();
|
|
|
|
before.image = QImage::fromData(query.value(3).toByteArray());
|
|
|
|
before.inSubfolder = isSubfolder(before.folder);
|
|
|
|
|
|
|
|
Bookmark after = before;
|
|
|
|
after.image = icon.pixmap(16).toImage();
|
|
|
|
|
|
|
|
query.prepare("UPDATE bookmarks SET icon = ? WHERE id = ?");
|
|
|
|
QByteArray ba;
|
|
|
|
QBuffer buffer(&ba);
|
|
|
|
buffer.open(QIODevice::WriteOnly);
|
|
|
|
after.image.save(&buffer, "PNG");
|
|
|
|
query.bindValue(0, buffer.data());
|
|
|
|
query.bindValue(1, id);
|
|
|
|
|
|
|
|
if (!query.exec()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit bookmarkEdited(before, after);
|
|
|
|
mApp->sendMessages(Qz::AM_BookmarksChanged, true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-04-15 20:45:22 +02:00
|
|
|
bool BookmarksModel::createFolder(const QString &name)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-03-06 15:28:52 +01:00
|
|
|
if (isFolder(name)) {
|
2011-10-22 22:29:33 +02:00
|
|
|
return false;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-22 22:29:33 +02:00
|
|
|
|
2012-03-06 15:28:52 +01:00
|
|
|
QSqlQuery query;
|
2011-10-28 23:17:38 +02:00
|
|
|
query.prepare("INSERT INTO folders (name, subfolder) VALUES (?, 'no')");
|
2011-04-15 20:45:22 +02:00
|
|
|
query.bindValue(0, name);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!query.exec()) {
|
2011-04-15 20:45:22 +02:00
|
|
|
return false;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-04-15 20:45:22 +02:00
|
|
|
|
|
|
|
emit folderAdded(name);
|
2012-01-21 23:19:38 +01:00
|
|
|
mApp->sendMessages(Qz::AM_BookmarksChanged, true);
|
2011-04-15 20:45:22 +02:00
|
|
|
return true;
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-04-05 10:27:35 +02:00
|
|
|
void BookmarksModel::removeFolder(const QString &name)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2011-12-17 14:30:54 +01:00
|
|
|
if (name == _bookmarksMenu || name == _bookmarksToolbar) {
|
2012-04-05 10:27:35 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-04-15 20:45:22 +02:00
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
QSqlQuery query;
|
2011-04-15 20:45:22 +02:00
|
|
|
query.prepare("SELECT id FROM bookmarks WHERE folder = ? ");
|
|
|
|
query.bindValue(0, name);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!query.exec()) {
|
2012-04-05 10:27:35 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-04-05 10:27:35 +02:00
|
|
|
|
|
|
|
QList<int> list;
|
2011-11-06 17:01:23 +01:00
|
|
|
while (query.next()) {
|
2012-04-05 10:27:35 +02:00
|
|
|
list.append(query.value(0).toInt());
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-04-05 10:27:35 +02:00
|
|
|
removeBookmark(list);
|
2011-04-15 20:45:22 +02:00
|
|
|
|
|
|
|
query.prepare("DELETE FROM folders WHERE name=?");
|
|
|
|
query.bindValue(0, name);
|
2012-04-05 10:27:35 +02:00
|
|
|
query.exec();
|
2011-04-15 20:45:22 +02:00
|
|
|
|
|
|
|
emit folderDeleted(name);
|
2012-04-05 10:27:35 +02:00
|
|
|
|
2012-01-21 23:19:38 +01:00
|
|
|
mApp->sendMessages(Qz::AM_BookmarksChanged, true);
|
2011-04-15 20:45:22 +02:00
|
|
|
}
|
|
|
|
|
2011-10-28 17:52:42 +02:00
|
|
|
bool BookmarksModel::renameFolder(const QString &before, const QString &after)
|
|
|
|
{
|
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare("SELECT name FROM folders WHERE name = ?");
|
|
|
|
query.bindValue(0, after);
|
|
|
|
query.exec();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (query.next()) {
|
2011-10-28 17:52:42 +02:00
|
|
|
return false;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-28 17:52:42 +02:00
|
|
|
|
|
|
|
query.prepare("UPDATE folders SET name=? WHERE name=?");
|
|
|
|
query.bindValue(0, after);
|
|
|
|
query.bindValue(1, before);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!query.exec()) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-10-28 17:52:42 +02:00
|
|
|
|
|
|
|
query.prepare("UPDATE bookmarks SET folder=? WHERE folder=?");
|
|
|
|
query.bindValue(0, after);
|
|
|
|
query.bindValue(1, before);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!query.exec()) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-10-28 17:52:42 +02:00
|
|
|
|
|
|
|
emit folderRenamed(before, after);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-19 21:30:26 +01:00
|
|
|
QList<Bookmark> BookmarksModel::folderBookmarks(const QString &name)
|
|
|
|
{
|
|
|
|
QList<Bookmark> list;
|
|
|
|
|
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare("SELECT id, url, title, folder, icon FROM bookmarks WHERE folder=?");
|
|
|
|
query.addBindValue(name);
|
|
|
|
query.exec();
|
|
|
|
while (query.next()) {
|
|
|
|
Bookmark bookmark;
|
|
|
|
bookmark.id = query.value(0).toInt();
|
|
|
|
bookmark.url = query.value(1).toUrl();
|
|
|
|
bookmark.title = query.value(2).toString();
|
|
|
|
bookmark.folder = query.value(3).toString();
|
2012-01-17 19:27:24 +01:00
|
|
|
bookmark.image = QImage::fromData(query.value(4).toByteArray());
|
2011-12-19 21:30:26 +01:00
|
|
|
bookmark.inSubfolder = isSubfolder(bookmark.folder);
|
|
|
|
|
|
|
|
list.append(bookmark);
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2011-10-28 17:52:42 +02:00
|
|
|
bool BookmarksModel::createSubfolder(const QString &name)
|
|
|
|
{
|
2012-03-06 15:28:52 +01:00
|
|
|
if (isFolder(name)) {
|
2011-10-28 17:52:42 +02:00
|
|
|
return false;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-28 17:52:42 +02:00
|
|
|
|
2012-03-06 15:28:52 +01:00
|
|
|
QSqlQuery query;
|
2011-10-28 17:52:42 +02:00
|
|
|
query.prepare("INSERT INTO folders (name, subfolder) VALUES (?, 'yes')");
|
|
|
|
query.bindValue(0, name);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!query.exec()) {
|
2011-10-28 17:52:42 +02:00
|
|
|
return false;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-28 17:52:42 +02:00
|
|
|
|
|
|
|
emit subfolderAdded(name);
|
2012-01-21 23:19:38 +01:00
|
|
|
mApp->sendMessages(Qz::AM_BookmarksChanged, true);
|
2011-10-28 17:52:42 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BookmarksModel::isSubfolder(const QString &name)
|
|
|
|
{
|
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare("SELECT subfolder FROM folders WHERE name = ?");
|
|
|
|
query.bindValue(0, name);
|
|
|
|
query.exec();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!query.next()) {
|
2011-10-28 17:52:42 +02:00
|
|
|
return false;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-28 17:52:42 +02:00
|
|
|
|
|
|
|
return query.value(0).toString() == "yes";
|
|
|
|
}
|
|
|
|
|
2011-04-15 20:45:22 +02:00
|
|
|
bool BookmarksModel::bookmarksEqual(const Bookmark &one, const Bookmark &two)
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (one.id != two.id) {
|
2011-04-15 20:45:22 +02:00
|
|
|
return false;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
if (one.title != two.title) {
|
2011-04-15 20:45:22 +02:00
|
|
|
return false;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
if (one.folder != two.folder) {
|
2011-04-15 20:45:22 +02:00
|
|
|
return false;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
if (one.url != two.url) {
|
2011-04-15 20:45:22 +02:00
|
|
|
return false;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-04-15 20:45:22 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString BookmarksModel::toTranslatedFolder(const QString &name)
|
|
|
|
{
|
|
|
|
QString trFolder;
|
2011-11-06 17:01:23 +01:00
|
|
|
if (name == "bookmarksMenu") {
|
2011-04-15 20:45:22 +02:00
|
|
|
trFolder = tr("Bookmarks In Menu");
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else if (name == "bookmarksToolbar") {
|
2011-04-15 20:45:22 +02:00
|
|
|
trFolder = tr("Bookmarks In ToolBar");
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else if (name == "unsorted") {
|
2011-04-15 20:45:22 +02:00
|
|
|
trFolder = tr("Unsorted Bookmarks");
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-04-15 20:45:22 +02:00
|
|
|
trFolder = name;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-04-15 20:45:22 +02:00
|
|
|
return trFolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString BookmarksModel::fromTranslatedFolder(const QString &name)
|
|
|
|
{
|
|
|
|
QString folder;
|
2011-11-06 17:01:23 +01:00
|
|
|
if (name == tr("Bookmarks In Menu")) {
|
2011-04-15 20:45:22 +02:00
|
|
|
folder = "bookmarksMenu";
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else if (name == tr("Bookmarks In ToolBar")) {
|
2011-04-15 20:45:22 +02:00
|
|
|
folder = "bookmarksToolbar";
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else if (name == tr("Unsorted Bookmarks")) {
|
2011-04-15 20:45:22 +02:00
|
|
|
folder = "unsorted";
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-04-15 20:45:22 +02:00
|
|
|
folder = name;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-04-15 20:45:22 +02:00
|
|
|
return folder;
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|