File indexing completed on 2024-05-05 04:40:10

0001 /*
0002     SPDX-FileCopyrightText: 2006-2009 David Nolden <david.nolden.kdevelop@art-master.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "localpatchsource.h"
0008 
0009 #include <QIcon>
0010 #include <QTemporaryFile>
0011 #include <QWidget>
0012 
0013 #include <KLocalizedString>
0014 #include <KProcess>
0015 #include <KShell>
0016 
0017 
0018 #include "ui_localpatchwidget.h"
0019 #include "debug.h"
0020 
0021 LocalPatchSource::LocalPatchSource()
0022 {
0023 }
0024 
0025 LocalPatchSource::~LocalPatchSource()
0026 {
0027     if ( !m_command.isEmpty() && !m_filename.isEmpty() ) {
0028         QFile::remove( m_filename.toLocalFile() );
0029     }
0030 }
0031 
0032 QString LocalPatchSource::name() const
0033 {
0034     return i18n( "Custom Patch" );
0035 }
0036 
0037 QIcon LocalPatchSource::icon() const
0038 {
0039     return QIcon::fromTheme(QStringLiteral("text-x-patch"));
0040 }
0041 
0042 void LocalPatchSource::update()
0043 {
0044     if( !m_command.isEmpty() ) {
0045         QTemporaryFile temp(QDir::tempPath() + QLatin1String("/patchreview_XXXXXX.diff"));
0046         if( temp.open() ) {
0047             temp.setAutoRemove( false );
0048             QString filename = temp.fileName();
0049             qCDebug(PLUGIN_PATCHREVIEW) << "temp file: " << filename;
0050             temp.close();
0051             KProcess proc;
0052             proc.setWorkingDirectory( m_baseDir.toLocalFile() );
0053             proc.setOutputChannelMode( KProcess::OnlyStdoutChannel );
0054             proc.setStandardOutputFile( filename );
0055             ///Try to apply, if it works, the patch is not applied
0056             proc << KShell::splitArgs( m_command );
0057 
0058             qCDebug(PLUGIN_PATCHREVIEW) << "calling " << m_command;
0059 
0060             if ( proc.execute() ) {
0061                 qCWarning(PLUGIN_PATCHREVIEW) << "returned with bad exit code";
0062                 return;
0063             }
0064             if ( !m_filename.isEmpty() ) {
0065                 QFile::remove( m_filename.toLocalFile() );
0066             }
0067             m_filename = QUrl::fromLocalFile( filename );
0068             qCDebug(PLUGIN_PATCHREVIEW) << "success, diff: " << m_filename;
0069         }else{
0070             qCWarning(PLUGIN_PATCHREVIEW) << "PROBLEM";
0071         }
0072     }
0073     if (m_widget) {
0074         m_widget->updatePatchFromEdit();
0075     }
0076     emit patchChanged();
0077 }
0078 
0079 void LocalPatchSource::createWidget()
0080 {
0081     delete m_widget;
0082     m_widget = new LocalPatchWidget(this, nullptr);
0083 }
0084 
0085 QWidget* LocalPatchSource::customWidget() const
0086 {
0087     return m_widget;
0088 }
0089 
0090 LocalPatchWidget::LocalPatchWidget(LocalPatchSource* lpatch, QWidget* parent)
0091     : QWidget(parent)
0092     , m_lpatch(lpatch)
0093     , m_ui(new Ui::LocalPatchWidget)
0094 {
0095     m_ui->setupUi(this);
0096     m_ui->baseDir->setMode( KFile::Directory );
0097     syncPatch();
0098     connect(m_lpatch, &LocalPatchSource::patchChanged, this, &LocalPatchWidget::syncPatch);
0099 }
0100 
0101 void LocalPatchWidget::syncPatch()
0102 {
0103     m_ui->command->setText( m_lpatch->command());
0104     m_ui->filename->setUrl( m_lpatch->file() );
0105     m_ui->baseDir->setUrl( m_lpatch->baseDir() );
0106     m_ui->applied->setCheckState( m_lpatch->isAlreadyApplied() ? Qt::Checked : Qt::Unchecked );
0107 
0108     if ( m_lpatch->command().isEmpty() )
0109         m_ui->tabWidget->setCurrentIndex( m_ui->tabWidget->indexOf( m_ui->fileTab ) );
0110     else
0111         m_ui->tabWidget->setCurrentIndex( m_ui->tabWidget->indexOf( m_ui->commandTab ) );
0112 }
0113 
0114 void LocalPatchWidget::updatePatchFromEdit()
0115 {
0116     m_lpatch->setCommand(m_ui->command->text());
0117     m_lpatch->setFilename(m_ui->filename->url());
0118     m_lpatch->setBaseDir(m_ui->baseDir->url());
0119     m_lpatch->setAlreadyApplied(m_ui->applied->checkState() == Qt::Checked);
0120 }
0121 
0122 #include "moc_localpatchsource.cpp"