1 /*************************************************************************** 2 * * 3 * This program is free software; you can redistribute it and/or modify * 4 * it under the terms of the GNU General Public License as published by * 5 * the Free Software Foundation; either version 3 of the License, or * 6 * (at your option) any later version. * 7 * * 8 ***************************************************************************/ 9 10 #pragma once 11 12 #include <QApplication> 13 #include <QEvent> 14 #include <QTimer> 15 #include <QSessionManager> 16 #include "WulforSettings.h" 17 #include "MainWindow.h" 18 #include "qtsingleapp/qtsinglecoreapplication.h" 19 20 class EiskaltEventFilter: public QObject{ 21 Q_OBJECT 22 public: EiskaltEventFilter()23 EiskaltEventFilter(): counter(0), has_activity(true) { 24 timer.setInterval(60000); 25 26 connect(&timer, SIGNAL(timeout()), this, SLOT(tick())); 27 28 timer.start(); 29 } 30 ~EiskaltEventFilter()31 virtual ~EiskaltEventFilter() {} 32 33 protected: eventFilter(QObject * obj,QEvent * event)34 virtual bool eventFilter(QObject *obj, QEvent *event){ 35 switch (event->type()){ 36 case QEvent::MouseButtonPress: 37 case QEvent::MouseButtonRelease: 38 case QEvent::MouseButtonDblClick: 39 case QEvent::MouseMove: 40 case QEvent::KeyPress: 41 case QEvent::KeyRelease: 42 case QEvent::Wheel: 43 { 44 has_activity = true; 45 counter = 0; 46 47 break; 48 } 49 default: 50 { 51 has_activity = false; 52 53 break; 54 } 55 } 56 57 return QObject::eventFilter(obj, event); 58 } 59 60 private Q_SLOTS: tick()61 void tick(){ 62 if (!has_activity) 63 ++counter; 64 65 if (WBGET(WB_APP_AUTOAWAY_BY_TIMER)){ 66 int mins = WIGET(WI_APP_AUTOAWAY_INTERVAL); 67 68 if (!mins) 69 return; 70 71 int mins_done = (counter*timer.interval()/1000)/60; 72 73 if (mins <= mins_done) 74 dcpp::Util::setAway(true); 75 } 76 else if (has_activity && !dcpp::Util::getManualAway()) 77 dcpp::Util::setAway(false); 78 } 79 80 private: 81 QTimer timer; 82 int counter; 83 bool has_activity; 84 }; 85 86 class EiskaltApp: public QtSingleCoreApplication { 87 Q_OBJECT 88 public: EiskaltApp(int & argc,char * argv[],const QString & uniqKey)89 EiskaltApp(int &argc, char *argv[], const QString& uniqKey): QtSingleCoreApplication(argc, argv, uniqKey) 90 { 91 installEventFilter(&ef); 92 } 93 commitData(QSessionManager & manager)94 void commitData(QSessionManager& manager){ 95 if (MainWindow::getInstance()){ 96 MainWindow::getInstance()->beginExit(); 97 MainWindow::getInstance()->close(); 98 } 99 100 manager.release(); 101 } 102 saveState(QSessionManager &)103 void saveState(QSessionManager &){ /** Do nothing */ } 104 105 private: 106 EiskaltEventFilter ef; 107 }; 108