Warning, file /sdk/cervisia/addignoremenu.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  *  Copyright (c) 2008 Christian Loose <christian.loose@kdemail.net>
0003  *
0004  * This program is free software; you can redistribute it and/or modify
0005  * it under the terms of the GNU General Public License as published by
0006  * the Free Software Foundation; either version 2 of the License, or
0007  * (at your option) any later version.
0008  *
0009  * This program is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  * GNU General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU General Public License
0015  * along with this program; if not, write to the Free Software
0016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0017  */
0018 
0019 #include "addignoremenu.h"
0020 using namespace Cervisia;
0021 
0022 #include <QFile>
0023 #include <QMenu>
0024 #include <QTextStream>
0025 
0026 #include <KLocalizedString>
0027 #include <KMessageBox>
0028 
0029 AddIgnoreMenu::AddIgnoreMenu(const QString &directory, const QStringList &fileList, QWidget *parent)
0030     : QObject(parent)
0031     , m_menu(0)
0032 {
0033     if (!fileList.isEmpty()) {
0034         m_menu = new QMenu(i18n("Add to Ignore List"), parent);
0035 
0036         foreach (const QString &fileName, fileList)
0037             m_fileList << QFileInfo(directory + '/' + fileName);
0038 
0039         addActions();
0040 
0041         connect(m_menu, SIGNAL(triggered(QAction *)), this, SLOT(actionTriggered(QAction *)));
0042     }
0043 }
0044 
0045 QMenu *AddIgnoreMenu::menu()
0046 {
0047     return m_menu;
0048 }
0049 
0050 void AddIgnoreMenu::actionTriggered(QAction *action)
0051 {
0052     // action with wildcard?
0053     if (action->data().toBool()) {
0054         QFileInfo fi = m_fileList.at(0);
0055         appendIgnoreFile(fi.absolutePath(), "*." + fi.completeSuffix());
0056     } else {
0057         foreach (const QFileInfo &fi, m_fileList)
0058             appendIgnoreFile(fi.absolutePath(), fi.fileName());
0059     }
0060 }
0061 
0062 void AddIgnoreMenu::addActions()
0063 {
0064     if (m_fileList.count() > 1) {
0065         QAction *action = m_menu->addAction(i18np("Ignore File", "Ignore %1 Files", m_fileList.count()));
0066         action->setData(false);
0067     } else {
0068         QFileInfo fi = m_fileList.at(0);
0069         QAction *action = m_menu->addAction(fi.fileName());
0070         action->setData(false);
0071 
0072         QString extension = fi.completeSuffix();
0073         if (!extension.isEmpty()) {
0074             QAction *action = m_menu->addAction("*." + extension);
0075             action->setData(true);
0076         }
0077     }
0078 }
0079 
0080 // append the filename to the .cvsignore file in the same subdirectory
0081 void AddIgnoreMenu::appendIgnoreFile(const QString &path, const QString &fileName)
0082 {
0083     QFile ignoreFile(path + "/.cvsignore");
0084     if (!ignoreFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
0085         KMessageBox::error(0, i18n("Cannot open file '%1' for writing.", ignoreFile.fileName()), "Cervisia");
0086         return;
0087     }
0088 
0089     QTextStream ts(&ignoreFile);
0090     ts << fileName << endl;
0091 
0092     ignoreFile.close();
0093 }