| 1 |
# COPYRIGHT (C) 2020-2024 Nicotine+ Contributors |
| 2 |
# |
| 3 |
# GNU GENERAL PUBLIC LICENSE |
| 4 |
# Version 3, 29 June 2007 |
| 5 |
# |
| 6 |
# This program is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
from pynicotine.core import core |
| 20 |
from pynicotine.events import events |
| 21 |
from pynicotine.gtkgui.widgets import ui |
| 22 |
from pynicotine.gtkgui.widgets.accelerator import Accelerator |
| 23 |
from pynicotine.gtkgui.widgets.dialogs import Dialog |
| 24 |
from pynicotine.gtkgui.widgets.dialogs import EntryDialog |
| 25 |
from pynicotine.gtkgui.widgets.dialogs import OptionDialog |
| 26 |
from pynicotine.gtkgui.widgets.popupmenu import PopupMenu |
| 27 |
from pynicotine.gtkgui.widgets.textentry import CompletionEntry |
| 28 |
from pynicotine.gtkgui.widgets.treeview import TreeView |
| 29 |
|
| 30 |
|
| 31 |
class WishList(Dialog): |
| 32 |
|
| 33 |
def __init__(self, application): |
| 34 |
|
| 35 |
( |
| 36 |
self.container, |
| 37 |
self.list_container, |
| 38 |
self.wish_entry |
| 39 |
) = ui.load(scope=self, path="dialogs/wishlist.ui") |
| 40 |
|
| 41 |
super().__init__( |
| 42 |
parent=application.window, |
| 43 |
modal=False, |
| 44 |
content_box=self.container, |
| 45 |
show_callback=self.on_show, |
| 46 |
title=_("Wishlist"), |
| 47 |
width=600, |
| 48 |
height=600 |
| 49 |
) |
| 50 |
application.add_window(self.widget) |
| 51 |
|
| 52 |
self.application = application |
| 53 |
self.list_view = TreeView( |
| 54 |
application.window, parent=self.list_container, multi_select=True, activate_row_callback=self.on_edit_wish, |
| 55 |
delete_accelerator_callback=self.on_remove_wish, |
| 56 |
columns={ |
| 57 |
"wish": { |
| 58 |
"column_type": "text", |
| 59 |
"title": _("Wish"), |
| 60 |
"default_sort_type": "ascending" |
| 61 |
} |
| 62 |
} |
| 63 |
) |
| 64 |
|
| 65 |
self.list_view.freeze() |
| 66 |
|
| 67 |
for search_item in core.search.searches.values(): |
| 68 |
if search_item.mode == "wishlist": |
| 69 |
self.add_wish(search_item.term, select=False) |
| 70 |
|
| 71 |
self.list_view.unfreeze() |
| 72 |
|
| 73 |
self.completion_entry = CompletionEntry(self.wish_entry, self.list_view.model) |
| 74 |
Accelerator("<Shift>Tab", self.list_view.widget, self.on_list_focus_entry_accelerator) # skip column header |
| 75 |
|
| 76 |
self.popup_menu = PopupMenu(application, self.list_view.widget) |
| 77 |
self.popup_menu.add_items( |
| 78 |
("#" + _("_Search for Item"), self.on_search_wish), |
| 79 |
("#" + _("Edit…"), self.on_edit_wish), |
| 80 |
("", None), |
| 81 |
("#" + _("Remove"), self.on_remove_wish) |
| 82 |
) |
| 83 |
|
| 84 |
for event_name, callback in ( |
| 85 |
("add-wish", self.add_wish), |
| 86 |
("remove-wish", self.remove_wish) |
| 87 |
): |
| 88 |
events.connect(event_name, callback) |
| 89 |
|
| 90 |
def destroy(self): |
| 91 |
|
| 92 |
self.list_view.destroy() |
| 93 |
self.completion_entry.destroy() |
| 94 |
self.popup_menu.destroy() |
| 95 |
|
| 96 |
super().destroy() |
| 97 |
|
| 98 |
def on_list_focus_entry_accelerator(self, *_args): |
| 99 |
self.wish_entry.grab_focus() |
| 100 |
return True |
| 101 |
|
| 102 |
def on_add_wish(self, *_args): |
| 103 |
|
| 104 |
wish = self.wish_entry.get_text().strip() |
| 105 |
|
| 106 |
if not wish: |
| 107 |
return |
| 108 |
|
| 109 |
wish_exists = (wish in self.list_view.iterators) |
| 110 |
self.wish_entry.set_text("") |
| 111 |
|
| 112 |
core.search.add_wish(wish) |
| 113 |
|
| 114 |
if not wish_exists: |
| 115 |
return |
| 116 |
|
| 117 |
self.select_wish(wish) |
| 118 |
|
| 119 |
def on_edit_wish_response(self, dialog, _response_id, old_wish): |
| 120 |
|
| 121 |
wish = dialog.get_entry_value().strip() |
| 122 |
|
| 123 |
if not wish: |
| 124 |
return |
| 125 |
|
| 126 |
core.search.remove_wish(old_wish) |
| 127 |
core.search.add_wish(wish) |
| 128 |
self.select_wish(wish) |
| 129 |
|
| 130 |
def on_edit_wish(self, *_args): |
| 131 |
|
| 132 |
for iterator in self.list_view.get_selected_rows(): |
| 133 |
old_wish = self.list_view.get_row_value(iterator, "wish") |
| 134 |
|
| 135 |
EntryDialog( |
| 136 |
parent=self, |
| 137 |
title=_("Edit Wish"), |
| 138 |
message=_("Enter new value for wish '%s':") % old_wish, |
| 139 |
default=old_wish, |
| 140 |
action_button_label=_("_Edit"), |
| 141 |
callback=self.on_edit_wish_response, |
| 142 |
callback_data=old_wish |
| 143 |
).present() |
| 144 |
return |
| 145 |
|
| 146 |
def on_search_wish(self, *_args): |
| 147 |
|
| 148 |
for iterator in self.list_view.get_selected_rows(): |
| 149 |
wish = self.list_view.get_row_value(iterator, "wish") |
| 150 |
core.search.do_search(wish, mode="global") |
| 151 |
return |
| 152 |
|
| 153 |
def on_remove_wish(self, *_args): |
| 154 |
|
| 155 |
for iterator in reversed(list(self.list_view.get_selected_rows())): |
| 156 |
wish = self.list_view.get_row_value(iterator, "wish") |
| 157 |
core.search.remove_wish(wish) |
| 158 |
|
| 159 |
self.wish_entry.grab_focus() |
| 160 |
return True |
| 161 |
|
| 162 |
def clear_wishlist_response(self, *_args): |
| 163 |
|
| 164 |
for wish in self.list_view.iterators.copy(): |
| 165 |
core.search.remove_wish(wish) |
| 166 |
|
| 167 |
self.wish_entry.grab_focus() |
| 168 |
|
| 169 |
def on_clear_wishlist(self, *_args): |
| 170 |
|
| 171 |
OptionDialog( |
| 172 |
parent=self, |
| 173 |
title=_("Clear Wishlist?"), |
| 174 |
message=_("Do you really want to clear your wishlist?"), |
| 175 |
destructive_response_id="ok", |
| 176 |
callback=self.clear_wishlist_response |
| 177 |
).present() |
| 178 |
|
| 179 |
def add_wish(self, wish, select=True): |
| 180 |
self.list_view.add_row([wish], select_row=select) |
| 181 |
|
| 182 |
def remove_wish(self, wish): |
| 183 |
|
| 184 |
iterator = self.list_view.iterators.get(wish) |
| 185 |
|
| 186 |
if iterator is not None: |
| 187 |
self.list_view.remove_row(iterator) |
| 188 |
|
| 189 |
def select_wish(self, wish): |
| 190 |
|
| 191 |
iterator = self.list_view.iterators.get(wish) |
| 192 |
|
| 193 |
if iterator is not None: |
| 194 |
self.list_view.select_row(iterator) |
| 195 |
|
| 196 |
def on_show(self, *_args): |
| 197 |
|
| 198 |
page = self.application.window.search.get_current_page() |
| 199 |
|
| 200 |
if page is None: |
| 201 |
return |
| 202 |
|
| 203 |
text = None |
| 204 |
|
| 205 |
for tab in self.application.window.search.pages.values(): |
| 206 |
if tab is not None and tab.container == page: |
| 207 |
text = tab.text |
| 208 |
break |
| 209 |
|
| 210 |
if not text: |
| 211 |
self.list_view.unselect_all_rows() |
| 212 |
return |
| 213 |
|
| 214 |
iterator = self.list_view.iterators.get(text) |
| 215 |
|
| 216 |
if iterator is not None: |
| 217 |
# Highlight existing wish row |
| 218 |
|
| 219 |
self.list_view.select_row(iterator) |
| 220 |
self.wish_entry.set_text("") |
| 221 |
return |
| 222 |
|
| 223 |
# Pre-fill text field with search term from active search tab |
| 224 |
self.list_view.unselect_all_rows() |
| 225 |
self.wish_entry.set_text(text) |
| 226 |
self.wish_entry.grab_focus() |