1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

Merge branch 'newsystray'

Fixes #541
Fixes #659
Fixes #807
Fixes #579
This commit is contained in:
Tux3 / Mlkj / !Lev.uXFMLA 2015-01-29 17:46:47 +01:00
commit 5fd2828dfa
No known key found for this signature in database
GPG Key ID: 7E086DD661263264
8 changed files with 264 additions and 10 deletions

View File

@ -53,6 +53,23 @@ TIMESTAMP = $$system($1 2>null||echo 0||a;rm null;date +%s||echo 0) # I'm so sor
DEFINES += TIMESTAMP=$$TIMESTAMP
DEFINES += LOG_TO_FILE
contains(ENABLE_SYSTRAY_UNITY_BACKEND, YES) {
DEFINES += ENABLE_SYSTRAY_UNITY_BACKEND
INCLUDEPATH += "/usr/include/libappindicator-0.1"
INCLUDEPATH += "/usr/include/gtk-2.0"
INCLUDEPATH += "/usr/include/glib-2.0"
INCLUDEPATH += "/usr/lib/x86_64-linux-gnu/glib-2.0/include"
INCLUDEPATH += "/usr/include/cairo"
INCLUDEPATH += "/usr/include/pango-1.0"
INCLUDEPATH += "/usr/lib/x86_64-linux-gnu/gtk-2.0/include"
INCLUDEPATH += "/usr/include/gdk-pixbuf-2.0"
INCLUDEPATH += "/usr/include/atk-1.0"
INCLUDEPATH += "/usr/include/libdbusmenu-glib-0.4"
LIBS += -lgobject-2.0 -lappindicator -lgtk-x11-2.0
}
contains(DISABLE_PLATFORM_EXT, YES) {
} else {
@ -189,7 +206,9 @@ HEADERS += src/widget/form/addfriendform.h \
src/misc/serialize.h \
src/widget/form/settings/advancedform.h \
src/audio.h \
src/widget/callconfirmwidget.h
src/widget/callconfirmwidget.h \
src/widget/systemtrayicon.h \
src/widget/systemtrayicon_private.h
SOURCES += \
src/widget/form/addfriendform.cpp \
@ -257,7 +276,8 @@ SOURCES += \
src/misc/serialize.cpp \
src/widget/form/settings/advancedform.cpp \
src/audio.cpp \
src/widget/callconfirmwidget.cpp
src/widget/callconfirmwidget.cpp \
src/widget/systemtrayicon.cpp
contains(DEFINES, QTOX_FILTER_AUDIO) {
HEADERS += src/audiofilterer.h

View File

@ -29,12 +29,7 @@
#include <QList>
#include <QStyleFactory>
#ifdef Q_OS_LINUX
#define SHOW_SYSTEM_TRAY_DEFAULT (bool) false
#else // OS is not linux
#define SHOW_SYSTEM_TRAY_DEFAULT (bool) true
#endif
const QString Settings::OLDFILENAME = "settings.ini";
const QString Settings::FILENAME = "qtox.ini";

View File

@ -24,7 +24,7 @@ CallConfirmWidget::CallConfirmWidget(const QWidget *Anchor) :
setPalette(palette);
QVBoxLayout *layout = new QVBoxLayout(this);
QLabel *callLabel = new QLabel(tr("Incoming call..."), this);
QLabel *callLabel = new QLabel(QObject::tr("Incoming call..."), this);
callLabel->setAlignment(Qt::AlignHCenter);
QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Horizontal, this);
QPushButton *accept = new QPushButton(this), *reject = new QPushButton(this);

View File

@ -0,0 +1,162 @@
#include "systemtrayicon.h"
#include <QString>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QFile>
#include <QDebug>
#include "src/misc/settings.h"
SystemTrayIcon::SystemTrayIcon()
{
QString desktop = getenv("XDG_CURRENT_DESKTOP");
if (false);
#ifdef ENABLE_SYSTRAY_UNITY_BACKEND
else if (desktop.toLower() == "unity")
{
QString settingsDir = Settings::getSettingsDirPath();
QFile iconFile(settingsDir+"/icon.png");
if (iconFile.open(QIODevice::Truncate | QIODevice::WriteOnly))
{
QFile resIconFile(":/img/icon.png");
if (resIconFile.open(QIODevice::ReadOnly))
iconFile.write(resIconFile.readAll());
resIconFile.close();
iconFile.close();
}
backendType = SystrayBackendType::Unity;
unityMenu = gtk_menu_new();
unityIndicator = app_indicator_new_with_path(
"qTox",
"icon",
APP_INDICATOR_CATEGORY_APPLICATION_STATUS,
settingsDir.toStdString().c_str()
);
app_indicator_set_menu(unityIndicator, GTK_MENU(unityMenu));
}
#endif
else
{
qtIcon = new QSystemTrayIcon;
backendType = SystrayBackendType::Qt;
connect(qtIcon, &QSystemTrayIcon::activated, this, &SystemTrayIcon::activated);
}
}
QString SystemTrayIcon::extractIconToFile(QIcon icon, QString name)
{
QString iconPath;
(void) icon;
(void) name;
#ifdef ENABLE_SYSTRAY_UNITY_BACKEND
iconPath = Settings::getSettingsDirPath()+"/"+name+".png";
QSize iconSize = icon.actualSize(QSize{64,64});
icon.pixmap(iconSize).save(iconPath);
#endif
return iconPath;
}
void SystemTrayIcon::setContextMenu(QMenu* menu)
{
if (false);
#ifdef ENABLE_SYSTRAY_UNITY_BACKEND
else if (backendType == SystrayBackendType::Unity)
{
for (QAction* a : menu->actions())
{
gtk_image_menu_item_new();
QString aText = a->text().replace('&',"");
GtkWidget* item;
if (a->isSeparator())
item = gtk_menu_item_new();
else if (a->icon().isNull())
item = gtk_menu_item_new_with_label(aText.toStdString().c_str());
else
{
QString iconPath = extractIconToFile(a->icon(),"iconmenu"+a->icon().name());
GtkWidget* image = gtk_image_new_from_file(iconPath.toStdString().c_str());
item = gtk_image_menu_item_new_with_label(aText.toStdString().c_str());
gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
gtk_image_menu_item_set_always_show_image(GTK_IMAGE_MENU_ITEM(item),TRUE);
}
gtk_menu_shell_append(GTK_MENU_SHELL(unityMenu), item);
void (*callback)(GtkMenu*, gpointer data) = [](GtkMenu*, gpointer a)
{
((QAction*)a)->activate(QAction::Trigger);
};
g_signal_connect(item, "activate", G_CALLBACK(callback), a);
gtk_widget_show(item);
}
app_indicator_set_menu(unityIndicator, GTK_MENU(unityMenu));
DbusmenuServer *menuServer;
DbusmenuMenuitem *rootMenuItem;
g_object_get(unityIndicator, "dbus-menu-server", &menuServer, NULL);
g_object_get(menuServer, "root-node", &rootMenuItem, NULL);
void (*callback)(DbusmenuMenuitem *, gpointer) =
[](DbusmenuMenuitem *, gpointer data)
{
((SystemTrayIcon*)data)->activated(QSystemTrayIcon::Unknown);
};
g_signal_connect(rootMenuItem, "about-to-show", G_CALLBACK(callback), this);
}
#endif
else
{
qtIcon->setContextMenu(menu);
}
}
void SystemTrayIcon::show()
{
setVisible(true);
}
void SystemTrayIcon::hide()
{
setVisible(false);
}
void SystemTrayIcon::setVisible(bool newState)
{
if (false);
#ifdef ENABLE_SYSTRAY_UNITY_BACKEND
else if (backendType == SystrayBackendType::Unity)
{
if (newState)
app_indicator_set_status(unityIndicator, APP_INDICATOR_STATUS_ACTIVE);
else
app_indicator_set_status(unityIndicator, APP_INDICATOR_STATUS_PASSIVE);
}
#endif
else
{
if (newState)
qtIcon->show();
else
qtIcon->hide();
}
}
void SystemTrayIcon::setIcon(QIcon &&icon)
{
if (false);
#ifdef ENABLE_SYSTRAY_UNITY_BACKEND
else if (backendType == SystrayBackendType::Unity)
{
// Alternate file names or appindicator will not reload the icon
if (app_indicator_get_icon(unityIndicator) == QString("icon2"))
{
extractIconToFile(icon,"icon");
app_indicator_set_icon_full(unityIndicator, "icon","qtox");
}
else
{
extractIconToFile(icon,"icon2");
app_indicator_set_icon_full(unityIndicator, "icon2","qtox");
}
}
#endif
else
{
qtIcon->setIcon(icon);
}
}

View File

@ -0,0 +1,36 @@
#ifndef SYSTEMTRAYICON_H
#define SYSTEMTRAYICON_H
#include "systemtrayicon_private.h"
#include <QObject>
class QSystemTrayIcon;
class QMenu;
class SystemTrayIcon : public QObject
{
Q_OBJECT
public:
SystemTrayIcon();
void setContextMenu(QMenu* menu);
void show();
void hide();
void setVisible(bool);
void setIcon(QIcon&& icon);
signals:
void activated(QSystemTrayIcon::ActivationReason);
private:
QString extractIconToFile(QIcon icon, QString name="icon");
private:
SystrayBackendType backendType;
QSystemTrayIcon* qtIcon;
#ifdef ENABLE_SYSTRAY_UNITY_BACKEND
AppIndicator *unityIndicator;
GtkWidget *unityMenu;
#endif
};
#endif // SYSTEMTRAYICON_H

View File

@ -0,0 +1,35 @@
#ifndef SYSTEMTRAYICON_PRIVATE_H
#define SYSTEMTRAYICON_PRIVATE_H
#include <QSystemTrayIcon>
#ifdef ENABLE_SYSTRAY_UNITY_BACKEND
#ifdef signals
#undef signals
#endif
extern "C" {
#include <libappindicator/app-indicator.h>
#include <gtk/gtk.h>
#include <libdbusmenu-glib/server.h>
}
#define signals public
#endif
enum class SystrayBackendType
{
Qt,
#ifdef ENABLE_SYSTRAY_UNITY_BACKEND
Unity
#endif
};
union SystrayBackend
{
QSystemTrayIcon *qt;
#ifdef ENABLE_SYSTRAY_UNITY_BACKEND
AppIndicator *unity;
#endif
};
#endif // SYSTEMTRAYICON_PRIVATE_H

View File

@ -36,6 +36,7 @@
#include "src/autoupdate.h"
#include "src/audio.h"
#include "src/platform/timer.h"
#include "systemtrayicon.h"
#include <QMessageBox>
#include <QDebug>
#include <QFile>
@ -84,7 +85,7 @@ void Widget::init()
if (QSystemTrayIcon::isSystemTrayAvailable())
{
icon = new QSystemTrayIcon(this);
icon = new SystemTrayIcon;
updateTrayIcon();
trayMenu = new QMenu;
@ -621,6 +622,10 @@ void Widget::onIconClick(QSystemTrayIcon::ActivationReason reason)
case QSystemTrayIcon::MiddleClick:
hide();
break;
case QSystemTrayIcon::Unknown:
if (isHidden())
forceShow();
break;
default:
break;
}

View File

@ -44,6 +44,7 @@ class FriendListWidget;
class MaskablePixmapWidget;
class QTimer;
class QTranslator;
class SystemTrayIcon;
class Widget : public QMainWindow
{
@ -149,7 +150,7 @@ private:
void saveSplitterGeometry();
QString askProfiles();
QString detectProfile();
QSystemTrayIcon *icon;
SystemTrayIcon *icon;
QMenu *trayMenu;
QAction *statusOnline,
*statusAway,