summaryrefslogtreecommitdiffstats
path: root/konqueror/sidebar/trees/bookmark_module/bookmark_module.cpp
blob: 675642c3639114813ea3daee78bf257b149f83cc (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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/* This file is part of the KDE project
   Copyright (C) 2000 David Faure <faure@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   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
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include <tqclipboard.h>
#include <tqcursor.h>
#include <tqpopupmenu.h>

#include <tdeaction.h>
#include <tdeactioncollection.h>
#include <tdeapplication.h>
#include <kbookmarkdrag.h>
#include <kiconloader.h>
#include <klineedit.h>
#include <tdemessagebox.h>

#include <dcopclient.h>
#include <dcopref.h>

#include "bookmark_module.h"
#include "bookmark_item.h"
#include <konqbookmarkmanager.h>
#include <kdebug.h>

KonqSidebarBookmarkModule::KonqSidebarBookmarkModule( KonqSidebarTree * parentTree )
    : TQObject( 0L ), KonqSidebarTreeModule( parentTree ),
      m_topLevelItem( 0L ), m_ignoreOpenChange(true)
{
    // formats handled by KBookmarkDrag:
    TQStringList formats;
    formats << "text/uri-list" << "application/x-xbel" << "text/plain";
    tree()->setDropFormats(formats);

    connect(tree(), TQT_SIGNAL(moved(TQListViewItem*,TQListViewItem*,TQListViewItem*)),
            this,   TQT_SLOT(slotMoved(TQListViewItem*,TQListViewItem*,TQListViewItem*)));
    connect(tree(), TQT_SIGNAL(dropped(TDEListView*,TQDropEvent*,TQListViewItem*,TQListViewItem*)),
            this,   TQT_SLOT(slotDropped(TDEListView*,TQDropEvent*,TQListViewItem*,TQListViewItem*)));

    connect(tree(), TQT_SIGNAL(expanded(TQListViewItem*)),
            this,   TQT_SLOT(slotOpenChange(TQListViewItem*)));
    connect(tree(), TQT_SIGNAL(collapsed(TQListViewItem*)),
            this,   TQT_SLOT(slotOpenChange(TQListViewItem*)));

    m_collection = new TDEActionCollection( this, "bookmark actions" );
    (void) new TDEAction( i18n("&Create New Folder"), "folder_new", 0, this,
                        TQT_SLOT( slotCreateFolder() ), m_collection, "create_folder");
    (void) new TDEAction( i18n("Delete Folder"), "editdelete", 0, this,
                        TQT_SLOT( slotDelete() ), m_collection, "delete_folder");
    (void) new TDEAction( i18n("Delete Bookmark"), "editdelete", 0, this,
                        TQT_SLOT( slotDelete() ), m_collection, "delete_bookmark");
    (void) new TDEAction( i18n("Properties"), "edit", 0, this,
                        TQT_SLOT( slotProperties() ), m_collection, "item_properties");
    (void) new TDEAction( i18n("Open in New Window"), "window_new", 0, this,
                        TQT_SLOT( slotOpenNewWindow() ), m_collection, "open_window");
    (void) new TDEAction( i18n("Open in New Tab"), "tab_new", 0, this,
                        TQT_SLOT( slotOpenTab() ), m_collection, "open_tab");
    (void) new TDEAction( i18n("Open Folder in Tabs"), "tab_new", 0, this,
                        TQT_SLOT( slotOpenTab() ), m_collection, "folder_open_tabs");
    (void) new TDEAction( i18n("Copy Link Address"), "editcopy", 0, this,
                        TQT_SLOT( slotCopyLocation() ), m_collection, "copy_location");

    KStdAction::editBookmarks( KonqBookmarkManager::self(), TQT_SLOT( slotEditBookmarks() ),
			       m_collection, "edit_bookmarks" );

    connect( KonqBookmarkManager::self(), TQT_SIGNAL(changed(const TQString &, const TQString &) ),
             TQT_SLOT( slotBookmarksChanged(const TQString &) ) );
}

KonqSidebarBookmarkModule::~KonqSidebarBookmarkModule()
{
}

void KonqSidebarBookmarkModule::addTopLevelItem( KonqSidebarTreeTopLevelItem * item )
{
    m_ignoreOpenChange = true;

    m_topLevelItem = item;
    fillListView();

    m_ignoreOpenChange = false;
}

bool KonqSidebarBookmarkModule::handleTopLevelContextMenu( KonqSidebarTreeTopLevelItem *, const TQPoint& ) 
{
    TQPopupMenu *menu = new TQPopupMenu;

    if (tree()->tabSupport()) {
	m_collection->action("folder_open_tabs")->plug(menu);
	menu->insertSeparator();
    }
    m_collection->action("create_folder")->plug(menu);
 
    menu->insertSeparator();
    m_collection->action("edit_bookmarks")->plug(menu);

    menu->exec( TQCursor::pos() );
    delete menu;

    return true;
}

void KonqSidebarBookmarkModule::showPopupMenu()
{
    KonqSidebarBookmarkItem *bi = dynamic_cast<KonqSidebarBookmarkItem*>( tree()->selectedItem() );
    if (!bi)
        return;

    bool tabSupported = tree()->tabSupport();
    TQPopupMenu *menu = new TQPopupMenu;

    if (bi->bookmark().isGroup()) {
        if (tabSupported) {
            m_collection->action("folder_open_tabs")->plug(menu);
            menu->insertSeparator();
        }
        m_collection->action("create_folder")->plug(menu);
        m_collection->action("delete_folder")->plug(menu);
    } else {
        m_collection->action("open_window")->plug(menu);
        if (tabSupported)
            m_collection->action("open_tab")->plug(menu);
        m_collection->action("copy_location")->plug(menu);
        menu->insertSeparator();
        m_collection->action("create_folder")->plug(menu);
        m_collection->action("delete_bookmark")->plug(menu);
    }
    menu->insertSeparator();
    m_collection->action("item_properties")->plug(menu);

    menu->exec( TQCursor::pos() );
    delete menu;
}

void KonqSidebarBookmarkModule::slotMoved(TQListViewItem *i, TQListViewItem*, TQListViewItem *after)
{
    KonqSidebarBookmarkItem *item = dynamic_cast<KonqSidebarBookmarkItem*>( i );
    if (!item)
        return;
    KBookmark bookmark = item->bookmark();

    KBookmark afterBookmark;
    KonqSidebarBookmarkItem *afterItem = dynamic_cast<KonqSidebarBookmarkItem*>(after);
    if (afterItem)
        afterBookmark = afterItem->bookmark();

    KBookmarkGroup oldParentGroup = bookmark.parentGroup();
    KBookmarkGroup parentGroup;
    // try to get the parent group (assume that the TQListViewItem has been reparented by TDEListView)...
    // if anything goes wrong, use the root.
    if (item->parent()) {
        bool error = false;

        KonqSidebarBookmarkItem *parent = dynamic_cast<KonqSidebarBookmarkItem*>( (item->parent()) );
        if (!parent) {
            error = true;
        } else {
            if (parent->bookmark().isGroup())
                parentGroup = parent->bookmark().toGroup();
            else
                error = true;
        }

        if (error)
            parentGroup = KonqBookmarkManager::self()->root();
    } else {
        // No parent! This means the user dropped it before the top level item
        // And TDEListView has moved the item there, we need to correct it
        tree()->moveItem(item, m_topLevelItem, 0L);
        parentGroup = KonqBookmarkManager::self()->root();
    }

    // remove the old reference.
    oldParentGroup.deleteBookmark( bookmark );

    // insert the new item.
    parentGroup.moveItem(bookmark, afterBookmark);

    // inform others about the changed groups. quite expensive, so do
    // our best to update them in only one emitChanged call.
    TQString oldAddress = oldParentGroup.address();
    TQString newAddress = parentGroup.address();
    if (oldAddress == newAddress) {
        KonqBookmarkManager::self()->emitChanged( parentGroup );
    } else {
        int i = 0;
        while (true) {
            TQChar c1 = oldAddress[i];
            TQChar c2 = newAddress[i];
            if (c1 == TQChar::null) {
                // oldParentGroup is probably parent of parentGroup.
                KonqBookmarkManager::self()->emitChanged( oldParentGroup );
                break;
            } else if (c2 == TQChar::null) {
                // parentGroup is probably parent of oldParentGroup.
                KonqBookmarkManager::self()->emitChanged( parentGroup );
                break;
            } else {
                if (c1 == c2) {
                    // step to the next character.
                    ++i;
                } else {
                    // ugh... need to update both groups separately.
                    KonqBookmarkManager::self()->emitChanged( oldParentGroup );
                    KonqBookmarkManager::self()->emitChanged( parentGroup );
                    break;
                }
            }
        }
    }
}

void KonqSidebarBookmarkModule::slotDropped(TDEListView *, TQDropEvent *e, TQListViewItem *parent, TQListViewItem *after)
{
    if (!KBookmarkDrag::canDecode(e))
        return;

    KBookmark afterBookmark;
    KonqSidebarBookmarkItem *afterItem = dynamic_cast<KonqSidebarBookmarkItem*>(after);
    if (afterItem)
        afterBookmark = afterItem->bookmark();

    KBookmarkGroup parentGroup;
    // try to get the parent group...
    if (after) {
        parentGroup = afterBookmark.parentGroup();
    } else if (parent) {
        if(KonqSidebarBookmarkItem *p = dynamic_cast<KonqSidebarBookmarkItem*>(parent))
        {
            if (!p)
                return;
            KBookmark bm = p->bookmark();
            if (bm.isGroup())
                parentGroup = bm.toGroup();
            else
                return;
        }
        else if(parent == m_topLevelItem)
        {
            parentGroup = KonqBookmarkManager::self()->root();
        }
    } else {
        // it's most probably the root...
        parentGroup = KonqBookmarkManager::self()->root();
    }

    TQValueList<KBookmark> bookmarks = KBookmarkDrag::decode(e);

    // copy
    TQValueList<KBookmark>::iterator it = bookmarks.begin();
    for (;it != bookmarks.end(); ++it) {
        // insert new item.
        parentGroup.moveItem(*it, afterBookmark);
    }

    KonqBookmarkManager::self()->emitChanged( parentGroup );
}

void KonqSidebarBookmarkModule::slotCreateFolder()
{
    KonqSidebarBookmarkItem *bi = dynamic_cast<KonqSidebarBookmarkItem*>( tree()->selectedItem() );
    KBookmarkGroup parentGroup;    
    if (bi)
    {
	if (bi->bookmark().isGroup())
	    parentGroup = bi->bookmark().toGroup();
	else
	    parentGroup = bi->bookmark().parentGroup();
    }
    else if(tree()->selectedItem() == m_topLevelItem)
    {
	parentGroup = KonqBookmarkManager::self()->root();
    }
    else
	return;

    KBookmark bookmark = parentGroup.createNewFolder(KonqBookmarkManager::self());
    if(bi && !(bi->bookmark().isGroup()))
	parentGroup.moveItem(bookmark, bi->bookmark());

    KonqBookmarkManager::self()->emitChanged( parentGroup );
}

void KonqSidebarBookmarkModule::slotDelete()
{
    KonqSidebarBookmarkItem *bi = dynamic_cast<KonqSidebarBookmarkItem*>( tree()->selectedItem() );
    if (!bi)
        return;

    KBookmark bookmark = bi->bookmark();
    bool folder = bookmark.isGroup();

    if (KMessageBox::warningYesNo(
            tree(),
            folder ? i18n("Are you sure you wish to remove the bookmark folder\n\"%1\"?").arg(bookmark.text())
                    : i18n("Are you sure you wish to remove the bookmark\n\"%1\"?").arg(bookmark.text()),
            folder ? i18n("Bookmark Folder Deletion")
                    : i18n("Bookmark Deletion"),
            KGuiItem( i18n("&Delete"), "editdelete"), KStdGuiItem::cancel())
            != KMessageBox::Yes
        )
        return;

    KBookmarkGroup parentBookmark = bookmark.parentGroup();
    parentBookmark.deleteBookmark( bookmark );

    KonqBookmarkManager::self()->emitChanged( parentBookmark );
}

void makeTextNodeMod(KBookmark bk, const TQString &m_nodename, const TQString &m_newText) {
    TQDomNode subnode = bk.internalElement().namedItem(m_nodename);
    if (subnode.isNull()) {
        subnode = bk.internalElement().ownerDocument().createElement(m_nodename);
        bk.internalElement().appendChild(subnode);
    }
    
    if (subnode.firstChild().isNull()) {
        TQDomText domtext = subnode.ownerDocument().createTextNode("");
        subnode.appendChild(domtext);
    }
    
    TQDomText domtext = subnode.firstChild().toText();
    
    TQString m_oldText = domtext.data();
    domtext.setData(m_newText);
}

void KonqSidebarBookmarkModule::slotProperties(KonqSidebarBookmarkItem *bi)
{
    if (!bi) {
        bi = dynamic_cast<KonqSidebarBookmarkItem*>( tree()->selectedItem() );
        if (!bi)
            return;
    }

    KBookmark bookmark = bi->bookmark();

    TQString folder = bookmark.isGroup() ? TQString::null : bookmark.url().pathOrURL();
    BookmarkEditDialog dlg( bookmark.fullText(), folder, 0, 0,
                            i18n("Bookmark Properties") );
    if ( dlg.exec() != KDialogBase::Accepted )
        return;

    makeTextNodeMod(bookmark, "title", dlg.finalTitle());
    if ( !dlg.finalUrl().isNull() )
    {
        KURL u = KURL::fromPathOrURL(dlg.finalUrl());
        bookmark.internalElement().setAttribute("href", u.url(0, 106));
    }

    KBookmarkGroup parentBookmark = bookmark.parentGroup();
    KonqBookmarkManager::self()->emitChanged( parentBookmark );
}

void KonqSidebarBookmarkModule::slotOpenNewWindow()
{
    KonqSidebarBookmarkItem *bi = dynamic_cast<KonqSidebarBookmarkItem*>( tree()->selectedItem() );
    if (!bi)
        return;

    emit tree()->createNewWindow( bi->bookmark().url() );
}

void KonqSidebarBookmarkModule::slotOpenTab()
{
    KonqSidebarBookmarkItem *bi = dynamic_cast<KonqSidebarBookmarkItem*>( tree()->selectedItem() );
    KBookmark bookmark;
    if (bi)
    {
	bookmark = bi->bookmark();
    }
    else if(tree()->selectedItem() == m_topLevelItem)
	bookmark = KonqBookmarkManager::self()->root();	
    else
	return;

    DCOPRef ref(kapp->dcopClient()->appId(), tree()->topLevelWidget()->name());

    if (bookmark.isGroup()) {
        KBookmarkGroup group = bookmark.toGroup();
        bookmark = group.first();
        while (!bookmark.isNull()) {
            if (!bookmark.isGroup() && !bookmark.isSeparator())
                ref.call( "newTab(TQString)", bookmark.url().url() );
            bookmark = group.next(bookmark);
        }
    } else {
        ref.call( "newTab(TQString)", bookmark.url().url() );
    }
}

void KonqSidebarBookmarkModule::slotCopyLocation()
{
    KonqSidebarBookmarkItem *bi = dynamic_cast<KonqSidebarBookmarkItem*>( tree()->selectedItem() );
    if (!bi)
        return;

    KBookmark bookmark = bi->bookmark();

    if ( !bookmark.isGroup() )
    {
        kapp->clipboard()->setData( KBookmarkDrag::newDrag(bookmark, 0),
                                    TQClipboard::Selection );
        kapp->clipboard()->setData( KBookmarkDrag::newDrag(bookmark, 0),
                                    TQClipboard::Clipboard );
    }
}

void KonqSidebarBookmarkModule::slotOpenChange(TQListViewItem* i)
{
    if (m_ignoreOpenChange)
        return;

    KonqSidebarBookmarkItem *bi = dynamic_cast<KonqSidebarBookmarkItem*>( i );
    if (!bi)
        return;

    KBookmark bookmark = bi->bookmark();

    bool open = bi->isOpen();

    if (!open)
        m_folderOpenState.remove(bookmark.address()); // no need to store closed folders...
    else
        m_folderOpenState[bookmark.address()] = open;
}

void KonqSidebarBookmarkModule::slotBookmarksChanged( const TQString & groupAddress )
{
    m_ignoreOpenChange = true;

    // update the right part of the tree
    KBookmarkGroup group = KonqBookmarkManager::self()->findByAddress( groupAddress ).toGroup();
    KonqSidebarBookmarkItem * item = findByAddress( groupAddress );
    Q_ASSERT(!group.isNull());
    Q_ASSERT(item);
    if (!group.isNull() && item)
    {
        // Delete all children of item
        TQListViewItem * child = item->firstChild();
        while( child ) {
            TQListViewItem * next = child->nextSibling();
            delete child;
            child = next;
        }
        fillGroup( item, group );
    }

    m_ignoreOpenChange = false;
}

void KonqSidebarBookmarkModule::fillListView()
{
    m_ignoreOpenChange = true;

    KBookmarkGroup root = KonqBookmarkManager::self()->root();
    fillGroup( m_topLevelItem, root );

    m_ignoreOpenChange = false;
}

void KonqSidebarBookmarkModule::fillGroup( KonqSidebarTreeItem * parentItem, KBookmarkGroup group )
{
    int n = 0;
    for ( KBookmark bk = group.first() ; !bk.isNull() ; bk = group.next(bk), ++n )
    {
            KonqSidebarBookmarkItem * item = new KonqSidebarBookmarkItem( parentItem, m_topLevelItem, bk, n );
            if ( bk.isGroup() )
            {
                KBookmarkGroup grp = bk.toGroup();
                fillGroup( item, grp );

                TQString address(grp.address());
                if (m_folderOpenState.contains(address))
                    item->setOpen(m_folderOpenState[address]);
                else
                    item->setOpen(false);
            }
            else if ( bk.isSeparator() )
                item->setVisible( false );
            else
                item->setExpandable( false );
    }
}

// Borrowed from KEditBookmarks
KonqSidebarBookmarkItem * KonqSidebarBookmarkModule::findByAddress( const TQString & address ) const
{
    TQListViewItem * item = m_topLevelItem;
    // The address is something like /5/10/2
    TQStringList addresses = TQStringList::split('/',address);
    for ( TQStringList::Iterator it = addresses.begin() ; it != addresses.end() ; ++it )
    {
        uint number = (*it).toUInt();
        item = item->firstChild();
        for ( uint i = 0 ; i < number ; ++i )
            item = item->nextSibling();
    }
    Q_ASSERT(item);
    return static_cast<KonqSidebarBookmarkItem *>(item);
}

// Borrowed&modified from KBookmarkMenu...
BookmarkEditDialog::BookmarkEditDialog(const TQString& title, const TQString& url,
                                       TQWidget * parent, const char * name, const TQString& caption )
  : KDialogBase(parent, name, true, caption,
                (Ok|Cancel),
                Ok, false, KGuiItem()),
    m_title(0), m_location(0)
{
    setButtonOK( i18n( "&Update" ) );

    TQWidget *main = new TQWidget( this );
    setMainWidget( main );

    bool folder = url.isNull();
    TQGridLayout *grid = new TQGridLayout( main, 2, folder?1:2, spacingHint() );

    TQLabel *nameLabel = new TQLabel(i18n("Name:"), main, "title label");
    grid->addWidget(nameLabel, 0, 0);
    m_title = new KLineEdit(main, "title edit");
    m_title->setText(title);
    nameLabel->setBuddy(m_title);
    grid->addWidget(m_title, 0, 1);
    if(!folder) {
        TQLabel *locationLabel = new TQLabel(i18n("Location:"), main, "location label");
        grid->addWidget(locationLabel, 1, 0);
        m_location = new KLineEdit(main, "location edit");
        m_location->setText(url);
        locationLabel->setBuddy(m_location);
        grid->addWidget(m_location, 1, 1);
    }
    main->setMinimumSize( 300, 0 );
}

void BookmarkEditDialog::slotOk()
{
    accept();
}

void BookmarkEditDialog::slotCancel()
{
    reject();
}

TQString BookmarkEditDialog::finalUrl() const
{
    if (m_location!=0)
        return m_location->text();
    else
        return TQString::null;
}

TQString BookmarkEditDialog::finalTitle() const
{
    if (m_title!=0) 
        return m_title->text();
    else
        return TQString::null;
}

extern "C"
{
   KDE_EXPORT KonqSidebarTreeModule* create_konq_sidebartree_bookmarks(KonqSidebarTree* par,const bool)
	{
		return new KonqSidebarBookmarkModule(par);
	} 
}

#include "bookmark_module.moc"