1 /*
2  * globalshortcutmanager.cpp - Class managing global shortcuts
3  * Copyright (C) 2006  Maciej Niedzielski
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  */
20 
21 #include "globalshortcutmanager.h"
22 
23 #include <QCoreApplication>
24 
25 #include "globalshortcuttrigger.h"
26 
27 /**
28  * \brief Constructs new GlobalShortcutManager.
29  */
GlobalShortcutManager()30 GlobalShortcutManager::GlobalShortcutManager()
31 	: QObject(QCoreApplication::instance())
32 {
33 }
34 
~GlobalShortcutManager()35 GlobalShortcutManager::~GlobalShortcutManager()
36 {
37 	clear();
38 }
39 
40 GlobalShortcutManager* GlobalShortcutManager::instance_ = 0;
41 
42 /**
43  * \brief Returns the instance of GlobalShortcutManager.
44  */
instance()45 GlobalShortcutManager* GlobalShortcutManager::instance()
46 {
47 	if (!instance_)
48 		instance_ = new GlobalShortcutManager();
49 	return instance_;
50 }
51 
52 /**
53  * \brief Connects a key sequence with a slot.
54  * \param key, global shortcut to be connected
55  * \param receiver, object which should receive the notification
56  * \param slot, the SLOT() of the \a receiver which should be triggerd if the \a key is activated
57  */
connect(const QKeySequence & key,QObject * receiver,const char * slot)58 void GlobalShortcutManager::connect(const QKeySequence& key, QObject* receiver, const char* slot)
59 {
60 	KeyTrigger* t = instance()->triggers_[key];
61 	if (!t) {
62 		t = new KeyTrigger(key);
63 		instance()->triggers_.insert(key, t);
64 	}
65 
66 	QObject::connect(t, SIGNAL(triggered()), receiver, slot);
67 }
68 
69 /**
70  * \brief Disonnects a key sequence from a slot.
71  * \param key, global shortcut to be disconnected
72  * \param receiver, object which \a slot is about to be disconnected
73  * \param slot, the SLOT() of the \a receiver which should no longer be triggerd if the \a key is activated
74  */
disconnect(const QKeySequence & key,QObject * receiver,const char * slot)75 void GlobalShortcutManager::disconnect(const QKeySequence& key, QObject* receiver, const char* slot)
76 {
77 	KeyTrigger* t = instance()->triggers_[key];
78 	if (!t) {
79 		return;
80 	}
81 
82 	QObject::disconnect(t, SIGNAL(triggered()), receiver, slot);
83 
84 	if (!t->isUsed()) {
85 		delete instance()->triggers_.take(key);
86 	}
87 }
88 
clear()89 void GlobalShortcutManager::clear()
90 {
91 	foreach (KeyTrigger* t, instance()->triggers_)
92 		delete t;
93 	instance()->triggers_.clear();
94 }
95