Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / konsole / src / BookmarkHandler.cpp
blob971570fac63b88197268d8154b08e97c13f5de8a
1 /* This file was part of the KDE libraries
3 Copyright 2002 Carsten Pfeiffer <pfeiffer@kde.org>
4 Copyright 2007-2008 Robert Knight <robertknight@gmail.com>
6 library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation, version 2
9 or ( at your option ), any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
22 // Born as kdelibs/kio/kfile/kfilebookmarkhandler.characterpp
24 // Own
25 #include "BookmarkHandler.h"
27 // Qt
28 #include <QtCore/QFile>
29 #include <QtCore/QFileInfo>
31 // KDE
32 #include <kshell.h>
34 #include <KBookmarkMenu>
35 #include <KDebug>
36 #include <KMenu>
37 #include <KStandardDirs>
39 // Konsole
40 #include "ViewProperties.h"
42 using namespace Konsole;
44 BookmarkHandler::BookmarkHandler( KActionCollection* collection,
45 KMenu* menu,
46 bool toplevel ,
47 QObject* parent )
48 : QObject( parent ),
49 KBookmarkOwner(),
50 m_toplevel(toplevel),
51 m_activeView(0)
53 setObjectName( "BookmarkHandler" );
55 m_menu = menu;
57 QString new_bm_file = KStandardDirs::locateLocal( "data", "konsole/bookmarks.xml" );
59 m_file = KStandardDirs::locate( "data", "konsole/bookmarks.xml" );
60 if ( m_file.isEmpty() )
61 m_file = KStandardDirs::locateLocal( "data", "konsole/bookmarks.xml" );
63 KBookmarkManager *manager = KBookmarkManager::managerForFile( m_file, "konsole" );
65 manager->setUpdate( true );
67 if (toplevel) {
68 m_bookmarkMenu = new KBookmarkMenu( manager, this, m_menu,
69 collection );
70 } else {
71 m_bookmarkMenu = new KBookmarkMenu( manager, this, m_menu,
72 NULL);
76 BookmarkHandler::~BookmarkHandler()
78 delete m_bookmarkMenu;
81 void BookmarkHandler::openBookmark( const KBookmark & bm, Qt::MouseButtons, Qt::KeyboardModifiers )
83 emit openUrl( bm.url() );
85 void BookmarkHandler::openFolderinTabs( const KBookmarkGroup& group )
87 emit openUrls(group.groupUrlList());
89 bool BookmarkHandler::enableOption(BookmarkOption option ) const
91 if(option == ShowAddBookmark || option == ShowEditBookmark)
92 return m_toplevel;
93 else
94 return KBookmarkOwner::enableOption(option);
97 QString BookmarkHandler::currentUrl() const
99 return urlForView(m_activeView);
102 QString BookmarkHandler::urlForView(ViewProperties* view) const
104 if ( view )
106 return view->url().prettyUrl();
108 else
110 return QString();
114 QString BookmarkHandler::currentTitle() const
116 return titleForView(m_activeView);
119 QString BookmarkHandler::titleForView(ViewProperties* view) const
121 const KUrl &u = view ? view->url() : KUrl();
122 if (u.isLocalFile())
124 QString path = u.path();
125 path = KShell::tildeExpand(path);
127 path = QFileInfo(path).baseName();
129 return path;
131 else if ( u.hasHost() )
133 if ( u.hasUser() )
134 return i18n("%1 on %2",u.user(),u.host());
135 else
136 return i18n("%1",u.host());
138 return u.prettyUrl();
141 bool BookmarkHandler::supportsTabs() const
143 return true;
146 QList<QPair<QString,QString> > BookmarkHandler::currentBookmarkList() const
148 QList<QPair<QString,QString> > list;
150 QListIterator<ViewProperties*> iter( m_views );
152 while ( iter.hasNext() )
154 ViewProperties* next = iter.next();
155 list << QPair<QString,QString>(titleForView(next) , urlForView(next));
158 return list;
161 void BookmarkHandler::setViews(const QList<ViewProperties*>& views)
163 m_views = views;
165 QList<ViewProperties*> BookmarkHandler::views() const
167 return m_views;
169 void BookmarkHandler::setActiveView( ViewProperties* view )
171 m_activeView = view;
173 ViewProperties* BookmarkHandler::activeView() const
175 return m_activeView;
178 #include "BookmarkHandler.moc"