File indexing completed on 2025-01-05 05:14:49
0001 /* 0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-3.0-or-later 0005 */ 0006 0007 #include "ignorefiledialog.h" 0008 0009 #include "gitmanager.h" 0010 0011 #include "libkommitwidgets_appdebug.h" 0012 #include <QFileInfo> 0013 #include <QPushButton> 0014 0015 #include <KLocalizedString> 0016 #include <KMessageBox> 0017 0018 IgnoreFileDialog::IgnoreFileDialog(Git::Manager *git, const QString &filePath, QWidget *parent) 0019 : AppDialog(git, parent) 0020 { 0021 setupUi(this); 0022 0023 QFileInfo fi{filePath}; 0024 0025 if (fi.isDir()) 0026 groupBoxFileName->hide(); 0027 0028 mPath = fi.absolutePath() + QLatin1Char('/'); 0029 mPath = mPath.remove(git->path()); 0030 mName = fi.baseName(); 0031 mExt = fi.completeSuffix(); 0032 0033 if (mPath == QLatin1Char('/')) 0034 radioButtonDirIgnoreFile->setEnabled(false); 0035 0036 generateIgnorePattern(); 0037 0038 const auto isIgnored = git->isIgnored(mName + QLatin1Char('.') + mExt); 0039 0040 if (isIgnored) { 0041 groupBoxFileName->setEnabled(false); 0042 groupBoxIgnoreFile->setEnabled(false); 0043 groupBoxPath->setEnabled(false); 0044 KMessageBox::error(this, i18n("The file is ignored already")); 0045 mIsIgnoredAlready = true; 0046 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); 0047 } 0048 connect(buttonBox, &QDialogButtonBox::accepted, this, &IgnoreFileDialog::slotAccept); 0049 } 0050 0051 void IgnoreFileDialog::generateIgnorePattern() 0052 { 0053 QString s; 0054 if (radioButtonName->isChecked()) 0055 s = mName + QStringLiteral(".*"); 0056 else if (radioButtonExt->isChecked()) 0057 s = QStringLiteral("*.") + mExt; 0058 else { 0059 if (mExt.isEmpty()) 0060 s = mName; 0061 else 0062 s = mName + QLatin1Char('.') + mExt; 0063 } 0064 0065 if (radioButtonExactPath->isChecked()) 0066 s = mPath + s; 0067 0068 lineEdit->setText(s); 0069 qCDebug(KOMMIT_WIDGETS_LOG()) << getIgnoreFile(); 0070 } 0071 0072 void IgnoreFileDialog::slotAccept() 0073 { 0074 if (mIsIgnoredAlready) { 0075 accept(); 0076 return; 0077 } 0078 0079 if (lineEdit->text().trimmed().isEmpty()) { 0080 KMessageBox::error(this, i18n("Please enter the pattern")); 0081 return; 0082 } 0083 QFile f(getIgnoreFile()); 0084 if (!f.open(QIODevice::Append | QIODevice::Text)) { 0085 KMessageBox::error(this, i18n("Unable to open file: %1", getIgnoreFile())); 0086 return; 0087 } 0088 0089 f.write("\n" + lineEdit->text().toUtf8()); 0090 f.close(); 0091 0092 accept(); 0093 } 0094 0095 QString IgnoreFileDialog::getIgnoreFile() const 0096 { 0097 if (radioButtonRootIgnoreFile->isChecked()) 0098 return mGit->path() + QStringLiteral("/.gitignore"); 0099 else if (radioButtonDirIgnoreFile->isChecked()) 0100 return mGit->path() + mPath + QStringLiteral(".gitignore"); 0101 else 0102 return mGit->path() + QStringLiteral("/.git/info/exclude"); 0103 } 0104 0105 #include "moc_ignorefiledialog.cpp"