/* statisticsdialog.cpp - Kopete History Dialog Copyright (c) 2003-2004 by Marc Cramdal ************************************************************************* * * * 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 #include #include #include #include #include #include #include #include "kdialogbase.h" #include "klocale.h" #include "klistview.h" #include "khtml_part.h" #include "kstandarddirs.h" #include "kdatepicker.h" #include "ktimewidget.h" #include "kopetemetacontact.h" #include "kopeteonlinestatus.h" #include "statisticsdialog.h" #include "statisticscontact.h" #include "statisticswidget.h" #include "statisticsplugin.h" #include "statisticsdb.h" StatisticsDialog::StatisticsDialog(StatisticsContact *contact, StatisticsDB *db, TQWidget* parent, const char* name) : KDialogBase(parent, name, false, i18n("Statistics for %1").tqarg(contact->metaContact()->displayName()), Close, Close), m_db(db), m_contact(contact) { mainWidget = new StatisticsWidget(this); setMainWidget(mainWidget); setMinimumWidth(640); setMinimumHeight(400); adjustSize(); TQHBox *hbox = new TQHBox(this); generalHTMLPart = new KHTMLPart(hbox); generalHTMLPart->setOnlyLocalReferences(true); connect ( generalHTMLPart->browserExtension(), TQT_SIGNAL( openURLRequestDelayed( const KURL &, const KParts::URLArgs & ) ), this, TQT_SLOT( slotOpenURLRequest( const KURL &, const KParts::URLArgs & ) ) ); mainWidget->tabWidget->insertTab(hbox, i18n("General"), 0); mainWidget->tabWidget->setCurrentPage(0); mainWidget->timePicker->setTime(TQTime::currentTime()); mainWidget->datePicker->setDate(TQDate::tqcurrentDate()); connect(mainWidget->askButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotAskButtonClicked())); setFocus(); setEscapeButton(Close); generatePageGeneral(); } // We only generate pages when the user clicks on a link void StatisticsDialog::slotOpenURLRequest(const KURL& url, const KParts::URLArgs&) { 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(TQString Template) { TQString fileString = ::locate("appdata", "kopete_statistics.template.html"); TQString templateString; TQFile file(file); if (file.open(IO_ReadOnly)) { TQTextStream stream(&file); templateString = stream.read(); file.close(); } // The template is loaded in templateString now. templateString.strReplace( }*/ void StatisticsDialog::generatePageForMonth(const int monthOfYear) { TQStringList values = m_db->query(TQString("SELECT status, datetimebegin, datetimeend " "FROM contactstatus WHERE metacontactid LIKE '%1' ORDER BY datetimebegin;").tqarg(m_contact->statisticsContactId())); TQStringList values2; for (uint i=0; iquery(TQString("SELECT status, datetimebegin, datetimeend " "FROM contactstatus WHERE metacontactid LIKE '%1' ORDER BY datetimebegin;").tqarg(m_contact->statisticsContactId())); TQStringList values2; for (uint i=0; ibegin(); generalHTMLPart->write(TQString("" + i18n("

Statistics for %1

").tqarg(m_contact->metaContact()->displayName()) + "

%1


").tqarg(subTitle)); generalHTMLPart->write(i18n("
General
" "Days: " "Monday " "Tuesday " "Wednesday " "Thursday " "Friday " "Saturday " "Sunday
" "Months: " "January " "February " "March " "April " "May " "June " "July " "August " "September " "October " "November " "December " "

")); // mainWidget->listView->addColumn(i18n("tqStatus")); // mainWidget->listView->addColumn(i18n("Start Date")); // mainWidget->listView->addColumn(i18n("End Date")); // mainWidget->listView->addColumn(i18n("Start Date")); // mainWidget->listView->addColumn(i18n("End Date")); TQString todayString; todayString.append(i18n("

Today

")); 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 (uint i=0; i").tqarg(color, status, dateTime1.time().toString(), dateTime2.time().toString())); } // We add a listview item to the log list // TQDateTime listViewDT1, listViewDT2; // listViewDT1.setTime_t(values[i+1].toInt()); // listViewDT2.setTime_t(values[i+2].toInt()); // new KListViewItem(mainWidget->listView, values[i], values[i+1], values[i+2], listViewDT1.toString(), listViewDT2.toString()); } todayString.append("
tqStatusFromTo
%2%3%4
"); // 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("
")); generalHTMLPart->write(i18n("" "Total seen time : %2 hour(s)
").tqarg(m_contact->metaContact()->displayName()).tqarg(stringFromSeconds(totalTime))); generalHTMLPart->write(i18n("" "Total online time : %2 hour(s)
").tqarg(m_contact->metaContact()->displayName()).tqarg(stringFromSeconds(totalOnlineTime))); generalHTMLPart->write(i18n("Total busy time : %2 hour(s)
").tqarg(m_contact->metaContact()->displayName()).tqarg(stringFromSeconds(totalAwayTime))); generalHTMLPart->write(i18n("Total offline time : %2 hour(s)").tqarg(m_contact->metaContact()->displayName()).tqarg(stringFromSeconds(totalOfflineTime))); generalHTMLPart->write(TQString("
")); if (subTitle == i18n("General information")) /* * General stats that should not be shown on "day" or "month" pages */ { generalHTMLPart->write(TQString("
")); generalHTMLPart->write(i18n("Average message length : %1 characters
").tqarg(m_contact->messageLength())); generalHTMLPart->write(i18n("Time between two messages : %1 second(s)").tqarg(m_contact->timeBetweenTwoMessages())); generalHTMLPart->write(TQString("
")); generalHTMLPart->write(TQString("
")); generalHTMLPart->write(i18n("Last talk : %2
").tqarg(m_contact->metaContact()->displayName()).tqarg(KGlobal::locale()->formatDateTime(m_contact->lastTalk()))); generalHTMLPart->write(i18n("Last time contact was present : %2").tqarg(m_contact->metaContact()->displayName()).tqarg(KGlobal::locale()->formatDateTime(m_contact->lastPresent()))); generalHTMLPart->write(TQString("
")); //generalHTMLPart->write(TQString("
")); //generalHTMLPart->write(i18n("Main online events :
").tqarg(m_contact->metaContact()->displayName())); //TQValueList mainEvents = m_contact->mainEvents(Kopete::OnlineStatus::Online); //for (uint i=0; iwrite(TQString("%1
").tqarg(mainEvents[i].toString())); //generalHTMLPart->write(TQString("
")); generalHTMLPart->write("
"); generalHTMLPart->write(i18n("Is %1 since %2").tqarg( Kopete::OnlineStatus(m_contact->oldtqStatus()).description(), KGlobal::locale()->formatDateTime(m_contact->oldStatusDateTime()))); generalHTMLPart->write(TQString("
")); } /* * Chart which show the hours where plugin has seen this contact online */ generalHTMLPart->write(TQString("
")); generalHTMLPart->write(TQString("")); generalHTMLPart->write(TQString("")); generalHTMLPart->write(TQString( "" "" "" "
") + i18n("When have I seen this contact ?") + TQString("
")); TQString chartString; TQString colorPath = ::locate("appdata", "pics/statistics/black.png"); for (uint i=0; i<24; i++) { int hrWidth = tqRound((double)hours[i]/(double)hours[iMaxHours]*100.); chartString += TQString("metaContact()->displayName()).tqarg(hrWidth) +TQString("\">"); } generalHTMLPart->write(chartString); generalHTMLPart->write(TQString("
")+i18n("Online time")+TQString("")+i18n("Away time")+TQString("")+i18n("Offline time")+TQString("
")); generalHTMLPart->write(generateHTMLChart(hoursOnline, hoursAway, hoursOffline, i18n("online"), "blue")); generalHTMLPart->write(TQString("")); generalHTMLPart->write(generateHTMLChart(hoursAway, hoursOnline, hoursOffline, i18n("away"), "navy")); generalHTMLPart->write(TQString("")); generalHTMLPart->write(generateHTMLChart(hoursOffline, hoursAway, hoursOnline, i18n("offline"), "gray")); generalHTMLPart->write(TQString("
")); if (subTitle == i18n("General information")) /* On main page, show the different status of the contact today */ { generalHTMLPart->write(TQString(todayString)); } generalHTMLPart->write(TQString("")); generalHTMLPart->end(); } void StatisticsDialog::generatePageGeneral() { TQStringList values; values = m_db->query(TQString("SELECT status, datetimebegin, datetimeend " "FROM contactstatus WHERE metacontactid LIKE '%1' ORDER BY datetimebegin;") .tqarg(m_contact->statisticsContactId())); generatePageFromTQStringList(values, i18n("General information")); } TQString StatisticsDialog::generateHTMLChart(const int *hours, const int *hours2, const int *hours3, const TQString & caption, const TQString & color) { TQString chartString; TQString colorPath = ::locate("appdata", "pics/statistics/"+color+".png"); for (uint i=0; i<24; i++) { int totalTime = hours[i] + hours2[i] + hours3[i]; int hrWidth = tqRound((double)hours[i]/(double)totalTime*100.); chartString += TQString("metaContact()->displayName()). arg(hrWidth). arg(caption) +".\">"; } return chartString; } TQString StatisticsDialog::stringFromSeconds(const int seconds) { int h, m, s; h = seconds/3600; m = (seconds % 3600)/60; s = (seconds % 3600) % 60; return TQString::number(h)+":"+TQString::number(m)+":"+TQString::number(s); } void StatisticsDialog::slotAskButtonClicked() { if (mainWidget->questionComboBox->currentItem()==0) { TQString text = i18n("1 is date, 2 is contact name, 3 is online status", "%1, %2 was %3") .tqarg(KGlobal::locale()->formatDateTime(TQDateTime(mainWidget->datePicker->date(), mainWidget->timePicker->time()))) .tqarg(m_contact->metaContact()->displayName()) .tqarg(m_contact->statusAt(TQDateTime(mainWidget->datePicker->date(), mainWidget->timePicker->time()))); mainWidget->answerEdit->setText(text); } else if (mainWidget->questionComboBox->currentItem()==1) { mainWidget->answerEdit->setText(m_contact->mainStatusDate(mainWidget->datePicker->date())); } else if (mainWidget->questionComboBox->currentItem()==2) // Next online { } } #include "statisticsdialog.moc"