]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinbookmarkhandler.cpp
Fix selection rect after porting from QFontMetrics::width()
[dolphin.git] / src / dolphinbookmarkhandler.cpp
1 /***************************************************************************
2 * Copyright (C) 2019 by David Hallas <david@davidhallas.dk> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "dolphinbookmarkhandler.h"
21 #include "dolphinmainwindow.h"
22 #include "dolphinviewcontainer.h"
23 #include "global.h"
24 #include <KActionCollection>
25 #include <KBookmarkMenu>
26 #include <KIO/Global>
27 #include <QDebug>
28 #include <QDir>
29 #include <QStandardPaths>
30
31 DolphinBookmarkHandler::DolphinBookmarkHandler(DolphinMainWindow *mainWindow,
32 KActionCollection* collection,
33 QMenu* menu,
34 QObject* parent) :
35 QObject(parent),
36 m_mainWindow(mainWindow)
37 {
38 QString bookmarksFile = QStandardPaths::locate(QStandardPaths::GenericDataLocation,
39 QStringLiteral("kfile/bookmarks.xml"));
40 if (bookmarksFile.isEmpty()) {
41 QString genericDataLocation = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
42 if (genericDataLocation.isEmpty()) {
43 qWarning() << "GenericDataLocation is empty! Bookmarks will not be saved correctly.";
44 }
45 bookmarksFile = QStringLiteral("%1/dolphin").arg(genericDataLocation);
46 QDir().mkpath(bookmarksFile);
47 bookmarksFile += QLatin1String("/bookmarks.xml");
48 }
49 m_bookmarkManager = KBookmarkManager::managerForFile(bookmarksFile, QStringLiteral("dolphin"));
50 m_bookmarkManager->setUpdate(true);
51 m_bookmarkMenu.reset(new KBookmarkMenu(m_bookmarkManager, this, menu));
52
53 collection->addAction(QStringLiteral("add_bookmark"), m_bookmarkMenu->addBookmarkAction());
54 collection->addAction(QStringLiteral("edit_bookmarks"), m_bookmarkMenu->editBookmarksAction());
55 collection->addAction(QStringLiteral("add_bookmarks_list"), m_bookmarkMenu->bookmarkTabsAsFolderAction());
56 }
57
58 DolphinBookmarkHandler::~DolphinBookmarkHandler()
59 {
60 }
61
62 QString DolphinBookmarkHandler::currentTitle() const
63 {
64 return title(m_mainWindow->activeViewContainer());
65 }
66
67 QUrl DolphinBookmarkHandler::currentUrl() const
68 {
69 return url(m_mainWindow->activeViewContainer());
70 }
71
72 QString DolphinBookmarkHandler::currentIcon() const
73 {
74 return icon(m_mainWindow->activeViewContainer());
75 }
76
77 bool DolphinBookmarkHandler::supportsTabs() const
78 {
79 return true;
80 }
81
82 QList<KBookmarkOwner::FutureBookmark> DolphinBookmarkHandler::currentBookmarkList() const
83 {
84 const auto viewContainers = m_mainWindow->viewContainers();
85 QList<FutureBookmark> bookmarks;
86 bookmarks.reserve(viewContainers.size());
87 for (const auto viewContainer : viewContainers) {
88 bookmarks << FutureBookmark(title(viewContainer), url(viewContainer), icon(viewContainer));
89 }
90 return bookmarks;
91 }
92
93 bool DolphinBookmarkHandler::enableOption(KBookmarkOwner::BookmarkOption option) const
94 {
95 switch (option) {
96 case BookmarkOption::ShowAddBookmark: return true;
97 case BookmarkOption::ShowEditBookmark: return true;
98 }
99 return false;
100 }
101
102 void DolphinBookmarkHandler::openBookmark(const KBookmark& bookmark, Qt::MouseButtons, Qt::KeyboardModifiers)
103 {
104 m_mainWindow->changeUrl(bookmark.url());
105 }
106
107 void DolphinBookmarkHandler::openFolderinTabs(const KBookmarkGroup& bookmarkGroup)
108 {
109 m_mainWindow->openDirectories(bookmarkGroup.groupUrlList(), false);
110 }
111
112 void DolphinBookmarkHandler::openInNewTab(const KBookmark& bookmark)
113 {
114 m_mainWindow->openNewTabAfterCurrentTab(bookmark.url());
115 }
116
117 void DolphinBookmarkHandler::openInNewWindow(const KBookmark& bookmark)
118 {
119 Dolphin::openNewWindow({bookmark.url()}, m_mainWindow);
120 }
121
122 QString DolphinBookmarkHandler::title(DolphinViewContainer* viewContainer)
123 {
124 return viewContainer->caption();
125 }
126
127 QUrl DolphinBookmarkHandler::url(DolphinViewContainer* viewContainer)
128 {
129 return viewContainer->url();
130 }
131
132 QString DolphinBookmarkHandler::icon(DolphinViewContainer* viewContainer)
133 {
134 return KIO::iconNameForUrl(viewContainer->url());
135 }