← Back to team overview

openlp-core team mailing list archive

[Merge] lp:~patrick-zakweb/openlp/duplicate-removal-review into lp:openlp

 

mohij has proposed merging lp:~patrick-zakweb/openlp/duplicate-removal-review into lp:openlp.

Requested reviews:
  Andreas Preikschat (googol)
  Raoul Snyman (raoul-snyman)
  Tim Bentley (trb143)

For more details, see:
https://code.launchpad.net/~patrick-zakweb/openlp/duplicate-removal-review/+merge/149144

I resubmitted this request in a separate branch, because I removed the resources file to make the diff readable. This basically breaks the branch but makes it reviewable.
---
This is no real merge request. I would just be grateful for some feedback on how to continue.

This branch contains a logic and GUI to find, review and remove duplicate songs from the song database.
The GUI can be reached via "Tools->Find Duplicate Songs". As far as I have tested everything works.

There are several pain points in the code however:
-No tests for the wizard. I have no real idea which parts would be best for testing (I think "everything" is not a good answer for this question :-). Probably some refactoring of the code is necessary to make it testable.
-No good MVC separation for the song review widgets. Some feedback on how to improve this (hopefully without pulling off a full QItemModel subclass) would be appreciated.
-I changed the wizard.py class to allow *not* adding a final progress page. I am not sure whether this is ok.

------------------------------
- Correct many whitespacing issues
- Made test more standards compliant
------------------------------
- Merge master.
- Move customInit() up to OpenLpWizard.
- Move heaps of variables over to underscore separators (perhaps to many as the largest class is indirectly sublassing from QWidget).
- Shorten too long lines.
- Don't pass registry enabled classes around.
- Split up wizard and widget classes into two files.
- Shorten the test lyrics a bit.
------------------------------
-Mere master.
-PEP8-tify even more variables.
-Sentencify lots of comments.
-Utilize a smart statement courtesy of googol++.
------------------------------
-Replace DuplicateSongRemoval class with a set of functions.
-- 
https://code.launchpad.net/~patrick-zakweb/openlp/duplicate-removal-review/+merge/149144
Your team OpenLP Core is subscribed to branch lp:openlp.
=== modified file 'openlp/core/lib/settings.py'
--- openlp/core/lib/settings.py	2013-02-10 16:05:52 +0000
+++ openlp/core/lib/settings.py	2013-02-18 20:30:34 +0000
@@ -204,6 +204,7 @@
         u'shortcuts/songImportItem': [],
         u'shortcuts/themeScreen': [QtGui.QKeySequence(u'T')],
         u'shortcuts/toolsReindexItem': [],
+        u'shortcuts/toolsFindDuplicates': [],
         u'shortcuts/toolsAlertItem': [u'F7'],
         u'shortcuts/toolsFirstTimeWizard': [],
         u'shortcuts/toolsOpenDataFolder': [],

=== modified file 'openlp/core/ui/wizard.py'
--- openlp/core/ui/wizard.py	2013-02-07 08:42:17 +0000
+++ openlp/core/ui/wizard.py	2013-02-18 20:30:34 +0000
@@ -79,13 +79,30 @@
     """
     Generic OpenLP wizard to provide generic functionality and a unified look
     and feel.
+
+    ``parent``
+        The QWidget-derived parent of the wizard.
+
+    ``plugin``
+        Plugin this wizard is part of. The plugin will be saved in the "plugin" variable.
+        The plugin will also be used as basis for the file dialog methods this class provides.
+
+    ``name``
+        The object name this wizard should have.
+
+    ``image``
+        The image to display on the "welcome" page of the wizard. Should be 163x350.
+
+    ``add_progress_page``
+        Whether to add a progress page with a progressbar at the end of the wizard.
     """
-    def __init__(self, parent, plugin, name, image):
+    def __init__(self, parent, plugin, name, image, add_progress_page=True):
         """
         Constructor
         """
         QtGui.QWizard.__init__(self, parent)
         self.plugin = plugin
+        self.with_progress_page = add_progress_page
         self.setObjectName(name)
         self.openIcon = build_icon(u':/general/general_open.png')
         self.deleteIcon = build_icon(u':/general/general_delete.png')
@@ -96,8 +113,9 @@
         self.customInit()
         self.customSignals()
         QtCore.QObject.connect(self, QtCore.SIGNAL(u'currentIdChanged(int)'), self.onCurrentIdChanged)
-        QtCore.QObject.connect(self.errorCopyToButton, QtCore.SIGNAL(u'clicked()'), self.onErrorCopyToButtonClicked)
-        QtCore.QObject.connect(self.errorSaveToButton, QtCore.SIGNAL(u'clicked()'), self.onErrorSaveToButtonClicked)
+        if self.with_progress_page:
+            QtCore.QObject.connect(self.errorCopyToButton, QtCore.SIGNAL(u'clicked()'), self.onErrorCopyToButtonClicked)
+            QtCore.QObject.connect(self.errorSaveToButton, QtCore.SIGNAL(u'clicked()'), self.onErrorSaveToButtonClicked)
 
     def setupUi(self, image):
         """
@@ -110,7 +128,8 @@
             QtGui.QWizard.NoBackButtonOnLastPage)
         add_welcome_page(self, image)
         self.addCustomPages()
-        self.addProgressPage()
+        if self.with_progress_page:
+            self.addProgressPage()
         self.retranslateUi()
 
     def registerFields(self):
@@ -172,7 +191,7 @@
         Stop the wizard on cancel button, close button or ESC key.
         """
         log.debug(u'Wizard cancelled by user.')
-        if self.currentPage() == self.progressPage:
+        if self.with_progress_page and self.currentPage() == self.progressPage:
             Registry().execute(u'openlp_stop_wizard')
         self.done(QtGui.QDialog.Rejected)
 
@@ -180,13 +199,19 @@
         """
         Perform necessary functions depending on which wizard page is active.
         """
-        if self.page(pageId) == self.progressPage:
+        if self.with_progress_page and self.page(pageId) == self.progressPage:
             self.preWizard()
             self.performWizard()
             self.postWizard()
         else:
             self.customPageChanged(pageId)
 
+    def customInit(self):
+        """
+        Song wizard specific initialization.
+        """
+        pass
+
     def customPageChanged(self, pageId):
         """
         Called when changing to a page other than the progress page

=== added file 'openlp/plugins/songs/forms/duplicatesongremovalform.py'
--- openlp/plugins/songs/forms/duplicatesongremovalform.py	1970-01-01 00:00:00 +0000
+++ openlp/plugins/songs/forms/duplicatesongremovalform.py	2013-02-18 20:30:34 +0000
@@ -0,0 +1,354 @@
+# -*- coding: utf-8 -*-
+# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
+
+###############################################################################
+# OpenLP - Open Source Lyrics Projection                                      #
+# --------------------------------------------------------------------------- #
+# Copyright (c) 2008-2013 Raoul Snyman                                        #
+# Portions copyright (c) 2008-2013 Tim Bentley, Gerald Britton, Jonathan      #
+# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub,      #
+# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer.   #
+# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru,          #
+# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith,             #
+# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock,              #
+# Frode Woldsund, Martin Zibricky, Patrick Zimmermann                         #
+# --------------------------------------------------------------------------- #
+# 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; version 2 of the License.                              #
+#                                                                             #
+# 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., 59  #
+# Temple Place, Suite 330, Boston, MA 02111-1307 USA                          #
+###############################################################################
+"""
+The duplicate song removal logic for OpenLP.
+"""
+import logging
+import os
+
+from PyQt4 import QtCore, QtGui
+
+from openlp.core.lib import Registry, translate
+from openlp.core.ui.wizard import OpenLPWizard, WizardStrings
+from openlp.core.utils import AppLocation
+from openlp.plugins.songs.lib.db import Song, MediaFile
+from openlp.plugins.songs.forms.songreviewwidget import SongReviewWidget
+from openlp.plugins.songs.lib.songcompare import songs_probably_equal
+
+log = logging.getLogger(__name__)
+
+class DuplicateSongRemovalForm(OpenLPWizard):
+    """
+    This is the Duplicate Song Removal Wizard. It provides functionality to
+    search for and remove duplicate songs in the database.
+    """
+    log.info(u'DuplicateSongRemovalForm loaded')
+
+    def __init__(self, plugin):
+        """
+        Instantiate the wizard, and run any extra setup we need to.
+
+        ``parent``
+            The QWidget-derived parent of the wizard.
+
+        ``plugin``
+            The songs plugin.
+        """
+        self.duplicate_song_list = []
+        self.review_current_count = 0
+        self.review_total_count = 0
+        OpenLPWizard.__init__(self, self.main_window, plugin, u'duplicateSongRemovalWizard',
+            u':/wizards/wizard_duplicateremoval.bmp', False)
+
+    def customSignals(self):
+        """
+        Song wizard specific signals.
+        """
+        QtCore.QObject.connect(self.finishButton, QtCore.SIGNAL(u'clicked()'), self.onWizardExit)
+        QtCore.QObject.connect(self.cancelButton, QtCore.SIGNAL(u'clicked()'), self.onWizardExit)
+
+    def addCustomPages(self):
+        """
+        Add song wizard specific pages.
+        """
+        # Add custom pages.
+        self.searching_page = QtGui.QWizardPage()
+        self.searching_page.setObjectName(u'searching_page')
+        self.searching_vertical_layout = QtGui.QVBoxLayout(self.searching_page)
+        self.searching_vertical_layout.setObjectName(u'searching_vertical_layout')
+        self.duplicate_search_progress_bar = QtGui.QProgressBar(self.searching_page)
+        self.duplicate_search_progress_bar.setObjectName(u'duplicate_search_progress_bar')
+        self.duplicate_search_progress_bar.setFormat(WizardStrings.PercentSymbolFormat)
+        self.searching_vertical_layout.addWidget(self.duplicate_search_progress_bar)
+        self.found_duplicates_edit = QtGui.QPlainTextEdit(self.searching_page)
+        self.found_duplicates_edit.setUndoRedoEnabled(False)
+        self.found_duplicates_edit.setReadOnly(True)
+        self.found_duplicates_edit.setObjectName(u'found_duplicates_edit')
+        self.searching_vertical_layout.addWidget(self.found_duplicates_edit)
+        self.searching_page_id = self.addPage(self.searching_page)
+        self.review_page = QtGui.QWizardPage()
+        self.review_page.setObjectName(u'review_page')
+        self.review_layout = QtGui.QVBoxLayout(self.review_page)
+        self.review_layout.setObjectName(u'review_layout')
+        self.songs_horizontal_scroll_area = QtGui.QScrollArea(self.review_page)
+        self.songs_horizontal_scroll_area.setObjectName(u'songs_horizontal_scroll_area')
+        self.songs_horizontal_scroll_area.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
+        self.songs_horizontal_scroll_area.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
+        self.songs_horizontal_scroll_area.setFrameStyle(QtGui.QFrame.NoFrame)
+        self.songs_horizontal_scroll_area.setWidgetResizable(True)
+        self.songs_horizontal_scroll_area.setStyleSheet(
+            u'QScrollArea#songs_horizontal_scroll_area {background-color:transparent;}')
+        self.songs_horizontal_songs_widget = QtGui.QWidget(self.songs_horizontal_scroll_area)
+        self.songs_horizontal_songs_widget.setObjectName(u'songs_horizontal_songs_widget')
+        self.songs_horizontal_songs_widget.setStyleSheet(
+            u'QWidget#songs_horizontal_songs_widget {background-color:transparent;}')
+        self.songs_horizontal_layout = QtGui.QHBoxLayout(self.songs_horizontal_songs_widget)
+        self.songs_horizontal_layout.setObjectName(u'songs_horizontal_layout')
+        self.songs_horizontal_layout.setSizeConstraint(QtGui.QLayout.SetMinAndMaxSize)
+        self.songs_horizontal_scroll_area.setWidget(self.songs_horizontal_songs_widget)
+        self.review_layout.addWidget(self.songs_horizontal_scroll_area)
+        self.review_page_id = self.addPage(self.review_page)
+        # Add a dummy page to the end, to prevent the finish button to appear and the next button do disappear on the
+        #review page.
+        self.dummy_page = QtGui.QWizardPage()
+        self.dummy_page_id = self.addPage(self.dummy_page)
+
+    def retranslateUi(self):
+        """
+        Song wizard localisation.
+        """
+        self.setWindowTitle(translate(u'Wizard', u'Wizard'))
+        self.titleLabel.setText(WizardStrings.HeaderStyle % translate(u'OpenLP.Ui',
+            u'Welcome to the Duplicate Song Removal Wizard'))
+        self.informationLabel.setText(translate("Wizard",
+            u'This wizard will help you to remove duplicate songs from the song database. You will have a chance to '
+            u'review every potential duplicate song before it is deleted. So no songs will be deleted without your '
+            u'explicit approval.'))
+        self.searching_page.setTitle(translate(u'Wizard', u'Searching for duplicate songs.'))
+        self.searching_page.setSubTitle(translate(u'Wizard', u'The song database is searched for double songs.'))
+        self.update_review_counter_text()
+        self.review_page.setSubTitle(translate(u'Wizard',
+            u'Here you can decide which songs to remove and which ones to keep.'))
+
+    def update_review_counter_text(self):
+        """
+        Set the wizard review page header text.
+        """
+        self.review_page.setTitle(translate(u'Wizard', u'Review duplicate songs (%s/%s)') % \
+                (self.review_current_count, self.review_total_count))
+
+    def customPageChanged(self, page_id):
+        """
+        Called when changing the wizard page.
+
+        ``page_id``
+            ID of the page the wizard changed to.
+        """
+        # Hide back button.
+        self.button(QtGui.QWizard.BackButton).hide()
+        if page_id == self.searching_page_id:
+            # Search duplicate songs.
+            max_songs = self.plugin.manager.get_object_count(Song)
+            if max_songs == 0 or max_songs == 1:
+                self.duplicate_search_progress_bar.setMaximum(1)
+                self.duplicate_search_progress_bar.setValue(1)
+                self.notify_no_duplicates()
+                return
+            # With x songs we have x*(x - 1) / 2 comparisons.
+            max_progress_count = max_songs * (max_songs - 1) / 2
+            self.duplicate_search_progress_bar.setMaximum(max_progress_count)
+            songs = self.plugin.manager.get_all_objects(Song)
+            for outer_song_counter in range(max_songs - 1):
+                for inner_song_counter in range(outer_song_counter + 1, max_songs):
+                    if songs_probably_equal(songs[outer_song_counter], songs[inner_song_counter]):
+                        duplicate_added = self.add_duplicates_to_song_list(songs[outer_song_counter],
+                            songs[inner_song_counter])
+                        if duplicate_added:
+                            self.found_duplicates_edit.appendPlainText(songs[outer_song_counter].title + "  =  " +
+                                songs[inner_song_counter].title)
+                    self.duplicate_search_progress_bar.setValue(self.duplicate_search_progress_bar.value() + 1)
+            self.review_total_count = len(self.duplicate_song_list)
+            if self.review_total_count == 0:
+                self.notify_no_duplicates()
+        elif page_id == self.review_page_id:
+            self.process_current_duplicate_entry()
+
+    def notify_no_duplicates(self):
+        """
+        Notifies the user, that there were no duplicates found in the database.
+        """
+        self.button(QtGui.QWizard.FinishButton).show()
+        self.button(QtGui.QWizard.FinishButton).setEnabled(True)
+        self.button(QtGui.QWizard.NextButton).hide()
+        QtGui.QMessageBox.information(self, translate(u'Wizard', u'Information'),
+            translate(u'Wizard', u'No duplicate songs have been found in the database.'),
+            QtGui.QMessageBox.StandardButtons(QtGui.QMessageBox.Ok))
+
+
+    def add_duplicates_to_song_list(self, search_song, duplicate_song):
+        """
+        Inserts a song duplicate (two similar songs) to the duplicate song list.
+        If one of the two songs is already part of the duplicate song list,
+        don't add another duplicate group but add the other song to that group.
+        Returns True if at least one of the songs was added, False if both were already
+        member of a group.
+
+        ``search_song``
+            The song we searched the duplicate for.
+
+        ``duplicate_song``
+            The duplicate song.
+        """
+        duplicate_group_found = False
+        duplicate_added = False
+        for duplicate_group in self.duplicate_song_list:
+            # Skip the first song in the duplicate lists, since the first one has to be an earlier song.
+            if search_song in duplicate_group and not duplicate_song in duplicate_group:
+                duplicate_group.append(duplicate_song)
+                duplicate_group_found = True
+                duplicate_added = True
+                break
+            elif not search_song in duplicate_group and duplicate_song in duplicate_group:
+                duplicate_group.append(search_song)
+                duplicate_group_found = True
+                duplicate_added = True
+                break
+            elif search_song in duplicate_group and duplicate_song in duplicate_group:
+                duplicate_group_found = True
+                duplicate_added = False
+                break
+        if not duplicate_group_found:
+            self.duplicate_song_list.append([search_song, duplicate_song])
+            duplicate_added = True
+        return duplicate_added
+
+    def onWizardExit(self):
+        """
+        Once the wizard is finished, refresh the song list,
+        since we potentially removed songs from it.
+        """
+        self.plugin.mediaItem.onSearchTextButtonClicked()
+
+    def setDefaults(self):
+        """
+        Set default form values for the song import wizard.
+        """
+        self.restart()
+        self.duplicate_search_progress_bar.setValue(0)
+        self.found_duplicates_edit.clear()
+
+    def validateCurrentPage(self):
+        """
+        Controls whether we should switch to the next wizard page. This method loops
+        on the review page as long as there are more song duplicates to review.
+        """
+        if self.currentId() == self.review_page_id:
+            # As long as it's not the last duplicate list entry we revisit the review page.
+            if len(self.duplicate_song_list) == 1:
+                return True
+            else:
+                self.proceed_to_next_review()
+                return False
+        return OpenLPWizard.validateCurrentPage(self)
+
+    def remove_button_clicked(self, song_review_widget):
+        """
+        Removes a song from the database, removes the GUI element representing the
+        song on the review page, and disable the remove button if only one duplicate
+        is left.
+
+        ``song_review_widget``
+            The SongReviewWidget whose song we should delete.
+        """
+        # Remove song from duplicate song list.
+        self.duplicate_song_list[-1].remove(song_review_widget.song)
+        # Remove song from the database.
+        item_id = song_review_widget.song.id
+        media_files = self.plugin.manager.get_all_objects(MediaFile,
+            MediaFile.song_id == item_id)
+        for media_file in media_files:
+            try:
+                os.remove(media_file.file_name)
+            except:
+                log.exception(u'Could not remove file: %s',
+                    media_file.file_name)
+        try:
+            save_path = os.path.join(AppLocation.get_section_data_path(
+                self.plugin.name), u'audio', str(item_id))
+            if os.path.exists(save_path):
+                os.rmdir(save_path)
+        except OSError:
+            log.exception(u'Could not remove directory: %s', save_path)
+        self.plugin.manager.delete_object(Song, item_id)
+        # Remove GUI elements for the song.
+        self.songs_horizontal_layout.removeWidget(song_review_widget)
+        song_review_widget.setParent(None)
+        # Check if we only have one duplicate left:
+        # 4 stretches + 1 SongReviewWidget = 5
+        # The SongReviewWidget is then at position 2.
+        if len(self.duplicate_song_list[-1]) == 1:
+            self.songs_horizontal_layout.itemAt(2).widget().song_remove_button.setEnabled(False)
+
+    def proceed_to_next_review(self):
+        """
+        Removes the previous review UI elements and calls process_current_duplicate_entry.
+        """
+        # Remove last duplicate group.
+        self.duplicate_song_list.pop()
+        # Remove all previous elements.
+        for i in reversed(range(self.songs_horizontal_layout.count())): 
+            item = self.songs_horizontal_layout.itemAt(i)
+            if isinstance(item, QtGui.QWidgetItem):
+                # The order is important here, if the .setParent(None) call is done
+                # before the .removeItem() call, a segfault occurs.
+                widget = item.widget()
+                self.songs_horizontal_layout.removeItem(item) 
+                widget.setParent(None)
+            else:
+                self.songs_horizontal_layout.removeItem(item)
+        # Process next set of duplicates.
+        self.process_current_duplicate_entry()
+    
+    def process_current_duplicate_entry(self):
+        """
+        Update the review counter in the wizard header, add song widgets for
+        the current duplicate group to review, if it's the last
+        duplicate song group, hide the "next" button and show the "finish" button.
+        """
+        # Update the counter.
+        self.review_current_count = self.review_total_count - (len(self.duplicate_song_list) - 1)
+        self.update_review_counter_text()
+        # Add song elements to the UI.
+        if len(self.duplicate_song_list) > 0:
+            # A stretch doesn't seem to stretch endlessly, so I add two to get enough stetch for 1400x1050.
+            self.songs_horizontal_layout.addStretch()
+            self.songs_horizontal_layout.addStretch()
+            for duplicate in self.duplicate_song_list[-1]:
+                song_review_widget = SongReviewWidget(self.review_page, duplicate)
+                QtCore.QObject.connect(song_review_widget,
+                        QtCore.SIGNAL(u'song_remove_button_clicked(PyQt_PyObject)'),
+                        self.remove_button_clicked)
+                self.songs_horizontal_layout.addWidget(song_review_widget)
+            self.songs_horizontal_layout.addStretch()
+            self.songs_horizontal_layout.addStretch()
+        # Change next button to finish button on last review.
+        if len(self.duplicate_song_list) == 1:
+            self.button(QtGui.QWizard.FinishButton).show()
+            self.button(QtGui.QWizard.FinishButton).setEnabled(True)
+            self.button(QtGui.QWizard.NextButton).hide()
+    
+    def _get_main_window(self):
+        """
+        Adds the main window to the class dynamically.
+        """
+        if not hasattr(self, u'_main_window'):
+            self._main_window = Registry().get(u'main_window')
+        return self._main_window
+
+    main_window = property(_get_main_window)

=== added file 'openlp/plugins/songs/forms/songreviewwidget.py'
--- openlp/plugins/songs/forms/songreviewwidget.py	1970-01-01 00:00:00 +0000
+++ openlp/plugins/songs/forms/songreviewwidget.py	2013-02-18 20:30:34 +0000
@@ -0,0 +1,171 @@
+# -*- coding: utf-8 -*-
+# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
+
+###############################################################################
+# OpenLP - Open Source Lyrics Projection                                      #
+# --------------------------------------------------------------------------- #
+# Copyright (c) 2008-2013 Raoul Snyman                                        #
+# Portions copyright (c) 2008-2013 Tim Bentley, Gerald Britton, Jonathan      #
+# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub,      #
+# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer.   #
+# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru,          #
+# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith,             #
+# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock,              #
+# Frode Woldsund, Martin Zibricky, Patrick Zimmermann                         #
+# --------------------------------------------------------------------------- #
+# 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; version 2 of the License.                              #
+#                                                                             #
+# 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., 59  #
+# Temple Place, Suite 330, Boston, MA 02111-1307 USA                          #
+###############################################################################
+"""
+A widget representing a song in the duplicate song removal wizard review page.
+"""
+from PyQt4 import QtCore, QtGui
+
+from openlp.core.lib import build_icon
+from openlp.plugins.songs.lib.xml import SongXML
+
+class SongReviewWidget(QtGui.QWidget):
+    """
+    A widget representing a song on the duplicate song review page.
+    It displays most of the information a song contains and
+    provides a "remove" button to remove the song from the database.
+    The remove logic is not implemented here, but a signal is provided
+    when the remove button is clicked.
+    """
+    def __init__(self, parent, song):
+        """
+        ``parent``
+            The QWidget-derived parent of the wizard.
+
+        ``song``
+            The Song which this SongReviewWidget should represent.
+        """
+        QtGui.QWidget.__init__(self, parent)
+        self.song = song
+        self.setupUi()
+        self.retranslateUi()
+        QtCore.QObject.connect(self.song_remove_button, QtCore.SIGNAL(u'clicked()'), self.on_remove_button_clicked)
+
+    def setupUi(self):
+        self.song_vertical_layout = QtGui.QVBoxLayout(self)
+        self.song_vertical_layout.setObjectName(u'song_vertical_layout')
+        self.song_group_box = QtGui.QGroupBox(self)
+        self.song_group_box.setObjectName(u'song_group_box')
+        self.song_group_box.setMinimumWidth(300)
+        self.song_group_box.setMaximumWidth(300)
+        self.song_group_box_layout = QtGui.QVBoxLayout(self.song_group_box)
+        self.song_group_box_layout.setObjectName(u'song_group_box_layout')
+        self.song_info_form_layout = QtGui.QFormLayout()
+        self.song_info_form_layout.setObjectName(u'song_info_form_layout')
+        # Add title widget.
+        self.song_title_label = QtGui.QLabel(self)
+        self.song_title_label.setObjectName(u'song_title_label')
+        self.song_info_form_layout.setWidget(0, QtGui.QFormLayout.LabelRole, self.song_title_label)
+        self.song_title_content = QtGui.QLabel(self)
+        self.song_title_content.setObjectName(u'song_title_content')
+        self.song_title_content.setText(self.song.title)
+        self.song_title_content.setWordWrap(True)
+        self.song_info_form_layout.setWidget(0, QtGui.QFormLayout.FieldRole, self.song_title_content)
+        # Add alternate title widget.
+        self.song_alternate_title_label = QtGui.QLabel(self)
+        self.song_alternate_title_label.setObjectName(u'song_alternate_title_label')
+        self.song_info_form_layout.setWidget(1, QtGui.QFormLayout.LabelRole, self.song_alternate_title_label)
+        self.song_alternate_title_content = QtGui.QLabel(self)
+        self.song_alternate_title_content.setObjectName(u'song_alternate_title_content')
+        self.song_alternate_title_content.setText(self.song.alternate_title)
+        self.song_alternate_title_content.setWordWrap(True)
+        self.song_info_form_layout.setWidget(1, QtGui.QFormLayout.FieldRole, self.song_alternate_title_content)
+        # Add CCLI number widget.
+        self.song_ccli_number_label = QtGui.QLabel(self)
+        self.song_ccli_number_label.setObjectName(u'song_ccli_number_label')
+        self.song_info_form_layout.setWidget(2, QtGui.QFormLayout.LabelRole, self.song_ccli_number_label)
+        self.song_ccli_number_content = QtGui.QLabel(self)
+        self.song_ccli_number_content.setObjectName(u'song_ccli_number_content')
+        self.song_ccli_number_content.setText(self.song.ccli_number)
+        self.song_ccli_number_content.setWordWrap(True)
+        self.song_info_form_layout.setWidget(2, QtGui.QFormLayout.FieldRole, self.song_ccli_number_content)
+        # Add copyright widget.
+        self.song_copyright_label = QtGui.QLabel(self)
+        self.song_copyright_label.setObjectName(u'song_copyright_label')
+        self.song_info_form_layout.setWidget(3, QtGui.QFormLayout.LabelRole, self.song_copyright_label)
+        self.song_copyright_content = QtGui.QLabel(self)
+        self.song_copyright_content.setObjectName(u'song_copyright_content')
+        self.song_copyright_content.setWordWrap(True)
+        self.song_copyright_content.setText(self.song.copyright)
+        self.song_info_form_layout.setWidget(3, QtGui.QFormLayout.FieldRole, self.song_copyright_content)
+        # Add comments widget.
+        self.song_comments_label = QtGui.QLabel(self)
+        self.song_comments_label.setObjectName(u'song_comments_label')
+        self.song_info_form_layout.setWidget(4, QtGui.QFormLayout.LabelRole, self.song_comments_label)
+        self.song_comments_content = QtGui.QLabel(self)
+        self.song_comments_content.setObjectName(u'song_comments_content')
+        self.song_comments_content.setText(self.song.comments)
+        self.song_comments_content.setWordWrap(True)
+        self.song_info_form_layout.setWidget(4, QtGui.QFormLayout.FieldRole, self.song_comments_content)
+        # Add authors widget.
+        self.song_authors_label = QtGui.QLabel(self)
+        self.song_authors_label.setObjectName(u'song_authors_label')
+        self.song_info_form_layout.setWidget(5, QtGui.QFormLayout.LabelRole, self.song_authors_label)
+        self.song_authors_content = QtGui.QLabel(self)
+        self.song_authors_content.setObjectName(u'song_authors_content')
+        self.song_authors_content.setWordWrap(True)
+        authors_text = u', '.join([author.display_name for author in self.song.authors])
+        self.song_authors_content.setText(authors_text)
+        self.song_info_form_layout.setWidget(5, QtGui.QFormLayout.FieldRole, self.song_authors_content)
+        # Add verse order widget.
+        self.song_verse_order_label = QtGui.QLabel(self)
+        self.song_verse_order_label.setObjectName(u'song_verse_order_label')
+        self.song_info_form_layout.setWidget(6, QtGui.QFormLayout.LabelRole, self.song_verse_order_label)
+        self.song_verse_order_content = QtGui.QLabel(self)
+        self.song_verse_order_content.setObjectName(u'song_verse_order_content')
+        self.song_verse_order_content.setText(self.song.verse_order)
+        self.song_verse_order_content.setWordWrap(True)
+        self.song_info_form_layout.setWidget(6, QtGui.QFormLayout.FieldRole, self.song_verse_order_content)
+        # Add verses widget.
+        self.song_group_box_layout.addLayout(self.song_info_form_layout)
+        self.song_info_verse_group_box = QtGui.QGroupBox(self.song_group_box)
+        self.song_info_verse_group_box.setObjectName(u'song_info_verse_group_box')
+        self.song_info_verse_group_box_layout = QtGui.QFormLayout(self.song_info_verse_group_box)
+        song_xml = SongXML()
+        verses = song_xml.get_verses(self.song.lyrics)
+        for verse in verses:
+            verse_marker = verse[0]['type'] + verse[0]['label']
+            verse_label = QtGui.QLabel(self.song_info_verse_group_box)
+            verse_label.setText(verse[1])
+            verse_label.setWordWrap(True)
+            self.song_info_verse_group_box_layout.addRow(verse_marker, verse_label)
+        self.song_group_box_layout.addWidget(self.song_info_verse_group_box)
+        self.song_group_box_layout.addStretch()
+        self.song_vertical_layout.addWidget(self.song_group_box)
+        self.song_remove_button = QtGui.QPushButton(self)
+        self.song_remove_button.setObjectName(u'song_remove_button')
+        self.song_remove_button.setIcon(build_icon(u':/songs/song_delete.png'))
+        self.song_remove_button.setSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
+        self.song_vertical_layout.addWidget(self.song_remove_button, alignment = QtCore.Qt.AlignHCenter)
+
+    def retranslateUi(self):
+        self.song_remove_button.setText(u'Remove')
+        self.song_title_label.setText(u'Title:')
+        self.song_alternate_title_label.setText(u'Alternate Title:')
+        self.song_ccli_number_label.setText(u'CCLI Number:')
+        self.song_verse_order_label.setText(u'Verse Order:')
+        self.song_copyright_label.setText(u'Copyright:')
+        self.song_comments_label.setText(u'Comments:')
+        self.song_authors_label.setText(u'Authors:')
+        self.song_info_verse_group_box.setTitle(u'Verses')
+
+    def on_remove_button_clicked(self):
+        """
+        Signal emitted when the "remove" button is clicked.
+        """
+        self.emit(QtCore.SIGNAL(u'song_remove_button_clicked(PyQt_PyObject)'), self)

=== modified file 'openlp/plugins/songs/songsplugin.py'
--- openlp/plugins/songs/songsplugin.py	2013-02-05 08:05:28 +0000
+++ openlp/plugins/songs/songsplugin.py	2013-02-18 20:30:34 +0000
@@ -48,6 +48,8 @@
 from openlp.plugins.songs.lib.mediaitem import SongSearch
 from openlp.plugins.songs.lib.importer import SongFormat
 from openlp.plugins.songs.lib.olpimport import OpenLPSongImport
+from openlp.plugins.songs.forms.duplicatesongremovalform import \
+    DuplicateSongRemovalForm
 
 log = logging.getLogger(__name__)
 __default_settings__ = {
@@ -92,10 +94,12 @@
         self.songImportItem.setVisible(True)
         self.songExportItem.setVisible(True)
         self.toolsReindexItem.setVisible(True)
+        self.tools_find_duplicates.setVisible(True)
         action_list = ActionList.get_instance()
         action_list.add_action(self.songImportItem, UiStrings().Import)
         action_list.add_action(self.songExportItem, UiStrings().Export)
         action_list.add_action(self.toolsReindexItem, UiStrings().Tools)
+        action_list.add_action(self.tools_find_duplicates, UiStrings().Tools)
 
     def addImportMenuItem(self, import_menu):
         """
@@ -131,7 +135,7 @@
 
     def addToolsMenuItem(self, tools_menu):
         """
-        Give the alerts plugin the opportunity to add items to the
+        Give the Songs plugin the opportunity to add items to the
         **Tools** menu.
 
         ``tools_menu``
@@ -145,6 +149,12 @@
             statustip=translate('SongsPlugin', 'Re-index the songs database to improve searching and ordering.'),
             visible=False, triggers=self.onToolsReindexItemTriggered)
         tools_menu.addAction(self.toolsReindexItem)
+        self.tools_find_duplicates = create_action(tools_menu, u'toolsFindDuplicates',
+            text=translate('SongsPlugin', 'Find &Duplicate Songs'),
+            statustip=translate('SongsPlugin',
+            'Find and remove duplicate songs in the song database.'),
+            visible=False, triggers=self.on_tools_find_duplicates_triggered)
+        tools_menu.addAction(self.tools_find_duplicates)
 
     def onToolsReindexItemTriggered(self):
         """
@@ -164,6 +174,12 @@
         self.manager.save_objects(songs)
         self.mediaItem.onSearchTextButtonClicked()
 
+    def on_tools_find_duplicates_triggered(self):
+        """
+        Search for duplicates in the song database.
+        """
+        DuplicateSongRemovalForm(self).exec_()
+
     def onSongImportItemClicked(self):
         if self.mediaItem:
             self.mediaItem.onImportClick()
@@ -284,10 +300,12 @@
         self.songImportItem.setVisible(False)
         self.songExportItem.setVisible(False)
         self.toolsReindexItem.setVisible(False)
+        self.tools_find_duplicates.setVisible(False)
         action_list = ActionList.get_instance()
         action_list.remove_action(self.songImportItem, UiStrings().Import)
         action_list.remove_action(self.songExportItem, UiStrings().Export)
         action_list.remove_action(self.toolsReindexItem, UiStrings().Tools)
+        action_list.remove_action(self.tools_find_duplicates, UiStrings().Tools)
         Plugin.finalise(self)
 
     def new_service_created(self):

=== modified file 'resources/images/openlp-2.qrc'
--- resources/images/openlp-2.qrc	2012-12-06 19:26:50 +0000
+++ resources/images/openlp-2.qrc	2013-02-18 20:30:34 +0000
@@ -20,6 +20,7 @@
     <file>song_author_edit.png</file>
     <file>song_topic_edit.png</file>
     <file>song_book_edit.png</file>
+    <file>song_delete.png</file>
   </qresource>
   <qresource prefix="bibles">
     <file>bibles_search_text.png</file>
@@ -98,6 +99,7 @@
     <file>wizard_importbible.bmp</file>
     <file>wizard_firsttime.bmp</file>
     <file>wizard_createtheme.bmp</file>
+    <file>wizard_duplicateremoval.bmp</file>
   </qresource>
   <qresource prefix="services">
     <file>service_collapse_all.png</file>

=== added file 'resources/images/wizard_duplicateremoval.bmp'
Binary files resources/images/wizard_duplicateremoval.bmp	1970-01-01 00:00:00 +0000 and resources/images/wizard_duplicateremoval.bmp	2013-02-18 20:30:34 +0000 differ
=== added directory 'tests/functional/openlp_plugins'
=== added directory 'tests/functional/openlp_plugins/songs'
=== added file 'tests/functional/openlp_plugins/songs/__init__.py'
--- tests/functional/openlp_plugins/songs/__init__.py	1970-01-01 00:00:00 +0000
+++ tests/functional/openlp_plugins/songs/__init__.py	2013-02-18 20:30:34 +0000
@@ -0,0 +1,8 @@
+import sip
+sip.setapi(u'QDate', 2)
+sip.setapi(u'QDateTime', 2)
+sip.setapi(u'QString', 2)
+sip.setapi(u'QTextStream', 2)
+sip.setapi(u'QTime', 2)
+sip.setapi(u'QUrl', 2)
+sip.setapi(u'QVariant', 2)

=== added file 'tests/functional/openlp_plugins/songs/test_lib.py'
--- tests/functional/openlp_plugins/songs/test_lib.py	1970-01-01 00:00:00 +0000
+++ tests/functional/openlp_plugins/songs/test_lib.py	2013-02-18 20:30:34 +0000
@@ -0,0 +1,97 @@
+# -*- coding: utf-8 -*-
+# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
+
+###############################################################################
+# OpenLP - Open Source Lyrics Projection                                      #
+# --------------------------------------------------------------------------- #
+# Copyright (c) 2008-2013 Raoul Snyman                                        #
+# Portions copyright (c) 2008-2013 Tim Bentley, Gerald Britton, Jonathan      #
+# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub,      #
+# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer.   #
+# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru,          #
+# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith,             #
+# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock,              #
+# Frode Woldsund, Martin Zibricky, Patrick Zimmermann                         #
+# --------------------------------------------------------------------------- #
+# 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; version 2 of the License.                              #
+#                                                                             #
+# 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., 59  #
+# Temple Place, Suite 330, Boston, MA 02111-1307 USA                          #
+###############################################################################
+
+from unittest import TestCase
+
+from mock import MagicMock
+
+from openlp.plugins.songs.lib.songcompare import songs_probably_equal
+
+class TestLib(TestCase):
+
+    def songs_probably_equal_test(self):
+        """
+        Test the songs_probably_equal function.
+        """
+        full_lyrics =u'''amazing grace how sweet the sound that saved a wretch like me i once was lost but now am
+        found was blind but now i see  twas grace that taught my heart to fear and grace my fears relieved how
+        precious did that grace appear the hour i first believed  through many dangers toils and snares i have already
+        come tis grace that brought me safe thus far and grace will lead me home'''
+        short_lyrics =u'''twas grace that taught my heart to fear and grace my fears relieved how precious did that
+        grace appear the hour i first believed'''
+        error_lyrics =u'''amazing how sweet the trumpet that saved a wrench like me i once was losst but now am
+        found waf blind but now i see  it was grace that taught my heart to fear and grace my fears relieved how
+        precious did that grace appppppppear the hour i first believedxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx snares i have
+        already come to this grace that brought me safe so far and grace will lead me home'''
+        different_lyrics=u'''on a hill far away stood an old rugged cross the emblem of suffering and shame and i love
+        that old cross where the dearest and best for a world of lost sinners was slain  so ill cherish the old rugged
+        cross till my trophies at last i lay down i will cling to the old rugged cross and exchange it some day for a
+        crown'''
+        song1 = MagicMock()
+        song2 = MagicMock()
+        
+        #GIVEN: Two equal songs.
+        song1.search_lyrics = full_lyrics
+        song2.search_lyrics = full_lyrics
+        
+        #WHEN: We compare those songs for equality.
+        result = songs_probably_equal(song1, song2)
+        
+        #THEN: The result should be True.
+        assert result is True, u'The result should be True'
+        
+        #GIVEN: A song and a short version of the same song.
+        song1.search_lyrics = full_lyrics
+        song2.search_lyrics = short_lyrics
+        
+        #WHEN: We compare those songs for equality.
+        result = songs_probably_equal(song1, song2)
+        
+        #THEN: The result should be True.
+        assert result is True, u'The result should be True'
+        
+        #GIVEN: A song and the same song with lots of errors.
+        song1.search_lyrics = full_lyrics
+        song2.search_lyrics = error_lyrics
+        
+        #WHEN: We compare those songs for equality.
+        result = songs_probably_equal(song1, song2)
+        
+        #THEN: The result should be True.
+        assert result is True, u'The result should be True'
+        
+        #GIVEN: Two different songs.
+        song1.search_lyrics = full_lyrics
+        song2.search_lyrics = different_lyrics
+        
+        #WHEN: We compare those songs for equality.
+        result = songs_probably_equal(song1, song2)
+        
+        #THEN: The result should be False.
+        assert result is False, u'The result should be False'


Follow ups