summaryrefslogtreecommitdiffstats
path: root/konqueror/keditbookmarks/actionsimpl.cpp
blob: 67e694dd55de4754c2e91c708f6cc3a95a8b7f7b (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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
// -*- indent-tabs-mode:nil -*-
// vim: set ts=4 sts=4 sw=4 et:
/* This file is part of the KDE project
   Copyright (C) 2000 David Faure <[email protected]>
   Copyright (C) 2002-2003 Alexander Kellett <[email protected]>

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

   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; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "actionsimpl.h"

#include "toplevel.h"
#include "commands.h"
#include "importers.h"
#include "favicons.h"
#include "testlink.h"
#include "listview.h"
#include "exporters.h"
#include "bookmarkinfo.h"

#include <stdlib.h>

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

#include <tdelocale.h>
#include <dcopclient.h>
#include <dcopref.h>
#include <kdebug.h>
#include <tdeapplication.h>

#include <tdeaction.h>
#include <kstdaction.h>
#include <kedittoolbar.h>
#include <tdefiledialog.h>
#include <kkeydialog.h>
#include <tdemessagebox.h>
#include <kinputdialog.h>
#include <krun.h>

#include <kdatastream.h>
#include <tdetempfile.h>
#include <kstandarddirs.h>

#include <tdeparts/part.h>
#include <tdeparts/componentfactory.h>

#include <kicondialog.h>
#include <kiconloader.h>

#include <kbookmarkdrag.h>
#include <kbookmarkmanager.h>
#include <kbookmarkimporter.h>

#include <kbookmarkimporter_ie.h>
#include <kbookmarkimporter_opera.h>
#include <kbookmarkexporter.h>

ActionsImpl* ActionsImpl::s_self = 0;

// decoupled from resetActions in toplevel.cpp
// as resetActions simply uses the action groups
// specified in the ui.rc file
void KEBApp::createActions() {

    ActionsImpl *actn = ActionsImpl::self();

    // save and quit should probably not be in the toplevel???
    (void) KStdAction::quit(
        TQT_TQOBJECT(this), TQT_SLOT( close() ), actionCollection());
    KStdAction::keyBindings(guiFactory(), TQT_SLOT(configureShortcuts()), actionCollection());
    (void) KStdAction::configureToolbars(
        TQT_TQOBJECT(this), TQT_SLOT( slotConfigureToolbars() ), actionCollection());

    if (m_browser) {
        (void) KStdAction::open(
            TQT_TQOBJECT(actn), TQT_SLOT( slotLoad() ), actionCollection());
        (void) KStdAction::saveAs(
            TQT_TQOBJECT(actn), TQT_SLOT( slotSaveAs() ), actionCollection());
    }

    (void) KStdAction::cut(TQT_TQOBJECT(actn), TQT_SLOT( slotCut() ), actionCollection());
    (void) KStdAction::copy(TQT_TQOBJECT(actn), TQT_SLOT( slotCopy() ), actionCollection());
    (void) KStdAction::paste(TQT_TQOBJECT(actn), TQT_SLOT( slotPaste() ), actionCollection());
    (void) KStdAction::print(TQT_TQOBJECT(actn), TQT_SLOT( slotPrint() ), actionCollection());

    // settings menu
    (void) new TDEToggleAction(
        i18n("&Show Netscape Bookmarks in Konqueror"), 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotShowNS() ), actionCollection(),
        "settings_showNS");

    // actions
    (void) new TDEAction(
        i18n("&Delete"), "edit-delete", Key_Delete,
        TQT_TQOBJECT(actn), TQT_SLOT( slotDelete() ), actionCollection(), "delete");
    (void) new TDEAction(
        i18n("Rename"), "text", Key_F2,
        TQT_TQOBJECT(actn), TQT_SLOT( slotRename() ), actionCollection(), "rename");
    (void) new TDEAction(
        i18n("C&hange URL"), "text", Key_F3,
        TQT_TQOBJECT(actn), TQT_SLOT( slotChangeURL() ), actionCollection(), "changeurl");
    (void) new TDEAction(
        i18n("C&hange Comment"), "text", Key_F4,
        TQT_TQOBJECT(actn), TQT_SLOT( slotChangeComment() ), actionCollection(), "changecomment");
    (void) new TDEAction(
        i18n("Chan&ge Icon..."), "icons", 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotChangeIcon() ), actionCollection(), "changeicon");
    (void) new TDEAction(
        i18n("Update Favicon"), 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotUpdateFavIcon() ), actionCollection(), "updatefavicon");
    (void) new TDEAction(
        i18n("Recursive Sort"), 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotRecursiveSort() ), actionCollection(), "recursivesort");
    (void) new TDEAction(
        i18n("&New Folder..."), "folder-new", CTRL+Key_N,
        TQT_TQOBJECT(actn), TQT_SLOT( slotNewFolder() ), actionCollection(), "newfolder");
    (void) new TDEAction(
        i18n("&New Bookmark"), "www", 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotNewBookmark() ), actionCollection(), "newbookmark");
    (void) new TDEAction(
        i18n("&Insert Separator"), CTRL+Key_I,
        TQT_TQOBJECT(actn), TQT_SLOT( slotInsertSeparator() ), actionCollection(),
        "insertseparator");
    (void) new TDEAction(
        i18n("&Sort Alphabetically"), 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotSort() ), actionCollection(), "sort");
    (void) new TDEAction(
        i18n("Set as T&oolbar Folder"), "bookmark_toolbar", 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotSetAsToolbar() ), actionCollection(), "setastoolbar");
    (void) new TDEAction(
        i18n("Show in T&oolbar"), "bookmark_toolbar", 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotShowInToolbar() ), actionCollection(), "showintoolbar");
    (void) new TDEAction(
        i18n("Hide in T&oolbar"), "bookmark_toolbar", 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotHideInToolbar() ), actionCollection(), "hideintoolbar");
    (void) new TDEAction(
        i18n("&Expand All Folders"), 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotExpandAll() ), actionCollection(), "expandall");
    (void) new TDEAction(
        i18n("Collapse &All Folders"), 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotCollapseAll() ), actionCollection(), "collapseall" );
    (void) new TDEAction(
        i18n("&Open in Konqueror"), "document-open", 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotOpenLink() ), actionCollection(), "openlink" );
    (void) new TDEAction(
        i18n("Check &Status"), "bookmark", 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotTestSelection() ), actionCollection(), "testlink" );

    (void) new TDEAction(
        i18n("Check Status: &All"), 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotTestAll() ), actionCollection(), "testall" );
    (void) new TDEAction(
        i18n("Update All &Favicons"), 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotUpdateAllFavIcons() ), actionCollection(),
        "updateallfavicons" );
    (void) new TDEAction(
        i18n("Cancel &Checks"), 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotCancelAllTests() ), actionCollection(), "canceltests" );
    (void) new TDEAction(
        i18n("Cancel &Favicon Updates"), 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotCancelFavIconUpdates() ), actionCollection(),
        "cancelfaviconupdates" );
    (void) new TDEAction(
        i18n("Import &Netscape Bookmarks..."), "netscape", 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotImport() ), actionCollection(), "importNS");
    (void) new TDEAction(
        i18n("Import &Opera Bookmarks..."), "opera", 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotImport() ), actionCollection(), "importOpera");
    (void) new TDEAction(
        i18n("Import All &Crash Sessions as Bookmarks..."), 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotImport() ), actionCollection(), "importCrashes");
    (void) new TDEAction(
        i18n("Import &Galeon Bookmarks..."), 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotImport() ), actionCollection(), "importGaleon");
    (void) new TDEAction(
        i18n("Import &KDE2/KDE3 Bookmarks..."), 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotImport() ), actionCollection(), "importKDE2");
    (void) new TDEAction(
        i18n("Import &IE Bookmarks..."), 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotImport() ), actionCollection(), "importIE");
    (void) new TDEAction(
        i18n("Import &Mozilla Bookmarks..."), "mozilla", 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotImport() ), actionCollection(), "importMoz");
    (void) new TDEAction(
        i18n("Export to &Netscape Bookmarks"), "netscape", 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotExportNS() ), actionCollection(), "exportNS");
    (void) new TDEAction(
        i18n("Export to &Opera Bookmarks..."), "opera", 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotExportOpera() ), actionCollection(), "exportOpera");
    (void) new TDEAction(
        i18n("Export to &HTML Bookmarks..."), "html", 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotExportHTML() ), actionCollection(), "exportHTML");
    (void) new TDEAction(
        i18n("Export to &IE Bookmarks..."), 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotExportIE() ), actionCollection(), "exportIE");
    (void) new TDEAction(
        i18n("Export to &Mozilla Bookmarks..."), "mozilla", 0,
        TQT_TQOBJECT(actn), TQT_SLOT( slotExportMoz() ), actionCollection(), "exportMoz");
}

void ActionsImpl::slotLoad() {
    TQString bookmarksFile
        = KFileDialog::getOpenFileName(TQString::null, "*.xml", KEBApp::self());
    if (bookmarksFile.isNull())
        return;
    KEBApp::self()->m_caption = TQString::null;
    KEBApp::self()->m_bookmarksFilename = bookmarksFile;
    KEBApp::self()->construct();
}

void ActionsImpl::slotSaveAs() {
    KEBApp::self()->bkInfo()->commitChanges();
    TQString saveFilename
        = KFileDialog::getSaveFileName(TQString::null, "*.xml", KEBApp::self());
    if (!saveFilename.isEmpty())
        CurrentMgr::self()->saveAs(saveFilename);
}

void CurrentMgr::doExport(ExportType type, const TQString & _path) {
    if(KEBApp::self())
        KEBApp::self()->bkInfo()->commitChanges();
    TQString path(_path);
    // TODO - add a factory and make all this use the base class
    if (type == OperaExport) {
        if (path.isNull())
            path = KOperaBookmarkImporterImpl().findDefaultLocation(true);
        KOperaBookmarkExporterImpl exporter(mgr(), path);
        exporter.write(mgr()->root());
        return;

    } else if (type == HTMLExport) {
        if (path.isNull())
            path = KFileDialog::getSaveFileName(
                        TQDir::homeDirPath(),
                        i18n("*.html|HTML Bookmark Listing") );
        HTMLExporter exporter;
        exporter.write(mgr()->root(), path);
        return;

    } else if (type == IEExport) {
        if (path.isNull())
            path = KIEBookmarkImporterImpl().findDefaultLocation(true);
        KIEBookmarkExporterImpl exporter(mgr(), path);
        exporter.write(mgr()->root());
        return;
    }

    bool moz = (type == MozillaExport);

    if (path.isNull())
        path = (moz) ? KNSBookmarkImporter::mozillaBookmarksFile(true)
            : KNSBookmarkImporter::netscapeBookmarksFile(true);

    if (!path.isEmpty()) {
        KNSBookmarkExporter exporter(mgr(), path);
        exporter.write(moz);
    }
}

void KEBApp::setActionsEnabled(SelcAbilities sa) {
    TDEActionCollection * coll = actionCollection();

    TQStringList toEnable;

    if (sa.multiSelect || (sa.singleSelect && !sa.root))
        toEnable << "edit_copy";

    if (sa.multiSelect || (sa.singleSelect && !sa.root && !sa.urlIsEmpty && !sa.group && !sa.separator))
            toEnable << "openlink";

    if (!m_readOnly) {
        if (sa.notEmpty)
            toEnable << "testall" << "updateallfavicons";

        if ( sa.multiSelect || (sa.singleSelect && !sa.root) )
                toEnable << "delete" << "edit_cut";

        if( sa.singleSelect)
            if (m_canPaste)
                toEnable << "edit_paste";

        if( sa.multiSelect || (sa.singleSelect && !sa.root && (sa.group || !sa.urlIsEmpty) && !sa.separator))
            toEnable << "testlink" << "updatefavicon";

        if(sa.multiSelect)
            toEnable << "showintoolbar" << "hideintoolbar";
        else if(sa.itemSelected)
            toEnable << (sa.tbShowState ? "hideintoolbar" : "showintoolbar");

        if (sa.singleSelect && !sa.root && !sa.separator) {
            toEnable << "rename" << "changeicon" << "changecomment";
            if (!sa.group)
                toEnable << "changeurl";
        }

        if (sa.singleSelect) {
            toEnable << "newfolder" << "newbookmark" << "insertseparator";
            if (sa.group)
                toEnable << "sort" << "recursivesort" << "setastoolbar";
        }
    }

    for ( TQStringList::Iterator it = toEnable.begin();
            it != toEnable.end(); ++it )
    {
        coll->action((*it).ascii())->setEnabled(true);
        // kdDebug() << (*it) << endl;
    }
}

void KEBApp::setCancelFavIconUpdatesEnabled(bool enabled) {
    actionCollection()->action("cancelfaviconupdates")->setEnabled(enabled);
}

void KEBApp::setCancelTestsEnabled(bool enabled) {
    actionCollection()->action("canceltests")->setEnabled(enabled);
}

void ActionsImpl::slotCut() {
    KEBApp::self()->bkInfo()->commitChanges();
    slotCopy();
    DeleteManyCommand *mcmd = new DeleteManyCommand( i18n("Cut Items"), ListView::self()->selectedAddresses() );
    CmdHistory::self()->addCommand(mcmd);

}

void ActionsImpl::slotCopy() {
    KEBApp::self()->bkInfo()->commitChanges();
    // this is not a command, because it can't be undone
    Q_ASSERT(ListView::self()->selectedItemsMap().count() != 0);
    TQValueList<KBookmark> bookmarks
        = ListView::self()->itemsToBookmarks(ListView::self()->selectedItemsMap());
    KBookmarkDrag* data = KBookmarkDrag::newDrag(bookmarks, 0 /* not this ! */);
    kapp->clipboard()->setData(data, TQClipboard::Clipboard);
}

void ActionsImpl::slotPaste() {
    KEBApp::self()->bkInfo()->commitChanges();
    KEBMacroCommand *mcmd =
        CmdGen::insertMimeSource(
                            i18n("Paste"),
                            kapp->clipboard()->data(TQClipboard::Clipboard),
                            ListView::self()->userAddress());
    CmdHistory::self()->didCommand(mcmd);
}

/* -------------------------------------- */

void ActionsImpl::slotNewFolder() {
    KEBApp::self()->bkInfo()->commitChanges();
    bool ok;
    TQString str = KInputDialog::getText( i18n( "Create New Bookmark Folder" ),
            i18n( "New folder:" ), TQString::null, &ok );
    if (!ok)
        return;

    CreateCommand *cmd = new CreateCommand(
                                ListView::self()->userAddress(),
                                str, "bookmark_folder", /*open*/ true);
    CmdHistory::self()->addCommand(cmd);
}

void ActionsImpl::slotNewBookmark() {
    KEBApp::self()->bkInfo()->commitChanges();
    // TODO - make a setCurrentItem(Command *) which uses finaladdress interface
    CreateCommand * cmd = new CreateCommand(
                                ListView::self()->userAddress(),
                                TQString::null, "www", KURL("http://"));
    CmdHistory::self()->addCommand(cmd);
}

void ActionsImpl::slotInsertSeparator() {
    KEBApp::self()->bkInfo()->commitChanges();
    CreateCommand * cmd = new CreateCommand(ListView::self()->userAddress());
    CmdHistory::self()->addCommand(cmd);
}

void ActionsImpl::slotImport() {
    KEBApp::self()->bkInfo()->commitChanges();
    // kdDebug() << "ActionsImpl::slotImport() where sender()->name() == "
    //           << sender()->name() << endl;
    ImportCommand* import
        = ImportCommand::performImport(TQT_TQOBJECT_CONST(sender())->name()+6, KEBApp::self());
    if (!import)
        return;
    CmdHistory::self()->addCommand(import);
    ListView::self()->setCurrent( ListView::self()->getItemAtAddress(import->groupAddress()), true);
}

// TODO - this is getting ugly and repetitive. cleanup!

void ActionsImpl::slotExportOpera() {
    KEBApp::self()->bkInfo()->commitChanges();
    CurrentMgr::self()->doExport(CurrentMgr::OperaExport); }
void ActionsImpl::slotExportHTML() {
    KEBApp::self()->bkInfo()->commitChanges();
    CurrentMgr::self()->doExport(CurrentMgr::HTMLExport); }
void ActionsImpl::slotExportIE() {
    KEBApp::self()->bkInfo()->commitChanges();
    CurrentMgr::self()->doExport(CurrentMgr::IEExport); }
void ActionsImpl::slotExportNS() {
    KEBApp::self()->bkInfo()->commitChanges();
    CurrentMgr::self()->doExport(CurrentMgr::NetscapeExport); }
void ActionsImpl::slotExportMoz() {
    KEBApp::self()->bkInfo()->commitChanges();
    CurrentMgr::self()->doExport(CurrentMgr::MozillaExport); }

/* -------------------------------------- */

static TQCString s_appId, s_objId;
static KParts::ReadOnlyPart *s_part;

void ActionsImpl::slotPrint() {
    KEBApp::self()->bkInfo()->commitChanges();
    s_part = KParts::ComponentFactory
                        ::createPartInstanceFromQuery<KParts::ReadOnlyPart>(
                                "text/html", TQString::null);
    s_part->setProperty("pluginsEnabled", TQVariant(false, 1));
    s_part->setProperty("javaScriptEnabled", TQVariant(false, 1));
    s_part->setProperty("javaEnabled", TQVariant(false, 1));

    // doc->openStream( "text/html", KURL() );
    // doc->writeStream( TQCString( "<HTML><BODY>FOO</BODY></HTML>" ) );
    // doc->closeStream();

    HTMLExporter exporter;
    KTempFile tmpf(locateLocal("tmp", "print_bookmarks"), ".html");
    TQTextStream *tstream = tmpf.textStream();
    tstream->setEncoding(TQTextStream::Unicode);
    (*tstream) << exporter.toString(CurrentMgr::self()->mgr()->root(), true);
    tmpf.close();

    s_appId = kapp->dcopClient()->appId();
    s_objId = s_part->property("dcopObjectId").toString().latin1();
    connect(s_part, TQT_SIGNAL(completed()), this, TQT_SLOT(slotDelayedPrint()));

    s_part->openURL(KURL( tmpf.name() ));
}

void ActionsImpl::slotDelayedPrint() {
    Q_ASSERT(s_part);
    DCOPRef(s_appId, s_objId).send("print", false);
    delete s_part;
    s_part = 0;
}

/* -------------------------------------- */

void ActionsImpl::slotShowNS() {
    KEBApp::self()->bkInfo()->commitChanges();
    bool shown = KEBApp::self()->nsShown();
    CurrentMgr::self()->mgr()->setShowNSBookmarks(shown);
    // TODO - need to force a save here
    CurrentMgr::self()->reloadConfig();
}

void ActionsImpl::slotCancelFavIconUpdates() {
    FavIconsItrHolder::self()->cancelAllItrs();
}

void ActionsImpl::slotCancelAllTests() {
    TestLinkItrHolder::self()->cancelAllItrs();
}

void ActionsImpl::slotTestAll() {
    TestLinkItrHolder::self()->insertItr(
            new TestLinkItr(ListView::self()->allBookmarks()));
}

void ActionsImpl::slotUpdateAllFavIcons() {
    FavIconsItrHolder::self()->insertItr(
            new FavIconsItr(ListView::self()->allBookmarks()));
}

ActionsImpl::~ActionsImpl() {
    delete FavIconsItrHolder::self();
    delete TestLinkItrHolder::self();
}

/* -------------------------------------- */

void ActionsImpl::slotTestSelection() {
    KEBApp::self()->bkInfo()->commitChanges();
    TestLinkItrHolder::self()->insertItr(new TestLinkItr(ListView::self()->selectedBookmarksExpanded()));
}

void ActionsImpl::slotUpdateFavIcon() {
    KEBApp::self()->bkInfo()->commitChanges();
    FavIconsItrHolder::self()->insertItr(new FavIconsItr(ListView::self()->selectedBookmarksExpanded()));
}

/* -------------------------------------- */

class KBookmarkGroupList : private KBookmarkGroupTraverser {
public:
    KBookmarkGroupList(KBookmarkManager *);
    TQValueList<KBookmark> getList(const KBookmarkGroup &);
private:
    virtual void visit(const KBookmark &) { ; }
    virtual void visitEnter(const KBookmarkGroup &);
    virtual void visitLeave(const KBookmarkGroup &) { ; }
private:
    KBookmarkManager *m_manager;
    TQValueList<KBookmark> m_list;
};

KBookmarkGroupList::KBookmarkGroupList( KBookmarkManager *manager ) {
    m_manager = manager;
}

TQValueList<KBookmark> KBookmarkGroupList::getList( const KBookmarkGroup &grp ) {
    traverse(grp);
    return m_list;
}

void KBookmarkGroupList::visitEnter(const KBookmarkGroup &grp) {
    m_list << grp;
}

void ActionsImpl::slotRecursiveSort() {
    KEBApp::self()->bkInfo()->commitChanges();
    KBookmark bk = ListView::self()->firstSelected()->bookmark();
    Q_ASSERT(bk.isGroup());
    KEBMacroCommand *mcmd = new KEBMacroCommand(i18n("Recursive Sort"));
    KBookmarkGroupList lister(CurrentMgr::self()->mgr());
    TQValueList<KBookmark> bookmarks = lister.getList(bk.toGroup());
    bookmarks << bk.toGroup();
    for (TQValueListConstIterator<KBookmark> it = bookmarks.begin(); it != bookmarks.end(); ++it) {
        SortCommand *cmd = new SortCommand("", (*it).address());
        cmd->execute();
        mcmd->addCommand(cmd);
    }
    CmdHistory::self()->didCommand(mcmd);
}

void ActionsImpl::slotSort() {
    KEBApp::self()->bkInfo()->commitChanges();
    KBookmark bk = ListView::self()->firstSelected()->bookmark();
    Q_ASSERT(bk.isGroup());
    SortCommand *cmd = new SortCommand(i18n("Sort Alphabetically"), bk.address());
    CmdHistory::self()->addCommand(cmd);
}

/* -------------------------------------- */

void ActionsImpl::slotDelete() {
    KEBApp::self()->bkInfo()->commitChanges();
    DeleteManyCommand *mcmd = new DeleteManyCommand(i18n("Delete Items"), ListView::self()->selectedAddresses());
    CmdHistory::self()->addCommand(mcmd);
}

void ActionsImpl::slotOpenLink() {
    KEBApp::self()->bkInfo()->commitChanges();
    TQValueList<KBookmark> bks = ListView::self()->itemsToBookmarks(ListView::self()->selectedItemsMap());
    TQValueListIterator<KBookmark> it;
    for (it = bks.begin(); it != bks.end(); ++it) {
        if ((*it).isGroup() || (*it).isSeparator())
            continue;
        (void)new KRun((*it).url());
    }
}

/* -------------------------------------- */

void ActionsImpl::slotRename() {
    KEBApp::self()->bkInfo()->commitChanges();
    ListView::self()->rename(KEBListView::NameColumn);
}

void ActionsImpl::slotChangeURL() {
    KEBApp::self()->bkInfo()->commitChanges();
    ListView::self()->rename(KEBListView::UrlColumn);
}

void ActionsImpl::slotChangeComment() {
    KEBApp::self()->bkInfo()->commitChanges();
    ListView::self()->rename(KEBListView::CommentColumn);
}

void ActionsImpl::slotSetAsToolbar() {
    KEBApp::self()->bkInfo()->commitChanges();
    KBookmark bk = ListView::self()->firstSelected()->bookmark();
    Q_ASSERT(bk.isGroup());
    KEBMacroCommand *mcmd = CmdGen::setAsToolbar(bk);
    CmdHistory::self()->addCommand(mcmd);
}

void ActionsImpl::slotShowInToolbar() {
    KEBApp::self()->bkInfo()->commitChanges();
    TQValueList<KBookmark> bks = ListView::self()->itemsToBookmarks(ListView::self()->selectedItemsMap());
    KEBMacroCommand *mcmd = CmdGen::setShownInToolbar(bks, true);
    CmdHistory::self()->addCommand(mcmd);
}

void ActionsImpl::slotHideInToolbar() {
    KEBApp::self()->bkInfo()->commitChanges();
    TQValueList<KBookmark> bks = ListView::self()->itemsToBookmarks(ListView::self()->selectedItemsMap());
    KEBMacroCommand *mcmd = CmdGen::setShownInToolbar(bks, false);
    CmdHistory::self()->addCommand(mcmd);
}

void ActionsImpl::slotChangeIcon() {
    KEBApp::self()->bkInfo()->commitChanges();
    KBookmark bk = ListView::self()->firstSelected()->bookmark();
    TDEIconDialog dlg(KEBApp::self());
    TQString newIcon = dlg.selectIcon(TDEIcon::Small, TDEIcon::Place);
    if (newIcon.isEmpty())
        return;
    EditCommand *cmd = new EditCommand(
                                bk.address(),
                                EditCommand::Edition("icon", newIcon),
                                i18n("Icon"));
    CmdHistory::self()->addCommand(cmd);
}

void ActionsImpl::slotExpandAll() {
    ListView::self()->setOpen(true);
}

void ActionsImpl::slotCollapseAll() {
    ListView::self()->setOpen(false);
}

#include "actionsimpl.moc"