diff --git a/src/misc/settings.cpp b/src/misc/settings.cpp index e06c0d997..20a33b470 100644 --- a/src/misc/settings.cpp +++ b/src/misc/settings.cpp @@ -115,7 +115,8 @@ void Settings::load() useProxy = s.value("useProxy", false).toBool(); proxyAddr = s.value("proxyAddr", "").toString(); proxyPort = s.value("proxyPort", 0).toInt(); - currentProfile = s.value("currentProfile", "").toString(); + currentProfile = s.value("currentProfile", "").toString(); + autoAwayTime = s.value("autoAwayTime", 10).toInt(); s.endGroup(); s.beginGroup("Widgets"); @@ -221,6 +222,7 @@ void Settings::save(QString path) s.setValue("proxyAddr", proxyAddr); s.setValue("proxyPort", proxyPort); s.setValue("currentProfile", currentProfile); + s.setValue("autoAwayTime", autoAwayTime); s.endGroup(); s.beginGroup("Widgets"); @@ -442,6 +444,18 @@ void Settings::setEncryptLogs(bool newValue) encryptLogs = newValue; } +int Settings::getAutoAwayTime() const +{ + return autoAwayTime; +} + +void Settings::setAutoAwayTime(int newValue) +{ + if (newValue < 0) + newValue = 10; + autoAwayTime = newValue; +} + void Settings::setWidgetData(const QString& uniqueName, const QByteArray& data) { widgetSettings[uniqueName] = data; diff --git a/src/misc/settings.h b/src/misc/settings.h index cf88bde31..4e2146204 100644 --- a/src/misc/settings.h +++ b/src/misc/settings.h @@ -76,6 +76,9 @@ public: bool getEncryptLogs() const; void setEncryptLogs(bool newValue); + int getAutoAwayTime() const; + void setAutoAwayTime(int newValue); + QPixmap getSavedAvatar(const QString& ownerId); void saveAvatar(QPixmap& pic, const QString& ownerId); @@ -184,6 +187,8 @@ private: bool enableLogging; bool encryptLogs; + int autoAwayTime; + QHash widgetSettings; // GUI diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index 7a9146e47..d6ebfe41b 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include Widget *Widget::instance{nullptr}; @@ -107,6 +108,11 @@ Widget::Widget(QWidget *parent) // Disable some widgets until we're connected to the DHT ui->statusButton->setEnabled(false); + idleTimer = new QTimer(); + int mins = Settings::getInstance().getAutoAwayTime(); + if (mins > 0) + idleTimer->start(mins * 1000*60); + qRegisterMetaType("Status"); qRegisterMetaType("vpx_image"); qRegisterMetaType("uint8_t"); @@ -169,6 +175,7 @@ Widget::Widget(QWidget *parent) connect(setStatusAway, SIGNAL(triggered()), this, SLOT(setStatusAway())); connect(setStatusBusy, SIGNAL(triggered()), this, SLOT(setStatusBusy())); connect(&friendForm, SIGNAL(friendRequested(QString,QString)), this, SIGNAL(friendRequested(QString,QString))); + connect(idleTimer, &QTimer::timeout, this, &Widget::onUserAway); coreThread->start(); @@ -782,18 +789,49 @@ bool Widget::isFriendWidgetCurActiveWidget(Friend* f) bool Widget::event(QEvent * e) { - if (e->type() == QEvent::WindowActivate) - { - if (activeChatroomWidget != nullptr) - { - activeChatroomWidget->resetEventFlags(); - activeChatroomWidget->updateStatusLight(); - } + switch(e->type()) { + case QEvent::WindowActivate: + if (activeChatroomWidget != nullptr) + { + activeChatroomWidget->resetEventFlags(); + activeChatroomWidget->updateStatusLight(); + } + // http://qt-project.org/faq/answer/how_can_i_detect_a_period_of_no_user_interaction + // Detecting global inactivity, like Skype, is possible but not via Qt: + // http://stackoverflow.com/a/21905027/1497645 + case QEvent::MouseButtonPress: + case QEvent::MouseButtonRelease: + case QEvent::Wheel: + case QEvent::KeyPress: + case QEvent::KeyRelease: + if (autoAwayActive) + { + qDebug() << "Widget: auto away deactivated"; + autoAwayActive = false; + emit statusSet(Status::Online); + int mins = Settings::getInstance().getAutoAwayTime(); + if (mins > 0) + idleTimer->start(mins * 1000*60); + } + default: + break; } return QWidget::event(e); } +void Widget::onUserAway() +{ + if (Settings::getInstance().getAutoAwayTime() > 0 + && ui->statusButton->property("status").toString() == "online") // leave user-set statuses in place + { + qDebug() << "Widget: auto away activated"; + emit statusSet(Status::Away); + autoAwayActive = true; + } + idleTimer->stop(); +} + void Widget::setStatusOnline() { core->setStatus(Status::Online); diff --git a/src/widget/widget.h b/src/widget/widget.h index 572ba1922..4f50a133c 100644 --- a/src/widget/widget.h +++ b/src/widget/widget.h @@ -40,6 +40,7 @@ class Core; class Camera; class FriendListWidget; class MaskablePixmapWidget; +class QTimer; class Widget : public QMainWindow { @@ -109,6 +110,7 @@ private slots: void onGroupSendResult(int groupId, const QString& message, int result); void playRingtone(); void onIconClick(); + void onUserAway(); private: void hideMainForms(); @@ -132,6 +134,8 @@ private: FriendListWidget* contactListWidget; MaskablePixmapWidget* profilePicture; bool notify(QObject *receiver, QEvent *event); + bool autoAwayActive = false; + QTimer* idleTimer; }; #endif // WIDGET_H