OSDN Git Service

Add bookmark.
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / bookmark_editwindow.py
1 # Copyright (C) 2006 by Aiwota Programmer
2 # aiwotaprog@tetteke.tk
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 import pygtk
19 pygtk.require("2.0")
20 import gtk
21 import gtk.glade
22 import os.path
23
24 from bookmark_core import Bookmark
25
26 GLADE_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)),
27                          "..", "data")
28 GLADE_FILENAME = "bookmark_editwindow.glade"
29
30
31 class BookmarkEditWindow:
32
33     def __init__(self, on_newbookmark_complete, bookmark=None):
34         glade_path = os.path.join(GLADE_DIR, GLADE_FILENAME)
35         self.widget_tree = gtk.glade.XML(glade_path)
36         self.widget_tree.signal_autoconnect(self)
37         self.window = self.widget_tree.get_widget("window_editbookmark")
38         self.entry_name = self.widget_tree.get_widget("entry_name")
39         self.entry_uri = self.widget_tree.get_widget("entry_uri")
40         self.button_ok = self.widget_tree.get_widget("button_ok")
41         self.button_cancel = self.widget_tree.get_widget("button_cancel")
42         self.button_category_add = self.widget_tree.get_widget(
43             "button_category_add")
44         self.button_category_edit = self.widget_tree.get_widget(
45             "button_category_edit")
46         self.button_category_delete = self.widget_tree.get_widget(
47             "button_category_delete")
48         self.treeview_categories = self.widget_tree.get_widget(
49             "treeview_categories")
50
51         cell = gtk.CellRendererText()
52         cell.set_property("editable", True)
53         cell.connect("edited", self.on_cell_edited)
54         treeviewcolumn = gtk.TreeViewColumn(None, cell, text=0)
55         treeviewcolumn.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
56         self.treeview_categories.append_column(treeviewcolumn)
57         self.treeview_categories.set_fixed_height_mode(True)
58         self.treeview_categories.set_model(gtk.ListStore(str))
59
60         self.on_newbookmark_complete = on_newbookmark_complete
61         self.bookmark = bookmark
62         if bookmark:
63             self.entry_name.set_text(bookmark.name)
64             self.entry_uri.set_text(bookmark.uri)
65             model = self.treeview_categories.get_model()
66             for category in bookmark.categories:
67                 model.append([category])
68
69     def on_button_ok_clicked(self, widget):
70         bookmark_name = self.entry_name.get_text()
71         bookmark_uri = self.entry_uri.get_text()
72         if not bookmark_name or not bookmark_uri:
73             dialog = gtk.Dialog(
74                 "Confirmation", self.window,
75                 gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
76                 (gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
77             label = gtk.Label("NAME and URL must be set")
78             dialog.vbox.pack_start(label, True, True, 0)
79             label.show()
80             dialog.run()
81             dialog.destroy()
82         else:
83             bookmark_categories = []
84             for row in self.treeview_categories.get_model():
85                 category = row.model.get_value(row.iter, 0)
86                 if category and category not in bookmark_categories:
87                     bookmark_categories.append(category)
88             bookmark = Bookmark(uri=bookmark_uri, name=bookmark_name,
89                                 categories=bookmark_categories)
90             self.on_newbookmark_complete(bookmark)
91             self.window.destroy()
92
93     def on_button_cancel_clicked(self, widget):
94         self.window.destroy()
95
96     def on_button_category_add_clicked(self, widget):
97         dialog = gtk.Dialog(
98             "Input Category Name", self.window,
99             gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
100             (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
101              gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
102         dialog.set_default_response(gtk.RESPONSE_ACCEPT)
103         entry = gtk.Entry()
104         entry.set_property("activates-default", True)
105         dialog.vbox.pack_start(entry, True, True, 0)
106         entry.show()
107         response = dialog.run()
108         if response == gtk.RESPONSE_ACCEPT:
109             text = entry.get_text()
110             model = self.treeview_categories.get_model()
111             if model and text:
112                 model.append([text])
113         dialog.destroy()
114
115     def on_button_category_delete_clicked(self, widget):
116         selection = self.treeview_categories.get_selection()
117         model, iter = selection.get_selected()
118         if iter:
119             model.remove(iter)
120
121     def on_cell_edited(self, cell, path, new_text):
122         model = self.treeview_categories.get_model()
123         if model:
124             iter = model.get_iter(path)
125             if iter:
126                 model.set_value(iter, 0, new_text)