summaryrefslogtreecommitdiffstats
path: root/src/bookmarkssidebarpage.cpp
blob: 94a37eb0a2a43be1885a83668de4a95c32090b64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/***************************************************************************
 *   Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at>                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#include "bookmarkssidebarpage.h"

#include <tqlistbox.h>
#include <tqlayout.h>
#include <tqpainter.h>
#include <assert.h>
#include <tqpopupmenu.h>

#include <kbookmarkmanager.h>
#include <tdemessagebox.h>
#include <tdelocale.h>
#include <kurldrag.h>

#include "dolphinsettings.h"
#include "dolphin.h"
#include "dolphinview.h"
#include "editbookmarkdialog.h"

BookmarksSidebarPage::BookmarksSidebarPage(TQWidget* parent) :
    SidebarPage(parent)
{
    TQVBoxLayout* layout = new TQVBoxLayout(this);
    m_bookmarksList = new BookmarksListBox(this);
    m_bookmarksList->setPaletteBackgroundColor(colorGroup().background());

    layout->addWidget(m_bookmarksList);
    connect(m_bookmarksList, TQT_SIGNAL(mouseButtonClicked(int, TQListBoxItem*, const TQPoint&)),
            this, TQT_SLOT(slotMouseButtonClicked(int, TQListBoxItem*)));
    connect(m_bookmarksList, TQT_SIGNAL(contextMenuRequested(TQListBoxItem*, const TQPoint&)),
            this, TQT_SLOT(slotContextMenuRequested(TQListBoxItem*, const TQPoint&)));

    KBookmarkManager* manager = DolphinSettings::instance().bookmarkManager();
    connect(manager, TQT_SIGNAL(changed(const TQString&, const TQString&)),
            this, TQT_SLOT(updateBookmarks()));

    updateBookmarks();
}

BookmarksSidebarPage::~BookmarksSidebarPage()
{
}

void BookmarksSidebarPage::activeViewChanged()
{
    connectToActiveView();
}

void BookmarksSidebarPage::updateBookmarks()
{
    m_bookmarksList->clear();

    TDEIconLoader iconLoader;

    KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
    KBookmark bookmark = root.first();
    while (!bookmark.isNull()) {
        m_bookmarksList->insertItem( BookmarkItem::fromKbookmark(bookmark, iconLoader) );

        bookmark = root.next(bookmark);
    }

    connectToActiveView();
}

void BookmarksSidebarPage::slotMouseButtonClicked(int button, TQListBoxItem* item)
{
    if ((button != Qt::LeftButton) || (item == 0)) {
        return;
    }

    const int index = m_bookmarksList->index(item);
    KBookmark bookmark = DolphinSettings::instance().bookmark(index);
    Dolphin::mainWin().activeView()->setURL(bookmark.url());
}

void BookmarksSidebarPage::slotContextMenuRequested(TQListBoxItem* item,
                                                    const TQPoint& pos)
{
    const int insertID = 1;
    const int editID = 2;
    const int deleteID = 3;
    const int addID = 4;

    TQPopupMenu* popup = new TQPopupMenu();
    if (item == 0) {
        popup->insertItem(SmallIcon("document-new"), i18n("Add Bookmark..."), addID);
    }
    else {
        popup->insertItem(SmallIcon("document-new"), i18n("Insert Bookmark..."), insertID);
        popup->insertItem(SmallIcon("edit"), i18n("Edit..."), editID);
        popup->insertItem(SmallIcon("edit-delete"), i18n("Delete"), deleteID);
    }

    KBookmarkManager* manager = DolphinSettings::instance().bookmarkManager();
    KBookmarkGroup root = manager->root();
    const int index = m_bookmarksList->index(m_bookmarksList->selectedItem());

    const int result = popup->exec(pos);
    switch (result) {
        case insertID: {
            KBookmark newBookmark = EditBookmarkDialog::getBookmark(i18n("Insert Bookmark"),
                                                                    i18n("New bookmark"),
                                                                    KURL(),
                                                                    "bookmark");
            if (!newBookmark.isNull()) {
                root.addBookmark(manager, newBookmark);
                if (index > 0) {
                    KBookmark prevBookmark = DolphinSettings::instance().bookmark(index - 1);
                    root.moveItem(newBookmark, prevBookmark);
                }
                else {
                    // insert bookmark at first position (is a little bit tricky as KBookmarkGroup
                    // only allows to move items after existing items)
                    KBookmark firstBookmark = root.first();
                    root.moveItem(newBookmark, firstBookmark);
                    root.moveItem(firstBookmark, newBookmark);
                }
                manager->emitChanged(root);
            }
            break;
        }

        case editID: {
            KBookmark oldBookmark = DolphinSettings::instance().bookmark(index);
            KBookmark newBookmark = EditBookmarkDialog::getBookmark(i18n("Edit Bookmark"),
                                                                    oldBookmark.text(),
                                                                    oldBookmark.url(),
                                                                    oldBookmark.icon());
            if (!newBookmark.isNull()) {
                root.addBookmark(manager, newBookmark);
                root.moveItem(newBookmark, oldBookmark);
                root.deleteBookmark(oldBookmark);
                manager->emitChanged(root);
            }
            break;
        }

        case deleteID: {
            KBookmark bookmark = DolphinSettings::instance().bookmark(index);
            root.deleteBookmark(bookmark);
            manager->emitChanged(root);
            break;
        }

        case addID: {
            KBookmark bookmark = EditBookmarkDialog::getBookmark(i18n("Add Bookmark"),
                                                                 "New bookmark",
                                                                 KURL(),
                                                                 "bookmark");
            if (!bookmark.isNull()) {
                root.addBookmark(manager, bookmark);
                manager->emitChanged(root);
            }
        }

        default: break;
    }

    delete popup;
    popup = 0;

    DolphinView* view = Dolphin::mainWin().activeView();
    adjustSelection(view->url());
}


void BookmarksSidebarPage::adjustSelection(const KURL& url)
{
    // TODO (remarked in dolphin/TODO): the following code is quite equal
    // to BookmarkSelector::updateSelection().

    KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
    KBookmark bookmark = root.first();

    int maxLength = 0;
    int selectedIndex = -1;

    // Search the bookmark which is equal to the URL or at least is a parent URL.
    // If there are more than one possible parent URL candidates, choose the bookmark
    // which covers the bigger range of the URL.
    int i = 0;
    while (!bookmark.isNull()) {
        const KURL bookmarkURL = bookmark.url();
        if (bookmarkURL.isParentOf(url)) {
            const int length = bookmarkURL.prettyURL().length();
            if (length > maxLength) {
                selectedIndex = i;
                maxLength = length;
            }
        }
        bookmark = root.next(bookmark);
        ++i;
    }

    const bool block = m_bookmarksList->signalsBlocked();
    m_bookmarksList->blockSignals(true);
    if (selectedIndex < 0) {
        // no bookmark matches, hence deactivate any selection
        const int currentIndex = m_bookmarksList->index(m_bookmarksList->selectedItem());
        m_bookmarksList->setSelected(currentIndex, false);
    }
    else {
        // select the bookmark which is part of the current URL
        m_bookmarksList->setSelected(selectedIndex, true);
    }
    m_bookmarksList->blockSignals(block);
}

void BookmarksSidebarPage::slotURLChanged(const KURL& url)
{
    adjustSelection(url);
}

void BookmarksSidebarPage::connectToActiveView()
{
    DolphinView* view = Dolphin::mainWin().activeView();
    adjustSelection(view->url());
    connect(view, TQT_SIGNAL(signalURLChanged(const KURL&)),
            this, TQT_SLOT(slotURLChanged(const KURL&)));
}

BookmarksListBox::BookmarksListBox(TQWidget* parent) :
    TQListBox(parent)
{
    setAcceptDrops(true);
}
BookmarksListBox::~BookmarksListBox()
{
}

void BookmarksListBox::paintEvent(TQPaintEvent* /* event */)
{
    // don't invoke TQListBox::paintEvent(event) to prevent
    // that any kind of frame is drawn
}

void BookmarksListBox::contentsMousePressEvent(TQMouseEvent *event) 
{ 
    if (event->button() == Qt::LeftButton) 
        dragPos = event->pos();
    TQListBox::contentsMousePressEvent(event);
}

void BookmarksListBox::contentsMouseMoveEvent(TQMouseEvent *event)
{
    if (event->state() & Qt::LeftButton) {
        int distance = (event->pos() - dragPos).manhattanLength();
        if (distance > TQApplication::startDragDistance())
            startDrag();
    }
    TQListBox::contentsMouseMoveEvent(event); 
}

void BookmarksListBox::startDrag()
{
    int currentItem = TQListBox::currentItem();
    if (currentItem != -1) {
        BookmarkItem* bookmark = (BookmarkItem*)item(currentItem);
	if (bookmark!=0){
            KURL::List lst;
            lst.append( bookmark->url() );
            KURLDrag *drag = new KURLDrag(lst, this);
            drag->drag();
        }
    }
}

void BookmarksListBox::dragEnterEvent( TQDragEnterEvent *event )
{
    event->accept(KURLDrag::canDecode(event));
}

void BookmarksListBox::dropEvent( TQDropEvent *event )
{
    KURL::List urls;
    if (KURLDrag::decode(event, urls) && !urls.isEmpty()) {
        KBookmarkManager* manager = DolphinSettings::instance().bookmarkManager();
        KBookmarkGroup root = manager->root();

        KURL::List::iterator it;
        for(it=urls.begin(); it!=urls.end(); ++it) {
            root.addBookmark(manager, (*it).fileName(), (*it), "", false);
	}
	manager->emitChanged(root);
    }
}

BookmarkItem::BookmarkItem(const TQPixmap& pixmap, const TQString& text, const KURL& url) :
    TQListBoxPixmap(pixmap, text),
    m_url(url)
{
}

BookmarkItem::~BookmarkItem()
{
}

int BookmarkItem::height(const TQListBox* listBox) const
{
    return TQListBoxPixmap::height(listBox) + 8;
}

const KURL& BookmarkItem::url() const
{
    return m_url;
}

BookmarkItem* BookmarkItem::fromKbookmark(const KBookmark& bookmark, const TDEIconLoader& iconLoader)
{
    TQPixmap icon(iconLoader.loadIcon(bookmark.icon(), TDEIcon::NoGroup, TDEIcon::SizeMedium));
    return new BookmarkItem(icon, bookmark.text(), bookmark.url());
}

#include "bookmarkssidebarpage.moc"