File indexing completed on 2019-04-16 14:37:08

0001 #ifndef COMPONENT_H
0002 #define COMPONENT_H
0003 /* Copyright (C) 2008 Michael Jansen <kde@michael-jansen.biz>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018    Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "globalshortcut.h"
0022 #include "kglobalshortcutinfo.h"
0023 
0024 #include "kconfiggroup.h"
0025 
0026 #include <QObject>
0027 #include <QHash>
0028 
0029 class GlobalShortcut;
0030 class GlobalShortcutContext;
0031 class GlobalShortcutsRegistry;
0032 
0033 
0034 namespace KdeDGlobalAccel {
0035 
0036 /**
0037  * @author Michael Jansen <kde@michael-jansen.biz>
0038  */
0039 class Component : public QObject
0040     {
0041     Q_OBJECT
0042 
0043     Q_CLASSINFO("D-Bus Interface", "org.kde.kglobalaccel.Component")
0044 
0045     Q_SCRIPTABLE Q_PROPERTY( QString friendlyName READ friendlyName )
0046     Q_SCRIPTABLE Q_PROPERTY( QString uniqueName READ uniqueName )
0047 
0048 public:
0049 
0050     //! Creates a new component. The component will be registered with @p
0051     //! registry if specified and registered with dbus.
0052     Component(
0053             const QString &uniqueName,
0054             const QString &friendlyName,
0055             GlobalShortcutsRegistry *registry = nullptr);
0056 
0057     ~Component();
0058 
0059     bool activateGlobalShortcutContext(const QString &uniqueName);
0060 
0061     void activateShortcuts();
0062 
0063     //! Returns all shortcuts in context @context
0064     QList<GlobalShortcut *> allShortcuts(const QString &context = QStringLiteral("default")) const;
0065 
0066     //! Creates the new global shortcut context @p context
0067     bool createGlobalShortcutContext(const QString &context, const QString &friendlyName=QString());
0068 
0069     //! Return the current context
0070     GlobalShortcutContext* currentContext();
0071 
0072     //! Return uniqueName converted to a valid dbus path
0073     QDBusObjectPath dbusPath() const;
0074 
0075     //! Deactivate all currently active shortcuts
0076     void deactivateShortcuts(bool temporarily=false);
0077 
0078     //! Returns the friendly name
0079     QString friendlyName() const;
0080 
0081     //! Returns the currently active shortcut for key
0082     GlobalShortcut *getShortcutByKey(int key) const;
0083 
0084     //! Returns the shortcut context @p name or nullptr
0085     GlobalShortcutContext *shortcutContext(const QString &name);
0086     GlobalShortcutContext const *shortcutContext(const QString &name) const;
0087 
0088     /**
0089      * Returns the list of shortcuts (different context) registered with @p
0090      * key.
0091      */
0092     QList<GlobalShortcut *> getShortcutsByKey(int key) const;
0093 
0094     //! Returns the shortcut by unique name. Only the active context is
0095     //! searched.
0096     GlobalShortcut *getShortcutByName(
0097             const QString &uniqueName,
0098             const QString &context = QStringLiteral("default")) const;
0099 
0100     /**
0101      * Check if @a key is available for component @p component
0102      */
0103     bool isShortcutAvailable(
0104             int key,
0105             const QString &component,
0106             const QString &context) const;
0107 
0108     //! Load the settings from config group @p config
0109     void loadSettings(KConfigGroup &config);
0110 
0111     //! Sets the human readable name for this component.
0112     void setFriendlyName(const QString &);
0113 
0114     QString uniqueName() const;
0115 
0116     //! Unregister @a shortcut. This will remove its siblings from all contexts
0117     void unregisterShortcut(const QString &uniqueName);
0118 
0119     void writeSettings(KConfigGroup &config) const;
0120 
0121 protected:
0122     /**
0123      * Create a new globalShortcut by its name
0124      * @param uniqueName internal unique name to identify the shortcut
0125      * @param friendlyName name for the shortcut to be presented to the user
0126      * @param shortcutString string representation of the shortcut, such as "CTRL+S"
0127      * @param defaultShortcutString string representation of the default shortcut,
0128      *                   such as "CTRL+S", when the user choses to reset to default
0129      *                   the keyboard shortcut will return to this one.
0130      */
0131     GlobalShortcut *registerShortcut(const QString &uniqueName, const QString &friendlyName, const QString &shortcutString, const QString &defaultShortcutString);
0132 
0133 public Q_SLOTS:
0134 
0135     // For dbus Q_SCRIPTABLE has to be on slots. Scriptable methods are not
0136     // exported.
0137 
0138     /**
0139      * Remove all currently not used global shortcuts registrations for this
0140      * component and if nothing is left the component too.
0141      *
0142      * If the method returns true consider all information previously acquired
0143      * from this component as void.
0144      *
0145      * The method will cleanup in all contexts.
0146      *
0147      * @return @c true if a change was made, @c false if not.
0148      */
0149     Q_SCRIPTABLE virtual bool cleanUp();
0150 
0151     /**
0152      * Check if the component is currently active.
0153      *
0154      * A component is active if at least one of it's global shortcuts is
0155      * currently present.
0156      */
0157     Q_SCRIPTABLE bool isActive() const;
0158 
0159     //! Get all shortcutnames living in @a context
0160     Q_SCRIPTABLE QStringList shortcutNames(const QString &context = "default") const;
0161 
0162     //! Returns all shortcut in @a context
0163     Q_SCRIPTABLE QList<KGlobalShortcutInfo> allShortcutInfos(const QString &context = "default") const;
0164 
0165     //! Returns the shortcut contexts available for the component.
0166     Q_SCRIPTABLE QStringList getShortcutContexts() const;
0167 
0168     virtual void emitGlobalShortcutPressed(const GlobalShortcut &shortcut);
0169 
0170     Q_SCRIPTABLE void invokeShortcut(const QString &shortcutName, const QString &context = "default");
0171 
0172 Q_SIGNALS:
0173 
0174     //! Signals that a action for this component was triggered
0175     Q_SCRIPTABLE void globalShortcutPressed(const QString &componentUnique, const QString &shortcutUnique, qlonglong timestamp);
0176 
0177 private:
0178 
0179     QString _uniqueName;
0180     //the name as it would be found in a magazine article about the application,
0181     //possibly localized if a localized name exists.
0182     QString _friendlyName;
0183 
0184     GlobalShortcutsRegistry *_registry;
0185 
0186     GlobalShortcutContext *_current;
0187     QHash<QString, GlobalShortcutContext *> _contexts;
0188     };
0189 
0190 }
0191 
0192 
0193 #endif /* #ifndef COMPONENT_H */