2 * newalarmaction.cpp - menu action to select a new alarm type
4 * Copyright © 2007-2009,2011 by David Jarvie <djarvie@kde.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "newalarmaction.h"
24 #include "akonadimodel.h"
25 #include "collectionmodel.h"
26 #include "itemlistmodel.h"
27 #include "functions.h"
28 #include "shellprocess.h"
29 #include "templatemenuaction.h"
31 #include <kactionmenu.h>
32 #include <KLocalizedString>
33 #include <kstandardshortcut.h>
35 #include "kalarm_debug.h"
37 using namespace KAlarmCal
;
39 #define DISP_ICON QStringLiteral("window-new")
40 #define CMD_ICON QStringLiteral("new-command-alarm")
41 #define MAIL_ICON QStringLiteral("mail-message-new")
42 #define AUDIO_ICON QStringLiteral("new-audio-alarm")
43 #define TEMPLATE_ICON QStringLiteral("document-new-from-template")
44 #define DISP_KEY QKeySequence(Qt::CTRL + Qt::Key_D)
45 #define CMD_KEY QKeySequence(Qt::CTRL + Qt::Key_C)
46 #define MAIL_KEY QKeySequence(Qt::CTRL + Qt::Key_M)
47 #define AUDIO_KEY QKeySequence(Qt::CTRL + Qt::Key_U)
50 NewAlarmAction::NewAlarmAction(bool templates
, const QString
& label
, QObject
* parent
)
51 : KActionMenu(QIcon::fromTheme(QStringLiteral("document-new")), label
, parent
),
52 mTemplateAction(Q_NULLPTR
)
54 mDisplayAction
= new QAction(QIcon::fromTheme(DISP_ICON
), (templates
? i18nc("@item:inmenu", "&Display Alarm Template") : i18nc("@action", "New Display Alarm")), parent
);
55 menu()->addAction(mDisplayAction
);
56 mTypes
[mDisplayAction
] = EditAlarmDlg::DISPLAY
;
57 mCommandAction
= new QAction(QIcon::fromTheme(CMD_ICON
), (templates
? i18nc("@item:inmenu", "&Command Alarm Template") : i18nc("@action", "New Command Alarm")), parent
);
58 menu()->addAction(mCommandAction
);
59 mTypes
[mCommandAction
] = EditAlarmDlg::COMMAND
;
60 mEmailAction
= new QAction(QIcon::fromTheme(MAIL_ICON
), (templates
? i18nc("@item:inmenu", "&Email Alarm Template") : i18nc("@action", "New Email Alarm")), parent
);
61 menu()->addAction(mEmailAction
);
62 mTypes
[mEmailAction
] = EditAlarmDlg::EMAIL
;
63 mAudioAction
= new QAction(QIcon::fromTheme(AUDIO_ICON
), (templates
? i18nc("@item:inmenu", "&Audio Alarm Template") : i18nc("@action", "New Audio Alarm")), parent
);
64 menu()->addAction(mAudioAction
);
65 mTypes
[mAudioAction
] = EditAlarmDlg::AUDIO
;
68 mDisplayAction
->setShortcut(DISP_KEY
);
69 mCommandAction
->setShortcut(CMD_KEY
);
70 mEmailAction
->setShortcut(MAIL_KEY
);
71 mAudioAction
->setShortcut(AUDIO_KEY
);
73 // Include New From Template only in non-template menu
74 mTemplateAction
= new TemplateMenuAction(QIcon::fromTheme(TEMPLATE_ICON
), i18nc("@action", "New Alarm From &Template"), parent
);
75 menu()->addAction(mTemplateAction
);
76 connect(AkonadiModel::instance(), &AkonadiModel::collectionStatusChanged
, this, &NewAlarmAction::slotCalendarStatusChanged
);
77 connect(TemplateListModel::all(), &ItemListModel::haveEventsStatus
, this, &NewAlarmAction::slotCalendarStatusChanged
);
78 slotCalendarStatusChanged(); // initialise action states
81 connect(menu(), &QMenu::aboutToShow
, this, &NewAlarmAction::slotInitMenu
);
82 connect(menu(), &QMenu::triggered
, this, &NewAlarmAction::slotSelected
);
85 /******************************************************************************
86 * Called when the action is clicked.
88 void NewAlarmAction::slotInitMenu()
90 // Don't allow shell commands in kiosk mode
91 mCommandAction
->setEnabled(ShellProcess::authorised());
94 /******************************************************************************
95 * Called when an alarm type is selected from the New popup menu.
97 void NewAlarmAction::slotSelected(QAction
* action
)
99 QMap
<QAction
*, EditAlarmDlg::Type
>::ConstIterator it
= mTypes
.constFind(action
);
100 if (it
!= mTypes
.constEnd())
101 Q_EMIT
selected(it
.value());
104 /******************************************************************************
105 * Called when the status of a calendar has changed.
106 * Enable or disable the New From Template action appropriately.
108 void NewAlarmAction::slotCalendarStatusChanged()
110 // Find whether there are any writable active alarm calendars
111 bool active
= !CollectionControlModel::enabledCollections(CalEvent::ACTIVE
, true).isEmpty();
112 bool haveEvents
= TemplateListModel::all()->haveEvents();
113 mTemplateAction
->setEnabled(active
&& haveEvents
);