summaryrefslogtreecommitdiff
path: root/core/applets/batteryapplet/batterystatus.cpp
blob: 08ddc85dff3802c1db966c7684ab3642d1aed965 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284

#include "batterystatus.h"

/* OPIE */
#include <opie2/odevice.h>
#include <qpe/power.h>

/* QT */
#include <qpushbutton.h>
#include <qdrawutil.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qmessagebox.h>

using namespace Opie::Core;

BatteryStatus::BatteryStatus( const PowerStatus *p, QWidget *parent, WFlags f )
: QFrame( parent, 0, f), ps(p), bat2(false) {

	UpdateBatteryStatus();
}

BatteryStatus::~BatteryStatus() {}

void BatteryStatus::UpdateBatteryStatus() {

	jackPercent = 0;

	if ( ODevice::inst ( )-> series ( ) == Model_iPAQ ) {
		if ( ODevice::inst ( ) -> model() != Model_iPAQ_H191x )
			getProcApmStatusIpaq();
	}
	percent = ps->batteryPercentRemaining();
}

/*
 * Make use of the advanced apm interface of the ipaq
 */
bool BatteryStatus::getProcApmStatusIpaq() {

    bat2 = false;

    QFile procApmIpaq("/proc/hal/battery");

    if (procApmIpaq.open(IO_ReadOnly) ) {
        QStringList list;
        // since it is /proc we _must_ use QTextStream
        QTextStream stream ( &procApmIpaq);
        QString streamIn;
        streamIn = stream.read();
        list = QStringList::split("\n", streamIn);
       
        sec2 = sec1 = "";

        for(QStringList::Iterator line=list.begin(); line!=list.end(); line++) {
            // not nice, need a rewrite later
            if( (*line).startsWith(" Percentage") ) {
                if (bat2 == true) {
                    perc2 = (*line).mid(((*line).find('(')) +1,(*line).find(')')-(*line).find('(')-2);
                } else {
                    perc1 = (*line).mid(((*line).find('('))+1,(*line).find(')')-(*line).find('(')-2);
                }
            } else if( (*line).startsWith(" Life") ) {
                if (bat2 == true) {
                    sec2 = (*line).mid(((*line).find(':')+2), 5 );
                } else {
                    sec1 = (*line).mid(((*line).find(':')+2), 5 );
                }
            } else if( (*line).startsWith("Battery #1") ) {
                bat2 = true;
            } else if( (*line).startsWith(" Status") ) {
                if (bat2 == true) {
                    jackStatus = (*line).mid((*line).find('(')+1, (*line).find(')')-(*line).find('(')-1);
                } else {
                    ipaqStatus = (*line).mid((*line).find('(')+1, (*line).find(')')-(*line).find('(')-1);
                }
            } else if( (*line).startsWith(" Chemistry") ) {
                if (bat2 == true) {
                    jackChem = (*line).mid((*line).find('('), (*line).find(')')-(*line).find('(')+1);
                } else {
                    ipaqChem = (*line).mid((*line).find('('), (*line).find(')')-(*line).find('(')+1);
                }
            }
        }
    } else {
        QMessageBox::warning(this, tr("Failure"),tr("could not open file"));
    }

    procApmIpaq.close();
    jackPercent = perc2.toInt();
    ipaqPercent = perc1.toInt();

    if (perc2.isEmpty() || perc2 == "unknow" ) {
        perc2 = tr("no data");
    } else {
        perc2 += " %";
    }

    if (sec2 == "0" || sec2 == "" || sec2.isEmpty()) {
        sec2 = tr("no data");
    } else {
        sec2 += " min";
    }

    jackStatus.prepend( " ( " );
    jackStatus.append( " )" );
    return true;
}


void BatteryStatus::updatePercent( int pc ) {
    percent = pc;
    repaint(FALSE);
}

void BatteryStatus::drawSegment( QPainter *p, const QRect &r, const QColor &topgrad, const QColor &botgrad, const QColor &highlight, int hightlight_height ) {
    int h1, h2, s1, s2, v1, v2, ng = r.height(), hy = ng*30/100, hh = hightlight_height;
    topgrad.hsv( &h1, &s1, &v1 );
    botgrad.hsv( &h2, &s2, &v2 );
    for ( int j = 0; j < hy-2; j++ ) {
        p->setPen( QColor( h1 + ((h2-h1)*j)/(ng-1), s1 + ((s2-s1)*j)/(ng-1),
                           v1 + ((v2-v1)*j)/(ng-1),  QColor::Hsv ) );
        p->drawLine( r.x(), r.top()+hy-2-j, r.x()+r.width(), r.top()+hy-2-j );
    }
    for ( int j = 0; j < hh; j++ ) {
        p->setPen( highlight );
        p->drawLine( r.x(), r.top()+hy-2+j, r.x()+r.width(), r.top()+hy-2+j );
    }
    for ( int j = 0; j < ng-hy-hh; j++ ) {
        p->setPen( QColor( h1 + ((h2-h1)*j)/(ng-1), s1 + ((s2-s1)*j)/(ng-1),
                           v1 + ((v2-v1)*j)/(ng-1),  QColor::Hsv ) );
        p->drawLine( r.x(), r.top()+hy+hh-2+j, r.x()+r.width(), r.top()+hy+hh-2+j );
    }
}

QString  BatteryStatus::statusText() const {
    QString text;

    if ( ps->batteryStatus() == PowerStatus::Charging ) {
    if (bat2) {
            text = tr("Charging both devices");
        } else {
            text = tr("Charging");
        }
    } else if ( ps->batteryPercentAccurate() ) {
        text = tr( "Remaining Power: %1%" ).arg( percent );
        //text.sprintf( tr("Remaining Power") + ": %i%%", percent );
    } else {
        text = tr( "Battery status: " );
        switch ( ps->batteryStatus() ) {
        case PowerStatus::High:
            text.append( tr( "Good" ) );
            break;
        case PowerStatus::Low:
            text.append( tr( "Low" ) );
            break;
        case PowerStatus::VeryLow:
            text.append( tr( "Very Low" ) );
            break;
        case PowerStatus::Critical:
            text.append( tr( "Critical" ) );
            break;
        default: // NotPresent, etc.
            text.append( tr( "Unknown" ) );
        }
    }

    if ( ps->acStatus() == PowerStatus::Backup )
    {
        text.append( '\n' );
        text.append( tr( "On backup power" ) );
    }
    else if ( ps->acStatus() == PowerStatus::Online )
    {
        text.append( '\n' );
        text.append( tr( "Power on-line" ) );
    }
    else if ( ps->acStatus() == PowerStatus::Offline )
    {
        text.append( '\n' );
        text.append( tr( "External power disconnected" ) );
    }

    if ( ps->batteryTimeRemaining() >= 0 )
    {
        text.append( '\n' );
        text.append( tr("Remaining Time: %1m %2s" ).arg( ps->batteryTimeRemaining() / 60 )
                                                   .arg( ps->batteryTimeRemaining() % 60, 2 ) );
/*        text += "\n" + QString().sprintf(  tr("Remaining Time") + ": %im %02is",
              ps->batteryTimeRemaining() / 60, ps->batteryTimeRemaining() % 60 );*/
    }
    return text;
}

QString  BatteryStatus::statusTextIpaq() const {
    QString text = tr( "Remaining Power: %1 \n%2\nRemaining Time: %3" ).arg( perc2 )
                                                                   .arg( jackStatus )
                                                                   .arg( sec2 );
/*    QString text =  tr("Remaining Power: ") + perc2 + " " + jackStatus;
    text += "\n"  + tr("Remaining Time: ") + sec2;*/
    return text;
}

void BatteryStatus::paintEvent( QPaintEvent * ev ) {

    QPainter p( this );

    QString text = statusText();
    p.eraseRect( p.boundingRect( 10, 50, width() - 20, 40 , AlignVCenter, text ) );
    p.drawText( 10, 50, width() - 20, 40 , AlignVCenter, text );

    QColor c;
    QColor darkc;
    QColor lightc;
    if ( ps->acStatus() == PowerStatus::Offline ) {
        c = blue.light(120);
        darkc = c.dark(280);
        lightc = c.light(145);
    } else if ( ps->acStatus() == PowerStatus::Online ) {
        c = green.dark(130);
        darkc = c.dark(200);
        lightc = c.light(220);
    } else {
        c = red;
        darkc = c.dark(280);
        lightc = c.light(140);
    }
    if ( percent < 0 )
        return;

    int rightEnd1 = width() - 47;
    int rightEnd2 = width() - 35;
    int percent2 = (percent * rightEnd1) / 100;
    p.setPen( black );
    qDrawShadePanel( &p,   9, 10, rightEnd1 , 39, colorGroup(), TRUE, 1, NULL);
    qDrawShadePanel( &p, rightEnd2, 17,  12, 24, colorGroup(), TRUE, 1, NULL);
    drawSegment( &p, QRect( 10, 10, percent2, 40 ), lightc, darkc, lightc.light(115), 6 );
    drawSegment( &p, QRect( 11 + percent2, 10,  rightEnd1 - percent2, 40 ), white.light(80), black, white.light(90), 6 );
    drawSegment( &p, QRect( rightEnd2, 17, 10, 25 ), white.light(80), black, white.light(90), 2 );
    p.setPen( black);

    if ( ODevice::inst ( )-> series ( ) == Model_iPAQ && bat2 ) {

        p.drawText( 15, 30, tr("Ipaq  %1").arg( ipaqChem ) );

        QString jacketMsg;
        if (bat2) {
            p.setPen(black);
            QString text = statusTextIpaq();
            p.eraseRect( p.boundingRect( 10, 130, width() - 20, 40 , AlignVCenter, text ) );
            p.drawText( 10, 130,  width() - 20, 40 , AlignVCenter, text );
            jacketMsg = tr("Jacket  ").arg( jackChem );
        } else {
            jackPercent = 0;
            jacketMsg = tr("No jacket with battery inserted");
        }

        int jackPerc =  ( jackPercent * ( width() - 47 ) ) / 100;

        qDrawShadePanel( &p,   9, 90, rightEnd1, 39, colorGroup(), TRUE, 1, NULL);
        qDrawShadePanel( &p, rightEnd2, 97,  12, 24, colorGroup(), TRUE, 1, NULL);
        drawSegment( &p, QRect( 10, 90, jackPerc, 40 ), lightc, darkc, lightc.light(115), 6 );
        drawSegment( &p, QRect( 11 + jackPerc, 90, rightEnd1 - jackPerc, 40 ), white.light(80), black, white.light(90), 6 );
        drawSegment( &p, QRect( rightEnd2, 97, 10, 25 ), white.light(80), black, white.light(90), 2 );
        p.setPen( black );
        p.drawText(15, 100, width() - 20, 20 , AlignVCenter,  jacketMsg);
    }
    QFrame::paintEvent(ev);
}

QSize BatteryStatus::sizeHint() const {
    QString text = statusText();
    QString text2 = statusTextIpaq();
    QFontMetrics fm = fontMetrics();
    QRect r=fm.boundingRect( 10, 0, width(), height(), AlignVCenter, text );
    QRect r2=fm.boundingRect( 10, 0, width(), height(), AlignVCenter, text2 );

    if ( bat2 )  {
    return QSize( QMAX( QMIN( 200, qApp->desktop()->width() ),
                        r.width()+2*10 ), 2 * 10 + 100 + r.height() +  r2.height() );
    }
    return QSize( QMAX( QMIN( 200, qApp->desktop()->width() ),
                        r.width()+2*10 ), 2 * 10 + 40 + r.height() );
}