00001
00034 #include "desktopalarmwidget.h"
00035
00036 #include <limits>
00037
00038 #include <QApplication>
00039 #include <QIcon>
00040 #include <QInputDialog>
00041 #include <QMessageBox>
00042 #include <QtConcurrentRun>
00043
00044 #include "alarmconfiguration.h"
00045 #include "alarmserverconnector.h"
00046 #include "beedo.h"
00047 #include "oldgcccompat.h"
00048
00049 using namespace AlarmNotifications;
00050
00051 DesktopAlarmWidget::DesktopAlarmWidget ( const bool activateBeedo )
00052 : _activateBeedo ( activateBeedo ),
00053 _asc ( nullptr ),
00054 _run ( true ),
00055 _alarmActive ( false )
00056 {
00057 if ( _activateBeedo )
00058 Beedo::instance();
00059 _asc = new AlarmServerConnector ( true, _activateBeedo );
00060 _iconThread = QtConcurrent::run ( this, &DesktopAlarmWidget::observeAlarmStatus );
00061 connect ( this, SIGNAL ( alarmStatusChanged() ), this, SLOT ( changeTrayIcon() ) );
00062 connect ( this, SIGNAL ( notificationSwitchChanged ( bool ) ), this, SLOT ( notificationSwitchChange ( bool ) ) );
00063 }
00064
00065 DesktopAlarmWidget::~DesktopAlarmWidget()
00066 {
00067 _run = false;
00068 _iconThread.waitForFinished();
00069 delete _asc;
00070 if ( _activateBeedo )
00071 Beedo::instance().destroy();
00072 }
00073
00074 DesktopAlarmWidget::DesktopAlarmWidgetStatus DesktopAlarmWidget::getStatus() const noexcept
00075 {
00076
00077
00078 if ( _asc == nullptr )
00079 {
00080 return Disabled;
00081 }
00082 else
00083 {
00084 if ( _alarmActive )
00085 return ActiveAlarm;
00086 else
00087 return ActiveOK;
00088 }
00089 }
00090
00091 void DesktopAlarmWidget::toggleNotifications()
00092 {
00093 boost::lock_guard<boost::mutex> concurrency_lock ( _ascmutex );
00094 if ( _asc == nullptr )
00095 {
00096 _asc = new AlarmServerConnector ( true, _activateBeedo );
00097 emit notificationSwitchChanged ( true );
00098 }
00099 else
00100 {
00101 delete _asc;
00102 _asc = nullptr;
00103 emit notificationSwitchChanged ( false );
00104 }
00105 }
00106
00107 void DesktopAlarmWidget::configureNotificationTimeout()
00108 {
00109 bool ok = false;
00110 const int rawanswer = QInputDialog::getInteger (
00111 nullptr,
00112 QString::fromUtf8 ( "Configure notification timeout" ),
00113 QString::fromUtf8 ( "Plese enter the time in seconds that should pass between\nthe reception of an alarm from the CSS Alarm Server\nand the display of a desktop notification:" ),
00114 AlarmConfiguration::instance().getDesktopNotificationTimeout(),
00115 1,
00116 std::numeric_limits<int>::max(),
00117 1,
00118 &ok
00119 );
00120 if ( !ok )
00121 return;
00122 if ( rawanswer < 1 )
00123 return;
00124 const unsigned int answer = ( unsigned int ) rawanswer;
00125 AlarmConfiguration::instance().setDesktopNotificationTimeout ( answer );
00126 AlarmConfiguration::instance().WriteConfiguration();
00127 AlarmConfiguration::instance().ReReadConfiguration();
00128 }
00129
00130 void DesktopAlarmWidget::showStatusMessage ( )
00131 {
00132 QString messagetext;
00133 QString messagetitle = QString::fromUtf8 ( "Alarm notifications desktop widget" );
00134 unsigned short messagetype;
00135 {
00136 boost::lock_guard<boost::mutex> concurrency_lock ( _ascmutex );
00137 if ( _asc == nullptr )
00138 {
00139 messagetype = 1;
00140 messagetext = QString::fromUtf8 ( "The Alarm notifications desktop widget is currently disabled, so informations about alarms are not recevied from the alarm server!" );
00141 }
00142 else
00143 {
00144 if ( _asc->getNumberOfAlarms() == 0 )
00145 {
00146 messagetype = 0;
00147 messagetext = QString::fromUtf8 ( "No alarms are known to the Alarm notifications desktop widget.\n\nPlease note that alarms triggered before the start or re-activation of this widget have not been received, so if you want to be sure that nothing is wrong, look at the alarm display in CSS." );
00148 }
00149 else
00150 {
00151 messagetype = 2;
00152 messagetext = QString::fromUtf8 ( "ATTENTION!\n\nThere are %1 alarm(s) active in the Detector Control System! For detailed information look at the alarm display in CSS!" ).arg ( _asc->getNumberOfAlarms() );
00153 }
00154 }
00155 }
00156 switch ( messagetype )
00157 {
00158 case 2:
00159 QMessageBox::critical (
00160 nullptr,
00161 messagetitle,
00162 messagetext
00163 );
00164 break;
00165 case 1:
00166 QMessageBox::warning (
00167 nullptr,
00168 messagetitle,
00169 messagetext
00170 );
00171 break;
00172 case 0:
00173 default:
00174 QMessageBox::information (
00175 nullptr,
00176 messagetitle,
00177 messagetext
00178 );
00179 }
00180 }
00181
00182 void DesktopAlarmWidget::exitApplication()
00183 {
00184 QApplication::exit ( 0 );
00185 }
00186
00187 void DesktopAlarmWidget::observeAlarmStatus()
00188 {
00189 while ( _run )
00190 {
00191 {
00192 boost::lock_guard<boost::mutex> concurrency_lock ( _ascmutex );
00193 if ( _asc != nullptr )
00194 {
00195 if ( _alarmActive && _asc->getNumberOfAlarms() == 0 )
00196 {
00197 _alarmActive = false;
00198 emit alarmStatusChanged();
00199 }
00200 if ( !_alarmActive && _asc->getNumberOfAlarms() != 0 )
00201 {
00202 _alarmActive = true;
00203 emit alarmStatusChanged();
00204 }
00205 }
00206 }
00207 usleep ( 500*1000 );
00208 }
00209 }
00210
00211
00212
00213 #include "desktopalarmwidget.moc"