~neon/kdeplasma-addons/trunk

« back to all changes in this revision

Viewing changes to applets/bookmarks/bookmarksplasmoid.cpp

  • Committer: asouza
  • Date: 2011-02-01 19:41:58 UTC
  • Revision ID: svn-v4:283d02a7-25f6-0310-bc7c-ecb5cbfe19da:trunk/KDE/kdeplasma-addons:1218275
Move kdeplasma-addons to git

- You can find information about the project in the link below:

https://projects.kde.org/projects/kde/kdeplasma-addons/repository

- And you can clone the repository using:

git clone git://anongit.kde.org/kdeplasma-addons


Thanks eean and all the sysadmins for the help.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    This file is part of the Bookmarks plasmoid, part of the KDE project.
3
 
 
4
 
    Copyright 2009-2010 Friedrich W. H. Kossebau <kossebau@kde.org>
5
 
 
6
 
    This program is free software; you can redistribute it and/or
7
 
    modify it under the terms of the GNU General Public License as
8
 
    published by the Free Software Foundation; either version 2 of
9
 
    the License or (at your option) version 3 or any later version
10
 
    accepted by the membership of KDE e.V. (or its successor approved
11
 
    by the membership of KDE e.V.), which shall act as a proxy
12
 
    defined in Section 14 of version 3 of the license.
13
 
 
14
 
    This program is distributed in the hope that it will be useful,
15
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 
    GNU General Public License for more details.
18
 
 
19
 
    You should have received a copy of the GNU General Public License
20
 
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 
*/
22
 
 
23
 
#include "bookmarksplasmoid.h"
24
 
 
25
 
// Plasmoid
26
 
#include "generalconfigeditor.h"
27
 
#include "bookmarkowner.h"
28
 
// Plasma
29
 
#include <Plasma/IconWidget>
30
 
#include <Plasma/ToolTipManager>
31
 
// KDE
32
 
#include <KConfigDialog>
33
 
#include <KStandardAction>
34
 
#include <KIconLoader>
35
 
#include <KIcon>
36
 
#include <KBookmarkMenu>
37
 
#include <KBookmarkGroup>
38
 
// Qt
39
 
#include <QtGui/QPainter>
40
 
#include <QtGui/QGraphicsLinearLayout>
41
 
 
42
 
 
43
 
namespace Plasma
44
 
{
45
 
 
46
 
static const char bookmarkFolderAddressConfigKey[] = "BookmarkFolderAddress";
47
 
 
48
 
 
49
 
BookmarksPlasmoid::BookmarksPlasmoid(QObject* parent, const QVariantList& args)
50
 
  : Applet(parent, args),
51
 
    mIcon(0),
52
 
    mBookmarkManager(0),
53
 
    mBookmarkMenu(0),
54
 
    mBookmarkOwner(0)
55
 
{
56
 
}
57
 
 
58
 
 
59
 
void BookmarksPlasmoid::init()
60
 
{
61
 
    mBookmarkManager = KBookmarkManager::userBookmarksManager();
62
 
    mBookmarkManager->setEditorOptions(name(), true);
63
 
    connect(mBookmarkManager, SIGNAL(changed(const QString&, const QString&)), SLOT(onBookmarksChanged(const QString&)));
64
 
 
65
 
    // general
66
 
    setHasConfigurationInterface(true);
67
 
    connect(this, SIGNAL(activate()), SLOT(toggleMenu()));
68
 
    Plasma::ToolTipManager::self()->registerWidget(this);
69
 
 
70
 
    // context menu
71
 
    KAction* editorOpener = KStandardAction::editBookmarks(this, SLOT(editBookmarks()), this);
72
 
    mContextualActions.append(editorOpener);
73
 
 
74
 
    // view
75
 
    setAspectRatioMode(ConstrainedSquare);
76
 
    setBackgroundHints(NoBackground);
77
 
 
78
 
    QGraphicsLinearLayout* layout = new QGraphicsLinearLayout(this);
79
 
    layout->setContentsMargins(0, 0, 0, 0);
80
 
    layout->setSpacing(0);
81
 
 
82
 
    mIcon = new IconWidget(KIcon("bookmarks"),"",this);
83
 
    mIcon->setFlag(ItemIsMovable, false);
84
 
    connect(mIcon, SIGNAL(pressed(bool)), SLOT(toggleMenu(bool)));
85
 
    layout->addItem(mIcon);
86
 
 
87
 
 
88
 
    configChanged();
89
 
}
90
 
 
91
 
QList<QAction*> BookmarksPlasmoid::contextualActions()
92
 
{
93
 
    return mContextualActions;
94
 
}
95
 
 
96
 
void BookmarksPlasmoid::updateFolderData()
97
 
{
98
 
    const KBookmark bookmark = mBookmarkManager->findByAddress(mBookmarkFolderAddress);
99
 
 
100
 
    KBookmarkGroup bookmarkFolder =
101
 
        (bookmark.isNull() || ! bookmark.isGroup()) ? mBookmarkManager->root() : bookmark.toGroup();
102
 
 
103
 
    const bool isRoot = (! bookmarkFolder.hasParent());
104
 
 
105
 
    const QString iconName = isRoot ? QString::fromLatin1("bookmarks") : bookmarkFolder.icon();
106
 
    const QString folderName = isRoot ? i18nc("name of the container of all browser bookmarks",
107
 
                                              "Bookmarks") :
108
 
                                        bookmarkFolder.text();
109
 
    const QString comment = isRoot ? i18n("Quick access to your bookmarks.") : bookmarkFolder.description();
110
 
 
111
 
    // icon
112
 
    mIcon->setIcon(iconName);
113
 
    // tooltip
114
 
    Plasma::ToolTipContent toolTipContent(folderName, comment, KIcon(iconName));
115
 
    Plasma::ToolTipManager::self()->setContent(this, toolTipContent);
116
 
}
117
 
 
118
 
void BookmarksPlasmoid::toggleMenu(bool toggle)
119
 
{
120
 
    if (! toggle)
121
 
        return;
122
 
 
123
 
    Plasma::ToolTipManager::self()->hide(this);
124
 
    mIcon->setPressed();
125
 
 
126
 
    const bool isFirstTime = (mBookmarkOwner == 0);
127
 
    if (isFirstTime)
128
 
        mBookmarkOwner = new BookmarkOwner();
129
 
 
130
 
    delete mBookmarkMenu;
131
 
 
132
 
    KMenu* menu = new KMenu();
133
 
    menu->setAttribute(Qt::WA_DeleteOnClose);
134
 
    connect(menu, SIGNAL(aboutToHide()), mIcon, SLOT(setUnpressed()));
135
 
    // TODO: only renew if manager emits changed
136
 
    mBookmarkMenu = new KBookmarkMenu(mBookmarkManager, mBookmarkOwner, menu, mBookmarkFolderAddress);
137
 
 
138
 
    menu->popup(popupPosition(menu->size()));
139
 
    menu->move(popupPosition(menu->size()));
140
 
 
141
 
}
142
 
 
143
 
void BookmarksPlasmoid::toggleMenu()
144
 
{
145
 
    toggleMenu(true);
146
 
}
147
 
 
148
 
void BookmarksPlasmoid::createConfigurationInterface(KConfigDialog* parent)
149
 
{
150
 
    mGeneralConfigEditor = new GeneralConfigEditor(mBookmarkManager, parent);
151
 
    mGeneralConfigEditor->setBookmarkFolderAddress(mBookmarkFolderAddress);
152
 
    const QString pageName = i18nc("@title:tab name of settings page with general parameters","General");
153
 
    parent->addPage(mGeneralConfigEditor, pageName, icon());
154
 
    connect(parent, SIGNAL(applyClicked()), SLOT(applyConfigChanges()));
155
 
    connect(parent, SIGNAL(okClicked()), SLOT(applyConfigChanges()));
156
 
}
157
 
 
158
 
void BookmarksPlasmoid::applyConfigChanges()
159
 
{
160
 
    const QString& bookmarkFolderAddress = mGeneralConfigEditor->bookmarkFolderAddress();
161
 
 
162
 
    if (mBookmarkFolderAddress != bookmarkFolderAddress) {
163
 
        KConfigGroup configGroup = config();
164
 
        configGroup.writeEntry(bookmarkFolderAddressConfigKey, mBookmarkFolderAddress);
165
 
 
166
 
        emit configNeedsSaving();
167
 
    }
168
 
}
169
 
 
170
 
void BookmarksPlasmoid::configChanged()
171
 
{
172
 
    // read config
173
 
    KConfigGroup configGroup = config();
174
 
    const QString bookmarkFolderAddress = configGroup.readEntry(bookmarkFolderAddressConfigKey, mBookmarkFolderAddress);
175
 
 
176
 
    if (mBookmarkFolderAddress != bookmarkFolderAddress) {
177
 
        mBookmarkFolderAddress = bookmarkFolderAddress;
178
 
        updateFolderData();
179
 
    }
180
 
}
181
 
 
182
 
void BookmarksPlasmoid::editBookmarks()
183
 
{
184
 
    mBookmarkManager->slotEditBookmarksAtAddress(mBookmarkFolderAddress);
185
 
}
186
 
 
187
 
void BookmarksPlasmoid::onBookmarksChanged(const QString& address)
188
 
{
189
 
    Q_UNUSED(address);
190
 
 
191
 
    updateFolderData();
192
 
}
193
 
 
194
 
BookmarksPlasmoid::~BookmarksPlasmoid()
195
 
{
196
 
    delete mBookmarkMenu;
197
 
    delete mBookmarkOwner;
198
 
}
199
 
 
200
 
}