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
26 from http_sub import HTTPRedirectHandler302, HTTPDebugHandler
27 from bookmark_core import BookmarkFormatError, Bookmark
28 from bookmark_editwindow import BookmarkEditWindow
32 def init(bookmarks_path, bbsmenu_uri):
35 bookmark_list = BookmarkList(bookmarks_path, bbsmenu_uri)
39 class BookmarkList(object):
41 def __init__(self, bookmarks_path, bbsmenu_uri):
42 self.__bookmarks_path = bookmarks_path
43 self.__bbsmenu_uri = bbsmenu_uri
44 self.__bookmark_list = []
47 def set_view(self, view):
53 for line in file(self.__bookmarks_path):
54 line = line.rstrip("\n")
56 bookmark = Bookmark(formatted=line)
57 except BookmarkFormatError:
60 bookmark_list.append(bookmark)
64 self.__bookmark_list = bookmark_list
68 basedir = os.path.dirname(self.__bookmarks_path)
69 if not os.path.isdir(basedir):
71 f = file(self.__bookmarks_path, "w")
72 for bookmark in self.__bookmark_list:
73 line = bookmark.tabbed_text() + "\n"
80 def update_bbsmenu(self):
82 "<A HREF=(?P<uri>http://[^\s]+).*>(?P<name>[^<]+)</A>")
83 category_reg = re.compile(
84 "(?:<BR>){1,2}<B>(?P<category>[^<]+)</B><BR>")
86 new_bookmark_list = []
87 bookmark_uri_dict = {}
91 opener = urllib2.build_opener(HTTPRedirectHandler302, HTTPDebugHandler)
92 req = urllib2.Request(self.__bbsmenu_uri)
93 req.add_header("User-agent", config.User_Agent)
95 for line in opener.open(req):
96 line = line.decode(encoding, "replace").strip()
98 if not current_category:
99 m = category_reg.match(line)
101 current_category = m.group("category")
105 name = m.group("name")
107 category = current_category
108 if uri and uri in bookmark_uri_dict:
109 bookmark = bookmark_uri_dict[uri]
111 category not in bookmark.categories:
112 bookmark.categories.append(category)
114 categories = ["bbsmenu"]
116 categories.append(category)
117 bookmark = Bookmark(uri=uri, name=name,
118 categories=categories)
119 new_bookmark_list.append(bookmark)
120 bookmark_uri_dict[uri] = bookmark
122 current_category = ""
124 traceback.print_exc()
126 self._merge_bbsmenu(new_bookmark_list)
128 def _merge_bbsmenu(self, new_bbsmenu_list):
130 for bookmark in self.__bookmark_list:
131 if "bbsmenu" not in bookmark.categories:
132 old_list.append(bookmark)
133 new_bbsmenu_list += old_list
134 self.__bookmark_list = new_bbsmenu_list
137 for bookmark in self.__bookmark_list:
138 for category in bookmark.categories:
139 if category and category not in category_list:
140 category_list.append(category)
142 def get_category_list(self):
144 for bookmark in self.__bookmark_list:
145 for category in bookmark.categories:
146 if category and category not in categories:
147 # avoid empty and duplicate
148 categories.append(category)
151 def get_bookmark_list_in_category(self, category):
153 for bookmark in self.__bookmark_list:
154 if category in bookmark.categories:
155 bookmarks.append(bookmark)
158 def get_non_category_bookmark(self):
160 for bookmark in self.__bookmark_list:
161 have_category = False
162 for category in bookmark.categories:
166 if not have_category:
167 bookmarks.append(bookmark)
170 def add_new_bookmark(self, bookmark):
171 self.__bookmark_list.append(bookmark)
174 def delete_bookmark(self, bookmark):
176 index = self.__bookmark_list.index(bookmark)
180 del self.__bookmark_list[index]
183 def add_bookmark_with_edit(self, name="New Bookmark", uri=None,
186 def on_edit_complete(new_bookmark):
187 self.add_new_bookmark(new_bookmark)
188 self.new_bookmark_added()
192 bookmark = Bookmark(name=name, uri=uri, categories=categories)
193 BookmarkEditWindow(on_edit_complete, bookmark)
195 def new_bookmark_added(self):
197 self.__view.update_categories()