2010-03-27 13:47:32 +00:00
/***************************************************************************
* Copyright ( C ) 2010 by Volker Lanz < vl @ fidra . de > *
* *
* 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 . *
* *
* This program is distributed in the hope that it will be useful , *
* but WITHOUT ANY WARRANTY ; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the *
* GNU General Public License for more details . *
* *
* You should have received a copy of the GNU General Public License *
* along with this program ; if not , write to the *
* Free Software Foundation , Inc . , *
* 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include "gui/smartdialog.h"
# include "gui/smartdialogwidget.h"
# include "core/device.h"
# include "core/smartstatus.h"
# include "core/smartattribute.h"
# include "util/helpers.h"
2010-04-28 16:21:07 +01:00
# include "util/htmlreport.h"
2010-03-27 13:47:32 +00:00
2014-05-17 03:06:58 +01:00
# include <QDialogButtonBox>
2014-05-18 21:41:48 +01:00
# include <QFileDialog>
# include <QFontDatabase>
2014-05-17 03:06:58 +01:00
# include <QPushButton>
2014-05-18 21:41:48 +01:00
# include <QTemporaryFile>
2010-04-27 17:10:54 +01:00
# include <QTextDocument>
2014-05-18 21:41:48 +01:00
# include <QTextStream>
# include <QTreeWidgetItem>
2010-04-27 17:10:54 +01:00
# include <qglobal.h>
2014-05-18 21:41:48 +01:00
# include <KConfigGroup>
# include <KFormat>
# include <KLocalizedString>
# include <KIconThemes/KIconLoader>
# include <KIO/CopyJob>
# include <KJobUiDelegate>
# include <KMessageBox>
2010-04-27 17:10:54 +01:00
# include <sys/utsname.h>
# include <unistd.h>
2010-03-27 13:47:32 +00:00
# include <config.h>
/** Creates a new SmartDialog
@ param parent pointer to the parent widget
@ param d the Device
*/
SmartDialog : : SmartDialog ( QWidget * parent , Device & d ) :
2014-05-17 03:06:58 +01:00
QDialog ( parent ) ,
2010-03-27 13:47:32 +00:00
m_Device ( d ) ,
m_DialogWidget ( new SmartDialogWidget ( this ) )
{
2014-05-17 03:06:58 +01:00
QVBoxLayout * mainLayout = new QVBoxLayout ( this ) ;
setLayout ( mainLayout ) ;
mainLayout - > addWidget ( & dialogWidget ( ) ) ;
2014-05-18 21:41:48 +01:00
setWindowTitle ( xi18nc ( " @title:window " , " SMART Properties: <filename>%1</filename> " , device ( ) . deviceNode ( ) ) ) ;
2014-05-17 03:06:58 +01:00
buttonBox = new QDialogButtonBox ( this ) ;
buttonBox - > setStandardButtons ( QDialogButtonBox : : Save | QDialogButtonBox : : Close ) ;
buttonBox - > button ( QDialogButtonBox : : Save ) - > setText ( i18nc ( " @action:button " , " Save SMART Report " ) ) ;
buttonBox - > button ( QDialogButtonBox : : Save ) - > setIcon ( QIcon : : fromTheme ( " document-save " ) ) ;
mainLayout - > addWidget ( buttonBox ) ;
2010-03-27 13:47:32 +00:00
setupDialog ( ) ;
setupConnections ( ) ;
2014-05-17 03:06:58 +01:00
KConfigGroup kcg ( KSharedConfig : : openConfig ( ) , " smartDialog " ) ;
restoreGeometry ( kcg . readEntry < QByteArray > ( " Geometry " , QByteArray ( ) ) ) ;
2010-03-27 13:47:32 +00:00
}
/** Destroys a SmartDialog */
SmartDialog : : ~ SmartDialog ( )
{
2014-05-17 03:06:58 +01:00
KConfigGroup kcg ( KSharedConfig : : openConfig ( ) , " smartDialog " ) ;
kcg . writeEntry ( " Geometry " , saveGeometry ( ) ) ;
2010-03-27 13:47:32 +00:00
}
void SmartDialog : : setupDialog ( )
{
if ( device ( ) . smartStatus ( ) . isValid ( ) )
{
if ( device ( ) . smartStatus ( ) . status ( ) )
{
dialogWidget ( ) . statusText ( ) . setText ( i18nc ( " @label SMART disk status " , " good " ) ) ;
dialogWidget ( ) . statusIcon ( ) . setVisible ( false ) ;
}
else
{
dialogWidget ( ) . statusText ( ) . setText ( i18nc ( " @label SMART disk status " , " BAD " ) ) ;
2014-05-18 21:41:48 +01:00
dialogWidget ( ) . statusIcon ( ) . setPixmap ( KIconLoader ( ) . loadIcon ( QLatin1String ( " dialog-warning " ) , KIconLoader : : Small ) ) ;
2010-03-27 13:47:32 +00:00
}
dialogWidget ( ) . modelName ( ) . setText ( device ( ) . smartStatus ( ) . modelName ( ) ) ;
dialogWidget ( ) . firmware ( ) . setText ( device ( ) . smartStatus ( ) . firmware ( ) ) ;
dialogWidget ( ) . serialNumber ( ) . setText ( device ( ) . smartStatus ( ) . serial ( ) ) ;
dialogWidget ( ) . temperature ( ) . setText ( SmartStatus : : tempToString ( device ( ) . smartStatus ( ) . temp ( ) ) ) ;
const QString badSectors = device ( ) . smartStatus ( ) . badSectors ( ) > 0
2014-05-13 23:53:37 +01:00
? QLocale ( ) . toString ( device ( ) . smartStatus ( ) . badSectors ( ) )
2010-03-27 13:47:32 +00:00
: i18nc ( " @label SMART number of bad sectors " , " none " ) ;
dialogWidget ( ) . badSectors ( ) . setText ( badSectors ) ;
2014-05-18 21:41:48 +01:00
dialogWidget ( ) . poweredOn ( ) . setText ( KFormat ( ) . formatDuration ( device ( ) . smartStatus ( ) . poweredOn ( ) ) ) ;
2014-05-13 23:53:37 +01:00
dialogWidget ( ) . powerCycles ( ) . setText ( QLocale ( ) . toString ( device ( ) . smartStatus ( ) . powerCycles ( ) ) ) ;
2010-03-27 13:47:32 +00:00
dialogWidget ( ) . overallAssessment ( ) . setText ( SmartStatus : : overallAssessmentToString ( device ( ) . smartStatus ( ) . overall ( ) ) ) ;
dialogWidget ( ) . selfTests ( ) . setText ( SmartStatus : : selfTestStatusToString ( device ( ) . smartStatus ( ) . selfTestStatus ( ) ) ) ;
dialogWidget ( ) . treeSmartAttributes ( ) . clear ( ) ;
2014-05-18 21:41:48 +01:00
const QFont f = QFontDatabase : : systemFont ( QFontDatabase : : SmallestReadableFont ) ;
2010-03-27 13:47:32 +00:00
const QString size = f . pixelSize ( ) ! = - 1 ? QString ( " %1px " ) . arg ( f . pixelSize ( ) ) : QString ( " %1pt " ) . arg ( f . pointSize ( ) ) ;
const QString st = QString ( " <span style= \" font-family:%1;font-size:%2; \" > " ) . arg ( f . family ( ) ) . arg ( size ) ;
foreach ( const SmartAttribute & a , device ( ) . smartStatus ( ) . attributes ( ) )
{
QTreeWidgetItem * item = new QTreeWidgetItem (
QStringList ( )
2014-05-13 23:53:37 +01:00
< < QLocale ( ) . toString ( a . id ( ) )
2010-04-27 17:10:54 +01:00
< < QString ( " <b>%1</b><br/>%2 " ) . arg ( a . name ( ) ) . arg ( st + a . desc ( ) + " </span> " )
2010-03-27 13:47:32 +00:00
< < ( a . failureType ( ) = = SmartAttribute : : PreFailure ? i18nc ( " @item:intable " , " Pre-Failure " ) : i18nc ( " @item:intable " , " Old-Age " ) )
< < ( a . updateType ( ) = = SmartAttribute : : Online ? i18nc ( " @item:intable " , " Online " ) : i18nc ( " @item:intable " , " Offline " ) )
2014-05-13 23:53:37 +01:00
< < QLocale ( ) . toString ( a . worst ( ) )
< < QLocale ( ) . toString ( a . current ( ) )
< < QLocale ( ) . toString ( a . threshold ( ) )
2010-03-27 13:47:32 +00:00
< < a . raw ( )
< < a . assessmentToString ( )
< < a . value ( )
) ;
item - > setSizeHint ( 0 , QSize ( 0 , 64 ) ) ;
dialogWidget ( ) . treeSmartAttributes ( ) . addTopLevelItem ( item ) ;
}
}
else
dialogWidget ( ) . statusText ( ) . setText ( i18nc ( " @label " , " (unknown) " ) ) ;
setMinimumSize ( dialogWidget ( ) . size ( ) ) ;
resize ( dialogWidget ( ) . size ( ) ) ;
}
void SmartDialog : : setupConnections ( )
{
2014-05-17 03:06:58 +01:00
connect ( buttonBox - > button ( QDialogButtonBox : : Save ) , SIGNAL ( clicked ( ) ) , SLOT ( saveSmartReport ( ) ) ) ;
connect ( buttonBox - > button ( QDialogButtonBox : : Close ) , SIGNAL ( clicked ( ) ) , SLOT ( close ( ) ) ) ;
2010-04-27 17:10:54 +01:00
}
QString SmartDialog : : toHtml ( ) const
{
QString rval ;
QTextStream s ( & rval ) ;
2010-04-28 16:21:07 +01:00
if ( device ( ) . smartStatus ( ) . status ( ) )
s < < HtmlReport : : tableLine ( i18n ( " SMART status: " ) , i18nc ( " @label SMART disk status " , " good " ) ) ;
else
s < < HtmlReport : : tableLine ( i18n ( " SMART status: " ) , i18nc ( " @label SMART disk status " , " BAD " ) ) ;
const QString badSectors = device ( ) . smartStatus ( ) . badSectors ( ) > 0
2014-05-13 23:53:37 +01:00
? QLocale ( ) . toString ( device ( ) . smartStatus ( ) . badSectors ( ) )
2010-04-28 16:21:07 +01:00
: i18nc ( " @label SMART number of bad sectors " , " none " ) ;
s < < HtmlReport : : tableLine ( i18n ( " Model: " ) , device ( ) . smartStatus ( ) . modelName ( ) )
< < HtmlReport : : tableLine ( i18n ( " Serial number: " ) , device ( ) . smartStatus ( ) . serial ( ) )
< < HtmlReport : : tableLine ( i18n ( " Firmware revision: " ) , device ( ) . smartStatus ( ) . firmware ( ) )
< < HtmlReport : : tableLine ( i18n ( " Temperature: " ) , SmartStatus : : tempToString ( device ( ) . smartStatus ( ) . temp ( ) ) )
< < HtmlReport : : tableLine ( i18n ( " Bad sectors: " ) , badSectors )
2014-05-18 21:41:48 +01:00
< < HtmlReport : : tableLine ( i18n ( " Powered on for: " ) , KFormat ( ) . formatDuration ( device ( ) . smartStatus ( ) . poweredOn ( ) ) )
2014-05-13 23:53:37 +01:00
< < HtmlReport : : tableLine ( i18n ( " Power cycles: " ) , QLocale ( ) . toString ( device ( ) . smartStatus ( ) . powerCycles ( ) ) )
2010-04-28 16:21:07 +01:00
< < HtmlReport : : tableLine ( i18n ( " Self tests: " ) , SmartStatus : : selfTestStatusToString ( device ( ) . smartStatus ( ) . selfTestStatus ( ) ) )
< < HtmlReport : : tableLine ( i18n ( " Overall assessment: " ) , SmartStatus : : overallAssessmentToString ( device ( ) . smartStatus ( ) . overall ( ) ) ) ;
s < < " </table><br/> " ;
2010-04-27 17:10:54 +01:00
if ( device ( ) . smartStatus ( ) . isValid ( ) )
{
2014-05-18 21:41:48 +01:00
const QFont f = QFontDatabase : : systemFont ( QFontDatabase : : SmallestReadableFont ) ;
2010-04-27 17:10:54 +01:00
const QString size = f . pixelSize ( ) ! = - 1 ? QString ( " %1px " ) . arg ( f . pixelSize ( ) ) : QString ( " %1pt " ) . arg ( f . pointSize ( ) ) ;
const QString st = QString ( " <span style= \" font-family:%1;font-size:%2; \" > " ) . arg ( f . family ( ) ) . arg ( size ) ;
s < < " <table> \n " ;
foreach ( const SmartAttribute & a , device ( ) . smartStatus ( ) . attributes ( ) )
{
s < < " <tr> \n " ;
2014-05-13 23:53:37 +01:00
s < < " <td> " < < QLocale ( ) . toString ( a . id ( ) ) < < " </td> \n "
2010-04-27 17:10:54 +01:00
< < " <td> " < < QString ( " <b>%1</b><br/>%2 " ) . arg ( a . name ( ) ) . arg ( st + a . desc ( ) + " </span> " ) < < " </td> \n "
< < " <td> " < < ( a . failureType ( ) = = SmartAttribute : : PreFailure ? i18nc ( " @item:intable " , " Pre-Failure " ) : i18nc ( " @item:intable " , " Old-Age " ) ) < < " </td> \n "
< < " <td> " < < ( a . updateType ( ) = = SmartAttribute : : Online ? i18nc ( " @item:intable " , " Online " ) : i18nc ( " @item:intable " , " Offline " ) ) < < " </td> \n "
2014-05-13 23:53:37 +01:00
< < " <td> " < < QLocale ( ) . toString ( a . worst ( ) ) < < " </td> \n "
< < " <td> " < < QLocale ( ) . toString ( a . current ( ) ) < < " </td> \n "
< < " <td> " < < QLocale ( ) . toString ( a . threshold ( ) ) < < " </td> \n "
2010-04-27 17:10:54 +01:00
< < " <td> " < < a . raw ( ) < < " </td> \n "
< < " <td> " < < a . assessmentToString ( ) < < " </td> \n "
< < " <td> " < < a . value ( ) < < " </td> \n " ;
s < < " </tr> \n " ;
}
s < < " </table> \n " ;
}
else
s < < " (unknown) " ;
s . flush ( ) ;
return rval ;
}
void SmartDialog : : saveSmartReport ( )
{
2014-05-18 21:41:48 +01:00
const QUrl url = QFileDialog : : getSaveFileUrl ( ) ;
2010-04-27 17:10:54 +01:00
if ( url . isEmpty ( ) )
return ;
2014-05-18 21:41:48 +01:00
QTemporaryFile tempFile ;
2010-04-27 17:10:54 +01:00
if ( tempFile . open ( ) )
{
2010-04-28 16:21:07 +01:00
QTextStream s ( & tempFile ) ;
2010-05-01 14:19:50 +01:00
HtmlReport html ;
s < < html . header ( )
< < toHtml ( )
< < html . footer ( ) ;
2010-04-27 17:10:54 +01:00
tempFile . close ( ) ;
2014-05-18 21:41:48 +01:00
KIO : : CopyJob * job = KIO : : move ( QUrl : : fromLocalFile ( tempFile . fileName ( ) ) , url , KIO : : HideProgressInfo ) ;
job - > exec ( ) ;
if ( job - > error ( ) )
2010-04-27 17:10:54 +01:00
job - > ui ( ) - > showErrorMessage ( ) ;
}
else
2014-05-18 21:41:48 +01:00
KMessageBox : : sorry ( this , xi18nc ( " @info " , " Could not create temporary file when trying to save to <filename>%1</filename>. " , url . fileName ( ) ) , i18nc ( " @title:window " , " Could Not Save SMART Report. " ) ) ;
2010-04-27 17:10:54 +01:00
2010-03-27 13:47:32 +00:00
}