summaryrefslogtreecommitdiffstats
path: root/src/lmsensorsdock.cpp
blob: b5321abdd805fd705fa5ec64ada5da7703bcc271 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/***************************************************************************
                          lmsensorsdock.cpp  -  description
                             -------------------
    begin                : Sun Sep 23 2001
    copyright            : (C) 2001 by Miguel Novas
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "lmsensorsdock.h"

#include <ntqobjectlist.h>
#include <tdelocale.h>
#include <kiconloader.h>
#include <kapp.h>
#include <tdeaboutapplication.h>

#include <twin.h>
#include <netwm.h>

LMSensorsDock::LMSensorsDock(bool fNoDock, TQObject *parent, const char *name): TQObject(parent,name)
{
 noDock= fNoDock;
 
 sensors= new LMSensors;
 sensors->setMonitorized(true);

 connect(sensors,SIGNAL(configChanged(const char *)),this,SLOT(updateItemDock(const char *)));

 createAlarmControl();
 createWidgets();
 if(!noDock) createDockWidgets();
 createMenu();
}


LMSensorsDock::~LMSensorsDock()
{
 saveConfig();
 if(sensorsWidget) delete sensorsWidget;
 if(sensorsCfg) delete sensorsCfg;
 if(sensorsAlarm) delete sensorsAlarm;
 delete menu;
 if(sensors) delete sensors;
}


void LMSensorsDock::createMenu()
{
  menu= new TDEPopupMenu;
  menu->insertTitle(kapp->miniIcon(),"KSensors");
  menu->insertItem(SmallIcon("configure"),i18n("&Configure"), this, SLOT(createConfigWidget() ) ,0,101 );
  menu->insertSeparator();
  menu->insertItem(kapp->miniIcon()      ,i18n("&About KSensors"), this, SLOT(showAbout()          ) ,0,103 );
  menu->insertSeparator();
  if(!noDock) menu->insertItem(sensorsWidget ? i18n("&Minimize"):i18n("&Restore"), this, SLOT(minimizeRestoreWidget() ) ,0,100 );
  menu->insertItem(SmallIcon("exit"),i18n("&Exit"), this, SLOT(close()),0,102);
}

void LMSensorsDock::createWidgets()
{
 TDEGlobal::config()->setGroup("General");
 if(noDock) createShowWidget();
 else if(TDEGlobal::config()->readBoolEntry("showWidget",false)) {
    int desktop = TDEGlobal::config()->readNumEntry("desktop", 0);
    createShowWidget(desktop);
 }
}

void LMSensorsDock::saveConfig()
{
 TDEGlobal::config()->setGroup("General");
 TDEGlobal::config()->writeEntry("showWidget",(bool)sensorsWidget);
 if(sensorsWidget) {
   NETWinInfo wm_client(tqt_xdisplay(), sensorsWidget->winId(), tqt_xrootwin(), NET::WMDesktop);
   TDEGlobal::config()->writeEntry("desktop", wm_client.desktop());
 }   
}

void LMSensorsDock::createDockWidgets()
{
TQObjectList *chips= sensors->getSensorsChips();

 countSensors= 0;
 if(chips)
    for(LMSensorsChip *chip= (LMSensorsChip *)chips->first(); chip!=0; chip= (LMSensorsChip *)chips->next()) {
        TQObjectList *list= (TQObjectList *)chip->getSensors();
        if(list) {
           for(LMSensor *sensor= (LMSensor *)list->first(); sensor!=0; sensor= (LMSensor *)list->next()) {
             if( LMSensorDockPanel::readShowInDock(sensor->name()) )
               createDockSensor(sensor);
          }
        }
    }
 if(countSensors==0) createDockSensor(0);
}

void LMSensorsDock::createAlarmControl()
{
 sensorsAlarm= new LMSensorsAlarms(sensors);
}


void LMSensorsDock::minimizeRestoreWidget()
{
 if(sensorsWidget)  delete sensorsWidget;
 else               createShowWidget();
 updateMenu();
}

void LMSensorsDock::updateMenu()
{
 if(noDock && !sensorsWidget)
   close();
 else
   menu->changeItem(100, sensorsWidget ? i18n("&Minimize") : i18n("&Restore"));
}

void LMSensorsDock::createShowWidget(int desktop)
{
 if(!sensorsWidget)  {
   sensorsWidget= new LMSensorsWidget(sensors);
   connect( sensorsWidget, SIGNAL(rightMouseClicked(TQMouseEvent *)), this, SLOT(mouseEventReceived(TQMouseEvent *)) );
   connect( sensorsWidget, SIGNAL(destroyed()), this, SLOT(updateMenu()) );
 }
 if ((desktop != 0) && (desktop <= KWin::numberOfDesktops())) KWin::setOnDesktop(sensorsWidget->winId(), desktop);
 sensorsWidget->show();
}


void LMSensorsDock::createConfigWidget()
{
 if(!sensorsCfg) {
   sensorsCfg= new KSensorsCfg(sensors);
 }
 sensorsCfg->show();
}


void LMSensorsDock::close()
{
 delete this;
}

void LMSensorsDock::mouseEventReceived(TQMouseEvent *e)
{
 if(e->type()==TQEvent::MouseButtonRelease) {
   if( e->button()==LeftButton  ) minimizeRestoreWidget();
   if( e->button()==RightButton ) menu->popup(e->globalPos());
 }
}

void LMSensorsDock::createDockSensor(Sensor *sensor)
{
  if(countSensors==0) {
    TQObjectList *list= (TQObjectList *)children();
    if(list)
      for(LMSensorDockPanel *sensor= (LMSensorDockPanel *)list->first(); sensor!=0; sensor= (LMSensorDockPanel *)list->next())
       delete sensor;
  }
  const char *name= sensor ?  sensor->name() : 0;
  LMSensorDockPanel *dock= new LMSensorDockPanel(sensor,name);
  connect( dock, SIGNAL(mouseEvent(TQMouseEvent *)), this, SLOT(mouseEventReceived(TQMouseEvent *)) );
  insertChild(dock);
  dock->show();
  if(name) countSensors++;
}

void LMSensorsDock::deleteDockSensor(LMSensorDockPanel *sensor)
{
 delete sensor;
 if(--countSensors==0) createDockSensor(0);
}

void LMSensorsDock::updateItemDock(const char *name)
{
 if(!name) return;

 Sensor *sensor= sensors->getSensor(name);

// if((bool)sensor && sensor->getType()==LMSensor::lmTemp) {
   bool   fShow= LMSensorDockPanel::readShowInDock(name);
   LMSensorDockPanel *dockSensor= (LMSensorDockPanel *)child(name);
   if( fShow!=(bool)dockSensor ) {
     if(dockSensor) deleteDockSensor(dockSensor);
     else           createDockSensor(sensor);
   } else {
     if(dockSensor!=0)
       dockSensor->updateConfig();
   }
// }
}


void LMSensorsDock::showAbout()
{
TDEAboutApplication *dialog;

  dialog= new TDEAboutApplication;
  dialog->exec();
  delete dialog;
}