1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 18:56:34 +01:00

[Bookmarks] LocationCompleter now works

BookmarkItem: Added counting of bookmark's visits
This commit is contained in:
nowrep 2014-02-09 13:34:52 +01:00
parent 877eefec22
commit 83b8b0b666
12 changed files with 103 additions and 291 deletions

View File

@ -20,6 +20,7 @@
BookmarkItem::BookmarkItem(BookmarkItem::Type type, BookmarkItem* parent)
: m_type(type)
, m_parent(parent)
, m_visitCount(0)
, m_expanded(false)
, m_sidebarExpanded(false)
{
@ -108,6 +109,16 @@ void BookmarkItem::setKeyword(const QString &keyword)
m_keyword = keyword;
}
int BookmarkItem::visitCount() const
{
return m_visitCount;
}
void BookmarkItem::setVisitCount(int count)
{
m_visitCount = count;
}
bool BookmarkItem::isExpanded() const
{
return m_type == Root ? true : m_expanded;

View File

@ -60,6 +60,9 @@ public:
QString keyword() const;
void setKeyword(const QString &keyword);
int visitCount() const;
void setVisitCount(int count);
// Expanded state in Manager
bool isExpanded() const;
void setExpanded(bool expanded);
@ -83,6 +86,7 @@ private:
QString m_title;
QString m_description;
QString m_keyword;
int m_visitCount;
bool m_expanded;
bool m_sidebarExpanded;
};

View File

@ -142,6 +142,7 @@ void Bookmarks::readBookmarks(const QVariantList &list, BookmarkItem* parent)
item->setTitle(map.value("name").toString());
item->setDescription(map.value("description").toString());
item->setKeyword(map.value("keyword").toString());
item->setVisitCount(map.value("visit_count").toInt());
break;
case BookmarkItem::Folder:
@ -177,6 +178,7 @@ QVariantList Bookmarks::writeBookmarks(BookmarkItem* parent)
map.insert("name", child->title());
map.insert("description", child->description());
map.insert("keyword", child->keyword());
map.insert("visit_count", child->visitCount());
break;
case BookmarkItem::Folder:
@ -224,6 +226,33 @@ void Bookmarks::search(QList<BookmarkItem*>* items, BookmarkItem* parent, const
}
}
void Bookmarks::search(QList<BookmarkItem*>* items, BookmarkItem* parent, const QString &string, Qt::CaseSensitivity sensitive) const
{
Q_ASSERT(items);
Q_ASSERT(parent);
switch (parent->type()) {
case BookmarkItem::Root:
case BookmarkItem::Folder:
foreach (BookmarkItem* child, parent->children()) {
search(items, child, string, sensitive);
}
break;
case BookmarkItem::Url:
if (parent->title().contains(string, sensitive) ||
parent->url().toString().contains(string, sensitive) ||
parent->description().contains(string, sensitive) ||
parent->keyword().compare(string, sensitive) == 0) {
items->append(parent);
}
break;
default:
break;
}
}
void Bookmarks::loadSettings()
{
Settings settings;
@ -348,29 +377,6 @@ void Bookmarks::exportToHtml(const QString &fileName)
out << "</DL><p>" << endl;
}
QVector<Bookmark> Bookmarks::getFolderBookmarks(const QString &name)
{
QVector<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();
bookmark.image = QImage::fromData(query.value(4).toByteArray());
bookmark.inSubfolder = true;
list.append(bookmark);
}
return list;
}
QString Bookmarks::toTranslatedFolder(const QString &name)
{
QString trFolder;
@ -450,6 +456,13 @@ QList<BookmarkItem*> Bookmarks::searchBookmarks(const QUrl &url) const
return items;
}
QList<BookmarkItem*> Bookmarks::searchBookmarks(const QString &string, Qt::CaseSensitivity sensitive) const
{
QList<BookmarkItem*> items;
search(&items, m_root, string, sensitive);
return items;
}
bool Bookmarks::removeBookmark(BookmarkItem* item)
{
if (!canBeModified(item)) {

View File

@ -69,10 +69,6 @@ public:
void setShowingOnlyIconsInToolbar(bool state);
void setLastFolder(const QString &folder);
QVector<Bookmark> getFolderBookmarks(const QString &name);
bool saveBookmark(const QUrl &url, const QString &title, const QIcon &icon, const QString &folder = QLatin1String("unsorted"));
void exportToHtml(const QString &fileName);
static QString toTranslatedFolder(const QString &name);
@ -87,7 +83,11 @@ public:
BookmarkItem* lastUsedFolder() const;
bool isBookmarked(const QUrl &url);
// Search bookmarks (urls only) for exact url match
QList<BookmarkItem*> searchBookmarks(const QUrl &url) const;
// Search bookmarks (urls only) for contains match through all properties
QList<BookmarkItem*> searchBookmarks(const QString &string, Qt::CaseSensitivity sensitive = Qt::CaseInsensitive) const;
bool canBeModified(BookmarkItem* item) const;
@ -112,6 +112,7 @@ private:
QVariantList writeBookmarks(BookmarkItem* parent);
void search(QList<BookmarkItem*>* items, BookmarkItem* parent, const QUrl &url) const;
void search(QList<BookmarkItem*>* items, BookmarkItem* parent, const QString &string, Qt::CaseSensitivity sensitive) const;
bool m_showMostVisited;
bool m_showOnlyIconsInToolbar;

View File

@ -105,8 +105,12 @@ QVariant BookmarksModel::data(const QModelIndex &index, int role) const
return itm->description();
case KeywordRole:
return itm->keyword();
case VisitCountRole:
return -1;
case ExpandedRole:
return itm->isExpanded();
case SidebarExpandedRole:
return itm->isSidebarExpanded();
case Qt::ToolTipRole:
if (index.column() == 0 && itm->isUrl()) {
return QString("%1\n%2").arg(itm->title(), QString::fromUtf8(itm->url().toEncoded()));
@ -183,31 +187,6 @@ bool BookmarksModel::hasChildren(const QModelIndex &parent) const
return !itm->children().isEmpty();
}
#if 0
bool BookmarksModel::removeRows(int row, int count, const QModelIndex &parent)
{
if (!hasIndex(row, 0, parent) || !hasIndex(row + count, 0, parent)) {
return false;
}
BookmarkItem* itm = item(parent);
int offset = 0;
for (int i = 0; i < count; ++i) {
int idx = i + row + offset;
BookmarkItem* child = itm->children().at(idx);
if (!m_bookmarks->removeBookmark(child)) {
offset++;
continue;
}
}
return true;
}
#endif
Qt::DropActions BookmarksModel::supportedDropActions() const
{
return Qt::CopyAction | Qt::MoveAction;

View File

@ -36,7 +36,9 @@ public:
TitleRole = Qt::UserRole + 3,
DescriptionRole = Qt::UserRole + 4,
KeywordRole = Qt::UserRole + 5,
ExpandedRole = Qt::UserRole + 6,
VisitCountRole = Qt::UserRole + 6,
ExpandedRole = Qt::UserRole + 7,
SidebarExpandedRole = Qt::UserRole + 8,
MaxRole = ExpandedRole
};
@ -63,10 +65,6 @@ public:
BookmarkItem* item(const QModelIndex &index) const;
#if 0
bool removeRows(int row, int count, const QModelIndex &parent);
#endif
private slots:
void bookmarkChanged(BookmarkItem* item);

View File

@ -54,15 +54,6 @@ BookmarksToolbar::BookmarksToolbar(QupZilla* mainClass, QWidget* parent)
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customContextMenuRequested(QPoint)));
connect(m_bookmarks, SIGNAL(bookmarkAdded(Bookmarks::Bookmark)), this, SLOT(addBookmark(Bookmarks::Bookmark)));
connect(m_bookmarks, SIGNAL(bookmarkDeleted(Bookmarks::Bookmark)), this, SLOT(removeBookmark(Bookmarks::Bookmark)));
connect(m_bookmarks, SIGNAL(bookmarkEdited(Bookmarks::Bookmark,Bookmarks::Bookmark)), this, SLOT(bookmarkEdited(Bookmarks::Bookmark,Bookmarks::Bookmark)));
connect(m_bookmarks, SIGNAL(subfolderAdded(QString)), this, SLOT(subfolderAdded(QString)));
connect(m_bookmarks, SIGNAL(folderDeleted(QString)), this, SLOT(folderDeleted(QString)));
connect(m_bookmarks, SIGNAL(folderRenamed(QString,QString)), this, SLOT(folderRenamed(QString,QString)));
connect(m_bookmarks, SIGNAL(folderParentChanged(QString,bool)), this, SLOT(changeFolderParent(QString,bool)));
connect(m_bookmarks, SIGNAL(bookmarkParentChanged(QString,QByteArray,int,QUrl,QString,QString)), this, SLOT(changeBookmarkParent(QString,QByteArray,int,QUrl,QString,QString)));
setMaximumWidth(p_QupZilla->width());
refreshBookmarks();
@ -342,9 +333,9 @@ void BookmarksToolbar::loadFolderBookmarksInTabs()
return;
}
foreach (const Bookmark &b, m_bookmarks->getFolderBookmarks(folder)) {
p_QupZilla->tabWidget()->addView(b.url, b.title, Qz::NT_NotSelectedTab);
}
//foreach (const Bookmark &b, m_bookmarks->getFolderBookmarks(folder)) {
// p_QupZilla->tabWidget()->addView(b.url, b.title, Qz::NT_NotSelectedTab);
//}
}
void BookmarksToolbar::showMostVisited()
@ -369,187 +360,6 @@ int BookmarksToolbar::indexOfLastBookmark()
return 0;
}
void BookmarksToolbar::subfolderAdded(const QString &name)
{
ToolButton* b = new ToolButton(this);
b->setPopupMode(QToolButton::InstantPopup);
b->setToolButtonStyle(m_toolButtonStyle);
b->setIcon(style()->standardIcon(QStyle::SP_DirIcon));
b->setText(name);
connect(b, SIGNAL(middleMouseClicked()), this, SLOT(loadFolderBookmarksInTabs()));
Menu* menu = new Menu(name);
b->setMenu(menu);
connect(menu, SIGNAL(aboutToShow()), this, SLOT(aboutToShowFolderMenu()));
m_layout->insertWidget(m_layout->count() - 2, b);
}
void BookmarksToolbar::folderDeleted(const QString &name)
{
int index = indexOfLastBookmark();
for (int i = index; i < m_layout->count(); i++) {
ToolButton* button = qobject_cast<ToolButton*>(m_layout->itemAt(i)->widget());
if (!button) {
continue;
}
if (button->text() == name) {
delete button;
return;
}
}
}
void BookmarksToolbar::folderRenamed(const QString &before, const QString &after)
{
int index = indexOfLastBookmark();
for (int i = index; i < m_layout->count(); i++) {
ToolButton* button = qobject_cast<ToolButton*>(m_layout->itemAt(i)->widget());
if (!button) {
continue;
}
if (button->text() == before) {
button->setText(after);
button->menu()->setTitle(after);
return;
}
}
}
void BookmarksToolbar::changeBookmarkParent(const QString &name, const QByteArray &imageData, int id,
const QUrl &url, const QString &oldParent, const QString &newParent)
{
if (oldParent != _bookmarksToolbar && newParent != _bookmarksToolbar) {
return;
}
bool itemIsAboutToRemove = (newParent != _bookmarksToolbar);
Bookmark bookmark;
bookmark.id = id;
bookmark.url = url;
bookmark.title = name;
bookmark.folder = QLatin1String("bookmarksToolbar");
bookmark.image = QImage::fromData(imageData);
bookmark.inSubfolder = false;
if (itemIsAboutToRemove) {
removeBookmark(bookmark);
}
else {
addBookmark(bookmark);
}
}
void BookmarksToolbar::changeFolderParent(const QString &name, bool isSubfolder)
{
if (!isSubfolder) {
folderDeleted(name);
}
else {
subfolderAdded(name);
}
}
void BookmarksToolbar::addBookmark(const Bookmarks::Bookmark &bookmark)
{
if (bookmark.folder != QLatin1String("bookmarksToolbar")) {
return;
}
QString title = bookmark.title;
if (title.length() > 15) {
title.truncate(13);
title += "..";
}
QVariant v;
v.setValue<Bookmark>(bookmark);
ToolButton* button = new ToolButton(this);
button->setText(title);
button->setData(v);
button->setIcon(qIconProvider->iconFromImage(bookmark.image));
button->setToolButtonStyle(m_toolButtonStyle);
button->setToolTip(bookmark.url.toEncoded());
button->setAutoRaise(true);
button->setContextMenuPolicy(Qt::CustomContextMenu);
connect(button, SIGNAL(clicked()), this, SLOT(loadClickedBookmark()));
connect(button, SIGNAL(middleMouseClicked()), this, SLOT(loadClickedBookmarkInNewTab()));
connect(button, SIGNAL(controlClicked()), this, SLOT(loadClickedBookmarkInNewTab()));
connect(button, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showBookmarkContextMenu(QPoint)));
int indexForBookmark = indexOfLastBookmark();
m_layout->insertWidget(indexForBookmark, button);
QSqlQuery query;
query.prepare("UPDATE bookmarks SET position=? WHERE id=?");
query.addBindValue(indexForBookmark);
query.addBindValue(bookmark.id);
mApp->dbWriter()->executeQuery(query);
}
void BookmarksToolbar::removeBookmark(const Bookmarks::Bookmark &bookmark)
{
for (int i = 0; i < m_layout->count(); i++) {
ToolButton* button = qobject_cast<ToolButton*>(m_layout->itemAt(i)->widget());
if (!button) {
continue;
}
Bookmark book = button->data().value<Bookmark>();
if (book == bookmark) {
delete button;
return;
}
}
}
void BookmarksToolbar::bookmarkEdited(const Bookmarks::Bookmark &before, const Bookmarks::Bookmark &after)
{
if (before.folder == QLatin1String("bookmarksToolbar") && after.folder != QLatin1String("bookmarksToolbar")) {
// Editing from toolbar folder to other folder -> Remove bookmark
removeBookmark(before);
}
else if (before.folder != QLatin1String("bookmarksToolbar") && after.folder == QLatin1String("bookmarksToolbar")) {
// Editing from other folder to toolbar folder -> Add bookmark
addBookmark(after);
}
else {
// Editing bookmark already in toolbar
for (int i = 0; i < m_layout->count(); i++) {
ToolButton* button = qobject_cast<ToolButton*>(m_layout->itemAt(i)->widget());
if (!button) {
continue;
}
Bookmark book = button->data().value<Bookmark>();
if (book == before) {
QString title = after.title;
if (title.length() > 15) {
title.truncate(13);
title += "..";
}
QVariant v;
v.setValue<Bookmark>(after);
button->setText(title);
button->setData(v);
button->setIcon(qIconProvider->iconFromImage(after.image));
button->setToolTip(after.url.toEncoded());
return;
}
}
}
}
void BookmarksToolbar::refreshBookmarks()
{
QSqlQuery query;
@ -627,21 +437,21 @@ void BookmarksToolbar::aboutToShowFolderMenu()
}
menu->clear();
QString folder = menu->title();
//QString folder = menu->title();
foreach (const Bookmark &b, m_bookmarks->getFolderBookmarks(folder)) {
QString title = b.title;
if (title.length() > 40) {
title.truncate(40);
title += "..";
}
//foreach (const Bookmark &b, m_bookmarks->getFolderBookmarks(folder)) {
// QString title = b.title;
// if (title.length() > 40) {
// title.truncate(40);
// title += "..";
// }
Action* act = new Action(qIconProvider->iconFromImage(b.image), title);
act->setData(b.url);
connect(act, SIGNAL(triggered()), p_QupZilla, SLOT(loadActionUrl()));
connect(act, SIGNAL(ctrlTriggered()), p_QupZilla, SLOT(loadActionUrlInNewNotSelectedTab()));
menu->addAction(act);
}
// Action* act = new Action(qIconProvider->iconFromImage(b.image), title);
// act->setData(b.url);
// connect(act, SIGNAL(triggered()), p_QupZilla, SLOT(loadActionUrl()));
// connect(act, SIGNAL(ctrlTriggered()), p_QupZilla, SLOT(loadActionUrlInNewNotSelectedTab()));
// menu->addAction(act);
//}
if (menu->isEmpty()) {
menu->addAction(tr("Empty"))->setEnabled(false);
@ -661,7 +471,7 @@ void BookmarksToolbar::dropEvent(QDropEvent* e)
QUrl url = mime->urls().at(0);
QIcon icon = qIconProvider->iconFromImage(qvariant_cast<QImage>(mime->imageData()));
m_bookmarks->saveBookmark(url, title, icon, "bookmarksToolbar");
//m_bookmarks->saveBookmark(url, title, icon, "bookmarksToolbar");
}
void BookmarksToolbar::dragEnterEvent(QDragEnterEvent* e)

View File

@ -61,17 +61,6 @@ private slots:
void hidePanel();
void toggleShowOnlyIcons();
void addBookmark(const Bookmarks::Bookmark &bookmark);
void removeBookmark(const Bookmarks::Bookmark &bookmark);
void bookmarkEdited(const Bookmarks::Bookmark &before, const Bookmarks::Bookmark &after);
void subfolderAdded(const QString &name);
void folderDeleted(const QString &name);
void folderRenamed(const QString &before, const QString &after);
void changeBookmarkParent(const QString &name, const QByteArray &imageData, int id,
const QUrl &url, const QString &oldParent, const QString &newParent);
void changeFolderParent(const QString &name, bool isSubfolder);
private:
void dropEvent(QDropEvent* e);
void dragEnterEvent(QDragEnterEvent* e);

View File

@ -224,6 +224,7 @@ void BookmarksTools::openBookmark(QupZilla* window, BookmarkItem* item)
return;
}
item->setVisitCount(item->visitCount() + 1);
window->loadAddress(item->url());
}
@ -236,10 +237,12 @@ void BookmarksTools::openBookmarkInNewTab(QupZilla* window, BookmarkItem* item)
return;
}
item->setVisitCount(item->visitCount() + 1);
window->tabWidget()->addView(item->url(), item->title(), qzSettings->newTabPosition);
}
void BookmarksTools::openBookmarkInNewWindow(BookmarkItem* item)
{
item->setVisitCount(item->visitCount() + 1);
mApp->makeNewWindow(Qz::BW_NewWindow, item->url());
}

View File

@ -288,6 +288,7 @@ void BookmarksImportDialog::setFile()
void BookmarksImportDialog::addExportedBookmarks()
{
#if 0
QApplication::setOverrideCursor(Qt::WaitCursor);
Bookmarks* model = mApp->bookmarks();
@ -301,6 +302,7 @@ void BookmarksImportDialog::addExportedBookmarks()
db.commit();
QApplication::restoreOverrideCursor();
#endif
}
void BookmarksImportDialog::setupBrowser(Browser browser)

View File

@ -16,9 +16,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "locationcompletermodel.h"
#include "iconprovider.h"
#include "qzsettings.h"
#include "mainapplication.h"
#include "iconprovider.h"
#include "bookmarkitem.h"
#include "bookmarks.h"
#include "qzsettings.h"
#include "qupzilla.h"
#include "tabwidget.h"
@ -58,32 +60,31 @@ void LocationCompleterModel::refreshCompletions(const QString &string)
QList<QUrl> urlList;
QList<QStandardItem*> itemList;
// TODO: Those 2 SQL queries can be merged with UNION
if (showType == HistoryAndBookmarks || showType == Bookmarks) {
QSqlQuery query = createQuery(string, "history.count DESC", urlList, limit, true);
query.exec();
QList<BookmarkItem*> bookmarks = mApp->bookmarks()->searchBookmarks(string);
foreach (BookmarkItem* bookmark, bookmarks) {
Q_ASSERT(bookmark->isUrl());
while (query.next()) {
QStandardItem* item = new QStandardItem();
const QUrl url = query.value(1).toUrl();
item->setIcon(qIconProvider->iconFromImage(QImage::fromData(query.value(4).toByteArray())));
item->setText(url.toEncoded());
item->setData(query.value(0), IdRole);
item->setData(query.value(2), TitleRole);
item->setData(query.value(3), CountRole);
item->setIcon(_iconForUrl(bookmark->url()));
item->setText(bookmark->url().toEncoded());
item->setData(-1, IdRole);
item->setData(bookmark->title(), TitleRole);
item->setData(bookmark->visitCount(), CountRole);
item->setData(QVariant(true), BookmarkRole);
item->setData(string, SearchStringRole);
if (qzSettings->showSwitchTab) {
item->setData(QVariant::fromValue<TabPosition>(tabPositionForUrl(url)), TabPositionRole);
item->setData(QVariant::fromValue<TabPosition>(tabPositionForUrl(bookmark->url())), TabPositionRole);
}
urlList.append(url);
urlList.append(bookmark->url());
itemList.append(item);
}
limit -= query.size();
limit -= itemList.count();
}
if (showType == HistoryAndBookmarks || showType == History) {

View File

@ -63,6 +63,7 @@ private:
QSqlQuery createQuery(const QString &searchString, const QString &orderBy, const QList<QUrl> &alreadyFound,
int limit, bool bookmarks = false, bool exactMatch = false);
TabPosition tabPositionForUrl(const QUrl &url) const;
TabPosition tabPositionForEncodedUrl(const QString &encodedUrl) const;
void refreshTabPositions();