File indexing completed on 2024-05-05 04:47:27

0001 /****************************************************************************************
0002  * Copyright (c) 2011 Bart Cerneels <bart.cerneels@kde.org                              *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "BrowserMessageArea.h"
0018 
0019 #include "statusbar/LongMessageWidget.h"
0020 
0021 #define SHORT_MESSAGE_DURATION 5000
0022 #define POPUP_MESSAGE_DURATION 5000
0023 
0024 BrowserMessageArea::BrowserMessageArea( QWidget *parent )
0025     : BoxWidget( true, parent )
0026     , m_busy( false )
0027 {
0028     setObjectName( QStringLiteral("BrowserMessageArea") );
0029 
0030     m_progressBar = new CompoundProgressBar( this );
0031     connect( m_progressBar, &CompoundProgressBar::allDone, this, &BrowserMessageArea::hideProgress );
0032     layout()->addWidget( m_progressBar );
0033 
0034     m_progressBar->hide();
0035 
0036     m_messageLabel = new QLabel( this );
0037     m_messageLabel->setAlignment( Qt::AlignCenter );
0038     m_messageLabel->setWordWrap( true );
0039     m_messageLabel->hide();
0040 
0041     m_shortMessageTimer = new QTimer( this );
0042     m_shortMessageTimer->setSingleShot( true );
0043     connect( m_shortMessageTimer, &QTimer::timeout, this, &BrowserMessageArea::nextShortMessage );
0044 
0045     //register to carry MessageType across threads
0046     qRegisterMetaType<Amarok::Logger::MessageType>( "MessageType" );
0047     connect( this, &BrowserMessageArea::signalLongMessage,
0048              this, &BrowserMessageArea::slotLongMessage,
0049              Qt::QueuedConnection );
0050 }
0051 
0052 void
0053 BrowserMessageArea::shortMessageImpl( const QString &text )
0054 {
0055     if( !m_busy )
0056     {
0057         m_busy = true;
0058         m_messageLabel->setText( text );
0059         m_messageLabel->show();
0060         m_shortMessageTimer->start( SHORT_MESSAGE_DURATION );
0061     }
0062     else
0063     {
0064         m_shortMessageQueue.append( text );
0065     }
0066 }
0067 
0068 void
0069 BrowserMessageArea::longMessageImpl( const QString &text, MessageType type )
0070 {
0071     // The purpose of this Q_EMIT is to make the operation thread safe. If this
0072     // method is called from a non-GUI thread, the "Q_EMIT" relays it over the
0073     // event loop to the GUI thread, so that we can safely create widgets.
0074     Q_EMIT signalLongMessage( text, type );
0075 }
0076 
0077 void
0078 BrowserMessageArea::newProgressOperationImpl( KJob *job, const QString &text, QObject *context,
0079                                               const std::function<void ()> &function, Qt::ConnectionType type )
0080 {
0081     KJobProgressBar *newBar = new KJobProgressBar( nullptr, job );
0082     newBar->setDescription( text );
0083     connect( job, &KJob::destroyed, m_progressBar,
0084              &CompoundProgressBar::endProgressOperation );
0085     newBar->setAbortSlot( context, function, type );
0086     m_progressBar->addProgressBar( newBar, job );
0087     m_progressBar->show();
0088 
0089     m_busy = true;
0090 }
0091 
0092 void
0093 BrowserMessageArea::newProgressOperationImpl( QNetworkReply *reply, const QString &text, QObject *obj,
0094                                               const std::function<void ()> &function, Qt::ConnectionType type )
0095 {
0096     NetworkProgressBar *newBar = new NetworkProgressBar( nullptr, reply );
0097     newBar->setDescription( text );
0098     newBar->setAbortSlot( reply, &QNetworkReply::deleteLater );
0099     connect( reply, &QNetworkReply::destroyed, m_progressBar,
0100              &CompoundProgressBar::endProgressOperation );
0101     newBar->setAbortSlot( obj, function, type );
0102     m_progressBar->addProgressBar( newBar, reply );
0103     m_progressBar->show();
0104 
0105     m_busy = true;
0106 }
0107 
0108 void
0109 BrowserMessageArea::newProgressOperationImpl( QObject *sender, const QMetaMethod &increment, const QMetaMethod &end, const QString &text,
0110                                               int maximum, QObject *obj, const std::function<void ()> &function, Qt::ConnectionType type )
0111 {
0112     ProgressBar *newBar = new ProgressBar( nullptr );
0113     newBar->setDescription( text );
0114     newBar->setMaximum( maximum );
0115     connect( sender, &QObject::destroyed, m_progressBar,
0116              &CompoundProgressBar::endProgressOperation, Qt::QueuedConnection );
0117     int endIndex = m_progressBar->metaObject()->indexOfSlot( "endProgressOperation(QObject*)" );
0118     auto endSlot = m_progressBar->metaObject()->method( endIndex );
0119     connect( sender, end, m_progressBar, endSlot, Qt::QueuedConnection );
0120     int incrementIndex = m_progressBar->metaObject()->indexOfSlot( "slotIncrementProgress()" );
0121     auto incrementSlot = m_progressBar->metaObject()->method( incrementIndex );
0122     connect( sender, increment, m_progressBar, incrementSlot, Qt::QueuedConnection );
0123     if( sender->metaObject()->indexOfSignal( "totalSteps(int)" ) != -1 )
0124         connect( sender, SIGNAL(totalSteps(int)), newBar, SLOT(slotTotalSteps(int)) );
0125     newBar->setAbortSlot( obj, function, type );
0126     m_progressBar->addProgressBar( newBar, sender );
0127     m_progressBar->show();
0128 
0129     m_busy = true;
0130 }
0131 
0132 void
0133 BrowserMessageArea::hideProgress() //SLOT
0134 {
0135     m_progressBar->hide();
0136     m_busy = false;
0137 
0138     nextShortMessage();
0139 }
0140 
0141 void
0142 BrowserMessageArea::nextShortMessage()
0143 {
0144     if( m_shortMessageQueue.count() > 0 )
0145     {
0146         m_busy = true;
0147         m_messageLabel->setText( m_shortMessageQueue.takeFirst() );
0148         m_messageLabel->show();
0149         m_shortMessageTimer->start( SHORT_MESSAGE_DURATION );
0150     }
0151     else
0152     {
0153         m_messageLabel->hide();
0154         m_busy = false;
0155     }
0156 }
0157 
0158 void
0159 BrowserMessageArea::slotLongMessage( const QString &text, MessageType type )
0160 {
0161     Q_UNUSED(type)
0162 
0163     LongMessageWidget *message = new LongMessageWidget( text );
0164     connect( message, &LongMessageWidget::closed, message, &QObject::deleteLater );
0165 }