BookmarkHandler.cpp 4.81 KB
Newer Older
1
/*  This file was part of the KDE libraries
2

3
    Copyright 2002 Carsten Pfeiffer <pfeiffer@kde.org>
Jekyll Wu's avatar
Jekyll Wu committed
4
    Copyright 2007-2008 Robert Knight <robertknight@gmail.com>
Stephan Binner's avatar
Stephan Binner committed
5 6 7

    library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
8 9
    License as published by the Free Software Foundation, version 2
    or ( at your option ), any later version.
Stephan Binner's avatar
Stephan Binner committed
10 11 12 13 14 15 16 17

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
Dirk Mueller's avatar
Dirk Mueller committed
18
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Dirk Mueller's avatar
Dirk Mueller committed
19
    Boston, MA 02110-1301, USA.
Stephan Binner's avatar
Stephan Binner committed
20 21
*/

22
// Born as kdelibs/kio/kfile/kfilebookmarkhandler.cpp
23

24 25 26
// Own
#include "BookmarkHandler.h"

27
// Qt
28
#include <QtCore/QFileInfo>
29 30
#include <QtCore/QStandardPaths>
#include <QtCore/QDir>
31 32

// KDE
33
#include <KShell>
34
#include <KBookmarkMenu>
35
#include <KBookmarkOwner>
Alex Richardson's avatar
Alex Richardson committed
36
#include <KLocalizedString>
37 38

// Konsole
39
#include "ViewProperties.h"
40

41 42
using namespace Konsole;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
43
BookmarkHandler::BookmarkHandler(KActionCollection* collection,
Alex Richardson's avatar
Alex Richardson committed
44
                                 QMenu* menu,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
45 46
                                 bool toplevel,
                                 QObject* parent)
47 48 49 50 51 52 53 54
    : QObject(parent)
    , KBookmarkOwner()
    , _menu(menu)
    , _bookmarkMenu(0)
    , _file(QString())
    , _toplevel(toplevel)
    , _activeView(0)
    , _views(QList<ViewProperties*>())
55
{
56
    setObjectName(QStringLiteral("BookmarkHandler"));
Thorsten Roeder's avatar
Thorsten Roeder committed
57

58 59 60 61 62 63 64
    _file = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("konsole/bookmarks.xml"));

    if (_file.isEmpty()) {
        _file = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/konsole");
        QDir().mkpath(_file);
        _file += QStringLiteral("/bookmarks.xml");
    }
65

66
    KBookmarkManager* manager = KBookmarkManager::managerForFile(_file, QStringLiteral("konsole"));
67

Kurt Hindenburg's avatar
Kurt Hindenburg committed
68
    manager->setUpdate(true);
Stephan Kulow's avatar
Stephan Kulow committed
69

Jekyll Wu's avatar
Jekyll Wu committed
70
    if (toplevel)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
71
        _bookmarkMenu = new KBookmarkMenu(manager, this, _menu, collection);
Jekyll Wu's avatar
Jekyll Wu committed
72
    else
Kurt Hindenburg's avatar
Kurt Hindenburg committed
73
        _bookmarkMenu = new KBookmarkMenu(manager, this, _menu, 0);
74 75
}

76
BookmarkHandler::~BookmarkHandler()
77
{
78
    delete _bookmarkMenu;
79 80
}

Kurt Hindenburg's avatar
Kurt Hindenburg committed
81
void BookmarkHandler::openBookmark(const KBookmark& bm, Qt::MouseButtons, Qt::KeyboardModifiers)
82
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
83
    emit openUrl(bm.url());
84
}
Kurt Hindenburg's avatar
Kurt Hindenburg committed
85
void BookmarkHandler::openFolderinTabs(const KBookmarkGroup& group)
86
{
87
    emit openUrls(group.groupUrlList());
88
}
Kurt Hindenburg's avatar
Kurt Hindenburg committed
89
bool BookmarkHandler::enableOption(BookmarkOption option) const
90
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
91
    if (option == ShowAddBookmark || option == ShowEditBookmark)
92
        return _toplevel;
Daniel Teske's avatar
Daniel Teske committed
93 94
    else
        return KBookmarkOwner::enableOption(option);
95 96
}

97
QUrl BookmarkHandler::currentUrl() const
98
{
99
    return urlForView(_activeView);
100 101
}

102
QUrl BookmarkHandler::urlForView(ViewProperties* view) const
103
{
104
    if (view != nullptr)
105
        return QUrl(view->url());
106
    else
107
        return QUrl();
108 109
}

110
QString BookmarkHandler::currentTitle() const
111
{
112
    return titleForView(_activeView);
113 114 115 116
}

QString BookmarkHandler::titleForView(ViewProperties* view) const
{
117
    const QUrl& url = view != nullptr ? view->url() : QUrl();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
118
    if (url.isLocalFile()) {
119
        QString path = url.path();
120

121 122
        path = KShell::tildeExpand(path);
        path = QFileInfo(path).baseName();
123

124
        return path;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
125 126 127
    } else if (!url.host().isEmpty()) {
        if (!url.userName().isEmpty())
            return i18nc("@item:inmenu The user's name and host they are connected to via ssh", "%1 on %2", url.userName(), url.host());
128
        else
Kurt Hindenburg's avatar
Kurt Hindenburg committed
129
            return i18nc("@item:inmenu The host the user is connected to via ssh", "%1", url.host());
130
    }
131

Kurt Hindenburg's avatar
Kurt Hindenburg committed
132
    return url.toDisplayString();
133 134
}

135 136 137 138 139 140 141
QString BookmarkHandler::currentIcon() const
{
    return iconForView(_activeView);
}

QString BookmarkHandler::iconForView(ViewProperties* view) const
{
142
    if (view != nullptr)
143 144 145 146 147
        return view->icon().name();
    else
        return QString();
}

148 149 150 151 152
bool BookmarkHandler::supportsTabs() const
{
    return true;
}

153
QList<KBookmarkOwner::FutureBookmark> BookmarkHandler::currentBookmarkList() const
154
{
155
    QList<KBookmarkOwner::FutureBookmark> list;
156
    list.reserve(_views.size());
157

Kurt Hindenburg's avatar
Kurt Hindenburg committed
158
    foreach(ViewProperties* view, _views) {
159
        list << KBookmarkOwner::FutureBookmark(titleForView(view) , urlForView(view), iconForView(view));
160 161 162 163 164
    }

    return list;
}

Jekyll Wu's avatar
Jekyll Wu committed
165
void BookmarkHandler::setViews(const QList<ViewProperties*>& views)
166
{
167
    _views = views;
168 169 170
}
QList<ViewProperties*> BookmarkHandler::views() const
{
171
    return _views;
172
}
Kurt Hindenburg's avatar
Kurt Hindenburg committed
173
void BookmarkHandler::setActiveView(ViewProperties* view)
174
{
175
    _activeView = view;
176
}
177
ViewProperties* BookmarkHandler::activeView() const
178
{
179
    return _activeView;
180 181
}