From 896ee459af10c73d20d0ae093b4c02348ffedb18 Mon Sep 17 00:00:00 2001 From: Peter Penz Date: Fri, 6 Nov 2009 15:43:08 +0000 Subject: * enable the loading of version control plugins (thanks to Aaron for the hint) * Don't terminate the thread that receives the item states when changing a view. Instead let the thread get finished and ignore the result. svn path=/trunk/KDE/kdebase/apps/; revision=1045674 --- src/CMakeLists.txt | 9 +- src/dolphincolumnview.cpp | 2 +- src/fileviewsvnplugin.cpp | 353 -------------------- src/fileviewsvnplugin.desktop | 30 -- src/fileviewsvnplugin.h | 93 ------ src/fileviewversioncontrolplugin.desktop | 9 - src/versioncontrol/fileviewsvnplugin.cpp | 362 +++++++++++++++++++++ src/versioncontrol/fileviewsvnplugin.desktop | 30 ++ src/versioncontrol/fileviewsvnplugin.h | 93 ++++++ .../fileviewversioncontrolplugin.desktop | 9 + src/versioncontrol/updateitemstatesthread.cpp | 94 ++++++ src/versioncontrol/updateitemstatesthread.h | 68 ++++ src/versioncontrol/versioncontrolobserver.cpp | 297 +++++++++++++++++ src/versioncontrol/versioncontrolobserver.h | 144 ++++++++ src/versioncontrolobserver.cpp | 336 ------------------- src/versioncontrolobserver.h | 134 -------- src/viewextensionsfactory.cpp | 2 +- 17 files changed, 1104 insertions(+), 961 deletions(-) delete mode 100644 src/fileviewsvnplugin.cpp delete mode 100644 src/fileviewsvnplugin.desktop delete mode 100644 src/fileviewsvnplugin.h delete mode 100644 src/fileviewversioncontrolplugin.desktop create mode 100644 src/versioncontrol/fileviewsvnplugin.cpp create mode 100644 src/versioncontrol/fileviewsvnplugin.desktop create mode 100644 src/versioncontrol/fileviewsvnplugin.h create mode 100644 src/versioncontrol/fileviewversioncontrolplugin.desktop create mode 100644 src/versioncontrol/updateitemstatesthread.cpp create mode 100644 src/versioncontrol/updateitemstatesthread.h create mode 100644 src/versioncontrol/versioncontrolobserver.cpp create mode 100644 src/versioncontrol/versioncontrolobserver.h delete mode 100644 src/versioncontrolobserver.cpp delete mode 100644 src/versioncontrolobserver.h (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0c3416c1e..4bac99705 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -46,7 +46,8 @@ set(dolphinprivate_LIB_SRCS tooltips/ktooltip.cpp tooltips/ktooltipwindow.cpp tooltips/tooltipmanager.cpp - versioncontrolobserver.cpp + versioncontrol/updateitemstatesthread.cpp + versioncontrol/versioncontrolobserver.cpp viewextensionsfactory.cpp viewproperties.cpp zoomlevelinfo.cpp @@ -92,7 +93,7 @@ set(dolphinpart_SRCS ) set(fileviewsvnplugin_SRCS - fileviewsvnplugin.cpp + versioncontrol/fileviewsvnplugin.cpp ) kde4_add_plugin(dolphinpart ${dolphinpart_SRCS}) @@ -106,8 +107,8 @@ install(TARGETS fileviewsvnplugin DESTINATION ${PLUGIN_INSTALL_DIR}) install(FILES dolphinpart.rc DESTINATION ${DATA_INSTALL_DIR}/dolphinpart) install(FILES dolphinpart.desktop DESTINATION ${SERVICES_INSTALL_DIR} ) -install(FILES fileviewsvnplugin.desktop DESTINATION ${SERVICES_INSTALL_DIR}) -install(FILES fileviewversioncontrolplugin.desktop DESTINATION ${SERVICETYPES_INSTALL_DIR}) +install(FILES versioncontrol/fileviewsvnplugin.desktop DESTINATION ${SERVICES_INSTALL_DIR}) +install(FILES versioncontrol/fileviewversioncontrolplugin.desktop DESTINATION ${SERVICETYPES_INSTALL_DIR}) ########################################## diff --git a/src/dolphincolumnview.cpp b/src/dolphincolumnview.cpp index 9157c7ed4..d1f7e9d64 100644 --- a/src/dolphincolumnview.cpp +++ b/src/dolphincolumnview.cpp @@ -31,7 +31,7 @@ #include "draganddrophelper.h" #include "folderexpander.h" #include "tooltips/tooltipmanager.h" -#include "versioncontrolobserver.h" +#include "versioncontrol/versioncontrolobserver.h" #include "viewextensionsfactory.h" #include "zoomlevelinfo.h" diff --git a/src/fileviewsvnplugin.cpp b/src/fileviewsvnplugin.cpp deleted file mode 100644 index a54329954..000000000 --- a/src/fileviewsvnplugin.cpp +++ /dev/null @@ -1,353 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2009 by Peter Penz * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * - ***************************************************************************/ - -#include "fileviewsvnplugin.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -FileViewSvnPlugin::FileViewSvnPlugin() : - m_versionInfoHash(), - m_versionInfoKeys(), - m_updateAction(0), - m_showLocalChangesAction(0), - m_commitAction(0), - m_addAction(0), - m_removeAction(0), - m_command(), - m_errorMsg(), - m_operationCompletedMsg(), - m_contextDir(), - m_contextItems(), - m_tempFile() -{ - m_updateAction = new KAction(this); - m_updateAction->setIcon(KIcon("view-refresh")); - m_updateAction->setText(i18nc("@item:inmenu", "SVN Update")); - connect(m_updateAction, SIGNAL(triggered()), - this, SLOT(updateFiles())); - - m_showLocalChangesAction = new KAction(this); - m_showLocalChangesAction->setIcon(KIcon("view-split-left-right")); - m_showLocalChangesAction->setText(i18nc("@item:inmenu", "Show Local SVN Changes")); - connect(m_showLocalChangesAction, SIGNAL(triggered()), - this, SLOT(showLocalChanges())); - - m_commitAction = new KAction(this); - m_commitAction->setText(i18nc("@item:inmenu", "SVN Commit...")); - connect(m_commitAction, SIGNAL(triggered()), - this, SLOT(commitFiles())); - - m_addAction = new KAction(this); - m_addAction->setIcon(KIcon("list-add")); - m_addAction->setText(i18nc("@item:inmenu", "SVN Add")); - connect(m_addAction, SIGNAL(triggered()), - this, SLOT(addFiles())); - - m_removeAction = new KAction(this); - m_removeAction->setIcon(KIcon("list-remove")); - m_removeAction->setText(i18nc("@item:inmenu", "SVN Delete")); - connect(m_removeAction, SIGNAL(triggered()), - this, SLOT(removeFiles())); -} - -FileViewSvnPlugin::~FileViewSvnPlugin() -{ -} - -QString FileViewSvnPlugin::fileName() const -{ - return ".svn"; -} - -bool FileViewSvnPlugin::beginRetrieval(const QString& directory) -{ - Q_ASSERT(directory.endsWith('/')); - - QStringList arguments; - arguments << "status" << "--show-updates" << directory; - - QProcess process; - process.start("svn", arguments); - while (process.waitForReadyRead()) { - char buffer[1024]; - while (process.readLine(buffer, sizeof(buffer)) > 0) { - VersionState state = NormalVersion; - QString filePath(buffer); - - switch (buffer[0]) { - case '?': state = UnversionedVersion; break; - case 'M': state = LocallyModifiedVersion; break; - case 'A': state = AddedVersion; break; - case 'D': state = RemovedVersion; break; - case 'C': state = ConflictingVersion; break; - default: - if (filePath.contains('*')) { - state = UpdateRequiredVersion; - } - break; - } - - int pos = filePath.indexOf('/'); - const int length = filePath.length() - pos - 1; - filePath = filePath.mid(pos, length); - if (!filePath.isEmpty()) { - m_versionInfoHash.insert(filePath, state); - } - } - } - - m_versionInfoKeys = m_versionInfoHash.keys(); - return true; -} - -void FileViewSvnPlugin::endRetrieval() -{ -} - -KVersionControlPlugin::VersionState FileViewSvnPlugin::versionState(const KFileItem& item) -{ - const QString itemUrl = item.localPath(); - if (m_versionInfoHash.contains(itemUrl)) { - return m_versionInfoHash.value(itemUrl); - } - - if (!item.isDir()) { - // files that have not been listed by 'svn status' (= m_versionInfoHash) - // are under version control per definition - return NormalVersion; - } - - // The item is a directory. Check whether an item listed by 'svn status' (= m_versionInfoHash) - // is part of this directory. In this case a local modification should be indicated in the - // directory already. - foreach (const QString& key, m_versionInfoKeys) { - if (key.startsWith(itemUrl)) { - const VersionState state = m_versionInfoHash.value(key); - if (state == LocallyModifiedVersion) { - return LocallyModifiedVersion; - } - } - } - - return NormalVersion; -} - -QList FileViewSvnPlugin::contextMenuActions(const KFileItemList& items) -{ - Q_ASSERT(!items.isEmpty()); - foreach (const KFileItem& item, items) { - m_contextItems.append(item); - } - m_contextDir.clear(); - - // iterate all items and check the version state to know which - // actions can be enabled - const int itemsCount = items.count(); - int versionedCount = 0; - int editingCount = 0; - foreach (const KFileItem& item, items) { - const VersionState state = versionState(item); - if (state != UnversionedVersion) { - ++versionedCount; - } - - switch (state) { - case LocallyModifiedVersion: - case ConflictingVersion: - ++editingCount; - break; - default: - break; - } - } - m_commitAction->setEnabled(editingCount > 0); - m_addAction->setEnabled(versionedCount == 0); - m_removeAction->setEnabled(versionedCount == itemsCount); - - QList actions; - actions.append(m_updateAction); - actions.append(m_commitAction); - actions.append(m_addAction); - actions.append(m_removeAction); - return actions; -} - -QList FileViewSvnPlugin::contextMenuActions(const QString& directory) -{ - const bool enabled = m_contextItems.isEmpty(); - if (enabled) { - m_contextDir = directory; - } - - // Only enable the SVN actions if no SVN commands are - // executed currently (see slotOperationCompleted() and - // startSvnCommandProcess()). - m_updateAction->setEnabled(enabled); - m_showLocalChangesAction->setEnabled(enabled); - m_commitAction->setEnabled(enabled); - - QList actions; - actions.append(m_updateAction); - actions.append(m_showLocalChangesAction); - actions.append(m_commitAction); - return actions; -} - -void FileViewSvnPlugin::updateFiles() -{ - execSvnCommand("update", - i18nc("@info:status", "Updating SVN repository..."), - i18nc("@info:status", "Update of SVN repository failed."), - i18nc("@info:status", "Updated SVN repository.")); -} - -void FileViewSvnPlugin::showLocalChanges() -{ - Q_ASSERT(!m_contextDir.isEmpty()); - Q_ASSERT(m_contextItems.isEmpty()); - - const QString command = "mkfifo /tmp/fifo; svn diff " + - KShell::quoteArg(m_contextDir) + - " > /tmp/fifo & kompare /tmp/fifo; rm /tmp/fifo"; - KRun::runCommand(command, 0); -} - -void FileViewSvnPlugin::commitFiles() -{ - KDialog dialog(0, Qt::Dialog); - - KVBox* box = new KVBox(&dialog); - new QLabel(i18nc("@label", "Description:"), box); - QTextEdit* editor = new QTextEdit(box); - - dialog.setMainWidget(box); - dialog.setCaption(i18nc("@title:window", "SVN Commit")); - dialog.setButtons(KDialog::Ok | KDialog::Cancel); - dialog.setDefaultButton(KDialog::Ok); - dialog.setButtonText(KDialog::Ok, i18nc("@action:button", "Commit")); - - KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"), - "SvnCommitDialog"); - dialog.restoreDialogSize(dialogConfig); - - if (dialog.exec() == QDialog::Accepted) { - // Write the commit description into a temporary file, so - // that it can be read by the command "svn commit -F". The temporary - // file must stay alive until slotOperationCompleted() is invoked and will - // be destroyed when the version plugin is destructed. - if (!m_tempFile.open()) { - emit errorMessage(i18nc("@info:status", "Commit of SVN changes failed.")); - return; - } - - QTextStream out(&m_tempFile); - const QString fileName = m_tempFile.fileName(); - out << editor->toPlainText(); - m_tempFile.close(); - - execSvnCommand("commit -F " + KShell::quoteArg(fileName), - i18nc("@info:status", "Committing SVN changes..."), - i18nc("@info:status", "Commit of SVN changes failed."), - i18nc("@info:status", "Committed SVN changes.")); - } - - dialog.saveDialogSize(dialogConfig, KConfigBase::Persistent); -} - -void FileViewSvnPlugin::addFiles() -{ - execSvnCommand("add", - i18nc("@info:status", "Adding files to SVN repository..."), - i18nc("@info:status", "Adding of files to SVN repository failed."), - i18nc("@info:status", "Added files to SVN repository.")); -} - -void FileViewSvnPlugin::removeFiles() -{ - execSvnCommand("remove", - i18nc("@info:status", "Removing files from SVN repository..."), - i18nc("@info:status", "Removing of files from SVN repository failed."), - i18nc("@info:status", "Removed files from SVN repository.")); -} - -void FileViewSvnPlugin::slotOperationCompleted() -{ - if (m_contextItems.isEmpty()) { - emit operationCompletedMessage(m_operationCompletedMsg); - emit versionStatesChanged(); - } else { - startSvnCommandProcess(); - } -} - -void FileViewSvnPlugin::slotOperationError() -{ - emit errorMessage(m_errorMsg); - - // don't do any operation on other items anymore - m_contextItems.clear(); -} - -void FileViewSvnPlugin::execSvnCommand(const QString& svnCommand, - const QString& infoMsg, - const QString& errorMsg, - const QString& operationCompletedMsg) -{ - emit infoMessage(infoMsg); - - m_command = svnCommand; - m_errorMsg = errorMsg; - m_operationCompletedMsg = operationCompletedMsg; - - startSvnCommandProcess(); -} - -void FileViewSvnPlugin::startSvnCommandProcess() -{ - QProcess* process = new QProcess(this); - connect(process, SIGNAL(finished(int)), - this, SLOT(slotOperationCompleted())); - connect(process, SIGNAL(error(QProcess::ProcessError)), - this, SLOT(slotOperationError())); - - const QString program = "svn " + m_command + ' '; - if (!m_contextDir.isEmpty()) { - process->start(program + KShell::quoteArg(m_contextDir)); - m_contextDir.clear(); - } else { - const KFileItem item = m_contextItems.takeLast(); - process->start(program + KShell::quoteArg(item.localPath())); - // the remaining items of m_contextItems will be executed - // after the process has finished (see slotOperationFinished()) - } -} diff --git a/src/fileviewsvnplugin.desktop b/src/fileviewsvnplugin.desktop deleted file mode 100644 index 62b485b1a..000000000 --- a/src/fileviewsvnplugin.desktop +++ /dev/null @@ -1,30 +0,0 @@ -[Desktop Entry] -Type=Service -Name=Subversion -Name[bg]=Subversion -Name[csb]=Pòdwersëjô -Name[de]=Subversion -Name[en_GB]=Subversion -Name[eo]=Subversion -Name[et]=Subversion -Name[gl]=Subversion -Name[gu]=સબવર્ઝન -Name[he]=Subversion -Name[hr]=Subversion -Name[id]=Subversion -Name[is]=Subversion -Name[lt]=Subversion -Name[ms]=Subversion -Name[nb]=Subversion -Name[nds]=Subversion -Name[nl]=Subversion -Name[pt]=Subversion -Name[pt_BR]=Subversion -Name[sv]=Subversion -Name[tr]=Subversion -Name[uk]=Subversion -Name[x-test]=xxSubversionxx -Name[zh_TW]=Subversion -X-KDE-ServiceTypes=FileViewVersionControlPlugin -MimeType=text/plain; -X-KDE-Library=fileviewsvnplugin diff --git a/src/fileviewsvnplugin.h b/src/fileviewsvnplugin.h deleted file mode 100644 index 2cb3d71fb..000000000 --- a/src/fileviewsvnplugin.h +++ /dev/null @@ -1,93 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2009 by Peter Penz * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * - ***************************************************************************/ - -#ifndef FILEVIEWSVNPLUGIN_H -#define FILEVIEWSVNPLUGIN_H - -#include -#include -#include -#include - -// TODO: This class will be moved to kdevplatform as soon as kdevplatform will -// be released. Moving it to kdevplatform allows to reuse code for the context -// menu actions like commit, add, update, ... -class FileViewSvnPlugin : public KVersionControlPlugin -{ - Q_OBJECT - -public: - FileViewSvnPlugin(); - virtual ~FileViewSvnPlugin(); - virtual QString fileName() const; - virtual bool beginRetrieval(const QString& directory); - virtual void endRetrieval(); - virtual KVersionControlPlugin::VersionState versionState(const KFileItem& item); - virtual QList contextMenuActions(const KFileItemList& items); - virtual QList contextMenuActions(const QString& directory); - -private slots: - void updateFiles(); - void showLocalChanges(); - void commitFiles(); - void addFiles(); - void removeFiles(); - - void slotOperationCompleted(); - void slotOperationError(); - -private: - /** - * Executes the command "svn {svnCommand}" for the files that have been - * set by getting the context menu actions (see contextMenuActions()). - * @param infoMsg Message that should be shown before the command is executed. - * @param errorMsg Message that should be shown if the execution of the command - * has been failed. - * @param operationCompletedMsg - * Message that should be shown if the execution of the command - * has been completed successfully. - */ - void execSvnCommand(const QString& svnCommand, - const QString& infoMsg, - const QString& errorMsg, - const QString& operationCompletedMsg); - - void startSvnCommandProcess(); - -private: - QHash m_versionInfoHash; - QList m_versionInfoKeys; // cache for accessing the keys of the hash - - QAction* m_updateAction; - QAction* m_showLocalChangesAction; - QAction* m_commitAction; - QAction* m_addAction; - QAction* m_removeAction; - - QString m_command; - QString m_errorMsg; - QString m_operationCompletedMsg; - - QString m_contextDir; - KFileItemList m_contextItems; - - QTemporaryFile m_tempFile; -}; -#endif // FILEVIEWSVNPLUGIN_H - diff --git a/src/fileviewversioncontrolplugin.desktop b/src/fileviewversioncontrolplugin.desktop deleted file mode 100644 index 58abf0350..000000000 --- a/src/fileviewversioncontrolplugin.desktop +++ /dev/null @@ -1,9 +0,0 @@ -[Desktop Entry] -Type=ServiceType -X-KDE-ServiceType=FileViewVersionControlPlugin -Comment=Version Control Plugin for File Views -Comment[nl]=Plugin voor versiecontrole op bestandoverzichten -Comment[pt]='Plugin' de Controlo de Versões para as Áreas de Ficheiros -Comment[sv]=Insticksprogram för versionskontroll i filvyer -Comment[uk]=Додаток керування версіями для панелей перегляду файлів -Comment[x-test]=xxVersion Control Plugin for File Viewsxx diff --git a/src/versioncontrol/fileviewsvnplugin.cpp b/src/versioncontrol/fileviewsvnplugin.cpp new file mode 100644 index 000000000..7e56c724c --- /dev/null +++ b/src/versioncontrol/fileviewsvnplugin.cpp @@ -0,0 +1,362 @@ +/*************************************************************************** + * Copyright (C) 2009 by Peter Penz * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * + ***************************************************************************/ + +#include "fileviewsvnplugin.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +K_PLUGIN_FACTORY(FileViewSvnPluginFactory, registerPlugin();) +K_EXPORT_PLUGIN(FileViewSvnPluginFactory("fileviewsvnplugin")) + +FileViewSvnPlugin::FileViewSvnPlugin(QObject* parent, const QList& args) : + KVersionControlPlugin(parent), + m_versionInfoHash(), + m_versionInfoKeys(), + m_updateAction(0), + m_showLocalChangesAction(0), + m_commitAction(0), + m_addAction(0), + m_removeAction(0), + m_command(), + m_errorMsg(), + m_operationCompletedMsg(), + m_contextDir(), + m_contextItems(), + m_tempFile() +{ + Q_UNUSED(args); + + m_updateAction = new KAction(this); + m_updateAction->setIcon(KIcon("view-refresh")); + m_updateAction->setText(i18nc("@item:inmenu", "SVN Update")); + connect(m_updateAction, SIGNAL(triggered()), + this, SLOT(updateFiles())); + + m_showLocalChangesAction = new KAction(this); + m_showLocalChangesAction->setIcon(KIcon("view-split-left-right")); + m_showLocalChangesAction->setText(i18nc("@item:inmenu", "Show Local SVN Changes")); + connect(m_showLocalChangesAction, SIGNAL(triggered()), + this, SLOT(showLocalChanges())); + + m_commitAction = new KAction(this); + m_commitAction->setText(i18nc("@item:inmenu", "SVN Commit...")); + connect(m_commitAction, SIGNAL(triggered()), + this, SLOT(commitFiles())); + + m_addAction = new KAction(this); + m_addAction->setIcon(KIcon("list-add")); + m_addAction->setText(i18nc("@item:inmenu", "SVN Add")); + connect(m_addAction, SIGNAL(triggered()), + this, SLOT(addFiles())); + + m_removeAction = new KAction(this); + m_removeAction->setIcon(KIcon("list-remove")); + m_removeAction->setText(i18nc("@item:inmenu", "SVN Delete")); + connect(m_removeAction, SIGNAL(triggered()), + this, SLOT(removeFiles())); +} + +FileViewSvnPlugin::~FileViewSvnPlugin() +{ +} + +QString FileViewSvnPlugin::fileName() const +{ + return ".svn"; +} + +bool FileViewSvnPlugin::beginRetrieval(const QString& directory) +{ + Q_ASSERT(directory.endsWith('/')); + + QStringList arguments; + arguments << "status" << "--show-updates" << directory; + + QProcess process; + process.start("svn", arguments); + while (process.waitForReadyRead()) { + char buffer[1024]; + while (process.readLine(buffer, sizeof(buffer)) > 0) { + VersionState state = NormalVersion; + QString filePath(buffer); + + switch (buffer[0]) { + case '?': state = UnversionedVersion; break; + case 'M': state = LocallyModifiedVersion; break; + case 'A': state = AddedVersion; break; + case 'D': state = RemovedVersion; break; + case 'C': state = ConflictingVersion; break; + default: + if (filePath.contains('*')) { + state = UpdateRequiredVersion; + } + break; + } + + int pos = filePath.indexOf('/'); + const int length = filePath.length() - pos - 1; + filePath = filePath.mid(pos, length); + if (!filePath.isEmpty()) { + m_versionInfoHash.insert(filePath, state); + } + } + } + + m_versionInfoKeys = m_versionInfoHash.keys(); + return true; +} + +void FileViewSvnPlugin::endRetrieval() +{ +} + +KVersionControlPlugin::VersionState FileViewSvnPlugin::versionState(const KFileItem& item) +{ + const QString itemUrl = item.localPath(); + if (m_versionInfoHash.contains(itemUrl)) { + return m_versionInfoHash.value(itemUrl); + } + + if (!item.isDir()) { + // files that have not been listed by 'svn status' (= m_versionInfoHash) + // are under version control per definition + return NormalVersion; + } + + // The item is a directory. Check whether an item listed by 'svn status' (= m_versionInfoHash) + // is part of this directory. In this case a local modification should be indicated in the + // directory already. + foreach (const QString& key, m_versionInfoKeys) { + if (key.startsWith(itemUrl)) { + const VersionState state = m_versionInfoHash.value(key); + if (state == LocallyModifiedVersion) { + return LocallyModifiedVersion; + } + } + } + + return NormalVersion; +} + +QList FileViewSvnPlugin::contextMenuActions(const KFileItemList& items) +{ + Q_ASSERT(!items.isEmpty()); + foreach (const KFileItem& item, items) { + m_contextItems.append(item); + } + m_contextDir.clear(); + + // iterate all items and check the version state to know which + // actions can be enabled + const int itemsCount = items.count(); + int versionedCount = 0; + int editingCount = 0; + foreach (const KFileItem& item, items) { + const VersionState state = versionState(item); + if (state != UnversionedVersion) { + ++versionedCount; + } + + switch (state) { + case LocallyModifiedVersion: + case ConflictingVersion: + ++editingCount; + break; + default: + break; + } + } + m_commitAction->setEnabled(editingCount > 0); + m_addAction->setEnabled(versionedCount == 0); + m_removeAction->setEnabled(versionedCount == itemsCount); + + QList actions; + actions.append(m_updateAction); + actions.append(m_commitAction); + actions.append(m_addAction); + actions.append(m_removeAction); + return actions; +} + +QList FileViewSvnPlugin::contextMenuActions(const QString& directory) +{ + const bool enabled = m_contextItems.isEmpty(); + if (enabled) { + m_contextDir = directory; + } + + // Only enable the SVN actions if no SVN commands are + // executed currently (see slotOperationCompleted() and + // startSvnCommandProcess()). + m_updateAction->setEnabled(enabled); + m_showLocalChangesAction->setEnabled(enabled); + m_commitAction->setEnabled(enabled); + + QList actions; + actions.append(m_updateAction); + actions.append(m_showLocalChangesAction); + actions.append(m_commitAction); + return actions; +} + +void FileViewSvnPlugin::updateFiles() +{ + execSvnCommand("update", + i18nc("@info:status", "Updating SVN repository..."), + i18nc("@info:status", "Update of SVN repository failed."), + i18nc("@info:status", "Updated SVN repository.")); +} + +void FileViewSvnPlugin::showLocalChanges() +{ + Q_ASSERT(!m_contextDir.isEmpty()); + Q_ASSERT(m_contextItems.isEmpty()); + + const QString command = "mkfifo /tmp/fifo; svn diff " + + KShell::quoteArg(m_contextDir) + + " > /tmp/fifo & kompare /tmp/fifo; rm /tmp/fifo"; + KRun::runCommand(command, 0); +} + +void FileViewSvnPlugin::commitFiles() +{ + KDialog dialog(0, Qt::Dialog); + + KVBox* box = new KVBox(&dialog); + new QLabel(i18nc("@label", "Description:"), box); + QTextEdit* editor = new QTextEdit(box); + + dialog.setMainWidget(box); + dialog.setCaption(i18nc("@title:window", "SVN Commit")); + dialog.setButtons(KDialog::Ok | KDialog::Cancel); + dialog.setDefaultButton(KDialog::Ok); + dialog.setButtonText(KDialog::Ok, i18nc("@action:button", "Commit")); + + KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"), + "SvnCommitDialog"); + dialog.restoreDialogSize(dialogConfig); + + if (dialog.exec() == QDialog::Accepted) { + // Write the commit description into a temporary file, so + // that it can be read by the command "svn commit -F". The temporary + // file must stay alive until slotOperationCompleted() is invoked and will + // be destroyed when the version plugin is destructed. + if (!m_tempFile.open()) { + emit errorMessage(i18nc("@info:status", "Commit of SVN changes failed.")); + return; + } + + QTextStream out(&m_tempFile); + const QString fileName = m_tempFile.fileName(); + out << editor->toPlainText(); + m_tempFile.close(); + + execSvnCommand("commit -F " + KShell::quoteArg(fileName), + i18nc("@info:status", "Committing SVN changes..."), + i18nc("@info:status", "Commit of SVN changes failed."), + i18nc("@info:status", "Committed SVN changes.")); + } + + dialog.saveDialogSize(dialogConfig, KConfigBase::Persistent); +} + +void FileViewSvnPlugin::addFiles() +{ + execSvnCommand("add", + i18nc("@info:status", "Adding files to SVN repository..."), + i18nc("@info:status", "Adding of files to SVN repository failed."), + i18nc("@info:status", "Added files to SVN repository.")); +} + +void FileViewSvnPlugin::removeFiles() +{ + execSvnCommand("remove", + i18nc("@info:status", "Removing files from SVN repository..."), + i18nc("@info:status", "Removing of files from SVN repository failed."), + i18nc("@info:status", "Removed files from SVN repository.")); +} + +void FileViewSvnPlugin::slotOperationCompleted() +{ + if (m_contextItems.isEmpty()) { + emit operationCompletedMessage(m_operationCompletedMsg); + emit versionStatesChanged(); + } else { + startSvnCommandProcess(); + } +} + +void FileViewSvnPlugin::slotOperationError() +{ + emit errorMessage(m_errorMsg); + + // don't do any operation on other items anymore + m_contextItems.clear(); +} + +void FileViewSvnPlugin::execSvnCommand(const QString& svnCommand, + const QString& infoMsg, + const QString& errorMsg, + const QString& operationCompletedMsg) +{ + emit infoMessage(infoMsg); + + m_command = svnCommand; + m_errorMsg = errorMsg; + m_operationCompletedMsg = operationCompletedMsg; + + startSvnCommandProcess(); +} + +void FileViewSvnPlugin::startSvnCommandProcess() +{ + QProcess* process = new QProcess(this); + connect(process, SIGNAL(finished(int)), + this, SLOT(slotOperationCompleted())); + connect(process, SIGNAL(error(QProcess::ProcessError)), + this, SLOT(slotOperationError())); + + const QString program = "svn " + m_command + ' '; + if (!m_contextDir.isEmpty()) { + process->start(program + KShell::quoteArg(m_contextDir)); + m_contextDir.clear(); + } else { + const KFileItem item = m_contextItems.takeLast(); + process->start(program + KShell::quoteArg(item.localPath())); + // the remaining items of m_contextItems will be executed + // after the process has finished (see slotOperationFinished()) + } +} diff --git a/src/versioncontrol/fileviewsvnplugin.desktop b/src/versioncontrol/fileviewsvnplugin.desktop new file mode 100644 index 000000000..62b485b1a --- /dev/null +++ b/src/versioncontrol/fileviewsvnplugin.desktop @@ -0,0 +1,30 @@ +[Desktop Entry] +Type=Service +Name=Subversion +Name[bg]=Subversion +Name[csb]=Pòdwersëjô +Name[de]=Subversion +Name[en_GB]=Subversion +Name[eo]=Subversion +Name[et]=Subversion +Name[gl]=Subversion +Name[gu]=સબવર્ઝન +Name[he]=Subversion +Name[hr]=Subversion +Name[id]=Subversion +Name[is]=Subversion +Name[lt]=Subversion +Name[ms]=Subversion +Name[nb]=Subversion +Name[nds]=Subversion +Name[nl]=Subversion +Name[pt]=Subversion +Name[pt_BR]=Subversion +Name[sv]=Subversion +Name[tr]=Subversion +Name[uk]=Subversion +Name[x-test]=xxSubversionxx +Name[zh_TW]=Subversion +X-KDE-ServiceTypes=FileViewVersionControlPlugin +MimeType=text/plain; +X-KDE-Library=fileviewsvnplugin diff --git a/src/versioncontrol/fileviewsvnplugin.h b/src/versioncontrol/fileviewsvnplugin.h new file mode 100644 index 000000000..4804971c0 --- /dev/null +++ b/src/versioncontrol/fileviewsvnplugin.h @@ -0,0 +1,93 @@ +/*************************************************************************** + * Copyright (C) 2009 by Peter Penz * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * + ***************************************************************************/ + +#ifndef FILEVIEWSVNPLUGIN_H +#define FILEVIEWSVNPLUGIN_H + +#include +#include +#include +#include + +// TODO: This class will be moved to kdevplatform as soon as kdevplatform will +// be released. Moving it to kdevplatform allows to reuse code for the context +// menu actions like commit, add, update, ... +class FileViewSvnPlugin : public KVersionControlPlugin +{ + Q_OBJECT + +public: + FileViewSvnPlugin(QObject* parent, const QList& args); + virtual ~FileViewSvnPlugin(); + virtual QString fileName() const; + virtual bool beginRetrieval(const QString& directory); + virtual void endRetrieval(); + virtual KVersionControlPlugin::VersionState versionState(const KFileItem& item); + virtual QList contextMenuActions(const KFileItemList& items); + virtual QList contextMenuActions(const QString& directory); + +private slots: + void updateFiles(); + void showLocalChanges(); + void commitFiles(); + void addFiles(); + void removeFiles(); + + void slotOperationCompleted(); + void slotOperationError(); + +private: + /** + * Executes the command "svn {svnCommand}" for the files that have been + * set by getting the context menu actions (see contextMenuActions()). + * @param infoMsg Message that should be shown before the command is executed. + * @param errorMsg Message that should be shown if the execution of the command + * has been failed. + * @param operationCompletedMsg + * Message that should be shown if the execution of the command + * has been completed successfully. + */ + void execSvnCommand(const QString& svnCommand, + const QString& infoMsg, + const QString& errorMsg, + const QString& operationCompletedMsg); + + void startSvnCommandProcess(); + +private: + QHash m_versionInfoHash; + QList m_versionInfoKeys; // cache for accessing the keys of the hash + + QAction* m_updateAction; + QAction* m_showLocalChangesAction; + QAction* m_commitAction; + QAction* m_addAction; + QAction* m_removeAction; + + QString m_command; + QString m_errorMsg; + QString m_operationCompletedMsg; + + QString m_contextDir; + KFileItemList m_contextItems; + + QTemporaryFile m_tempFile; +}; +#endif // FILEVIEWSVNPLUGIN_H + diff --git a/src/versioncontrol/fileviewversioncontrolplugin.desktop b/src/versioncontrol/fileviewversioncontrolplugin.desktop new file mode 100644 index 000000000..58abf0350 --- /dev/null +++ b/src/versioncontrol/fileviewversioncontrolplugin.desktop @@ -0,0 +1,9 @@ +[Desktop Entry] +Type=ServiceType +X-KDE-ServiceType=FileViewVersionControlPlugin +Comment=Version Control Plugin for File Views +Comment[nl]=Plugin voor versiecontrole op bestandoverzichten +Comment[pt]='Plugin' de Controlo de Versões para as Áreas de Ficheiros +Comment[sv]=Insticksprogram för versionskontroll i filvyer +Comment[uk]=Додаток керування версіями для панелей перегляду файлів +Comment[x-test]=xxVersion Control Plugin for File Viewsxx diff --git a/src/versioncontrol/updateitemstatesthread.cpp b/src/versioncontrol/updateitemstatesthread.cpp new file mode 100644 index 000000000..7483e056e --- /dev/null +++ b/src/versioncontrol/updateitemstatesthread.cpp @@ -0,0 +1,94 @@ +/*************************************************************************** + * Copyright (C) 2009 by Peter Penz * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * + ***************************************************************************/ + +#include "updateitemstatesthread.h" + +UpdateItemStatesThread::UpdateItemStatesThread(QObject* parent) : + QThread(parent), + m_retrievedItems(false), + m_mutex(QMutex::Recursive), + m_itemStates() +{ +} + +UpdateItemStatesThread::~UpdateItemStatesThread() +{ +} + +void UpdateItemStatesThread::setData(KVersionControlPlugin* plugin, + const QList& itemStates) +{ + m_plugin = plugin; + m_itemStates = itemStates; +} + +void UpdateItemStatesThread::run() +{ + Q_ASSERT(!m_itemStates.isEmpty()); + Q_ASSERT(m_plugin != 0); + + // The items from m_itemStates may be located in different directory levels. The version + // plugin requires the root directory for KVersionControlPlugin::beginRetrieval(). Instead + // of doing an expensive search, we utilize the knowledge of the implementation of + // VersionControlObserver::addDirectory() to be sure that the last item contains the root. + const QString directory = m_itemStates.last().item.url().directory(KUrl::AppendTrailingSlash); + + QMutexLocker locker(&m_mutex); + m_retrievedItems = false; + if (m_plugin->beginRetrieval(directory)) { + const int count = m_itemStates.count(); + for (int i = 0; i < count; ++i) { + m_itemStates[i].version = m_plugin->versionState(m_itemStates[i].item); + } + m_plugin->endRetrieval(); + m_retrievedItems = true; + } +} + +bool UpdateItemStatesThread::beginReadItemStates() +{ + return m_mutex.tryLock(300); +} + +void UpdateItemStatesThread::endReadItemStates() +{ + m_mutex.unlock(); +} + +QList UpdateItemStatesThread::itemStates() const +{ + return m_itemStates; +} + +bool UpdateItemStatesThread::retrievedItems() const +{ + return m_retrievedItems; +} + +void UpdateItemStatesThread::deleteWhenFinished() +{ + connect(this, SIGNAL(finished()), this, SLOT(slotFinished())); +} + +void UpdateItemStatesThread::slotFinished() +{ + deleteLater(); +} + +#include "updateitemstatesthread.moc" diff --git a/src/versioncontrol/updateitemstatesthread.h b/src/versioncontrol/updateitemstatesthread.h new file mode 100644 index 000000000..36768d0e9 --- /dev/null +++ b/src/versioncontrol/updateitemstatesthread.h @@ -0,0 +1,68 @@ +/*************************************************************************** + * Copyright (C) 2009 by Peter Penz * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * + ***************************************************************************/ + +#ifndef UPDATEITEMSTATESTHREAD_H +#define UPDATEITEMSTATESTHREAD_H + +#include +#include + +#include +#include + +class KVersionControlPlugin; + +/** + * The performance of updating the version state of items depends + * on the used plugin. To prevent that Dolphin gets blocked by a + * slow plugin, the updating is delegated to a thread. + */ +class LIBDOLPHINPRIVATE_EXPORT UpdateItemStatesThread : public QThread +{ + Q_OBJECT + +public: + UpdateItemStatesThread(QObject* parent); + virtual ~UpdateItemStatesThread(); + + void setData(KVersionControlPlugin* plugin, + const QList& itemStates); + + bool beginReadItemStates(); + void endReadItemStates(); + QList itemStates() const; + + bool retrievedItems() const; + + void deleteWhenFinished(); + +protected: + virtual void run(); + +private slots: + void slotFinished(); + +private: + bool m_retrievedItems; + KVersionControlPlugin* m_plugin; + QMutex m_mutex; + QList m_itemStates; +}; + +#endif // UPDATEITEMSTATESTHREAD_H diff --git a/src/versioncontrol/versioncontrolobserver.cpp b/src/versioncontrol/versioncontrolobserver.cpp new file mode 100644 index 000000000..aea60b28d --- /dev/null +++ b/src/versioncontrol/versioncontrolobserver.cpp @@ -0,0 +1,297 @@ +/*************************************************************************** + * Copyright (C) 2009 by Peter Penz * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * + ***************************************************************************/ + +#include "versioncontrolobserver.h" + +#include + +#include +#include +#include +#include +#include + +#include "updateitemstatesthread.h" + +#include +#include +#include +#include + +VersionControlObserver::VersionControlObserver(QAbstractItemView* view) : + QObject(view), + m_pendingItemStatesUpdate(false), + m_versionedDirectory(false), + m_silentUpdate(false), + m_view(view), + m_dirLister(0), + m_dolphinModel(0), + m_dirVerificationTimer(0), + m_plugin(0), + m_updateItemStatesThread(0) +{ + Q_ASSERT(view != 0); + + QAbstractProxyModel* proxyModel = qobject_cast(view->model()); + m_dolphinModel = (proxyModel == 0) ? + qobject_cast(view->model()) : + qobject_cast(proxyModel->sourceModel()); + if (m_dolphinModel != 0) { + m_dirLister = m_dolphinModel->dirLister(); + connect(m_dirLister, SIGNAL(completed()), + this, SLOT(delayedDirectoryVerification())); + + // The verification timer specifies the timeout until the shown directory + // is checked whether it is versioned. Per default it is assumed that users + // don't iterate through versioned directories and a high timeout is used + // The timeout will be decreased as soon as a versioned directory has been + // found (see verifyDirectory()). + m_dirVerificationTimer = new QTimer(this); + m_dirVerificationTimer->setSingleShot(true); + m_dirVerificationTimer->setInterval(500); + connect(m_dirVerificationTimer, SIGNAL(timeout()), + this, SLOT(verifyDirectory())); + } +} + +VersionControlObserver::~VersionControlObserver() +{ + if (m_updateItemStatesThread != 0) { + disconnect(m_updateItemStatesThread, SIGNAL(finished()), + this, SLOT(applyUpdatedItemStates())); + m_updateItemStatesThread->deleteWhenFinished(); + m_updateItemStatesThread = 0; + } +} + +QList VersionControlObserver::contextMenuActions(const KFileItemList& items) const +{ + QList actions; + if (isVersioned() && m_updateItemStatesThread->beginReadItemStates()) { + actions = m_plugin->contextMenuActions(items); + m_updateItemStatesThread->endReadItemStates(); + } + return actions; +} + +QList VersionControlObserver::contextMenuActions(const QString& directory) const +{ + QList actions; + if (isVersioned() && m_updateItemStatesThread->beginReadItemStates()) { + actions = m_plugin->contextMenuActions(directory); + m_updateItemStatesThread->endReadItemStates(); + } + + return actions; +} + +void VersionControlObserver::delayedDirectoryVerification() +{ + m_silentUpdate = false; + m_dirVerificationTimer->start(); +} + +void VersionControlObserver::silentDirectoryVerification() +{ + m_silentUpdate = true; + m_dirVerificationTimer->start(); +} + +void VersionControlObserver::verifyDirectory() +{ + KUrl versionControlUrl = m_dirLister->url(); + if (!versionControlUrl.isLocalFile()) { + return; + } + + if (m_plugin != 0) { + m_plugin->disconnect(); + } + + m_plugin = searchPlugin(versionControlUrl); + const bool foundVersionInfo = (m_plugin != 0); + if (!foundVersionInfo && m_versionedDirectory) { + // Version control systems like Git provide the version information + // file only in the root directory. Check whether the version information file can + // be found in one of the parent directories. + + // TODO... + } + + if (foundVersionInfo) { + if (!m_versionedDirectory) { + m_versionedDirectory = true; + + // The directory is versioned. Assume that the user will further browse through + // versioned directories and decrease the verification timer. + m_dirVerificationTimer->setInterval(100); + connect(m_dirLister, SIGNAL(refreshItems(const QList>&)), + this, SLOT(delayedDirectoryVerification())); + connect(m_dirLister, SIGNAL(newItems(const KFileItemList&)), + this, SLOT(delayedDirectoryVerification())); + connect(m_plugin, SIGNAL(versionStatesChanged()), + this, SLOT(silentDirectoryVerification())); + connect(m_plugin, SIGNAL(infoMessage(const QString&)), + this, SIGNAL(infoMessage(const QString&))); + connect(m_plugin, SIGNAL(errorMessage(const QString&)), + this, SIGNAL(errorMessage(const QString&))); + connect(m_plugin, SIGNAL(operationCompletedMessage(const QString&)), + this, SIGNAL(operationCompletedMessage(const QString&))); + } + updateItemStates(); + } else if (m_versionedDirectory) { + m_versionedDirectory = false; + + // The directory is not versioned. Reset the verification timer to a higher + // value, so that browsing through non-versioned directories is not slown down + // by an immediate verification. + m_dirVerificationTimer->setInterval(500); + disconnect(m_dirLister, SIGNAL(refreshItems(const QList>&)), + this, SLOT(delayedDirectoryVerification())); + disconnect(m_dirLister, SIGNAL(newItems(const KFileItemList&)), + this, SLOT(delayedDirectoryVerification())); + } +} + +void VersionControlObserver::applyUpdatedItemStates() +{ + if (!m_updateItemStatesThread->retrievedItems()) { + // ignore m_silentUpdate for an error message + emit errorMessage(i18nc("@info:status", "Update of version information failed.")); + return; + } + + // QAbstractItemModel::setData() triggers a bottleneck in combination with QListView + // (a detailed description of the root cause is given in the class KFilePreviewGenerator + // from kdelibs). To bypass this bottleneck, the signals of the model are temporary blocked. + // This works as the update of the data does not require a relayout of the views used in Dolphin. + const bool signalsBlocked = m_dolphinModel->signalsBlocked(); + m_dolphinModel->blockSignals(true); + + const QList itemStates = m_updateItemStatesThread->itemStates(); + foreach (const ItemState& itemState, itemStates) { + m_dolphinModel->setData(itemState.index, + QVariant(static_cast(itemState.version)), + Qt::DecorationRole); + } + + m_dolphinModel->blockSignals(signalsBlocked); + m_view->viewport()->repaint(); + + if (!m_silentUpdate) { + // Using an empty message results in clearing the previously shown information message and showing + // the default status bar information. This is useful as the user already gets feedback that the + // operation has been completed because of the icon emblems. + emit operationCompletedMessage(QString()); + } + + if (m_pendingItemStatesUpdate) { + m_pendingItemStatesUpdate = false; + updateItemStates(); + } +} + +void VersionControlObserver::updateItemStates() +{ + Q_ASSERT(m_plugin != 0); + if (m_updateItemStatesThread == 0) { + m_updateItemStatesThread = new UpdateItemStatesThread(this); + connect(m_updateItemStatesThread, SIGNAL(finished()), + this, SLOT(applyUpdatedItemStates())); + } + if (m_updateItemStatesThread->isRunning()) { + // An update is currently ongoing. Wait until the thread has finished + // the update (see applyUpdatedItemStates()). + m_pendingItemStatesUpdate = true; + return; + } + + QList itemStates; + addDirectory(QModelIndex(), itemStates); + if (!itemStates.isEmpty()) { + if (!m_silentUpdate) { + emit infoMessage(i18nc("@info:status", "Updating version information...")); + } + m_updateItemStatesThread->setData(m_plugin, itemStates); + m_updateItemStatesThread->start(); // applyUpdatedItemStates() is called when finished + } +} + +void VersionControlObserver::addDirectory(const QModelIndex& parentIndex, QList& itemStates) +{ + const int rowCount = m_dolphinModel->rowCount(parentIndex); + for (int row = 0; row < rowCount; ++row) { + const QModelIndex index = m_dolphinModel->index(row, DolphinModel::Version, parentIndex); + addDirectory(index, itemStates); + + ItemState itemState; + itemState.index = index; + itemState.item = m_dolphinModel->itemForIndex(index); + itemState.version = KVersionControlPlugin::UnversionedVersion; + + itemStates.append(itemState); + } +} + +KVersionControlPlugin* VersionControlObserver::searchPlugin(const KUrl& directory) const +{ + static bool pluginsAvailable = true; + static QList plugins; + + if (!pluginsAvailable) { + // a searching for plugins has already been done, but no + // plugins are installed + return 0; + } + + if (plugins.isEmpty()) { + // No searching for plugins has been done yet. Query the KServiceTypeTrader for + // all fileview version control plugins and remember them in 'plugins'. + const KService::List pluginServices = KServiceTypeTrader::self()->query("FileViewVersionControlPlugin"); + for (KService::List::ConstIterator it = pluginServices.constBegin(); it != pluginServices.constEnd(); ++it) { + KVersionControlPlugin* plugin = (*it)->createInstance(); + Q_ASSERT(plugin != 0); + plugins.append(plugin); + } + if (plugins.isEmpty()) { + pluginsAvailable = false; + return 0; + } + } + + // verify whether the current directory contains revision information + // like .svn, .git, ... + foreach (KVersionControlPlugin* plugin, plugins) { + KUrl fileUrl = directory; + fileUrl.addPath(plugin->fileName()); + const KFileItem item = m_dirLister->findByUrl(fileUrl); + if (!item.isNull()) { + return plugin; + } + } + + return 0; +} + +bool VersionControlObserver::isVersioned() const +{ + return m_dolphinModel->hasVersionData() && (m_plugin != 0); +} + +#include "versioncontrolobserver.moc" diff --git a/src/versioncontrol/versioncontrolobserver.h b/src/versioncontrol/versioncontrolobserver.h new file mode 100644 index 000000000..2d1cd02b3 --- /dev/null +++ b/src/versioncontrol/versioncontrolobserver.h @@ -0,0 +1,144 @@ +/*************************************************************************** + * Copyright (C) 2009 by Peter Penz * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * + ***************************************************************************/ + +#ifndef REVISIONCONTROLOBSERVER_H +#define REVISIONCONTROLOBSERVER_H + +#include + +#include +#include +#include +#include +#include +#include +#include + +class DolphinModel; +class KDirLister; +class KFileItemList; +class QAbstractItemView; +class QAction; +class QTimer; +class UpdateItemStatesThread; + +/** + * @brief Observes all version control plugins. + * + * The item view gets updated automatically if the currently shown + * directory is under version control. + * + * @see VersionControlPlugin + */ +class LIBDOLPHINPRIVATE_EXPORT VersionControlObserver : public QObject +{ + Q_OBJECT + +public: + VersionControlObserver(QAbstractItemView* view); + virtual ~VersionControlObserver(); + + QList contextMenuActions(const KFileItemList& items) const; + QList contextMenuActions(const QString& directory) const; + +signals: + /** + * Is emitted if an information message with the content \a msg + * should be shown. + */ + void infoMessage(const QString& msg); + + /** + * Is emitted if an error message with the content \a msg + * should be shown. + */ + void errorMessage(const QString& msg); + + /** + * Is emitted if an "operation completed" message with the content \a msg + * should be shown. + */ + void operationCompletedMessage(const QString& msg); + +private slots: + /** + * Invokes verifyDirectory() with a small delay. If delayedDirectoryVerification() + * is invoked before the delay has been exceeded, the delay will be reset. This + * assures that a lot of short requests for directory verification only result + * in one (expensive) call. + */ + void delayedDirectoryVerification(); + + /** + * Invokes verifyDirectory() with a small delay. In opposite to + * delayedDirectoryVerification() it and assures that the verification of + * the directory is done silently without information messages. + */ + void silentDirectoryVerification(); + + void verifyDirectory(); + void applyUpdatedItemStates(); + +private: + struct ItemState + { + QPersistentModelIndex index; + KFileItem item; + KVersionControlPlugin::VersionState version; + }; + + void updateItemStates(); + + /** + * Adds recursively all items from the directory \p parentIndex into + * the list \p itemStates. + */ + void addDirectory(const QModelIndex& parentIndex, QList& itemStates); + + /** + * Returns a matching plugin for the given directory. + * 0 is returned, if no matching plugin has been found. + */ + KVersionControlPlugin* searchPlugin(const KUrl& directory) const; + + /** + * Returns true, if the directory contains a version control information. + */ + bool isVersioned() const; + +private: + bool m_pendingItemStatesUpdate; + bool m_versionedDirectory; + bool m_silentUpdate; // if true, no messages will be send during the update + // of version states + + QAbstractItemView* m_view; + KDirLister* m_dirLister; + DolphinModel* m_dolphinModel; + + QTimer* m_dirVerificationTimer; + + KVersionControlPlugin* m_plugin; + UpdateItemStatesThread* m_updateItemStatesThread; + + friend class UpdateItemStatesThread; +}; + +#endif // REVISIONCONTROLOBSERVER_H + diff --git a/src/versioncontrolobserver.cpp b/src/versioncontrolobserver.cpp deleted file mode 100644 index 9dc7daa41..000000000 --- a/src/versioncontrolobserver.cpp +++ /dev/null @@ -1,336 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2009 by Peter Penz * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * - ***************************************************************************/ - -#include "versioncontrolobserver.h" - -#include "dolphinmodel.h" - -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -/** - * The performance of updating the version state of items depends - * on the used plugin. To prevent that Dolphin gets blocked by a - * slow plugin, the updating is delegated to a thread. - */ -class UpdateItemStatesThread : public QThread -{ -public: - UpdateItemStatesThread(QObject* parent, QMutex* pluginMutex); - void setData(KVersionControlPlugin* plugin, - const QList& itemStates); - QList itemStates() const; - bool retrievedItems() const; - -protected: - virtual void run(); - -private: - bool m_retrievedItems; - KVersionControlPlugin* m_plugin; - QMutex* m_pluginMutex; - QList m_itemStates; -}; - -UpdateItemStatesThread::UpdateItemStatesThread(QObject* parent, QMutex* pluginMutex) : - QThread(parent), - m_retrievedItems(false), - m_pluginMutex(pluginMutex), - m_itemStates() -{ -} - -void UpdateItemStatesThread::setData(KVersionControlPlugin* plugin, - const QList& itemStates) -{ - m_plugin = plugin; - m_itemStates = itemStates; -} - -void UpdateItemStatesThread::run() -{ - Q_ASSERT(!m_itemStates.isEmpty()); - Q_ASSERT(m_plugin != 0); - - // The items from m_itemStates may be located in different directory levels. The version - // plugin requires the root directory for KVersionControlPlugin::beginRetrieval(). Instead - // of doing an expensive search, we utilize the knowledge of the implementation of - // VersionControlObserver::addDirectory() to be sure that the last item contains the root. - const QString directory = m_itemStates.last().item.url().directory(KUrl::AppendTrailingSlash); - - QMutexLocker locker(m_pluginMutex); - m_retrievedItems = false; - if (m_plugin->beginRetrieval(directory)) { - const int count = m_itemStates.count(); - for (int i = 0; i < count; ++i) { - m_itemStates[i].version = m_plugin->versionState(m_itemStates[i].item); - } - m_plugin->endRetrieval(); - m_retrievedItems = true; - } -} - -QList UpdateItemStatesThread::itemStates() const -{ - return m_itemStates; -} - -bool UpdateItemStatesThread::retrievedItems() const -{ - return m_retrievedItems; -} - -// ------------------------------------------------------------------------------------------------ - -VersionControlObserver::VersionControlObserver(QAbstractItemView* view) : - QObject(view), - m_pendingItemStatesUpdate(false), - m_versionedDirectory(false), - m_silentUpdate(false), - m_view(view), - m_dirLister(0), - m_dolphinModel(0), - m_dirVerificationTimer(0), - m_pluginMutex(QMutex::Recursive), - m_plugin(0), - m_updateItemStatesThread(0) -{ - Q_ASSERT(view != 0); - - QAbstractProxyModel* proxyModel = qobject_cast(view->model()); - m_dolphinModel = (proxyModel == 0) ? - qobject_cast(view->model()) : - qobject_cast(proxyModel->sourceModel()); - if (m_dolphinModel != 0) { - m_dirLister = m_dolphinModel->dirLister(); - connect(m_dirLister, SIGNAL(completed()), - this, SLOT(delayedDirectoryVerification())); - - // The verification timer specifies the timeout until the shown directory - // is checked whether it is versioned. Per default it is assumed that users - // don't iterate through versioned directories and a high timeout is used - // The timeout will be decreased as soon as a versioned directory has been - // found (see verifyDirectory()). - m_dirVerificationTimer = new QTimer(this); - m_dirVerificationTimer->setSingleShot(true); - m_dirVerificationTimer->setInterval(500); - connect(m_dirVerificationTimer, SIGNAL(timeout()), - this, SLOT(verifyDirectory())); - } -} - -VersionControlObserver::~VersionControlObserver() -{ - if (m_updateItemStatesThread != 0) { - m_updateItemStatesThread->terminate(); - m_updateItemStatesThread->wait(); - } - delete m_plugin; - m_plugin = 0; -} - -QList VersionControlObserver::contextMenuActions(const KFileItemList& items) const -{ - if (m_dolphinModel->hasVersionData() && (m_plugin != 0)) { - QMutexLocker locker(&m_pluginMutex); - return m_plugin->contextMenuActions(items); - } - return QList(); -} - -QList VersionControlObserver::contextMenuActions(const QString& directory) const -{ - if (m_dolphinModel->hasVersionData() && (m_plugin != 0)) { - QMutexLocker locker(&m_pluginMutex); - return m_plugin->contextMenuActions(directory); - } - - return QList(); -} - -void VersionControlObserver::delayedDirectoryVerification() -{ - m_silentUpdate = false; - m_dirVerificationTimer->start(); -} - -void VersionControlObserver::silentDirectoryVerification() -{ - m_silentUpdate = true; - m_dirVerificationTimer->start(); -} - -#include -void VersionControlObserver::verifyDirectory() -{ - KUrl versionControlUrl = m_dirLister->url(); - if (!versionControlUrl.isLocalFile()) { - return; - } - - if (m_plugin == 0) { - return; // TODO: does not work yet, m_plugin will always be 0 - - kDebug() << "Searching FileViewVersionControlPlugins..."; - const KService::List plugins = KServiceTypeTrader::self()->query("FileViewVersionControlPlugin"); - for (KService::List::ConstIterator it = plugins.constBegin(); it != plugins.constEnd(); ++it) { - kDebug() << "found plugin" << (*it)->desktopEntryName(); - m_plugin = (*it)->createInstance(); - } - - connect(m_plugin, SIGNAL(infoMessage(const QString&)), - this, SIGNAL(infoMessage(const QString&))); - connect(m_plugin, SIGNAL(errorMessage(const QString&)), - this, SIGNAL(errorMessage(const QString&))); - connect(m_plugin, SIGNAL(operationCompletedMessage(const QString&)), - this, SIGNAL(operationCompletedMessage(const QString&))); - } - - versionControlUrl.addPath(m_plugin->fileName()); - const KFileItem item = m_dirLister->findByUrl(versionControlUrl); - - bool foundVersionInfo = !item.isNull(); - if (!foundVersionInfo && m_versionedDirectory) { - // Version control systems like Git provide the version information - // file only in the root directory. Check whether the version information file can - // be found in one of the parent directories. - - // TODO... - } - - if (foundVersionInfo) { - if (!m_versionedDirectory) { - m_versionedDirectory = true; - - // The directory is versioned. Assume that the user will further browse through - // versioned directories and decrease the verification timer. - m_dirVerificationTimer->setInterval(100); - connect(m_dirLister, SIGNAL(refreshItems(const QList>&)), - this, SLOT(delayedDirectoryVerification())); - connect(m_dirLister, SIGNAL(newItems(const KFileItemList&)), - this, SLOT(delayedDirectoryVerification())); - connect(m_plugin, SIGNAL(versionStatesChanged()), - this, SLOT(silentDirectoryVerification())); - } - updateItemStates(); - } else if (m_versionedDirectory) { - m_versionedDirectory = false; - - // The directory is not versioned. Reset the verification timer to a higher - // value, so that browsing through non-versioned directories is not slown down - // by an immediate verification. - m_dirVerificationTimer->setInterval(500); - disconnect(m_dirLister, SIGNAL(refreshItems(const QList>&)), - this, SLOT(delayedDirectoryVerification())); - disconnect(m_dirLister, SIGNAL(newItems(const KFileItemList&)), - this, SLOT(delayedDirectoryVerification())); - disconnect(m_plugin, SIGNAL(versionStatesChanged()), - this, SLOT(silentDirectoryVerification())); - } -} - -void VersionControlObserver::applyUpdatedItemStates() -{ - if (!m_updateItemStatesThread->retrievedItems()) { - // ignore m_silentUpdate for an error message - emit errorMessage(i18nc("@info:status", "Update of version information failed.")); - return; - } - - // QAbstractItemModel::setData() triggers a bottleneck in combination with QListView - // (a detailed description of the root cause is given in the class KFilePreviewGenerator - // from kdelibs). To bypass this bottleneck, the signals of the model are temporary blocked. - // This works as the update of the data does not require a relayout of the views used in Dolphin. - const bool signalsBlocked = m_dolphinModel->signalsBlocked(); - m_dolphinModel->blockSignals(true); - - const QList itemStates = m_updateItemStatesThread->itemStates(); - foreach (const ItemState& itemState, itemStates) { - m_dolphinModel->setData(itemState.index, - QVariant(static_cast(itemState.version)), - Qt::DecorationRole); - } - - m_dolphinModel->blockSignals(signalsBlocked); - m_view->viewport()->repaint(); - - if (!m_silentUpdate) { - // Using an empty message results in clearing the previously shown information message and showing - // the default status bar information. This is useful as the user already gets feedback that the - // operation has been completed because of the icon emblems. - emit operationCompletedMessage(QString()); - } - - if (m_pendingItemStatesUpdate) { - m_pendingItemStatesUpdate = false; - updateItemStates(); - } -} - -void VersionControlObserver::updateItemStates() -{ - Q_ASSERT(m_plugin != 0); - if (m_updateItemStatesThread == 0) { - m_updateItemStatesThread = new UpdateItemStatesThread(this, &m_pluginMutex); - connect(m_updateItemStatesThread, SIGNAL(finished()), - this, SLOT(applyUpdatedItemStates())); - } - if (m_updateItemStatesThread->isRunning()) { - // An update is currently ongoing. Wait until the thread has finished - // the update (see applyUpdatedItemStates()). - m_pendingItemStatesUpdate = true; - return; - } - - QList itemStates; - addDirectory(QModelIndex(), itemStates); - if (!itemStates.isEmpty()) { - if (!m_silentUpdate) { - emit infoMessage(i18nc("@info:status", "Updating version information...")); - } - m_updateItemStatesThread->setData(m_plugin, itemStates); - m_updateItemStatesThread->start(); // applyUpdatedItemStates() is called when finished - } -} - -void VersionControlObserver::addDirectory(const QModelIndex& parentIndex, QList& itemStates) -{ - const int rowCount = m_dolphinModel->rowCount(parentIndex); - for (int row = 0; row < rowCount; ++row) { - const QModelIndex index = m_dolphinModel->index(row, DolphinModel::Version, parentIndex); - addDirectory(index, itemStates); - - ItemState itemState; - itemState.index = index; - itemState.item = m_dolphinModel->itemForIndex(index); - itemState.version = KVersionControlPlugin::UnversionedVersion; - - itemStates.append(itemState); - } -} - -#include "versioncontrolobserver.moc" diff --git a/src/versioncontrolobserver.h b/src/versioncontrolobserver.h deleted file mode 100644 index 44a0adcad..000000000 --- a/src/versioncontrolobserver.h +++ /dev/null @@ -1,134 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2009 by Peter Penz * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * - ***************************************************************************/ - -#ifndef REVISIONCONTROLOBSERVER_H -#define REVISIONCONTROLOBSERVER_H - -#include - -#include -#include -#include -#include -#include -#include -#include - -class DolphinModel; -class KDirLister; -class KFileItemList; -class QAbstractItemView; -class QAction; -class QTimer; -class UpdateItemStatesThread; - -/** - * @brief Observes all version control plugins. - * - * The item view gets updated automatically if the currently shown - * directory is under version control. - * - * @see VersionControlPlugin - */ -class LIBDOLPHINPRIVATE_EXPORT VersionControlObserver : public QObject -{ - Q_OBJECT - -public: - VersionControlObserver(QAbstractItemView* view); - virtual ~VersionControlObserver(); - - QList contextMenuActions(const KFileItemList& items) const; - QList contextMenuActions(const QString& directory) const; - -signals: - /** - * Is emitted if an information message with the content \a msg - * should be shown. - */ - void infoMessage(const QString& msg); - - /** - * Is emitted if an error message with the content \a msg - * should be shown. - */ - void errorMessage(const QString& msg); - - /** - * Is emitted if an "operation completed" message with the content \a msg - * should be shown. - */ - void operationCompletedMessage(const QString& msg); - -private slots: - /** - * Invokes verifyDirectory() with a small delay. If delayedDirectoryVerification() - * is invoked before the delay has been exceeded, the delay will be reset. This - * assures that a lot of short requests for directory verification only result - * in one (expensive) call. - */ - void delayedDirectoryVerification(); - - /** - * Invokes verifyDirectory() with a small delay. In opposite to - * delayedDirectoryVerification() it and assures that the verification of - * the directory is done silently without information messages. - */ - void silentDirectoryVerification(); - - void verifyDirectory(); - void applyUpdatedItemStates(); - -private: - struct ItemState - { - QPersistentModelIndex index; - KFileItem item; - KVersionControlPlugin::VersionState version; - }; - - void updateItemStates(); - - /** - * Adds recursively all items from the directory \p parentIndex into - * the list \p itemStates. - */ - void addDirectory(const QModelIndex& parentIndex, QList& itemStates); - -private: - bool m_pendingItemStatesUpdate; - bool m_versionedDirectory; - bool m_silentUpdate; // if true, no messages will be send during the update - // of version states - - QAbstractItemView* m_view; - KDirLister* m_dirLister; - DolphinModel* m_dolphinModel; - - QTimer* m_dirVerificationTimer; - - mutable QMutex m_pluginMutex; - KVersionControlPlugin* m_plugin; - UpdateItemStatesThread* m_updateItemStatesThread; - - friend class UpdateItemStatesThread; -}; - -#endif // REVISIONCONTROLOBSERVER_H - diff --git a/src/viewextensionsfactory.cpp b/src/viewextensionsfactory.cpp index e3ec64664..86311e28d 100644 --- a/src/viewextensionsfactory.cpp +++ b/src/viewextensionsfactory.cpp @@ -28,7 +28,7 @@ #include "selectionmanager.h" #include "settings/dolphinsettings.h" #include "tooltips/tooltipmanager.h" -#include "versioncontrolobserver.h" +#include "versioncontrol/versioncontrolobserver.h" #include "dolphin_generalsettings.h" -- cgit v1.3