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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
|
/*
statisticsdialog.cpp - Kopete Statistics Dialog
Copyright (c) 2003-2004 by Marc Cramdal <marc.cramdal@gmail.com>
Copyright (c) 2007 by the Kopete Developers <kopete-devel@kde.org>
*************************************************************************
* *
* 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 "statisticsdialog.h"
#include <qtabwidget.h>
#include <qwidget.h>
#include <qstring.h>
#include <QBuffer>
#include <KDateTable>
#include <KGlobalSettings>
#include <KColorScheme>
#include <QScrollArea>
#include <QObject>
#include <khbox.h>
#include <kdialog.h>
#include <klocalizedstring.h>
#include <khtml_part.h>
#include <kstandarddirs.h>
#include "kopetemetacontact.h"
#include "kopeteonlinestatus.h"
#include "statisticscontact.h"
#include "ui_statisticswidgetbase.h"
#include "statisticsplugin.h"
#include "statisticsdb.h"
StatisticsDialog::StatisticsDialog ( StatisticsContact *contact, StatisticsDB *db, QWidget* parent ) : KDialog ( parent ), m_db ( db ), m_contact ( contact )
{
setAttribute ( Qt::WA_DeleteOnClose, true );
setButtons ( KDialog::Close );
setDefaultButton ( KDialog::Close );
setCaption ( i18n ( "Statistics for %1", contact->metaContact()->displayName() ) );
QWidget * w = new QWidget ( this );
dialogUi = new Ui::StatisticsWidgetUI();
dialogUi->setupUi ( w );
setMainWidget ( w );
KHBox *generalHBox = new KHBox ( this );
generalHTMLPart = new KHTMLPart ( generalHBox );
connect ( generalHTMLPart->browserExtension(), SIGNAL (openUrlRequestDelayed(KUrl,KParts::OpenUrlArguments,KParts::BrowserArguments)),
this, SLOT (slotOpenURLRequest(KUrl,KParts::OpenUrlArguments,KParts::BrowserArguments)) );
generalHTMLPart->setJScriptEnabled ( false );
generalHTMLPart->setJavaEnabled ( false );
generalHTMLPart->setMetaRefreshEnabled ( false );
generalHTMLPart->setPluginsEnabled ( false );
generalHTMLPart->setOnlyLocalReferences ( true );
dialogUi->tabWidget->insertTab ( 0, generalHBox, i18n ( "General" ) );
dialogUi->tabWidget->setCurrentIndex ( 0 );
KColorScheme scheme ( QPalette::Active, KColorScheme::View );
m_onlineColor = scheme.background ( KColorScheme::ActiveBackground ).color().darker(130);
m_awayColor = scheme.background ( KColorScheme::NeutralBackground ).color().darker(130);
m_offlineColor = scheme.background ( KColorScheme::AlternateBackground ).color().darker(130);
m_backgroundColor = scheme.background ( KColorScheme::NormalBackground ).color().darker(130);
m_textColor = scheme.foreground ( KColorScheme::NormalText ).color();
calendarHTMLPart = new KHTMLPart ( dialogUi->calendarHBox );
calendarHTMLPart->setJScriptEnabled ( false );
calendarHTMLPart->setJavaEnabled ( false );
calendarHTMLPart->setMetaRefreshEnabled ( false );
calendarHTMLPart->setPluginsEnabled ( false );
calendarHTMLPart->setOnlyLocalReferences ( true );
dialogUi->calendarKey->setTextFormat ( Qt::RichText );
dialogUi->calendarKey->setText ( i18n ( "Key: "
"<font color=\"%1\">Online</font> "
"<font color=\"%2\">Away</font> "
"<font color=\"%3\">Offline</font>",
m_onlineColor.name(),
m_awayColor.name(),
m_offlineColor.name() ) );
dialogUi->datePicker->setDate ( QDate::currentDate() );
connect ( dialogUi->datePicker, SIGNAL (dateChanged(QDate)), this, SLOT (fillCalendarCells()) );
connect ( dialogUi->datePicker, SIGNAL (dateChanged(QDate)), this, SLOT (generateOneDayStats()) );
setFocus();
setEscapeButton ( Close );
generatePageGeneral();
fillCalendarCells();
generateOneDayStats();
}
StatisticsDialog::~StatisticsDialog()
{
delete dialogUi;
}
// We only generate pages when the user clicks on a link
void StatisticsDialog::slotOpenURLRequest ( const KUrl& url, const KParts::OpenUrlArguments &, const KParts::BrowserArguments & )
{
if ( url.protocol() == "main" )
{
generatePageGeneral();
}
else if ( url.protocol() == "dayofweek" )
{
generatePageForDay ( url.path().toInt() );
}
else if ( url.protocol() == "monthofyear" )
{
generatePageForMonth ( url.path().toInt() );
}
}
/*void StatisticsDialog::parseTemplate(QString Template)
{
QString fileString = ::locate("appdata", "kopete_statistics.template.html");
QString templateString;
QFile file(file);
if (file.open(QIODevice::ReadOnly))
{
QTextStream stream(&file);
templateString = stream.read();
file.close();
}
// The template is loaded in templateString now.
templateString.strReplace(
}*/
void StatisticsDialog::generatePageForMonth ( const int monthOfYear )
{
QStringList values = m_db->query ( QString ( "SELECT status, datetimebegin, datetimeend "
"FROM contactstatus WHERE metacontactid LIKE '%1' ORDER BY datetimebegin;" ).arg ( m_contact->metaContactId() ) );
QStringList values2;
for ( int i=0; i<values.count(); i+=3 )
{
QDateTime dateTimeBegin;
dateTimeBegin.setTime_t ( values[i+1].toInt() );
/// @todo Same as for Day, check if second datetime is on the same month
if ( dateTimeBegin.date().month() == monthOfYear )
{
values2.push_back ( values[i] );
values2.push_back ( values[i+1] );
values2.push_back ( values[i+2] );
}
}
generatePageFromQStringList ( values2, QDate::longMonthName ( monthOfYear ) );
}
void StatisticsDialog::generatePageForDay ( const int dayOfWeek )
{
QStringList values = m_db->query ( QString ( "SELECT status, datetimebegin, datetimeend "
"FROM contactstatus WHERE metacontactid LIKE '%1' ORDER BY datetimebegin;" ).arg ( m_contact->metaContactId() ) );
QStringList values2;
for ( int i=0; i<values.count(); i+=3 )
{
QDateTime dateTimeBegin;
dateTimeBegin.setTime_t ( values[i+1].toInt() );
QDateTime dateTimeEnd;
dateTimeEnd.setTime_t ( values[i+2].toInt() );
if ( dateTimeBegin.date().dayOfWeek() == dayOfWeek )
{
if ( dateTimeEnd.date().dayOfWeek() != dayOfWeek )
// Day of week is not the same at beginning and at end of the event
{
values2.push_back ( values[i] );
values2.push_back ( values[i+1] );
// datetime from value[i+1]
dateTimeBegin = QDateTime ( dateTimeBegin.date(), QTime ( 0, 0, 0 ) );
dateTimeBegin.addSecs ( dateTimeBegin.time().secsTo ( QTime ( 23, 59, 59 ) ) );
values2.push_back ( QString::number ( dateTimeBegin.toTime_t() ) );
}
else
{
values2.push_back ( values[i] );
values2.push_back ( values[i+1] );
values2.push_back ( values[i+2] );
}
}
}
generatePageFromQStringList ( values2, QDate::longDayName ( dayOfWeek ) );
}
/// @todo chart problem at midnight.
void StatisticsDialog::generatePageFromQStringList ( QStringList values, const QString & subTitle )
{
KColorScheme colorScheme ( QPalette::Active, KColorScheme::View );
generalHTMLPart->begin();
generalHTMLPart->write ( QString ( "<html><head><style>.bar { margin:0px;} "
"body"
"{"
"font-size:11px"
"}"
".chart" // Style for the charts
"{ height:100px;"
"border-left:1px solid #999;"
"border-bottom:1px solid #999;"
"vertical-align:bottom;"
"}"
".statgroup" // Style for groups of similar statistics
"{ margin-bottom:10px;"
"background-color:white;"
"border-left: 5px solid #369;"
"border-top: 1px dashed #999;"
"border-bottom: 1px dashed #999;"
"margin-left: 10px;"
"margin-right: 5px;"
"padding:3px 3px 3px 10px;}"
"</style></head><body>" +
i18n ( "<h1>Statistics for %1</h1>", m_contact->metaContact()->displayName() ) +
"<h3>%1</h3><hr>" ).arg ( subTitle ) );
generalHTMLPart->write ( i18n ( "<div class=\"statgroup\"><b><a href=\"main:generalinfo\" title=\"General summary view\">General</a></b><br>"
"<span title=\"Select the day or month for which you want to view statistics\"><b>Days: </b>"
"<a href=\"dayofweek:1\">Monday</a> "
"<a href=\"dayofweek:2\">Tuesday</a> "
"<a href=\"dayofweek:3\">Wednesday</a> "
"<a href=\"dayofweek:4\">Thursday</a> "
"<a href=\"dayofweek:5\">Friday</a> "
"<a href=\"dayofweek:6\">Saturday</a> "
"<a href=\"dayofweek:7\">Sunday</a><br>"
"<b>Months: </b>"
"<a href=\"monthofyear:1\">January</a> "
"<a href=\"monthofyear:2\">February</a> "
"<a href=\"monthofyear:3\">March</a> "
"<a href=\"monthofyear:4\">April</a> "
"<a href=\"monthofyear:5\">May</a> "
"<a href=\"monthofyear:6\">June</a> "
"<a href=\"monthofyear:7\">July</a> "
"<a href=\"monthofyear:8\">August</a> "
"<a href=\"monthofyear:9\">September</a> "
"<a href=\"monthofyear:10\">October</a> "
"<a href=\"monthofyear:11\">November</a> "
"<a href=\"monthofyear:12\">December</a> "
"</span></div><br>" ) );
// dialogUi->listView->addColumn(i18n("Status"));
// dialogUi->listView->addColumn(i18n("Start Date"));
// dialogUi->listView->addColumn(i18n("End Date"));
// dialogUi->listView->addColumn(i18n("Start Date"));
// dialogUi->listView->addColumn(i18n("End Date"));
QString todayString;
todayString.append ( i18n ( "<div class=\"statgroup\" title=\"Contact status history for today\"><h2>Today</h2><table width=\"100%\"><tr><td>Status</td><td>From</td><td>To</td></tr>" ) );
bool today;
int totalTime = 0; // this is in seconds
int totalAwayTime = 0; // this is in seconds
int totalOnlineTime = 0; // this is in seconds
int totalOfflineTime = 0; // idem
int hours[24]; // in seconds, too
int iMaxHours = 0;
int hoursOnline[24]; // this is in seconds
int iMaxHoursOnline = 0;
int hoursAway[24]; // this is in seconds
int iMaxHoursAway = 0;
int hoursOffline[24]; // this is in seconds. Hours where we are sure contact is offline
int iMaxHoursOffline = 0;
for ( uint i=0; i<24; i++ )
{
hours[i] = 0;
hoursOnline[i] = 0;
hoursAway[i] = 0;
hoursOffline[i] = 0;
}
for ( int i=0; i<values.count(); i+=3 /* because SELECT 3 columns */ )
{
/* Here we try to interpret one database entry...
What's important here, is to not count two times the same hour for instance
This is why there are some if in all this stuff ;-)
*/
// it is the STARTDATE from the database
QDateTime dateTime1;
dateTime1.setTime_t ( values[i+1].toInt() );
// it is the ENDDATE from the database
QDateTime dateTime2;
dateTime2.setTime_t ( values[i+2].toInt() );
if ( dateTime1.date() == QDate::currentDate() || dateTime2.date() == QDate::currentDate() )
today = true;
else today = false;
totalTime += dateTime1.secsTo ( dateTime2 );
if ( Kopete::OnlineStatus::statusStringToType ( values[i] ) == Kopete::OnlineStatus::Online )
totalOnlineTime += dateTime1.secsTo ( dateTime2 );
else if ( Kopete::OnlineStatus::statusStringToType ( values[i] ) == Kopete::OnlineStatus::Away )
totalAwayTime += dateTime1.secsTo ( dateTime2 );
else if ( Kopete::OnlineStatus::statusStringToType ( values[i] ) == Kopete::OnlineStatus::Busy )
totalAwayTime += dateTime1.secsTo ( dateTime2 );
else if ( Kopete::OnlineStatus::statusStringToType ( values[i] ) == Kopete::OnlineStatus::Offline )
totalOfflineTime += dateTime1.secsTo ( dateTime2 );
/*
* To build the chart/hours
*/
// Number of hours between dateTime1 and dateTime2
int nbHours = ( int ) ( dateTime1.secsTo ( dateTime2 ) /3600.0 );
uint tempHour =
dateTime1.time().hour() == dateTime2.time().hour()
? dateTime1.secsTo ( dateTime2 ) // (*)
: 3600 - dateTime1.time().minute() *60 - dateTime1.time().second();
hours[dateTime1.time().hour() ] += tempHour;
if ( Kopete::OnlineStatus::statusStringToType ( values[i] ) == Kopete::OnlineStatus::Online )
hoursOnline[dateTime1.time().hour() ] += tempHour;
else if ( Kopete::OnlineStatus::statusStringToType ( values[i] ) == Kopete::OnlineStatus::Away )
hoursAway[dateTime1.time().hour() ] += tempHour;
else if ( Kopete::OnlineStatus::statusStringToType ( values[i] ) == Kopete::OnlineStatus::Busy )
hoursAway[dateTime1.time().hour() ] += tempHour;
else if ( Kopete::OnlineStatus::statusStringToType ( values[i] ) == Kopete::OnlineStatus::Offline )
hoursOffline[dateTime1.time().hour() ] += tempHour;
for ( int j= dateTime1.time().hour() +1; j < dateTime1.time().hour() + nbHours - 1; j++ )
{
hours[j%24] += 3600;
if ( Kopete::OnlineStatus::statusStringToType ( values[i] ) == Kopete::OnlineStatus::Online )
hoursOnline[j%24] += 3600;
else if ( Kopete::OnlineStatus::statusStringToType ( values[i] ) == Kopete::OnlineStatus::Away )
hoursAway[j%24] += 3600;
else if ( Kopete::OnlineStatus::statusStringToType ( values[i] ) == Kopete::OnlineStatus::Busy )
hoursAway[j%24] += 3600;
else if ( Kopete::OnlineStatus::statusStringToType ( values[i] ) == Kopete::OnlineStatus::Offline )
hoursOffline[j%24] += 3600;
}
if ( dateTime1.time().hour() != dateTime2.time().hour() )
// We don't want to count this if the hour from dateTime2 is the same than the one from dateTime1
// since it as already been taken in account in the (*) instruction
{
tempHour = dateTime2.time().minute() *60 +dateTime2.time().second();
hours[dateTime2.time().hour() ] += tempHour;
if ( Kopete::OnlineStatus::statusStringToType ( values[i] ) == Kopete::OnlineStatus::Online )
hoursOnline[dateTime2.time().hour() ] += tempHour;
else if ( Kopete::OnlineStatus::statusStringToType ( values[i] ) == Kopete::OnlineStatus::Away )
hoursAway[dateTime2.time().hour() ] += tempHour;
else if ( Kopete::OnlineStatus::statusStringToType ( values[i] ) == Kopete::OnlineStatus::Busy )
hoursAway[dateTime2.time().hour() ] += tempHour;
else if ( Kopete::OnlineStatus::statusStringToType ( values[i] ) == Kopete::OnlineStatus::Offline )
hoursOffline[dateTime2.time().hour() ] += tempHour;
}
if ( today )
{
QString status;
if ( Kopete::OnlineStatus::statusStringToType ( values[i] ) == Kopete::OnlineStatus::Online )
status = i18n ( "Online" );
else if ( Kopete::OnlineStatus::statusStringToType ( values[i] ) == Kopete::OnlineStatus::Away )
status = i18n ( "Away" );
else if ( Kopete::OnlineStatus::statusStringToType ( values[i] ) == Kopete::OnlineStatus::Busy )
status = i18n ( "Busy" );
else if ( Kopete::OnlineStatus::statusStringToType ( values[i] ) == Kopete::OnlineStatus::Offline )
status = i18n ( "Offline" );
todayString.append ( QString ( "<tr><td>%2</td><td>%3</td><td>%4</td></tr>" ).arg ( status, dateTime1.time().toString(), dateTime2.time().toString() ) );
}
// We add a listview item to the log list
// QDateTime listViewDT1, listViewDT2;
// listViewDT1.setTime_t(values[i+1].toInt());
// listViewDT2.setTime_t(values[i+2].toInt());
// new K3ListViewItem(dialogUi->listView, values[i], values[i+1], values[i+2], listViewDT1.toString(), listViewDT2.toString());
}
todayString.append ( "</table></div>" );
// Get the max from the hours*
for ( uint i=1; i<24; i++ )
{
if ( hours[iMaxHours] < hours[i] )
iMaxHours = i;
if ( hoursOnline[iMaxHoursOnline] < hoursOnline[i] )
iMaxHoursOnline = i;
if ( hoursOffline[iMaxHoursOffline] < hoursOffline[i] )
iMaxHoursOffline = i;
if ( hoursAway[iMaxHoursAway] < hoursAway[i] )
iMaxHoursAway = i;
}
//
/*
* Here we really generate the page
*/
// Some "total times"
generalHTMLPart->write ( i18n ( "<div class=\"statgroup\">" ) );
generalHTMLPart->write ( i18n ( "<b title=\"The total time %1 was visible\">"
"Total visible time :</b> %2<br>", m_contact->metaContact()->displayName(), stringFromSeconds ( totalOnlineTime + totalAwayTime ) ) );
generalHTMLPart->write ( i18n ( "<b title=\"The total time %1 was online\">"
"Total online time :</b> %2<br>", m_contact->metaContact()->displayName(), stringFromSeconds ( totalOnlineTime ) ) );
generalHTMLPart->write ( i18n ( "<b title=\"The total time %1 was away\">Total busy time :</b> %2<br>", m_contact->metaContact()->displayName(), stringFromSeconds ( totalAwayTime ) ) );
generalHTMLPart->write ( i18n ( "<b title=\"The total time %1 was offline\">Total offline time :</b> %2", m_contact->metaContact()->displayName(), stringFromSeconds ( totalOfflineTime ) ) );
generalHTMLPart->write ( QString ( "</div>" ) );
if ( subTitle == i18n ( "General information" ) )
/*
* General stats that should not be shown on "day" or "month" pages
*/
{
generalHTMLPart->write ( QString ( "<div class=\"statgroup\">" ) );
generalHTMLPart->write ( i18np ( "<b>Average message length:</b> %1 character<br>",
"<b>Average message length:</b> %1 characters<br>", m_contact->messageLength() ) );
generalHTMLPart->write ( i18np ( "<b>Time between two messages:</b> %1 second",
"<b>Time between two messages:</b> %1 seconds", m_contact->timeBetweenTwoMessages() ) );
generalHTMLPart->write ( QString ( "</div>" ) );
generalHTMLPart->write ( QString ( "<div class=\"statgroup\">" ) );
generalHTMLPart->write ( i18n ( "<b title=\"The last time you talked with %1\">Last talk :</b> %2<br>", m_contact->metaContact()->displayName(), KGlobal::locale()->formatDateTime ( m_contact->lastTalk() ) ) );
generalHTMLPart->write ( i18n ( "<b title=\"The last time %1 was online or away\">Last time present :</b> %2", m_contact->metaContact()->displayName(), KGlobal::locale()->formatDateTime ( m_contact->lastPresent() ) ) );
generalHTMLPart->write ( QString ( "</div>" ) );
//generalHTMLPart->write(QString("<div class=\"statgroup\">"));
//generalHTMLPart->write(i18n("<b title=\"%1 uses to set his status online at these hours (EXPERIMENTAL)\">Main online events :</b><br>").arg(m_contact->metaContact()->displayName()));
//QValueList<QTime> mainEvents = m_contact->mainEvents(Kopete::OnlineStatus::Online);
//for (uint i=0; i<mainEvents.count(); i++)
//generalHTMLPart->write(QString("%1<br>").arg(mainEvents[i].toString()));
//generalHTMLPart->write(QString("</div>"));
generalHTMLPart->write ( "<div title=\"" +i18n ( "Current status" ) + "\" class=\"statgroup\">" );
generalHTMLPart->write ( i18n ( "Is <b>%1</b> since <b>%2</b>",
Kopete::OnlineStatus ( m_contact->oldStatus() ).description(),
KGlobal::locale()->formatDateTime ( m_contact->oldStatusDateTime() ) ) );
generalHTMLPart->write ( QString ( "</div>" ) );
}
/*
* Chart which shows the hours where plugin has seen this contact online
*/
generalHTMLPart->write ( QString ( "<div class=\"statgroup\">" ) );
generalHTMLPart->write ( QString ( "<table width=\"100%\"><tr><td colspan=\"3\">" )
+ i18nc ( "TRANSLATOR: please reverse 'left' and 'right' as appropriate for your language",
"When was this contact visible?<br />All charts are in 24 blocks, "
"one per hour, encompassing one day. %1 is on the left, "
"and %2 is on the right.", KGlobal::locale()->formatTime ( QTime ( 0, 0 ) ),
KGlobal::locale()->formatTime ( QTime ( 23, 59 ) ) )
+ QString ( "</td></tr>" ) );
generalHTMLPart->write ( QString ( "<tr><td height=\"200\" valign=\"bottom\" colspan=\"3\" class=\"chart\">" ) );
QString chartString;
QByteArray colorPath;
QPixmap pixmap ( 1, 1 );
pixmap.fill ( Qt::black );
// Generate base64 picture.
QByteArray tempArray;
QBuffer tempBuffer ( &tempArray );
tempBuffer.open ( QIODevice::WriteOnly );
if ( pixmap.save ( &tempBuffer, "PNG" ) )
colorPath = tempArray.toBase64();
for ( uint i=0; i<24; i++ )
{
int hrWidth = qRound ( ( double ) ( hoursOnline[i] + hoursAway[i] ) / ( double ) hours[iMaxHours]*100. );
chartString += QString ( "<img class=\"margin:0px;\" height=\"" )
+ ( totalTime ? QString::number ( hrWidth ) : QString::number ( 0 ) )
+ QString ( "\" src=\"data:image/png;base64," )
+colorPath
+"\" width=\"4%\" title=\""
+i18n ( "Between %1 and %2, %3 was visible for %4% of the hour.",
KGlobal::locale()->formatTime ( QTime ( i, 0 ) ),
KGlobal::locale()->formatTime ( QTime ( ( i+1 ) % 24, 0 ) ),
m_contact->metaContact()->displayName(), hrWidth )
+ QString ( "\">" );
}
generalHTMLPart->write ( chartString );
generalHTMLPart->write ( QString ( "</td></tr>" ) );
generalHTMLPart->write ( QString ( "<tr><td>" )
+i18n ( "Online time" )
+QString ( "</td><td>" )
+i18n ( "Away time" )
+QString ( "</td><td>" )
+i18n ( "Offline time" )
+QString ( "</td></tr><td valign=\"bottom\" width=\"33%\" class=\"chart\">" ) );
generalHTMLPart->write ( generateHTMLChart ( hoursOnline, hoursAway, hoursOffline, i18n ( "online" ),
m_onlineColor ) );
generalHTMLPart->write ( QString ( "</td><td valign=\"bottom\" width=\"33%\" class=\"chart\">" ) );
generalHTMLPart->write ( generateHTMLChart ( hoursAway, hoursOnline, hoursOffline, i18n ( "away" ),
m_awayColor ) );
generalHTMLPart->write ( QString ( "</td><td valign=\"bottom\" width=\"33%\" class=\"chart\">" ) );
generalHTMLPart->write ( generateHTMLChart ( hoursOffline, hoursAway, hoursOnline, i18n ( "offline" ),
m_offlineColor ) );
generalHTMLPart->write ( QString ( "</td></tr></table></div>" ) );
if ( subTitle == i18n ( "General information" ) )
/* On main page, show the different status of the contact today
*/
{
generalHTMLPart->write ( QString ( todayString ) );
}
generalHTMLPart->write ( QString ( "</body></html>" ) );
generalHTMLPart->end();
}
void StatisticsDialog::generatePageGeneral()
{
QStringList values;
values = m_db->query ( QString ( "SELECT status, datetimebegin, datetimeend "
"FROM contactstatus WHERE metacontactid LIKE '%1' ORDER BY datetimebegin;" )
.arg ( m_contact->metaContactId() ) );
generatePageFromQStringList ( values, i18n ( "General information" ) );
}
QString StatisticsDialog::generateHTMLChart ( const int *hours, const int *hours2, const int *hours3, const QString & caption, const QColor & color )
{
QString chartString;
QByteArray colorPath;
QPixmap pixmap ( 1, 1 );
pixmap.fill ( color );
// Generate base64 picture.
QByteArray tempArray;
QBuffer tempBuffer ( &tempArray );
tempBuffer.open ( QIODevice::WriteOnly );
if ( pixmap.save ( &tempBuffer, "PNG" ) )
colorPath = tempArray.toBase64();
for ( uint i=0; i<24; i++ )
{
int totalTime = hours[i] + hours2[i] + hours3[i];
int hrWidth = qRound ( ( double ) hours[i]/ ( double ) totalTime*100. );
chartString += QString ( "<img class=\"margin:0px;\" height=\"" )
+ ( totalTime ? QString::number ( hrWidth ) : QString::number ( 0 ) )
+ QString ( "\" src=\"data:image/png;base64," )
+ colorPath
+ "\" width=\"4%\" title=\""
+ i18n ( "Between %1 and %2, %3 was %4% %5.",
KGlobal::locale()->formatTime ( QTime ( i, 0 ) ),
KGlobal::locale()->formatTime ( QTime ( ( i+1 ) % 24, 0 ) ),
m_contact->metaContact()->displayName(),
hrWidth,
caption )
+"\">";
}
return chartString;
}
QString StatisticsDialog::stringFromSeconds ( const int seconds )
{
return KGlobal::locale()->prettyFormatDuration ( (unsigned long)seconds * 1000 );
}
void StatisticsDialog::fillCalendarCells()
{
QDateTime firstOfMonth ( dialogUi->datePicker->date() );
QDateTime lastOfMonth ( dialogUi->datePicker->date() );
firstOfMonth.setDate ( QDate ( firstOfMonth.date().year(), firstOfMonth.date().month(), 1 ) );
lastOfMonth.setDate ( QDate ( lastOfMonth.date().year(), lastOfMonth.date().month(),
lastOfMonth.date().daysInMonth() ) );
QStringList values = m_db->query ( QString ( "SELECT status, datetimebegin, datetimeend "
"FROM contactstatus WHERE metacontactid LIKE '%1' AND "
"datetimebegin BETWEEN '%2' AND '%3' "
"AND datetimeend BETWEEN '%4' AND '%5';" )
.arg ( m_contact->metaContactId() )
.arg ( firstOfMonth.toTime_t() )
.arg ( lastOfMonth.toTime_t() )
.arg ( firstOfMonth.toTime_t() )
.arg ( lastOfMonth.toTime_t() ) );
QVector <Kopete::OnlineStatus> statuses ( 32, Kopete::OnlineStatus::Unknown );
// list of dates and statuses using begintimes
for ( int i=0; i < values.count(); i += 3 )
{
QDate date ( QDateTime::fromTime_t ( values.at ( i+1 ).toUInt() ).date() );
Kopete::OnlineStatus status = Kopete::OnlineStatus::statusStringToType ( values.at ( i ) );
if ( status > statuses.at ( date.day() ) )
{
statuses[date.day() ] = status;
}
}
// list of dates and statuses using endtimes
for ( int i=0; i < values.count(); i += 3 )
{
QDate date ( QDateTime::fromTime_t ( values.at ( i+2 ).toUInt() ).date() );
Kopete::OnlineStatus status = Kopete::OnlineStatus::statusStringToType ( values.at ( i ) );
if ( status > statuses.at ( date.day() ) )
{
statuses[date.day() ] = status;
}
}
for ( int i=0; i < statuses.count(); i++ )
{
QColor color ( m_backgroundColor );
if ( statuses.at ( i ) == Kopete::OnlineStatus::Online )
color = m_onlineColor;
else if ( statuses.at ( i ) == Kopete::OnlineStatus::Away )
color = m_awayColor;
else if ( statuses.at ( i ) == Kopete::OnlineStatus::Busy )
color = m_awayColor;
else if ( statuses.at ( i ) == Kopete::OnlineStatus::Offline )
color = m_offlineColor;
dialogUi->datePicker->dateTable()->setCustomDatePainting (
QDate ( firstOfMonth.date().year(), firstOfMonth.date().month(), i ),
m_textColor, KDateTable::RectangleMode, color );
}
dialogUi->datePicker->update();
}
void StatisticsDialog::generateOneDayStats ()
{
QDate day = dialogUi->datePicker->date();
QDateTime topOfDay = QDateTime ( day );
QDateTime endOfDay = topOfDay.addDays ( 1 );
QStringList values = m_db->query ( QString ( "SELECT status, datetimebegin, datetimeend "
"FROM contactstatus WHERE metacontactid LIKE '%1' AND "
"datetimebegin BETWEEN '%2' AND '%3' "
"AND datetimeend BETWEEN '%4' AND '%5';" )
.arg ( m_contact->metaContactId() )
.arg ( topOfDay.toTime_t() )
.arg ( endOfDay.toTime_t() )
.arg ( topOfDay.toTime_t() )
.arg ( endOfDay.toTime_t() ) );
QString todayString;
todayString.append ( i18n ( "<h2>%1</h2><table width=\"100%\"><tr><td>Status</td><td>From</td><td>To</td></tr>",
KGlobal::locale()->formatDate ( topOfDay.date(), KLocale::ShortDate ) ) );
for ( int i=0; i<values.count(); i+=3 )
{
// it is the STARTDATE from the database
QDateTime dateTime1;
dateTime1.setTime_t ( values[i+1].toInt() );
// it is the ENDDATE from the database
QDateTime dateTime2;
dateTime2.setTime_t ( values[i+2].toInt() );
QString status;
if ( Kopete::OnlineStatus::statusStringToType ( values[i] ) == Kopete::OnlineStatus::Online )
status = i18n ( "Online" );
else if ( Kopete::OnlineStatus::statusStringToType ( values[i] ) == Kopete::OnlineStatus::Away )
status = i18n ( "Away" );
else if ( Kopete::OnlineStatus::statusStringToType ( values[i] ) == Kopete::OnlineStatus::Busy )
status = i18n ( "Busy" );
else if ( Kopete::OnlineStatus::statusStringToType ( values[i] ) == Kopete::OnlineStatus::Offline )
status = i18n ( "Offline" );
todayString.append ( QString ( "<tr><td>%2</td><td>%3</td><td>%4</td></tr>" ).arg ( status, dateTime1.time().toString(), dateTime2.time().toString() ) );
}
todayString.append ( "</table></div>" );
calendarHTMLPart->begin();
calendarHTMLPart->write ( QString ( "<html><head><style>.bar { margin:0px;} "
"body"
"{"
"font-size:11px"
"}"
"</style></head><body>" ) );
calendarHTMLPart->write ( todayString );
calendarHTMLPart->write ( QString ( "</body></html>" ) );
calendarHTMLPart->end();
}
#include "statisticsdialog.moc"
|