summaryrefslogtreecommitdiffstats
path: root/kcpuload/kcpuload/kcpuload.cpp
blob: 369ff697d2f1118bf955de0fc8fd26c1499b5b45 (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

/***************************************************************************
 *                                                                         *
 *   KCPULoad is copyright (c) 1999-2000, Markus Gustavsson                *
 *                         (c) 2002, Ben Burton                            *
 *                                                                         *
 *   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 "icontoggleaction.h"
#include "kcpudock.h"
#include "kcpuload.h"
#include "kcpuproc.h"

#include <kaction.h>
#include <kconfig.h>
#include <klocale.h>
#include <kpopupmenu.h>

KCPULoad::KCPULoad(QWidget *parent, const char *name) :
        StatPopup(true, parent, name) {
    // Create the /proc reading class and check for SMP.
    proc = new KCPUProc();
    supportSMP = proc->hasSMP();

    // Set up actions and read the config file.
    setupActions();

    // the vector must not be reallocated during resizing because the class
    // that it contains (Reading) cannot be copied safely
    r.reserve(proc->cpu.size());

    // Create system tray windows.
    resizeReadings(supportSMP && actSMP->isChecked() ? proc->cpu.size() : 1);

    // Initialise the pop-up window.
    readPopupState();

    // Off we go!
    requestResize();
    if (isActive())
        startUpdates();
}

KCPULoad::~KCPULoad() {
    delete proc;
}

void KCPULoad::setSMP(bool set) {
    if (! supportSMP)
        return;

    resizeReadings(set ? proc->cpu.size() : 1);

    requestResize();
    if (isActive())
        takeReading();

    config->setGroup("General Options");
    config->writeEntry("SMP", set);
    config->sync();
}

QString KCPULoad::dockName(int which) const {
    return i18n("CPU %1").arg(which+1);
}

QColor KCPULoad::defaultDockColor(int which) const {
    static const QColor c[] = {
        QColor(0, 255, 0),
        QColor(255, 0, 0),
        QColor(255, 255, 0),
        QColor(0, 255, 255)
    };
    return c[which % (sizeof(c)/sizeof(c[0]))];
}

void KCPULoad::setupCustomActions() {
    if (supportSMP) {
        bool bVal = config->readBoolEntry("SMP", false);
        actSMP = new IconToggleAction(i18n("Enable S&MP"), "smp",
            i18n("S&MP Enabled"), "smpon", 0, coll, "smp");
        actSMP->setChecked(bVal);
        connect(actSMP, SIGNAL(toggled(bool)), this, SLOT(setSMP(bool)));
    }
}

void KCPULoad::insertCustomItems(KPopupMenu* menu) {
    if (supportSMP) {
        actSMP->plug(menu);
        menu->insertSeparator();
    }
}

void KCPULoad::takeReadingInternal() {
      proc->readLoad();
  
    if (r.size() > 1) {
          if (isSplit()) {
            for (int i = 0; i < r.size(); i++) {
                r[i].upper = proc->cpu[i].userPercent();
                r[i].lower = proc->cpu[i].systemPercent();
            }
          } else {
            for (int i = 0; i < r.size(); i++) {
                r[i].upper = proc->cpu[i].totalPercent();
            }
          }
    } else if (r.size() > 0) {
          if (isSplit()) {
            r[0].upper = proc->all.userPercent();
            r[0].lower = proc->all.systemPercent();
          } else {
            r[0].upper = proc->all.totalPercent();
          }
      }
  
      if (isVisible()) {
        if (r.size() > 1) {
              if (isSplit()) {
		QString user =
                    i18n("Current CPU User: %1%")
                    .arg(proc->all.userPercent());
                QString sys =
                    i18n("Current CPU System: %1%")
                    .arg(proc->all.systemPercent());
                for (int i = 0; i < r.size(); i++) {
                    user += i18n(", C%1: %2%").arg(i+1).arg(r[i].upper);
                    sys  += i18n(", C%1: %2%").arg(i+1).arg(r[i].lower);
                }
                fullReading = i18n("%1.\n%2.").arg(user, sys);
              } else {
                QString total =
                    i18n("Current CPU usage: %1%")
                    .arg(proc->all.totalPercent());
                for (int i = 0; i < r.size(); i++) {
                    total += i18n(", C%1: %2%").arg(i+1).arg(r[i].upper);
                }
                fullReading = i18n("%1.").arg(total);
              }
        } else if (r.size() > 0) {
              if (isSplit()) {
                  fullReading = i18n(
                      "Current CPU User: %1%.\n"
                      "Current CPU System: %2%.")
                    .arg(r[0].upper).arg(r[0].lower);
              } else {
                fullReading = i18n("Current CPU usage: %1%.").arg(r[0].upper);
              }
          }
      }
  }
  
void KCPULoad::resizeReadings(int n)
{
    int i = r.size();
    r.resize(n);
    for (; i < n; i++) {
        // action is needed by KCPUDock constructor
        r[i].Init(i, this);
        KCPUDock* dock = new KCPUDock(i, this);
        dock->setCPULabel(i+1);
        r[i].dock = dock;
    }
    // special case single CPU or total
    if (n == 1) {
        static_cast<KCPUDock*>(r[0].dock)->setCPULabel(0);
    } else if (n > 1) {
        static_cast<KCPUDock*>(r[0].dock)->setCPULabel(1);
    }
}

#include "kcpuload.moc"