summaryrefslogtreecommitdiffstats
path: root/akregator/src/actionmanagerimpl.cpp
blob: 9376024f35edb8225f79acb1dcfea3bdedf37a3f (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
/*
    This file is part of Akregator.

    Copyright (C) 2005 Frank Osterfeld <frank.osterfeld at kdemail.net>

    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.

    As a special exception, permission is given to link this program
    with any edition of TQt, and distribute the resulting executable,
    without including the source code for TQt in the source distribution.
*/

#include <tqwidget.h>
#include <tdeaction.h>
#include <tdeactioncollection.h>
#include <tdelocale.h>
#include <tdepopupmenu.h>
#include <tdeshortcut.h>
#include <kxmlguifactory.h>

#include <tqmap.h>
#include <tqstring.h>
#include <tqvaluelist.h>

#include "actionmanagerimpl.h"
#include "akregatorconfig.h"
#include "akregator_part.h"
#include "akregator_view.h"
#include "articlelistview.h"
#include "articleviewer.h"
#include "feed.h"
#include "feedlistview.h"
#include "fetchqueue.h"
#include "folder.h"
#include "listtabwidget.h"
#include "kernel.h"
#include "speechclient.h"
#include "tag.h"
#include "tagaction.h"
#include "tagnode.h"
#include "tagset.h"
#include "trayicon.h"
#include "treenode.h"
#include "treenodevisitor.h"
#include "tabwidget.h"
#include "tdestdaccel.h"



#include <kdebug.h>

namespace Akregator
{

class ActionManagerImpl::NodeSelectVisitor : public TreeNodeVisitor
{
    public:
    NodeSelectVisitor(ActionManagerImpl* manager) : m_manager(manager) {}
    virtual ~NodeSelectVisitor() {}

    virtual bool visitFeed(Feed* node)
    {
        TDEAction* remove = m_manager->action("feed_remove");
        if (remove)
            remove->setEnabled(true);
        TDEAction* hp = m_manager->action("feed_homepage");
        if (hp)
            hp->setEnabled(!node->htmlUrl().isEmpty());
        m_manager->action("feed_fetch")->setText(i18n("&Fetch Feed"));
        m_manager->action("feed_remove")->setText(i18n("&Delete Feed"));
        m_manager->action("feed_modify")->setText(i18n("&Edit Feed..."));
        m_manager->action("feed_mark_all_as_read")->setText(i18n("&Mark Feed as Read"));

        return true;
    }

    virtual bool visitFolder(Folder* node)
    {
        TDEAction* remove = m_manager->action("feed_remove");
        if (remove)
            remove->setEnabled(node->parent()); // root nodes must not be deleted
        TDEAction* hp = m_manager->action("feed_homepage");
        if (hp)
            hp->setEnabled(false);

        m_manager->action("feed_fetch")->setText(i18n("&Fetch Feeds"));
        m_manager->action("feed_remove")->setText(i18n("&Delete Folder"));
        m_manager->action("feed_modify")->setText(i18n("&Rename Folder"));
        m_manager->action("feed_mark_all_as_read")->setText(i18n("&Mark Feeds as Read"));

        return true;
    }

    virtual bool visitTagNode(TagNode* /*node*/)
    {
        TDEAction* remove = m_manager->action("feed_remove");
        if (remove)
            remove->setEnabled(true);
        TDEAction* hp = m_manager->action("feed_homepage");
        if (hp)
            hp->setEnabled(false);
        m_manager->action("feed_mark_all_as_read")->setText(i18n("&Mark Articles as Read"));
        m_manager->action("feed_remove")->setText(i18n("&Delete Tag"));
        m_manager->action("feed_modify")->setText(i18n("&Edit Tag..."));

        return true;
    }
    private:
    ActionManagerImpl* m_manager;
};

class ActionManagerImpl::ActionManagerImplPrivate
{
public:

    NodeSelectVisitor* nodeSelectVisitor;
    ArticleListView* articleList;
    ListTabWidget* listTabWidget;
    View* view;
    ArticleViewer* articleViewer;
    Part* part;
    TrayIcon* trayIcon;
    TDEActionMenu* tagMenu;
    TDEActionCollection* actionCollection;
    TagSet* tagSet;
    TQMap<TQString, TagAction*> tagActions;
    TabWidget* tabWidget;
    TDEAction* speakSelectedArticlesAction;
};

void ActionManagerImpl::slotUpdateTagActions(bool enabled, const TQStringList& tagIds)
{
    if (Settings::showTaggingGUI() && d->tagMenu)
    {
        d->tagMenu->setEnabled(enabled);
        TQValueList<TagAction*> actions = d->tagActions.values();

        for (TQValueList<TagAction*>::ConstIterator it = actions.begin(); it != actions.end(); ++it)
        {
            (*it)->setChecked(tagIds.contains((*it)->tag().id()));
        }
    }
}

void ActionManagerImpl::setTagSet(TagSet* tagSet)
{
    if (tagSet == d->tagSet)
        return;

    if (d->tagSet != 0)
    {
        disconnect(d->tagSet, TQT_SIGNAL(signalTagAdded(const Tag&)), this, TQT_SLOT(slotTagAdded(const Tag&)));
        disconnect(d->tagSet, TQT_SIGNAL(signalTagRemoved(const Tag&)), this, TQT_SLOT(slotTagRemoved(const Tag&)));
    }

    d->tagSet = tagSet;

    if (tagSet != 0)
    {
        connect(d->tagSet, TQT_SIGNAL(signalTagAdded(const Tag&)), this, TQT_SLOT(slotTagAdded(const Tag&)));
        connect(d->tagSet, TQT_SIGNAL(signalTagRemoved(const Tag&)), this, TQT_SLOT(slotTagRemoved(const Tag&)));
    }

    TQValueList<TagAction*> actions = d->tagActions.values();
    for (TQValueList<TagAction*>::ConstIterator it = actions.begin(); it != actions.end(); ++it)
    {
        d->tagMenu->remove(*it);
        delete *it;
    }


    d->tagActions.clear();

    //TODO: remove actions from menus, delete actions, clear maps

    if (tagSet != 0L)
    {
        TQValueList<Tag> list = tagSet->toMap().values();
        for (TQValueList<Tag>::ConstIterator it = list.begin(); it != list.end(); ++it)
            slotTagAdded(*it);
    }
}

void ActionManagerImpl::slotTagAdded(const Tag& tag)
{
    if (!Settings::showTaggingGUI())
        return;

    if (!d->tagActions.contains(tag.id()))
    {
        d->tagActions[tag.id()] = new TagAction(tag, d->view, TQT_SLOT(slotAssignTag(const Tag&, bool)), d->tagMenu);
        d->tagMenu->insert(d->tagActions[tag.id()]);
    }
}

void ActionManagerImpl::slotTagRemoved(const Tag& tag)
{
    if (!Settings::showTaggingGUI())
        return;

    TQString id = tag.id();
    TagAction* action = d->tagActions[id];
    d->tagMenu->remove(action);
    d->tagActions.remove(id);
    delete action;
}

void ActionManagerImpl::slotNodeSelected(TreeNode* node)
{
    if (node != 0)
        d->nodeSelectVisitor->visit(node);
}

ActionManagerImpl::ActionManagerImpl(Part* part, TQObject* parent, const char* name) : ActionManager(parent, name), d(new ActionManagerImplPrivate)
{
    d->nodeSelectVisitor = new NodeSelectVisitor(this);
    d->part = part;
    d->tagSet = 0;
    d->listTabWidget = 0;
    d->articleList = 0;
    d->trayIcon = 0;
    d->articleViewer = 0;
    d->view = 0;
    d->tabWidget = 0;
    d->tagMenu = 0;
    d->speakSelectedArticlesAction = 0;
    d->actionCollection = part->actionCollection();
    initPart();
}

ActionManagerImpl::~ActionManagerImpl()
{
    delete d->nodeSelectVisitor;
    delete d;
    d = 0;
}

void ActionManagerImpl::initTrayIcon(TrayIcon* trayIcon)
{
    if (d->trayIcon)
        return;
    else d->trayIcon = trayIcon;

    TDEPopupMenu* traypop = trayIcon->contextMenu();

    if (actionCollection()->action("feed_fetch_all"))
        actionCollection()->action("feed_fetch_all")->plug(traypop, 1);
    if (actionCollection()->action("akregator_configure_akregator"))
        actionCollection()->action("akregator_configure_akregator")->plug(traypop, 2);
}

void ActionManagerImpl::initPart()
{
    new TDEAction(i18n("&Import Feeds..."), "", "", d->part, TQT_SLOT(fileImport()), d->actionCollection, "file_import");
    new TDEAction(i18n("&Export Feeds..."), "", "", d->part, TQT_SLOT(fileExport()), d->actionCollection, "file_export");
    //new TDEAction(i18n("&Get Feeds From Web..."), "", "", d->part, TQT_SLOT(fileGetFeeds()), d->actionCollection, "file_getfromweb");

    new TDEAction(i18n("Send &Link Address..."), "mail_generic", "", d->part, TQT_SLOT(fileSendLink()), d->actionCollection, "file_sendlink");
    new TDEAction(i18n("Send &File..."), "mail_generic", "", d->part, TQT_SLOT(fileSendFile()), d->actionCollection, "file_sendfile");

    KStdAction::configureNotifications(d->part, TQT_SLOT(showKNotifyOptions()), d->actionCollection); // options_configure_notifications
    new TDEAction( i18n("Configure &Akregator..."), "configure", "", d->part, TQT_SLOT(showOptions()), d->actionCollection, "akregator_configure_akregator" );
}

void ActionManagerImpl::initView(View* view)
{
    if (d->view)
        return;
    else
        d->view = view;

    // tag actions
    new TDEAction(i18n("&New Tag..."), "", "",  d->view, TQT_SLOT(slotNewTag()), actionCollection(), "tag_new");

    // Feed/Feed Group popup menu
    new TDEAction(i18n("&Open Homepage"), "", "Ctrl+H",  d->view, TQT_SLOT(slotOpenHomepage()), actionCollection(), "feed_homepage");
    new TDEAction(i18n("&Add Feed..."), "bookmark_add", "Insert", d->view, TQT_SLOT(slotFeedAdd()), actionCollection(), "feed_add");
    new TDEAction(i18n("Ne&w Folder..."), "folder-new", "Shift+Insert", d->view, TQT_SLOT(slotFeedAddGroup()), actionCollection(), "feed_add_group");
    new TDEAction(i18n("&Delete Feed"), "edit-delete", "Alt+Delete", d->view, TQT_SLOT(slotFeedRemove()), actionCollection(), "feed_remove");
    new TDEAction(i18n("&Edit Feed..."), "edit", "F2", d->view, TQT_SLOT(slotFeedModify()), actionCollection(), "feed_modify");
        TDEActionMenu* vm = new TDEActionMenu( i18n( "&View Mode" ), actionCollection(), "view_mode" );

    TDERadioAction *ra = new TDERadioAction(i18n("&Normal View"), "view_top_bottom", "Ctrl+Shift+1", d->view, TQT_SLOT(slotNormalView()), actionCollection(), "normal_view");
    ra->setExclusiveGroup( "ViewMode" );
    vm->insert(ra);

    ra = new TDERadioAction(i18n("&Widescreen View"), "view_left_right", "Ctrl+Shift+2", d->view, TQT_SLOT(slotWidescreenView()), actionCollection(), "widescreen_view");
    ra->setExclusiveGroup( "ViewMode" );
    vm->insert(ra);

    ra = new TDERadioAction(i18n("C&ombined View"), "view_text", "Ctrl+Shift+3", d->view, TQT_SLOT(slotCombinedView()), actionCollection(), "combined_view");
    ra->setExclusiveGroup( "ViewMode" );
    vm->insert(ra);

    // toolbar / feed menu
    new TDEAction(i18n("&Fetch Feed"), "go-down", TDEStdAccel::shortcut(TDEStdAccel::Reload), d->view, TQT_SLOT(slotFetchCurrentFeed()), actionCollection(), "feed_fetch");
    new TDEAction(i18n("Fe&tch All Feeds"), "go-bottom", "Ctrl+L", d->view, TQT_SLOT(slotFetchAllFeeds()), actionCollection(), "feed_fetch_all");

    TDEAction* stopAction = new TDEAction(i18n( "&Abort Fetches" ), "process-stop", Key_Escape, Kernel::self()->fetchQueue(), TQT_SLOT(slotAbort()), actionCollection(), "feed_stop");
    stopAction->setEnabled(false);

    new TDEAction(i18n("&Mark Feed as Read"), "goto", "Ctrl+R", d->view, TQT_SLOT(slotMarkAllRead()), actionCollection(), "feed_mark_all_as_read");
    new TDEAction(i18n("Ma&rk All Feeds as Read"), "goto", "Ctrl+Shift+R", d->view, TQT_SLOT(slotMarkAllFeedsRead()), actionCollection(), "feed_mark_all_feeds_as_read");

    // Settings menu
    TDEToggleAction* sqf = new TDEToggleAction(i18n("Show Quick Filter"), TQString(), 0, d->view, TQT_SLOT(slotToggleShowQuickFilter()), actionCollection(), "show_quick_filter");
    sqf->setChecked( Settings::showQuickFilter() );

    new TDEAction( i18n("Open in Tab"), "tab_new", "Shift+Return", d->view, TQT_SLOT(slotOpenCurrentArticle()), actionCollection(), "article_open" );
    new TDEAction( i18n("Open in Background Tab"), TQString(), "tab_new", d->view, TQT_SLOT(slotOpenCurrentArticleBackgroundTab()), actionCollection(), "article_open_background_tab" );
    new TDEAction( i18n("Open in External Browser"), "window-new", "Ctrl+Shift+Return", d->view, TQT_SLOT(slotOpenCurrentArticleExternal()), actionCollection(), "article_open_external" );
    new TDEAction( i18n("Copy Link Address"), TQString(), TQString(), d->view, TQT_SLOT(slotCopyLinkAddress()), actionCollection(), "article_copy_link_address" );

    new TDEAction(i18n("Pre&vious Unread Article"), "", Key_Minus, d->view, TQT_SLOT(slotPrevUnreadArticle()),actionCollection(), "go_prev_unread_article");
    new TDEAction(i18n("Ne&xt Unread Article"), "", Key_Plus, d->view, TQT_SLOT(slotNextUnreadArticle()),actionCollection(), "go_next_unread_article");

    new TDEAction(i18n("&Delete"), "edit-delete", "Delete", d->view, TQT_SLOT(slotArticleDelete()), actionCollection(), "article_delete");

    if (Settings::showTaggingGUI())
    {
        d->tagMenu = new TDEActionMenu ( i18n( "&Set Tags" ), "rss_tag",  actionCollection(), "article_tagmenu" );
        d->tagMenu->setEnabled(false); // only enabled when articles are selected
    }
    TDEActionMenu* statusMenu = new TDEActionMenu ( i18n( "&Mark As" ),
                                    actionCollection(), "article_set_status" );

    d->speakSelectedArticlesAction = new TDEAction(i18n("&Speak Selected Articles"), "kttsd", "", d->view, TQT_SLOT(slotTextToSpeechRequest()), actionCollection(), "akr_texttospeech");

    TDEAction* abortTTS = new TDEAction(i18n( "&Stop Speaking" ), "media-playback-stop", Key_Escape, SpeechClient::self(), TQT_SLOT(slotAbortJobs()), actionCollection(), "akr_aborttexttospeech");
    abortTTS->setEnabled(false);

    connect(SpeechClient::self(), TQT_SIGNAL(signalActivated(bool)),
    abortTTS, TQT_SLOT(setEnabled(bool)));

    statusMenu->insert(new TDEAction(KGuiItem(i18n("as in: mark as read","&Read"), "",
                       i18n("Mark selected article as read")),
    "Ctrl+E", d->view, TQT_SLOT(slotSetSelectedArticleRead()),
    actionCollection(), "article_set_status_read"));

    statusMenu->insert(new TDEAction(KGuiItem(i18n("&New"), "",
                        i18n("Mark selected article as new")),
    "Ctrl+N", d->view, TQT_SLOT(slotSetSelectedArticleNew()),
    actionCollection(), "article_set_status_new" ));


    statusMenu->insert(new TDEAction(KGuiItem(i18n("&Unread"), "",
                       i18n("Mark selected article as unread")),
    "Ctrl+U", d->view, TQT_SLOT(slotSetSelectedArticleUnread()),
    actionCollection(), "article_set_status_unread"));

    TDEToggleAction* importantAction = new TDEToggleAction(i18n("&Mark as Important"), "flag", "Ctrl+I", actionCollection(), "article_set_status_important");
    importantAction->setCheckedState(i18n("Remove &Important Mark"));
    connect(importantAction, TQT_SIGNAL(toggled(bool)), d->view, TQT_SLOT(slotArticleToggleKeepFlag(bool)));


    new TDEAction( i18n("Move Node Up"), TQString(), "Shift+Alt+Up", view, TQT_SLOT(slotMoveCurrentNodeUp()), d->actionCollection, "feedstree_move_up" );
    new TDEAction( i18n("Move Node Down"), TQString(),  "Shift+Alt+Down", view, TQT_SLOT(slotMoveCurrentNodeDown()), d->actionCollection, "feedstree_move_down" );
    new TDEAction( i18n("Move Node Left"), TQString(), "Shift+Alt+Left", view, TQT_SLOT(slotMoveCurrentNodeLeft()), d->actionCollection, "feedstree_move_left" );
    new TDEAction( i18n("Move Node Right"), TQString(), "Shift+Alt+Right", view, TQT_SLOT(slotMoveCurrentNodeRight()), d->actionCollection, "feedstree_move_right");
}

void ActionManagerImpl::initArticleViewer(ArticleViewer* articleViewer)
{
    if (d->articleViewer)
        return;
    else
        d->articleViewer = articleViewer;
}

void ActionManagerImpl::initArticleListView(ArticleListView* articleList)
{
    if (d->articleList)
        return;
    else
        d->articleList = articleList;

    new TDEAction( i18n("&Previous Article"), TQString(), "Left", articleList, TQT_SLOT(slotPreviousArticle()), actionCollection(), "go_previous_article" );
    new TDEAction( i18n("&Next Article"), TQString(), "Right", articleList, TQT_SLOT(slotNextArticle()), actionCollection(), "go_next_article" );
}

void ActionManagerImpl::initListTabWidget(ListTabWidget* listTabWidget)
{
    if (d->listTabWidget)
        return;
    else
        d->listTabWidget = listTabWidget;

    new TDEAction(i18n("&Previous Feed"), "", "P", listTabWidget,  TQT_SLOT(slotPrevFeed()),actionCollection(), "go_prev_feed");
    new TDEAction(i18n("&Next Feed"), "", "N", listTabWidget, TQT_SLOT(slotNextFeed()),actionCollection(), "go_next_feed");
    new TDEAction(i18n("N&ext Unread Feed"), "", "Alt+Plus", listTabWidget, TQT_SLOT(slotNextUnreadFeed()),actionCollection(), "go_next_unread_feed");
    new TDEAction(i18n("Prev&ious Unread Feed"), "", "Alt+Minus", listTabWidget, TQT_SLOT(slotPrevUnreadFeed()),actionCollection(), "go_prev_unread_feed");

    new TDEAction( i18n("Go to Top of Tree"), TQString(), "Ctrl+Home", listTabWidget, TQT_SLOT(slotItemBegin()), d->actionCollection, "feedstree_home" );
    new TDEAction( i18n("Go to Bottom of Tree"), TQString(), "Ctrl+End", listTabWidget, TQT_SLOT(slotItemEnd()), d->actionCollection, "feedstree_end" );
    new TDEAction( i18n("Go Left in Tree"), TQString(), "Ctrl+Left", listTabWidget, TQT_SLOT(slotItemLeft()), d->actionCollection, "feedstree_left" );
    new TDEAction( i18n("Go Right in Tree"), TQString(), "Ctrl+Right", listTabWidget, TQT_SLOT(slotItemRight()), d->actionCollection, "feedstree_right" );
    new TDEAction( i18n("Go Up in Tree"), TQString(), "Ctrl+Up", listTabWidget, TQT_SLOT(slotItemUp()), d->actionCollection, "feedstree_up" );
    new TDEAction( i18n("Go Down in Tree"), TQString(), "Ctrl+Down", listTabWidget, TQT_SLOT(slotItemDown()), d->actionCollection, "feedstree_down" );
}

void ActionManagerImpl::initTabWidget(TabWidget* tabWidget)
{
    if (d->tabWidget)
        return;
    else
        d->tabWidget = tabWidget;

    new TDEAction(i18n("Select Next Tab"), "", "Ctrl+Period", d->tabWidget, TQT_SLOT(slotNextTab()),actionCollection(), "select_next_tab");
    new TDEAction(i18n("Select Previous Tab"), "", "Ctrl+Comma", d->tabWidget, TQT_SLOT(slotPreviousTab()),actionCollection(), "select_previous_tab");
    new TDEAction( i18n("Detach Tab"), "tab_breakoff", CTRL+SHIFT+Key_B, d->tabWidget, TQT_SLOT(slotDetachTab()), actionCollection(), "tab_detach" );
    new TDEAction( i18n("Copy Link Address"), TQString(), TQString(), d->tabWidget, TQT_SLOT(slotCopyLinkAddress()), actionCollection(), "tab_copylinkaddress" );
    new TDEAction( i18n("&Close Tab"), "tab_remove", TDEStdAccel::close(), d->tabWidget, TQT_SLOT(slotCloseTab()), actionCollection(), "tab_remove" );
}

TQWidget* ActionManagerImpl::container(const char* name)
{
    return d->part->factory()->container(name, d->part);
}


TDEActionCollection* ActionManagerImpl::actionCollection()
{
    return d->actionCollection;
}
TDEAction* ActionManagerImpl::action(const char* name, const char* classname)
{
    return d->actionCollection != 0 ? d->actionCollection->action(name, classname) : 0;
}

} // namespace Akregator

#include "actionmanagerimpl.moc"