1/*
2 Copyright (c) 2008 Volker Krause <vkrause@kde.org>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19
20#include "infodialog.h"
21
22#include <KCursor>
23#include <KDebug>
24#include <KGlobal>
25
26#include <QApplication>
27#include <QHBoxLayout>
28#include <QLabel>
29#include <QListWidget>
30#include <QProgressBar>
31#include <QScrollBar>
32#include <QVBoxLayout>
33
34enum {
35 // The max value of the scrollbar. Don't change this without making the kmail
36 // migrator use this. It still uses hardcoded "100".
37 MAX_PROGRESS = 100
38};
39
40bool InfoDialog::mError = false;
41
42InfoDialog::InfoDialog( bool closeWhenDone ) :
43 mMigratorCount( 0 ),
44 mChange( false ),
45 mCloseWhenDone( closeWhenDone ),
46 mAutoScrollList( true )
47{
48 setAttribute( Qt::WA_DeleteOnClose );
49
50 KGlobal::ref();
51 setButtons( Close );
52 enableButton( Close, false );
53
54 QWidget *widget = new QWidget( this );
55 QVBoxLayout *widgetLayout = new QVBoxLayout( widget );
56
57 mList = new QListWidget( widget );
58 mList->setMinimumWidth( 640 );
59 widgetLayout->addWidget( mList );
60
61 QHBoxLayout *statusLayout = new QHBoxLayout;
62 widgetLayout->addLayout( statusLayout );
63
64 mStatusLabel = new QLabel( widget );
65 mStatusLabel->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Preferred );
66 statusLayout->addWidget( mStatusLabel );
67
68 mProgressBar = new QProgressBar( widget );
69 mProgressBar->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
70 mProgressBar->setMinimumWidth( 200 );
71 statusLayout->addWidget( mProgressBar );
72
73 setMainWidget( widget );
74}
75
76InfoDialog::~InfoDialog()
77{
78 KGlobal::deref();
79}
80
81static KMigratorBase::MessageType convertType( MigratorBase::MessageType type )
82{
83 switch ( type ) {
84 case MigratorBase::Success: return KMigratorBase::Success;
85 case MigratorBase::Error: return KMigratorBase::Error;
86 case MigratorBase::Skip: return KMigratorBase::Skip;
87 case MigratorBase::Warning: return KMigratorBase::Warning;
88 case MigratorBase::Info: return KMigratorBase::Info;
89 }
90 return KMigratorBase::Info;
91}
92
93void InfoDialog::message( MigratorBase::MessageType type, const QString& msg )
94{
95 message( convertType(type), msg );
96}
97
98void InfoDialog::message(KMigratorBase::MessageType type, const QString & msg)
99{
100 bool autoScroll = mAutoScrollList;
101
102 QListWidgetItem *item = new QListWidgetItem( msg, mList );
103 switch ( type ) {
104 case KMigratorBase::Success:
105 item->setIcon( KIcon( QLatin1String("dialog-ok-apply") ) );
106 mChange = true;
107 kDebug() << msg;
108 break;
109 case KMigratorBase::Skip:
110 item->setIcon( KIcon( QLatin1String("dialog-ok") ) );
111 kDebug() << msg;
112 break;
113 case KMigratorBase::Info:
114 item->setIcon( KIcon( QLatin1String("dialog-information") ) );
115 kDebug() << msg;
116 break;
117 case KMigratorBase::Warning:
118 item->setIcon( KIcon( QLatin1String("dialog-warning") ) );
119 kDebug() << msg;
120 break;
121 case KMigratorBase::Error: {
122 item->setIcon( KIcon( QLatin1String("dialog-error") ) );
123 QFont currentFont = font();
124 currentFont.setBold( true );
125 item->setFont( currentFont );
126 mError = true;
127 kError() << msg;
128 }
129 break;
130 default:
131 kError() << "WTF?";
132 }
133
134 mAutoScrollList = autoScroll;
135
136 if ( autoScroll ) {
137 mList->scrollToItem( item );
138 }
139}
140
141void InfoDialog::migratorAdded()
142{
143 ++mMigratorCount;
144 QApplication::setOverrideCursor( KCursor( QLatin1String( "wait" ), Qt::WaitCursor ) );
145}
146
147void InfoDialog::migratorDone()
148{
149 QApplication::restoreOverrideCursor();
150
151 --mMigratorCount;
152 if ( mMigratorCount == 0 ) {
153 enableButton( Close, true );
154 status( QString() );
155 if ( mCloseWhenDone && !hasError() && !hasChange() )
156 accept();
157 }
158}
159
160void InfoDialog::status( const QString &msg )
161{
162 mStatusLabel->setText( msg );
163 if ( msg.isEmpty() ) {
164 progress( 0, MAX_PROGRESS, MAX_PROGRESS );
165 mProgressBar->setFormat( QString() );
166 }
167}
168
169void InfoDialog::progress( int value )
170{
171 mProgressBar->setFormat( QLatin1String( "%p%" ) );
172 mProgressBar->setValue( value );
173}
174
175void InfoDialog::progress( int min, int max, int value )
176{
177 mProgressBar->setFormat( QLatin1String( "%p%" ) );
178 mProgressBar->setRange( min, max );
179 mProgressBar->setValue( value );
180}
181
182void InfoDialog::scrollBarMoved( int value )
183{
184 mAutoScrollList = ( value == mList->verticalScrollBar()->maximum() );
185}
186
187