Skip to content

Commit

Permalink
Merge pull request #40 from Shatur95/improve-error-messages
Browse files Browse the repository at this point in the history
Improve error messages
  • Loading branch information
Skycoder42 committed Jul 3, 2020
2 parents c373d83 + 483e073 commit 4f76792
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
14 changes: 10 additions & 4 deletions QHotkey/qhotkey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,10 @@ bool QHotkeyPrivate::addShortcutInvoked(QHotkey *hotkey)
QHotkey::NativeShortcut shortcut = hotkey->_nativeShortcut;

if(!shortcuts.contains(shortcut)) {
if(!registerShortcut(shortcut))
if(!registerShortcut(shortcut)) {
qCWarning(logQHotkey) << QHotkey::tr("Failed to register %1. Error: %2").arg(hotkey->shortcut().toString(), error);
return false;
}
}

shortcuts.insert(shortcut, hotkey);
Expand All @@ -296,9 +298,13 @@ bool QHotkeyPrivate::removeShortcutInvoked(QHotkey *hotkey)
return false;
hotkey->_registered = false;
emit hotkey->registeredChanged(true);
if(shortcuts.count(shortcut) == 0)
return unregisterShortcut(shortcut);
else
if(shortcuts.count(shortcut) == 0) {
if (!unregisterShortcut(shortcut)) {
qCWarning(logQHotkey) << QHotkey::tr("Failed to unregister %1. Error: %2").arg(hotkey->shortcut().toString(), error);
return false;
} else
return true;
} else
return true;
}

Expand Down
6 changes: 2 additions & 4 deletions QHotkey/qhotkey_mac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,7 @@ bool QHotkeyPrivateMac::registerShortcut(QHotkey::NativeShortcut shortcut)
0,
&eventRef);
if (status != noErr) {
qCWarning(logQHotkey) << "Failed to register hotkey. Error:"
<< status;
error = QString::number(status);
return false;
} else {
this->hotkeyRefs.insert(shortcut, eventRef);
Expand All @@ -241,8 +240,7 @@ bool QHotkeyPrivateMac::unregisterShortcut(QHotkey::NativeShortcut shortcut)
EventHotKeyRef eventRef = QHotkeyPrivateMac::hotkeyRefs.value(shortcut);
OSStatus status = UnregisterEventHotKey(eventRef);
if (status != noErr) {
qCWarning(logQHotkey) << "Failed to unregister hotkey. Error:"
<< status;
error = QString::number(status);
return false;
} else {
this->hotkeyRefs.remove(shortcut);
Expand Down
2 changes: 2 additions & 0 deletions QHotkey/qhotkey_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class QHOTKEY_SHARED_EXPORT QHotkeyPrivate : public QObject, public QAbstractNat
virtual bool registerShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement
virtual bool unregisterShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement

QString error;

private:
QHash<QPair<Qt::Key, Qt::KeyboardModifiers>, QHotkey::NativeShortcut> mapping;
QMultiHash<QHotkey::NativeShortcut, QHotkey*> shortcuts;
Expand Down
6 changes: 2 additions & 4 deletions QHotkey/qhotkey_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,7 @@ bool QHotkeyPrivateWin::registerShortcut(QHotkey::NativeShortcut shortcut)
if(ok)
return true;
else {
qCWarning(logQHotkey) << "Failed to register hotkey. Error:"
<< qPrintable(QHotkeyPrivateWin::formatWinError(::GetLastError()));
error = QHotkeyPrivateWin::formatWinError(::GetLastError());
return false;
}
}
Expand All @@ -279,8 +278,7 @@ bool QHotkeyPrivateWin::unregisterShortcut(QHotkey::NativeShortcut shortcut)
if(ok)
return true;
else {
qCWarning(logQHotkey) << "Failed to unregister hotkey. Error:"
<< qPrintable(QHotkeyPrivateWin::formatWinError(::GetLastError()));
error = QHotkeyPrivateWin::formatWinError(::GetLastError());
return false;
}
}
Expand Down
6 changes: 2 additions & 4 deletions QHotkey/qhotkey_x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@ bool QHotkeyPrivateX11::registerShortcut(QHotkey::NativeShortcut shortcut)
XSync(display, False);

if(errorHandler.hasError) {
qCWarning(logQHotkey) << "Failed to register hotkey. Error:"
<< qPrintable(errorHandler.errorString);
error = errorHandler.errorString;
this->unregisterShortcut(shortcut);
return false;
} else
Expand All @@ -194,8 +193,7 @@ bool QHotkeyPrivateX11::unregisterShortcut(QHotkey::NativeShortcut shortcut)
XSync(display, False);

if(errorHandler.hasError) {
qCWarning(logQHotkey) << "Failed to unregister hotkey. Error:"
<< qPrintable(errorHandler.errorString);
error = errorHandler.errorString;
return false;
} else
return true;
Expand Down

0 comments on commit 4f76792

Please sign in to comment.