diff --git a/qutebrowser/completion/models/sqlmodel.py b/qutebrowser/completion/models/sqlmodel.py index 39182628c..dc66f3480 100644 --- a/qutebrowser/completion/models/sqlmodel.py +++ b/qutebrowser/completion/models/sqlmodel.py @@ -82,13 +82,14 @@ class SqlCompletionModel(QAbstractItemModel): """ def __init__(self, column_widths=(30, 70, 0), columns_to_filter=None, - parent=None): + delete_cur_item=None, parent=None): super().__init__(parent) self.columns_to_filter = columns_to_filter or [0] self.column_widths = column_widths self._categories = [] self.srcmodel = self # TODO: dummy for compat with old API self.pattern = '' + self.delete_cur_item = delete_cur_item def new_category(self, name, select='*', where=None, sort_by=None, sort_order=None, limit=None): @@ -110,10 +111,6 @@ class SqlCompletionModel(QAbstractItemModel): columns_to_filter=self.columns_to_filter) self._categories.append(cat) - def delete_cur_item(self, completion): - """Delete the selected item.""" - raise NotImplementedError - def data(self, index, role=Qt.DisplayRole): """Return the item data for index. diff --git a/qutebrowser/completion/models/urlmodel.py b/qutebrowser/completion/models/urlmodel.py index 2f573aab7..161a9266b 100644 --- a/qutebrowser/completion/models/urlmodel.py +++ b/qutebrowser/completion/models/urlmodel.py @@ -21,6 +21,39 @@ from qutebrowser.completion.models import sqlmodel from qutebrowser.config import config +from qutebrowser.utils import qtutils, log, objreg + + +_URLCOL = 0 +_TEXTCOL = 1 + + +def _delete_url(completion): + """Delete the selected item. + + Args: + completion: The Completion object to use. + """ + index = completion.currentIndex() + qtutils.ensure_valid(index) + category = index.parent() + index = category.child(index.row(), _URLCOL) + catname = category.data() + url = index.data() + qtutils.ensure_valid(category) + + if catname == 'Bookmarks': + log.completion.debug('Deleting bookmark {}'.format(url)) + bookmark_manager = objreg.get('bookmark-manager') + bookmark_manager.delete(url) + else: + assert catname == 'Quickmarks', 'Unknown category {}'.format(catname) + quickmark_manager = objreg.get('quickmark-manager') + sibling = index.sibling(index.row(), _TEXTCOL) + qtutils.ensure_valid(sibling) + name = sibling.data() + log.completion.debug('Deleting quickmark {}'.format(name)) + quickmark_manager.delete(name) def url(): @@ -28,18 +61,16 @@ def url(): Used for the `open` command. """ - urlcol = 0 - textcol = 1 - model = sqlmodel.SqlCompletionModel(column_widths=(40, 50, 10), - columns_to_filter=[urlcol, textcol]) + columns_to_filter=[_URLCOL, _TEXTCOL], + delete_cur_item=_delete_url) limit = config.get('completion', 'web-history-max-items') timefmt = config.get('completion', 'timestamp-format') select_time = "strftime('{}', atime, 'unixepoch')".format(timefmt) + model.new_category('Quickmarks', select='url, name') + model.new_category('Bookmarks') model.new_category('History', limit=limit, select='url, title, {}'.format(select_time), where='not redirect') - model.new_category('Quickmarks', select='url, name') - model.new_category('Bookmarks') return model diff --git a/tests/unit/completion/test_models.py b/tests/unit/completion/test_models.py index d0732f660..ddc746aa6 100644 --- a/tests/unit/completion/test_models.py +++ b/tests/unit/completion/test_models.py @@ -30,6 +30,7 @@ from qutebrowser.browser import history from qutebrowser.completion.models import miscmodels, urlmodel, configmodel from qutebrowser.config import sections, value from qutebrowser.misc import sql +from qutebrowser.utils import objreg def _check_completions(model, expected): @@ -115,7 +116,7 @@ def _patch_config_section_desc(monkeypatch, stubs, symbol): monkeypatch.setattr(symbol, section_desc) -def _mock_view_index(model, category_idx, child_idx, qtbot): +def _mock_view_index(model, category_num, child_num, qtbot): """Create a tree view from a model and set the current index. Args: @@ -126,8 +127,9 @@ def _mock_view_index(model, category_idx, child_idx, qtbot): view = QTreeView() qtbot.add_widget(view) view.setModel(model) - idx = model.indexFromItem(model.item(category_idx).child(child_idx)) - view.setCurrentIndex(idx) + parent = model.index(category_num, 0) + child = model.index(child_num, 0, parent=parent) + view.setCurrentIndex(child) return view @@ -138,6 +140,9 @@ def quickmarks(init_sql): table.insert(['aw', 'https://wiki.archlinux.org']) table.insert(['ddg', 'https://duckduckgo.com']) table.insert(['wiki', 'https://wikipedia.org']) + objreg.register('quickmark-manager', table) + yield table + objreg.delete('quickmark-manager') @pytest.fixture @@ -147,6 +152,9 @@ def bookmarks(init_sql): table.insert(['https://github.com', 'GitHub']) table.insert(['https://python.org', 'Welcome to Python.org']) table.insert(['http://qutebrowser.org', 'qutebrowser | qutebrowser']) + objreg.register('bookmark-manager', table) + yield table + objreg.delete('bookmark-manager') @pytest.fixture @@ -295,7 +303,6 @@ def test_url_completion(qtmodeltester, config_stub, web_history, quickmarks, }) -@pytest.mark.skip def test_url_completion_delete_bookmark(qtmodeltester, config_stub, web_history, quickmarks, bookmarks, qtbot): @@ -309,12 +316,11 @@ def test_url_completion_delete_bookmark(qtmodeltester, config_stub, # delete item (1, 0) -> (bookmarks, 'https://github.com' ) view = _mock_view_index(model, 1, 0, qtbot) model.delete_cur_item(view) - assert 'https://github.com' not in bookmarks.marks - assert 'https://python.org' in bookmarks.marks - assert 'http://qutebrowser.org' in bookmarks.marks + assert 'https://github.com' not in bookmarks + assert 'https://python.org' in bookmarks + assert 'http://qutebrowser.org' in bookmarks -@pytest.mark.skip def test_url_completion_delete_quickmark(qtmodeltester, config_stub, web_history, quickmarks, bookmarks, qtbot): @@ -328,9 +334,9 @@ def test_url_completion_delete_quickmark(qtmodeltester, config_stub, # delete item (0, 1) -> (quickmarks, 'ddg' ) view = _mock_view_index(model, 0, 1, qtbot) model.delete_cur_item(view) - assert 'aw' in quickmarks.marks - assert 'ddg' not in quickmarks.marks - assert 'wiki' in quickmarks.marks + assert 'aw' in quickmarks + assert 'ddg' not in quickmarks + assert 'wiki' in quickmarks def test_session_completion(qtmodeltester, session_manager_stub):