2015-02-07 00:32:25 +00:00
/*************************************************************************
* Copyright ( C ) 2008 by Volker Lanz < vl @ fidra . de > *
2016-03-02 18:33:40 +00:00
* Copyright ( C ) 2016 by Andrius Š tikonas < andrius @ stikonas . eu > *
2015-02-07 00:32:25 +00:00
* *
* 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 3 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 , see < http : //www.gnu.org/licenses/>.*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-09-24 20:32:01 +01:00
2010-02-11 18:47:51 +00:00
# include "gui/applyprogressdialog.h"
2008-09-24 20:32:01 +01:00
2010-02-11 18:47:51 +00:00
# include "gui/applyprogressdialogwidget.h"
# include "gui/applyprogressdetailswidget.h"
2008-09-24 20:32:01 +01:00
2016-01-12 00:12:49 +00:00
# include <core/operationrunner.h>
2008-09-24 20:32:01 +01:00
2016-01-12 00:12:49 +00:00
# include <ops/operation.h>
2008-09-24 20:32:01 +01:00
2016-01-12 00:12:49 +00:00
# include <jobs/job.h>
2008-09-24 20:32:01 +01:00
2016-01-12 00:12:49 +00:00
# include <util/report.h>
# include <util/htmlreport.h>
2008-09-24 20:32:01 +01:00
2014-05-18 21:41:48 +01:00
# include <QApplication>
2008-09-24 20:32:01 +01:00
# include <QCloseEvent>
2014-05-17 03:06:58 +01:00
# include <QDialogButtonBox>
2008-09-24 20:32:01 +01:00
# include <QFont>
# include <QFile>
2014-05-18 21:41:48 +01:00
# include <QFileDialog>
# include <QKeyEvent>
2014-05-17 03:06:58 +01:00
# include <QPushButton>
2014-05-18 21:41:48 +01:00
# include <QTemporaryFile>
2014-05-13 14:47:35 +01:00
# include <QTextStream>
2008-09-24 20:32:01 +01:00
2014-05-18 21:41:48 +01:00
# include <KAboutData>
2014-05-17 03:06:58 +01:00
# include <KConfigGroup>
2016-05-22 20:03:49 +01:00
# include <KRun>
2014-05-18 21:41:48 +01:00
# include <KIO/CopyJob>
# include <KJobUiDelegate>
# include <KLocalizedString>
2015-04-09 21:26:40 +01:00
# include <KIconLoader>
2014-05-18 21:41:48 +01:00
# include <KMessageBox>
2014-05-17 03:06:58 +01:00
# include <KSharedConfig>
2008-09-24 20:32:01 +01:00
2014-05-20 22:33:02 +01:00
const QString ApplyProgressDialog : : m_TimeFormat = QStringLiteral ( " hh:mm:ss " ) ;
2008-09-24 20:32:01 +01:00
2009-05-06 06:43:08 +01:00
static QWidget * mainWindow ( QWidget * w )
{
2015-07-13 15:17:41 +01:00
while ( w & & w - > parentWidget ( ) )
w = w - > parentWidget ( ) ;
return w ;
2009-05-06 06:43:08 +01:00
}
2008-09-24 20:32:01 +01:00
/** Creates a new ProgressDialog
2015-07-13 15:17:41 +01:00
@ param parent pointer to the parent widget
@ param orunner the OperationRunner whose progress this dialog is showing
2008-09-24 20:32:01 +01:00
*/
2010-02-11 18:47:51 +00:00
ApplyProgressDialog : : ApplyProgressDialog ( QWidget * parent , OperationRunner & orunner ) :
2015-07-13 15:17:41 +01:00
QDialog ( parent ) ,
m_ProgressDialogWidget ( new ApplyProgressDialogWidget ( this ) ) ,
m_ProgressDetailsWidget ( new ApplyProgressDetailsWidget ( this ) ) ,
m_OperationRunner ( orunner ) ,
2016-04-14 19:18:36 +01:00
m_Report ( nullptr ) ,
2015-07-13 15:17:41 +01:00
m_SavedParentTitle ( mainWindow ( this ) - > windowTitle ( ) ) ,
m_Timer ( this ) ,
m_Time ( ) ,
2016-04-14 19:18:36 +01:00
m_CurrentOpItem ( nullptr ) ,
m_CurrentJobItem ( nullptr ) ,
2015-07-13 15:17:41 +01:00
m_LastReportUpdate ( 0 )
2008-09-24 20:32:01 +01:00
{
2015-07-13 15:17:41 +01:00
QVBoxLayout * mainLayout = new QVBoxLayout ( this ) ;
setLayout ( mainLayout ) ;
mainLayout - > addWidget ( & dialogWidget ( ) ) ;
QFrame * detailsBox = new QFrame ( this ) ;
mainLayout - > addWidget ( detailsBox ) ;
QVBoxLayout * detailsLayout = new QVBoxLayout ( detailsBox ) ;
detailsLayout - > addWidget ( & detailsWidget ( ) ) ;
detailsWidget ( ) . hide ( ) ;
setAttribute ( Qt : : WA_ShowModal , true ) ;
dialogButtonBox = new QDialogButtonBox ;
okButton = dialogButtonBox - > addButton ( QDialogButtonBox : : Ok ) ;
cancelButton = dialogButtonBox - > addButton ( QDialogButtonBox : : Cancel ) ;
detailsButton = new QPushButton ;
2016-07-18 02:21:14 +01:00
detailsButton - > setText ( xi18nc ( " @action:button " , " &Details " ) + QStringLiteral ( " >> " ) ) ;
2015-07-13 15:17:41 +01:00
detailsButton - > setIcon ( QIcon : : fromTheme ( QStringLiteral ( " help-about " ) ) . pixmap ( IconSize ( KIconLoader : : Toolbar ) ) ) ;
dialogButtonBox - > addButton ( detailsButton , QDialogButtonBox : : ActionRole ) ;
mainLayout - > addWidget ( dialogButtonBox ) ;
dialogWidget ( ) . treeTasks ( ) . setColumnWidth ( 0 , width ( ) * 0.8 ) ;
detailsWidget ( ) . buttonBrowser ( ) . setIcon ( QIcon : : fromTheme ( QStringLiteral ( " document-open " ) ) . pixmap ( IconSize ( KIconLoader : : Toolbar ) ) ) ;
detailsWidget ( ) . buttonSave ( ) . setIcon ( QIcon : : fromTheme ( QStringLiteral ( " document-save " ) ) . pixmap ( IconSize ( KIconLoader : : Toolbar ) ) ) ;
setupConnections ( ) ;
KConfigGroup kcg ( KSharedConfig : : openConfig ( ) , " applyProgressDialog " ) ;
restoreGeometry ( kcg . readEntry < QByteArray > ( " Geometry " , QByteArray ( ) ) ) ;
2008-09-24 20:32:01 +01:00
}
/** Destroys a ProgressDialog */
2010-02-11 18:47:51 +00:00
ApplyProgressDialog : : ~ ApplyProgressDialog ( )
2008-09-24 20:32:01 +01:00
{
2015-07-13 15:17:41 +01:00
KConfigGroup kcg ( KSharedConfig : : openConfig ( ) , " applyProgressDialog " ) ;
kcg . writeEntry ( " Geometry " , saveGeometry ( ) ) ;
delete m_Report ;
2008-09-24 20:32:01 +01:00
}
2010-02-11 18:47:51 +00:00
void ApplyProgressDialog : : setupConnections ( )
2008-09-24 20:32:01 +01:00
{
2016-05-22 20:03:49 +01:00
connect ( & operationRunner ( ) , & OperationRunner : : progressSub , & dialogWidget ( ) . progressSub ( ) , & QProgressBar : : setValue ) ;
connect ( & operationRunner ( ) , & OperationRunner : : finished , this , & ApplyProgressDialog : : onAllOpsFinished ) ;
connect ( & operationRunner ( ) , & OperationRunner : : cancelled , this , & ApplyProgressDialog : : onAllOpsCancelled ) ;
connect ( & operationRunner ( ) , & OperationRunner : : error , this , & ApplyProgressDialog : : onAllOpsError ) ;
connect ( & operationRunner ( ) , & OperationRunner : : opStarted , this , & ApplyProgressDialog : : onOpStarted ) ;
connect ( & operationRunner ( ) , & OperationRunner : : opFinished , this , & ApplyProgressDialog : : onOpFinished ) ;
connect ( & timer ( ) , & QTimer : : timeout , this , & ApplyProgressDialog : : onSecondElapsed ) ;
connect ( & detailsWidget ( ) . buttonSave ( ) , & QPushButton : : clicked , this , & ApplyProgressDialog : : saveReport ) ;
connect ( & detailsWidget ( ) . buttonBrowser ( ) , & QPushButton : : clicked , this , & ApplyProgressDialog : : browserReport ) ;
2016-05-18 22:23:59 +01:00
connect ( dialogButtonBox , & QDialogButtonBox : : accepted , this , & ApplyProgressDialog : : accept ) ;
connect ( dialogButtonBox , & QDialogButtonBox : : rejected , this , & ApplyProgressDialog : : reject ) ;
connect ( detailsButton , & QPushButton : : clicked , this , & ApplyProgressDialog : : toggleDetails ) ;
2008-09-24 20:32:01 +01:00
}
/** Shows the dialog */
2010-02-11 18:47:51 +00:00
void ApplyProgressDialog : : show ( )
2008-09-24 20:32:01 +01:00
{
2016-07-18 02:21:14 +01:00
setStatus ( xi18nc ( " @info:progress " , " Setting up... " ) ) ;
2008-09-24 20:32:01 +01:00
2015-07-13 15:17:41 +01:00
resetReport ( ) ;
2008-09-24 20:32:01 +01:00
2015-07-13 15:17:41 +01:00
dialogWidget ( ) . progressTotal ( ) . setRange ( 0 , operationRunner ( ) . numJobs ( ) ) ;
dialogWidget ( ) . progressTotal ( ) . setValue ( 0 ) ;
2008-09-24 20:32:01 +01:00
2015-07-13 15:17:41 +01:00
dialogWidget ( ) . treeTasks ( ) . clear ( ) ;
okButton - > setVisible ( false ) ;
cancelButton - > setVisible ( true ) ;
2008-09-24 20:32:01 +01:00
2015-07-13 15:17:41 +01:00
timer ( ) . start ( 1000 ) ;
time ( ) . start ( ) ;
2008-12-27 12:01:29 +00:00
2015-07-13 15:17:41 +01:00
setLastReportUpdate ( 0 ) ;
2008-12-27 12:01:29 +00:00
2015-07-13 15:17:41 +01:00
onSecondElapsed ( ) ; // resets the total time output label
2008-12-27 12:01:29 +00:00
2015-07-13 15:17:41 +01:00
QDialog : : show ( ) ;
2008-09-24 20:32:01 +01:00
}
2010-02-11 18:47:51 +00:00
void ApplyProgressDialog : : resetReport ( )
2008-09-24 20:32:01 +01:00
{
2015-07-13 15:17:41 +01:00
delete m_Report ;
2016-04-14 19:18:36 +01:00
m_Report = new Report ( nullptr ) ;
2008-12-27 12:01:29 +00:00
2015-07-13 15:17:41 +01:00
detailsWidget ( ) . editReport ( ) . clear ( ) ;
detailsWidget ( ) . editReport ( ) . setCursorWidth ( 0 ) ;
detailsWidget ( ) . buttonSave ( ) . setEnabled ( false ) ;
detailsWidget ( ) . buttonBrowser ( ) . setEnabled ( false ) ;
2008-09-24 20:32:01 +01:00
2016-05-22 20:03:49 +01:00
connect ( & report ( ) , & Report : : outputChanged , this , & ApplyProgressDialog : : updateReportUnforced ) ;
2008-09-24 20:32:01 +01:00
}
2010-02-11 18:47:51 +00:00
void ApplyProgressDialog : : closeEvent ( QCloseEvent * e )
2008-09-24 20:32:01 +01:00
{
2015-07-13 15:17:41 +01:00
e - > ignore ( ) ;
operationRunner ( ) . isRunning ( ) ? onCancelButton ( ) : onOkButton ( ) ;
2008-09-24 20:32:01 +01:00
}
2014-05-17 03:06:58 +01:00
void ApplyProgressDialog : : toggleDetails ( )
2008-09-24 20:32:01 +01:00
{
2015-07-13 15:17:41 +01:00
const bool isVisible = detailsWidget ( ) . isVisible ( ) ;
detailsWidget ( ) . setVisible ( ! isVisible ) ;
2016-07-18 02:21:14 +01:00
detailsButton - > setText ( xi18nc ( " @action:button " , " &Details " ) + ( isVisible ? QStringLiteral ( " >> " ) : QStringLiteral ( " << " ) ) ) ;
2014-05-17 03:06:58 +01:00
}
2008-12-27 12:01:29 +00:00
2014-05-17 03:06:58 +01:00
void ApplyProgressDialog : : onDetailsButton ( )
{
2015-07-13 15:17:41 +01:00
updateReport ( true ) ;
return ;
2014-05-17 03:06:58 +01:00
}
void ApplyProgressDialog : : onCancelButton ( )
{
2015-07-13 15:17:41 +01:00
if ( operationRunner ( ) . isRunning ( ) ) {
// only cancel once
if ( operationRunner ( ) . isCancelling ( ) )
return ;
QApplication : : setOverrideCursor ( QCursor ( Qt : : WaitCursor ) ) ;
cancelButton - > setEnabled ( false ) ;
2016-07-18 02:21:14 +01:00
setStatus ( xi18nc ( " @info:progress " , " Waiting for operation to finish... " ) ) ;
2015-07-13 15:17:41 +01:00
repaint ( ) ;
dialogWidget ( ) . repaint ( ) ;
// suspend the runner, so it doesn't happily carry on while the user decides
// if he really wants to cancel
operationRunner ( ) . suspendMutex ( ) . lock ( ) ;
cancelButton - > setEnabled ( true ) ;
QApplication : : restoreOverrideCursor ( ) ;
2016-07-18 02:21:14 +01:00
if ( KMessageBox : : questionYesNo ( this , xi18nc ( " @info " , " Do you really want to cancel? " ) , xi18nc ( " @title:window " , " Cancel Running Operations " ) , KGuiItem ( xi18nc ( " @action:button " , " Yes, Cancel Operations " ) , QStringLiteral ( " dialog-ok " ) ) , KStandardGuiItem : : no ( ) ) = = KMessageBox : : Yes )
2015-07-13 15:17:41 +01:00
// in the meantime while we were showing the messagebox, the runner might have finished.
if ( operationRunner ( ) . isRunning ( ) )
operationRunner ( ) . cancel ( ) ;
operationRunner ( ) . suspendMutex ( ) . unlock ( ) ;
}
return ;
2014-05-17 03:06:58 +01:00
}
2008-09-24 20:32:01 +01:00
2014-05-17 03:06:58 +01:00
void ApplyProgressDialog : : onOkButton ( )
{
2015-07-13 15:17:41 +01:00
mainWindow ( this ) - > setWindowTitle ( savedParentTitle ( ) ) ;
2008-09-24 20:32:01 +01:00
2015-07-13 15:17:41 +01:00
QDialog : : accept ( ) ;
2008-09-24 20:32:01 +01:00
}
2010-02-11 18:47:51 +00:00
void ApplyProgressDialog : : onAllOpsFinished ( )
2008-09-24 20:32:01 +01:00
{
2016-07-18 02:21:14 +01:00
allOpsDone ( xi18nc ( " @info:progress " , " All operations successfully finished. " ) ) ;
2008-09-24 20:32:01 +01:00
}
2010-02-11 18:47:51 +00:00
void ApplyProgressDialog : : onAllOpsCancelled ( )
2008-09-24 20:32:01 +01:00
{
2016-07-18 02:21:14 +01:00
allOpsDone ( xi18nc ( " @info:progress " , " Operations cancelled. " ) ) ;
2008-09-24 20:32:01 +01:00
}
2010-02-11 18:47:51 +00:00
void ApplyProgressDialog : : onAllOpsError ( )
2008-09-24 20:32:01 +01:00
{
2016-07-18 02:21:14 +01:00
allOpsDone ( xi18nc ( " @info:progress " , " There were errors while applying operations. Aborted. " ) ) ;
2008-09-24 20:32:01 +01:00
}
2010-02-11 18:47:51 +00:00
void ApplyProgressDialog : : allOpsDone ( const QString & msg )
2008-09-24 20:32:01 +01:00
{
2015-07-13 15:17:41 +01:00
dialogWidget ( ) . progressTotal ( ) . setValue ( operationRunner ( ) . numJobs ( ) ) ;
cancelButton - > setVisible ( false ) ;
okButton - > setVisible ( true ) ;
detailsWidget ( ) . buttonSave ( ) . setEnabled ( true ) ;
detailsWidget ( ) . buttonBrowser ( ) . setEnabled ( true ) ;
timer ( ) . stop ( ) ;
updateReport ( true ) ;
setStatus ( msg ) ;
2008-09-24 20:32:01 +01:00
}
2016-05-22 20:03:49 +01:00
void ApplyProgressDialog : : updateReportUnforced ( )
{
updateReport ( false ) ;
}
2010-02-11 18:47:51 +00:00
void ApplyProgressDialog : : updateReport ( bool force )
2008-09-24 20:32:01 +01:00
{
2015-07-13 15:17:41 +01:00
// Rendering the HTML in the QTextEdit is extremely expensive. So make sure not to do that
// unnecessarily and not too often:
// (1) If the widget isn't visible, don't update.
// (2) Also don't update if the last update was n msecs ago, BUT
// (3) DO update if we're being forced to.
if ( force | | ( detailsWidget ( ) . isVisible ( ) & & time ( ) . elapsed ( ) - lastReportUpdate ( ) > 2000 ) ) {
detailsWidget ( ) . editReport ( ) . setHtml ( QStringLiteral ( " <html><body> " ) + report ( ) . toHtml ( ) + QStringLiteral ( " </body></html> " ) ) ;
detailsWidget ( ) . editReport ( ) . moveCursor ( QTextCursor : : End ) ;
detailsWidget ( ) . editReport ( ) . ensureCursorVisible ( ) ;
setLastReportUpdate ( time ( ) . elapsed ( ) ) ;
}
2008-09-24 20:32:01 +01:00
}
2010-02-11 18:47:51 +00:00
void ApplyProgressDialog : : onOpStarted ( int num , Operation * op )
2008-09-24 20:32:01 +01:00
{
2015-07-13 15:17:41 +01:00
addTaskOutput ( num , * op ) ;
setStatus ( op - > description ( ) ) ;
2008-09-24 20:32:01 +01:00
2015-07-13 15:17:41 +01:00
dialogWidget ( ) . progressSub ( ) . setValue ( 0 ) ;
dialogWidget ( ) . progressSub ( ) . setRange ( 0 , op - > totalProgress ( ) ) ;
2008-09-24 20:32:01 +01:00
2016-05-18 22:23:59 +01:00
connect ( op , & Operation : : jobStarted , this , & ApplyProgressDialog : : onJobStarted ) ;
connect ( op , & Operation : : jobFinished , this , & ApplyProgressDialog : : onJobFinished ) ;
2008-09-24 20:32:01 +01:00
}
2010-02-11 18:47:51 +00:00
void ApplyProgressDialog : : onJobStarted ( Job * job , Operation * op )
2008-09-24 20:32:01 +01:00
{
2015-07-13 15:17:41 +01:00
for ( qint32 i = 0 ; i < dialogWidget ( ) . treeTasks ( ) . topLevelItemCount ( ) ; i + + ) {
QTreeWidgetItem * item = dialogWidget ( ) . treeTasks ( ) . topLevelItem ( i ) ;
2016-04-14 19:18:36 +01:00
if ( item = = nullptr | | reinterpret_cast < const Operation * > ( item - > data ( 0 , Qt : : UserRole ) . toULongLong ( ) ) ! = op )
2015-07-13 15:17:41 +01:00
continue ;
QTreeWidgetItem * child = new QTreeWidgetItem ( ) ;
child - > setText ( 0 , job - > description ( ) ) ;
child - > setIcon ( 0 , job - > statusIcon ( ) ) ;
child - > setText ( 1 , QTime ( 0 , 0 ) . toString ( timeFormat ( ) ) ) ;
item - > addChild ( child ) ;
dialogWidget ( ) . treeTasks ( ) . scrollToBottom ( ) ;
setCurrentJobItem ( child ) ;
break ;
}
2008-09-24 20:32:01 +01:00
}
2010-02-11 18:47:51 +00:00
void ApplyProgressDialog : : onJobFinished ( Job * job , Operation * op )
2008-09-24 20:32:01 +01:00
{
2015-07-13 15:17:41 +01:00
if ( currentJobItem ( ) )
currentJobItem ( ) - > setIcon ( 0 , job - > statusIcon ( ) ) ;
2008-09-24 20:32:01 +01:00
2016-04-14 19:18:36 +01:00
setCurrentJobItem ( nullptr ) ;
2008-09-24 20:32:01 +01:00
2015-07-13 15:17:41 +01:00
const int current = dialogWidget ( ) . progressTotal ( ) . value ( ) ;
dialogWidget ( ) . progressTotal ( ) . setValue ( current + 1 ) ;
2008-12-27 12:01:29 +00:00
2015-07-13 15:17:41 +01:00
setParentTitle ( op - > description ( ) ) ;
updateReport ( true ) ;
2008-09-24 20:32:01 +01:00
}
2010-02-11 18:47:51 +00:00
void ApplyProgressDialog : : onOpFinished ( int num , Operation * op )
2008-09-24 20:32:01 +01:00
{
2015-07-13 15:17:41 +01:00
if ( currentOpItem ( ) ) {
currentOpItem ( ) - > setText ( 0 , opDesc ( num , * op ) ) ;
currentOpItem ( ) - > setIcon ( 0 , op - > statusIcon ( ) ) ;
}
2008-09-24 20:32:01 +01:00
2016-04-14 19:18:36 +01:00
setCurrentOpItem ( nullptr ) ;
2008-12-27 12:01:29 +00:00
2015-07-13 15:17:41 +01:00
setStatus ( op - > description ( ) ) ;
2008-09-24 20:32:01 +01:00
2015-07-13 15:17:41 +01:00
dialogWidget ( ) . progressSub ( ) . setValue ( op - > totalProgress ( ) ) ;
updateReport ( true ) ;
2008-09-24 20:32:01 +01:00
}
2010-02-11 18:47:51 +00:00
void ApplyProgressDialog : : setParentTitle ( const QString & s )
2008-09-24 20:32:01 +01:00
{
2015-07-13 15:17:41 +01:00
const int percent = dialogWidget ( ) . progressTotal ( ) . value ( ) * 100 / dialogWidget ( ) . progressTotal ( ) . maximum ( ) ;
mainWindow ( this ) - > setWindowTitle ( QString : : number ( percent ) + QStringLiteral ( " % - " ) + s + QStringLiteral ( " - " ) + savedParentTitle ( ) ) ;
2008-09-24 20:32:01 +01:00
}
2010-02-11 18:47:51 +00:00
void ApplyProgressDialog : : setStatus ( const QString & s )
2008-09-24 20:32:01 +01:00
{
2015-07-13 15:17:41 +01:00
setWindowTitle ( s ) ;
dialogWidget ( ) . status ( ) . setText ( s ) ;
2008-09-24 20:32:01 +01:00
2015-07-13 15:17:41 +01:00
setParentTitle ( s ) ;
2008-09-24 20:32:01 +01:00
}
2010-02-11 18:47:51 +00:00
QString ApplyProgressDialog : : opDesc ( int num , const Operation & op ) const
2008-09-24 20:32:01 +01:00
{
2016-07-18 02:21:14 +01:00
return xi18nc ( " @info:progress " , " [%1/%2] - %3: %4 " , num , operationRunner ( ) . numOperations ( ) , op . statusText ( ) , op . description ( ) ) ;
2008-09-24 20:32:01 +01:00
}
2010-02-11 18:47:51 +00:00
void ApplyProgressDialog : : addTaskOutput ( int num , const Operation & op )
2008-09-24 20:32:01 +01:00
{
2015-07-13 15:17:41 +01:00
QTreeWidgetItem * item = new QTreeWidgetItem ( ) ;
item - > setIcon ( 0 , op . statusIcon ( ) ) ;
item - > setText ( 0 , opDesc ( num , op ) ) ;
item - > setText ( 1 , QTime ( 0 , 0 ) . toString ( timeFormat ( ) ) ) ;
QFont f ;
f . setWeight ( QFont : : Bold ) ;
item - > setFont ( 0 , f ) ;
item - > setFont ( 1 , f ) ;
item - > setData ( 0 , Qt : : UserRole , reinterpret_cast < const qulonglong > ( & op ) ) ;
dialogWidget ( ) . treeTasks ( ) . addTopLevelItem ( item ) ;
dialogWidget ( ) . treeTasks ( ) . scrollToBottom ( ) ;
setCurrentOpItem ( item ) ;
2008-09-24 20:32:01 +01:00
}
2010-02-11 18:47:51 +00:00
void ApplyProgressDialog : : onSecondElapsed ( )
2008-09-24 20:32:01 +01:00
{
2015-07-13 15:17:41 +01:00
if ( currentJobItem ( ) ) {
QTime t = QTime : : fromString ( currentJobItem ( ) - > text ( 1 ) , timeFormat ( ) ) . addSecs ( 1 ) ;
currentJobItem ( ) - > setText ( 1 , t . toString ( timeFormat ( ) ) ) ;
}
if ( currentOpItem ( ) ) {
QTime t = QTime : : fromString ( currentOpItem ( ) - > text ( 1 ) , timeFormat ( ) ) . addSecs ( 1 ) ;
currentOpItem ( ) - > setText ( 1 , t . toString ( timeFormat ( ) ) ) ; ;
}
const QTime outputTime = QTime ( 0 , 0 ) . addMSecs ( time ( ) . elapsed ( ) ) ;
2016-07-18 02:21:14 +01:00
dialogWidget ( ) . totalTime ( ) . setText ( xi18nc ( " @info:progress " , " Total Time: %1 " , outputTime . toString ( timeFormat ( ) ) ) ) ;
2008-09-24 20:32:01 +01:00
}
2010-02-11 18:47:51 +00:00
void ApplyProgressDialog : : keyPressEvent ( QKeyEvent * e )
2008-09-24 20:32:01 +01:00
{
2015-07-13 15:17:41 +01:00
e - > accept ( ) ;
switch ( e - > key ( ) ) {
case Qt : : Key_Return :
case Qt : : Key_Enter :
if ( okButton - > isEnabled ( ) )
onOkButton ( ) ;
break ;
case Qt : : Key_Escape :
cancelButton - > isEnabled ( ) ? onCancelButton ( ) : onOkButton ( ) ;
break ;
default :
break ;
}
2008-09-24 20:32:01 +01:00
}
2010-02-11 18:47:51 +00:00
void ApplyProgressDialog : : saveReport ( )
2008-09-24 20:32:01 +01:00
{
2015-07-13 15:17:41 +01:00
const QUrl url = QFileDialog : : getSaveFileUrl ( ) ;
2008-09-24 20:32:01 +01:00
2015-07-13 15:17:41 +01:00
if ( url . isEmpty ( ) )
return ;
2008-12-27 12:01:29 +00:00
2015-07-13 15:17:41 +01:00
QTemporaryFile tempFile ;
2010-04-11 17:12:16 +01:00
2015-07-13 15:17:41 +01:00
if ( tempFile . open ( ) ) {
QTextStream s ( & tempFile ) ;
2014-05-18 21:41:48 +01:00
2015-07-13 15:17:41 +01:00
HtmlReport html ;
2014-05-18 21:41:48 +01:00
2015-07-13 15:17:41 +01:00
s < < html . header ( )
< < report ( ) . toHtml ( )
< < html . footer ( ) ;
2010-04-11 17:12:16 +01:00
2015-07-13 15:17:41 +01:00
tempFile . close ( ) ;
2010-04-11 17:12:16 +01:00
2015-07-13 15:17:41 +01:00
KIO : : CopyJob * job = KIO : : move ( QUrl : : fromLocalFile ( tempFile . fileName ( ) ) , url , KIO : : HideProgressInfo ) ;
job - > exec ( ) ;
if ( job - > error ( ) )
job - > ui ( ) - > showErrorMessage ( ) ;
} else
2016-07-18 02:21:14 +01:00
KMessageBox : : sorry ( this , xi18nc ( " @info " , " Could not create temporary file when trying to save to <filename>%1</filename>. " , url . fileName ( ) ) , xi18nc ( " @title:window " , " Could Not Save Report. " ) ) ;
2008-09-24 20:32:01 +01:00
}
2010-02-11 18:47:51 +00:00
void ApplyProgressDialog : : browserReport ( )
2008-09-24 20:32:01 +01:00
{
2015-07-13 15:17:41 +01:00
QTemporaryFile file ;
2008-09-24 20:32:01 +01:00
2015-07-13 15:17:41 +01:00
// Make sure the temp file is created somewhere another user can read it: KRun::runUrl() will open
// the file as the logged in user, not as the user running our application.
file . setFileTemplate ( QStringLiteral ( " /tmp/ " ) + QCoreApplication : : applicationName ( ) + QStringLiteral ( " -XXXXXX.html " ) ) ;
file . setAutoRemove ( false ) ;
2008-12-27 12:01:29 +00:00
2015-07-13 15:17:41 +01:00
if ( file . open ( ) ) {
QTextStream s ( & file ) ;
2010-05-01 14:19:50 +01:00
2015-07-13 15:17:41 +01:00
HtmlReport html ;
2014-05-18 21:41:48 +01:00
2015-07-13 15:17:41 +01:00
s < < html . header ( )
< < report ( ) . toHtml ( )
< < html . footer ( ) ;
2008-09-24 20:32:01 +01:00
2015-07-13 15:17:41 +01:00
// set the temp file's permission for everyone to read it.
file . setPermissions ( QFile : : ReadOwner | QFile : : WriteOwner | QFile : : ReadGroup | QFile : : ReadOther ) ;
2008-09-24 20:32:01 +01:00
2015-07-13 15:17:41 +01:00
if ( ! KRun : : runUrl ( QUrl : : fromLocalFile ( file . fileName ( ) ) , QStringLiteral ( " text/html " ) , this , true ) )
2016-07-18 02:21:14 +01:00
KMessageBox : : sorry ( this , xi18nc ( " @info " , " The configured external browser could not be run. Please check your settings. " ) , xi18nc ( " @title:window " , " Could Not Launch Browser. " ) ) ;
2015-07-13 15:17:41 +01:00
} else
KMessageBox : : sorry ( this , xi18nc ( " @info " , " Could not create temporary file <filename>%1</filename> for writing. " , file . fileName ( ) ) , i18nc ( " @title:window " , " Could Not Launch Browser. " ) ) ;
2008-09-24 20:32:01 +01:00
}