File indexing completed on 2024-09-15 08:03:02
0001 /* This file was part of the KDE libraries 0002 0003 SPDX-FileCopyrightText: 2002 Carsten Pfeiffer <pfeiffer@kde.org> 0004 SPDX-FileCopyrightText: 2007-2008 Robert Knight <robertknight@gmail.com> 0005 0006 SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 // Born as kdelibs/kio/kfile/kfilebookmarkhandler.cpp 0010 0011 // Own 0012 #include "BookmarkHandler.h" 0013 0014 // Qt 0015 #include <QDir> 0016 #include <QFileInfo> 0017 #include <QStandardPaths> 0018 0019 // KDE 0020 #include <KLocalizedString> 0021 #include <KShell> 0022 0023 // Konsole 0024 #include "BookmarkMenu.h" 0025 0026 using namespace Konsole; 0027 0028 BookmarkHandler::BookmarkHandler(KActionCollection *collection, QMenu *menu, bool toplevel, QObject *parent) 0029 : QObject(parent) 0030 , KBookmarkOwner() 0031 , _menu(menu) 0032 , _file(QString()) 0033 , _toplevel(toplevel) 0034 , _activeView(nullptr) 0035 , _views(QList<ViewProperties *>()) 0036 { 0037 setObjectName(QStringLiteral("BookmarkHandler")); 0038 0039 _file = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("konsole/bookmarks.xml")); 0040 0041 if (_file.isEmpty()) { 0042 _file = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/konsole"); 0043 QDir().mkpath(_file); 0044 _file += QStringLiteral("/bookmarks.xml"); 0045 } 0046 0047 KBookmarkManager *manager = new KBookmarkManager(_file, this); 0048 0049 // This constructor is only called with toplevel as true; regardless the 0050 // fourth argument can not be nullptr as it will crash in BookmarkMenu 0051 // TODO: remove the toplevel boolean argument in API 0052 auto *bookmarkMenu = new BookmarkMenu(manager, this, _menu, collection); 0053 bookmarkMenu->setParent(this); 0054 } 0055 0056 BookmarkHandler::~BookmarkHandler() = default; 0057 0058 void BookmarkHandler::openBookmark(const KBookmark &bm, Qt::MouseButtons, Qt::KeyboardModifiers) 0059 { 0060 Q_EMIT openUrl(bm.url()); 0061 } 0062 0063 void BookmarkHandler::openFolderinTabs(const KBookmarkGroup &group) 0064 { 0065 Q_EMIT openUrls(group.groupUrlList()); 0066 } 0067 0068 bool BookmarkHandler::enableOption(BookmarkOption option) const 0069 { 0070 if (option == ShowAddBookmark || option == ShowEditBookmark) { 0071 return _toplevel; 0072 } 0073 return KBookmarkOwner::enableOption(option); 0074 } 0075 0076 QUrl BookmarkHandler::currentUrl() const 0077 { 0078 return urlForView(_activeView); 0079 } 0080 0081 QUrl BookmarkHandler::urlForView(ViewProperties *view) const 0082 { 0083 if (view != nullptr) { 0084 return view->url(); 0085 } 0086 return {}; 0087 } 0088 0089 QString BookmarkHandler::currentTitle() const 0090 { 0091 return titleForView(_activeView); 0092 } 0093 0094 QString BookmarkHandler::titleForView(ViewProperties *view) const 0095 { 0096 const QUrl &url = view != nullptr ? view->url() : QUrl(); 0097 if (url.isLocalFile()) { 0098 QString path = url.path(); 0099 0100 path = KShell::tildeExpand(path); 0101 path = QFileInfo(path).completeBaseName(); 0102 0103 return path; 0104 } 0105 if (!url.host().isEmpty()) { 0106 if (!url.userName().isEmpty()) { 0107 return i18nc("@item:inmenu The user's name and host they are connected to via ssh", "%1 on %2", url.userName(), url.host()); 0108 } 0109 return i18nc("@item:inmenu The host the user is connected to via ssh", "%1", url.host()); 0110 } 0111 0112 return url.toDisplayString(); 0113 } 0114 0115 QString BookmarkHandler::currentIcon() const 0116 { 0117 return iconForView(_activeView); 0118 } 0119 0120 QString BookmarkHandler::iconForView(ViewProperties *view) const 0121 { 0122 if (view != nullptr) { 0123 return view->icon().name(); 0124 } 0125 return {}; 0126 } 0127 0128 QList<KBookmarkOwner::FutureBookmark> BookmarkHandler::currentBookmarkList() const 0129 { 0130 QList<KBookmarkOwner::FutureBookmark> list; 0131 list.reserve(_views.size()); 0132 0133 const QList<ViewProperties *> views = _views; 0134 for (ViewProperties *view : views) { 0135 list << KBookmarkOwner::FutureBookmark(titleForView(view), urlForView(view), iconForView(view)); 0136 } 0137 0138 return list; 0139 } 0140 0141 bool BookmarkHandler::supportsTabs() const 0142 { 0143 return true; 0144 } 0145 0146 void BookmarkHandler::setViews(const QList<ViewProperties *> &views) 0147 { 0148 _views = views; 0149 } 0150 0151 QList<ViewProperties *> BookmarkHandler::views() const 0152 { 0153 return _views; 0154 } 0155 0156 void BookmarkHandler::setActiveView(ViewProperties *view) 0157 { 0158 _activeView = view; 0159 } 0160 0161 ViewProperties *BookmarkHandler::activeView() const 0162 { 0163 return _activeView; 0164 } 0165 0166 #include "moc_BookmarkHandler.cpp"