1 # Copyright (C) 2006 by Aiwota Programmer
2 # aiwotaprog@tetteke.tk
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.
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.
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
25 from bookmark_core import BookmarkFormatError, Bookmark
26 from bookmark_editwindow import BookmarkEditWindow
30 def init(bookmarks_path, bbsmenu_uri):
33 bookmark_list = BookmarkList(bookmarks_path, bbsmenu_uri)
37 class BookmarkList(object):
39 def __init__(self, bookmarks_path, bbsmenu_uri):
40 self.__bookmarks_path = bookmarks_path
41 self.__bbsmenu_uri = bbsmenu_uri
42 self.__bookmark_list = []
45 def set_view(self, view):
51 for line in file(self.__bookmarks_path):
52 line = line.rstrip("\n")
54 bookmark = Bookmark(formatted=line)
55 except BookmarkFormatError:
58 bookmark_list.append(bookmark)
62 self.__bookmark_list = bookmark_list
66 basedir = os.path.dirname(self.__bookmarks_path)
67 if not os.path.isdir(basedir):
69 f = file(self.__bookmarks_path, "w")
70 for bookmark in self.__bookmark_list:
71 line = bookmark.tabbed_text() + "\n"
78 def update_bbsmenu(self):
80 "<A HREF=(?P<uri>http://[^\s]+).*>(?P<name>[^<]+)</A>")
81 category_reg = re.compile(
82 "(?:<BR>){1,2}<B>(?P<category>[^<]+)</B><BR>")
84 new_bookmark_list = []
85 bookmark_uri_dict = {}
90 for line in urllib.urlopen(self.__bbsmenu_uri):
91 line = line.decode(encoding, "replace").strip()
93 if not current_category:
94 m = category_reg.match(line)
96 current_category = m.group("category")
100 name = m.group("name")
102 category = current_category
103 if uri and uri in bookmark_uri_dict:
104 bookmark = bookmark_uri_dict[uri]
106 category not in bookmark.categories:
107 bookmark.categories.append(category)
109 categories = ["bbsmenu"]
111 categories.append(category)
112 bookmark = Bookmark(uri=uri, name=name,
113 categories=categories)
114 new_bookmark_list.append(bookmark)
115 bookmark_uri_dict[uri] = bookmark
117 current_category = ""
119 traceback.print_exc()
121 self._merge_bbsmenu(new_bookmark_list)
123 def _merge_bbsmenu(self, new_bbsmenu_list):
125 for bookmark in self.__bookmark_list:
126 if "bbsmenu" not in bookmark.categories:
127 old_list.append(bookmark)
128 new_bbsmenu_list += old_list
129 self.__bookmark_list = new_bbsmenu_list
132 for bookmark in self.__bookmark_list:
133 for category in bookmark.categories:
134 if category and category not in category_list:
135 category_list.append(category)
137 def get_category_list(self):
139 for bookmark in self.__bookmark_list:
140 for category in bookmark.categories:
141 if category and category not in categories:
142 # avoid empty and duplicate
143 categories.append(category)
146 def get_bookmark_list_in_category(self, category):
148 for bookmark in self.__bookmark_list:
149 if category in bookmark.categories:
150 bookmarks.append(bookmark)
153 def get_non_category_bookmark(self):
155 for bookmark in self.__bookmark_list:
156 have_category = False
157 for category in bookmark.categories:
161 if not have_category:
162 bookmarks.append(bookmark)
165 def add_new_bookmark(self, bookmark):
166 self.__bookmark_list.append(bookmark)
169 def delete_bookmark(self, bookmark):
171 index = self.__bookmark_list.index(bookmark)
175 del self.__bookmark_list[index]
178 def add_bookmark_with_edit(self, name="New Bookmark", uri=None,
181 def on_edit_complete(new_bookmark):
182 self.add_new_bookmark(new_bookmark)
183 self.new_bookmark_added()
187 bookmark = Bookmark(name=name, uri=uri, categories=categories)
188 BookmarkEditWindow(on_edit_complete, bookmark)
190 def new_bookmark_added(self):
192 self.__view.update_categories()