Using same notification options for all notification managers.

This commit is contained in:
John Preston 2016-10-08 11:38:53 +03:00
parent 6798b86655
commit 377d333f1c
11 changed files with 64 additions and 41 deletions

View File

@ -252,7 +252,14 @@ void MainWindow::psSetupTrayIcon() {
trayIcon->setToolTip(str_const_toString(AppName));
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(toggleTray(QSystemTrayIcon::ActivationReason)), Qt::UniqueConnection);
// This is very important for native notifications via libnotify!
// Some notification servers compose several notifications with a "Reply"
// action into one and after that a click on "Reply" button does not call
// the specified callback from any of the sent notification - libnotify
// just ignores ibus messages, but Qt tray icon at least emits this signal.
connect(trayIcon, SIGNAL(messageClicked()), this, SLOT(showFromTray()));
App::wnd()->updateTrayMenu();
}
psUpdateCounter();

View File

@ -247,7 +247,7 @@ class Manager::Impl {
public:
bool init();
void showNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, bool showUserpic, const QString &msg, bool showReplyButton);
void showNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, const QString &msg, bool hideNameAndPhoto, bool hideReplyButton);
void clearAll();
void clearFromHistory(History *history);
void clearNotification(PeerId peerId, MsgId msgId);
@ -267,7 +267,7 @@ private:
MsgId msgId = 0;
QString title;
QString body;
bool showUserpic = false;
bool hideNameAndPhoto = false;
};
QString _serverName;
@ -327,7 +327,7 @@ bool Manager::Impl::init() {
return !_serverName.isEmpty();
}
void Manager::Impl::showNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, bool showUserpic, const QString &msg, bool showReplyButton) {
void Manager::Impl::showNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, const QString &msg, bool hideNameAndPhoto, bool hideReplyButton) {
auto titleText = escapeNotificationHtml(title);
auto subtitleText = escapeNotificationHtml(subtitle);
auto msgText = escapeNotificationHtml(msg);
@ -341,7 +341,7 @@ void Manager::Impl::showNotification(PeerData *peer, MsgId msgId, const QString
notification.msgId = msgId;
notification.title = titleText;
notification.body = bodyText;
notification.showUserpic = showUserpic;
notification.hideNameAndPhoto = hideNameAndPhoto;
_queuedNotifications.push_back(notification);
showNextNotification();
@ -378,10 +378,10 @@ void Manager::Impl::showNextNotification() {
}
StorageKey key;
if (data.showUserpic) {
key = data.peer->userpicUniqueKey();
} else {
if (data.hideNameAndPhoto) {
key = StorageKey(0, 0);
} else {
key = data.peer->userpicUniqueKey();
}
notification->setImage(_cachedUserpics.get(key, data.peer));
@ -475,8 +475,8 @@ bool Manager::hasActionsSupport() const {
Manager::~Manager() = default;
void Manager::doShowNativeNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, bool showUserpic, const QString &msg, bool showReplyButton) {
_impl->showNotification(peer, msgId, title, subtitle, showUserpic, msg, showReplyButton);
void Manager::doShowNativeNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, const QString &msg, bool hideNameAndPhoto, bool hideReplyButton) {
_impl->showNotification(peer, msgId, title, subtitle, msg, hideNameAndPhoto, hideReplyButton);
}
void Manager::doClearAllFast() {

View File

@ -48,7 +48,7 @@ public:
~Manager();
protected:
void doShowNativeNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, bool showUserpic, const QString &msg, bool showReplyButton) override;
void doShowNativeNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, const QString &msg, bool hideNameAndPhoto, bool hideReplyButton) override;
void doClearAllFast() override;
void doClearFromHistory(History *history) override;

View File

@ -42,7 +42,7 @@ public:
~Manager();
protected:
void doShowNativeNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, bool showUserpic, const QString &msg, bool showReplyButton) override;
void doShowNativeNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, const QString &msg, bool hideNameAndPhoto, bool hideReplyButton) override;
void doClearAllFast() override;
void doClearFromHistory(History *history) override;

View File

@ -118,7 +118,7 @@ void defaultNotificationShown(QWidget *widget) {
class Manager::Impl {
public:
Impl();
void showNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, bool showUserpic, const QString &msg, bool showReplyButton);
void showNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, const QString &msg, bool hideNameAndPhoto, bool hideReplyButton);
void clearAll();
void clearFromHistory(History *history);
void updateDelegate();
@ -133,7 +133,7 @@ private:
Manager::Impl::Impl() : _delegate([[NotificationDelegate alloc] init]) {
}
void Manager::Impl::showNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, bool showUserpic, const QString &msg, bool showReplyButton) {
void Manager::Impl::showNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, const QString &msg, bool hideNameAndPhoto, bool hideReplyButton) {
@autoreleasepool {
NSUserNotification *notification = [[[NSUserNotification alloc] init] autorelease];
@ -147,13 +147,13 @@ void Manager::Impl::showNotification(PeerData *peer, MsgId msgId, const QString
[notification setTitle:Q2NSString(title)];
[notification setSubtitle:Q2NSString(subtitle)];
[notification setInformativeText:Q2NSString(msg)];
if (showUserpic && [notification respondsToSelector:@selector(setContentImage:)]) {
if (!hideNameAndPhoto && [notification respondsToSelector:@selector(setContentImage:)]) {
auto userpic = peer->genUserpic(st::notifyMacPhotoSize);
NSImage *img = [qt_mac_create_nsimage(userpic) autorelease];
[notification setContentImage:img];
}
if (showReplyButton && [notification respondsToSelector:@selector(setHasReplyButton:)]) {
if (!hideReplyButton && [notification respondsToSelector:@selector(setHasReplyButton:)]) {
[notification setHasReplyButton:YES];
}
@ -214,8 +214,8 @@ void Manager::updateDelegate() {
Manager::~Manager() = default;
void Manager::doShowNativeNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, bool showUserpic, const QString &msg, bool showReplyButton) {
_impl->showNotification(peer, msgId, title, subtitle, showUserpic, msg, showReplyButton);
void Manager::doShowNativeNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, const QString &msg, bool hideNameAndPhoto, bool hideReplyButton) {
_impl->showNotification(peer, msgId, title, subtitle, msg, hideNameAndPhoto, hideReplyButton);
}
void Manager::doClearAllFast() {

View File

@ -337,7 +337,7 @@ void finish() {
class Manager::Impl {
public:
bool showNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, bool showUserpic, const QString &msg, bool showReplyButton);
bool showNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, const QString &msg, bool hideNameAndPhoto, bool hideReplyButton);
void clearAll();
void clearFromHistory(History *history);
void beforeNotificationActivated(PeerId peerId, MsgId msgId);
@ -403,7 +403,7 @@ void Manager::Impl::clearNotification(PeerId peerId, MsgId msgId) {
}
}
bool Manager::Impl::showNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, bool showUserpic, const QString &msg, bool showReplyButton) {
bool Manager::Impl::showNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, const QString &msg, bool hideNameAndPhoto, bool hideReplyButton) {
if (!_notificationManager || !_notifier || !_notificationFactory) return false;
ComPtr<IXmlDocument> toastXml;
@ -416,10 +416,10 @@ bool Manager::Impl::showNotification(PeerData *peer, MsgId msgId, const QString
if (!SUCCEEDED(hr)) return false;
StorageKey key;
if (showUserpic) {
key = peer->userpicUniqueKey();
} else {
if (hideNameAndPhoto) {
key = StorageKey(0, 0);
} else {
key = peer->userpicUniqueKey();
}
auto userpicPath = _cachedUserpics.get(key, peer);
auto userpicPathWide = QDir::toNativeSeparators(userpicPath).toStdWString();
@ -514,8 +514,8 @@ void Manager::clearNotification(PeerId peerId, MsgId msgId) {
Manager::~Manager() = default;
void Manager::doShowNativeNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, bool showUserpic, const QString &msg, bool showReplyButton) {
_impl->showNotification(peer, msgId, title, subtitle, showUserpic, msg, showReplyButton);
void Manager::doShowNativeNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, const QString &msg, bool hideNameAndPhoto, bool hideReplyButton) {
_impl->showNotification(peer, msgId, title, subtitle, msg, hideNameAndPhoto, hideReplyButton);
}
void Manager::doClearAllFast() {

View File

@ -44,7 +44,7 @@ public:
~Manager();
protected:
void doShowNativeNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, bool showUserpic, const QString &msg, bool showReplyButton) override;
void doShowNativeNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, const QString &msg, bool hideNameAndPhoto, bool hideReplyButton) override;
void doClearAllFast() override;
void doClearFromHistory(History *history) override;
void onBeforeNotificationActivated(PeerId peerId, MsgId msgId) override;

View File

@ -47,6 +47,16 @@ void finish() {
Default::finish();
}
Manager::DisplayOptions Manager::getNotificationOptions(HistoryItem *item) {
auto hideEverything = (App::passcoded() || Global::ScreenIsLocked());
DisplayOptions result;
result.hideNameAndPhoto = hideEverything || (Global::NotifyView() > dbinvShowName);
result.hideMessageText = hideEverything || (Global::NotifyView() > dbinvShowPreview);
result.hideReplyButton = result.hideMessageText || !item || !item->history()->peer->canWrite();
return result;
}
void Manager::notificationActivated(PeerId peerId, MsgId msgId) {
onBeforeNotificationActivated(peerId, msgId);
if (auto window = App::wnd()) {
@ -87,18 +97,13 @@ void Manager::notificationReplied(PeerId peerId, MsgId msgId, const QString &rep
}
void NativeManager::doShowNotification(HistoryItem *item, int forwardedCount) {
auto hideEverything = (App::passcoded() || Global::ScreenIsLocked());
auto hideName = hideEverything || (Global::NotifyView() > dbinvShowName);
auto hidePreview = hideEverything || (Global::NotifyView() > dbinvShowPreview);
auto options = getNotificationOptions(item);
QString title = hideName ? qsl("Telegram Desktop") : item->history()->peer->name;
QString subtitle = hideName ? QString() : item->notificationHeader();
bool showUserpic = hideName ? false : true;
QString title = options.hideNameAndPhoto ? qsl("Telegram Desktop") : item->history()->peer->name;
QString subtitle = options.hideNameAndPhoto ? QString() : item->notificationHeader();
QString text = options.hideMessageText ? lang(lng_notification_preview) : (forwardedCount < 2 ? item->notificationText() : lng_forward_messages(lt_count, forwardedCount));
QString msg = hidePreview ? lang(lng_notification_preview) : (forwardedCount < 2 ? item->notificationText() : lng_forward_messages(lt_count, forwardedCount));
bool showReplyButton = hidePreview ? false : item->history()->peer->canWrite();
doShowNativeNotification(item->history()->peer, item->id, title, subtitle, showUserpic, msg, showReplyButton);
doShowNativeNotification(item->history()->peer, item->id, title, subtitle, text, options.hideNameAndPhoto, options.hideReplyButton);
}
} // namespace Notifications

View File

@ -53,6 +53,13 @@ public:
void notificationActivated(PeerId peerId, MsgId msgId);
void notificationReplied(PeerId peerId, MsgId msgId, const QString &reply);
struct DisplayOptions {
bool hideNameAndPhoto;
bool hideMessageText;
bool hideReplyButton;
};
static DisplayOptions getNotificationOptions(HistoryItem *item);
virtual ~Manager() = default;
protected:
@ -81,7 +88,7 @@ protected:
}
void doShowNotification(HistoryItem *item, int forwardedCount) override;
virtual void doShowNativeNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, bool showUserpic, const QString &msg, bool showReplyButton) = 0;
virtual void doShowNativeNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, const QString &msg, bool hideNameAndPhoto, bool hideReplyButton) = 0;
};

View File

@ -608,6 +608,9 @@ void Notification::actionsOpacityCallback() {
void Notification::updateNotifyDisplay() {
if (!_history || !_peer || (!_item && _forwardedCount < 2)) return;
auto options = Manager::getNotificationOptions(_item);
_hideReplyButton = options.hideReplyButton;
int32 w = width(), h = height();
QImage img(w * cIntRetinaFactor(), h * cIntRetinaFactor(), QImage::Format_ARGB32_Premultiplied);
if (cRetina()) img.setDevicePixelRatio(cRetinaFactor());
@ -620,7 +623,7 @@ void Notification::updateNotifyDisplay() {
p.fillRect(st::notifyBorderWidth, h - st::notifyBorderWidth, w - st::notifyBorderWidth, st::notifyBorderWidth, st::notifyBorder);
p.fillRect(0, st::notifyBorderWidth, st::notifyBorderWidth, h - st::notifyBorderWidth, st::notifyBorder);
if (!App::passcoded() && Global::NotifyView() <= dbinvShowName) {
if (!options.hideNameAndPhoto) {
_history->peer->loadUserpic(true, true);
_history->peer->paintUserpicLeft(p, st::notifyPhotoSize, st::notifyPhotoPos.x(), st::notifyPhotoPos.y(), width());
} else {
@ -632,14 +635,14 @@ void Notification::updateNotifyDisplay() {
int32 itemWidth = w - st::notifyPhotoPos.x() - st::notifyPhotoSize - st::notifyTextLeft - st::notifyClosePos.x() - st::notifyClose.width;
QRect rectForName(st::notifyPhotoPos.x() + st::notifyPhotoSize + st::notifyTextLeft, st::notifyTextTop, itemWidth, st::msgNameFont->height);
if (!App::passcoded() && Global::NotifyView() <= dbinvShowName) {
if (!options.hideNameAndPhoto) {
if (auto chatTypeIcon = Dialogs::Layout::ChatTypeIcon(_history->peer, false)) {
chatTypeIcon->paint(p, rectForName.topLeft(), w);
rectForName.setLeft(rectForName.left() + st::dialogsChatTypeSkip);
}
}
if (!App::passcoded() && Global::NotifyView() <= dbinvShowPreview) {
if (!options.hideMessageText) {
const HistoryItem *textCachedFor = 0;
Text itemTextCache(itemWidth);
QRect r(st::notifyPhotoPos.x() + st::notifyPhotoSize + st::notifyTextLeft, st::notifyItemTop + st::msgNameFont->height, itemWidth, 2 * st::dialogsTextFont->height);
@ -665,7 +668,7 @@ void Notification::updateNotifyDisplay() {
}
p.setPen(st::dialogsNameFg);
if (!App::passcoded() && Global::NotifyView() <= dbinvShowName) {
if (!options.hideNameAndPhoto) {
_history->peer->dialogName().drawElided(p, rectForName.left(), rectForName.top(), rectForName.width());
} else {
p.setFont(st::msgNameFont);
@ -704,7 +707,7 @@ void Notification::itemRemoved(HistoryItem *deleted) {
}
bool Notification::canReply() const {
return (_item != nullptr) && !App::passcoded() && (Global::NotifyView() <= dbinvShowPreview);
return !_hideReplyButton && (_item != nullptr) && !App::passcoded() && (Global::NotifyView() <= dbinvShowPreview);
}
void Notification::unlinkHistoryInManager() {

View File

@ -229,6 +229,7 @@ private:
QPixmap _cache;
bool _hideReplyButton = false;
bool _actionsVisible = false;
FloatAnimation a_actionsOpacity;
QPixmap _buttonsCache;