OSDN Git Service

Add bookmark.
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / bookmark_list.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 re
19 import urllib
20 import codecs
21 import traceback
22 import os
23 import os.path
24
25 from bookmark_core import BookmarkFormatError, Bookmark
26 from bookmark_editwindow import BookmarkEditWindow
27
28 bookmark_list = None
29
30 def init(bookmarks_path, bbsmenu_uri):
31     global bookmark_list
32
33     bookmark_list = BookmarkList(bookmarks_path, bbsmenu_uri)
34     bookmark_list.load()
35
36
37 class BookmarkList(object):
38
39     def __init__(self, bookmarks_path, bbsmenu_uri):
40         self.__bookmarks_path = bookmarks_path
41         self.__bbsmenu_uri = bbsmenu_uri
42         self.__bookmark_list = []
43         self.__view = None
44
45     def set_view(self, view):
46         self.__view = view
47
48     def load(self):
49         bookmark_list = []
50         try:
51             for line in file(self.__bookmarks_path):
52                 line = line.rstrip("\n")
53                 try:
54                     bookmark = Bookmark(formatted=line)
55                 except BookmarkFormatError:
56                     traceback.print_exc()
57                 else:
58                     bookmark_list.append(bookmark)
59         except IOError:
60             traceback.print_exc()
61         else:
62             self.__bookmark_list = bookmark_list
63
64     def save(self):
65         try:
66             basedir = os.path.dirname(self.__bookmarks_path)
67             if not os.path.isdir(basedir):
68                 os.makedirs(basedir)
69             f = file(self.__bookmarks_path, "w")
70             for bookmark in self.__bookmark_list:
71                 line = bookmark.tabbed_text() + "\n"
72                 f.write(line)
73         except:
74             traceback.print_exc()
75         else:
76             f.close()
77
78     def update_bbsmenu(self):
79         reg = re.compile(
80             "<A HREF=(?P<uri>http://[^\s]+).*>(?P<name>[^<]+)</A>")
81         category_reg = re.compile(
82             "(?:<BR>){1,2}<B>(?P<category>[^<]+)</B><BR>")
83
84         new_bookmark_list = []
85         bookmark_uri_dict = {}
86         encoding = "cp932"
87
88         current_category = ""
89         try:
90             for line in urllib.urlopen(self.__bbsmenu_uri):
91                 line = line.decode(encoding, "replace").strip()
92                 if line:
93                     if not current_category:
94                         m = category_reg.match(line)
95                         if m:
96                             current_category = m.group("category")
97                             continue
98                     m = reg.match(line)
99                     if m:
100                         name = m.group("name")
101                         uri = m.group("uri")
102                         category = current_category
103                         if uri and uri in bookmark_uri_dict:
104                             bookmark = bookmark_uri_dict[uri]
105                             if category and \
106                                category not in bookmark.categories:
107                                 bookmark.categories.append(category)
108                         elif name and uri:
109                             categories = ["bbsmenu"]
110                             if category:
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
116                 else:
117                     current_category = ""
118         except:
119             traceback.print_exc()
120         else:
121             self._merge_bbsmenu(new_bookmark_list)
122
123     def _merge_bbsmenu(self, new_bbsmenu_list):
124         old_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
130         self.save()
131         category_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)
136
137     def get_category_list(self):
138         categories = []
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)
144         return categories
145
146     def get_bookmark_list_in_category(self, category):
147         bookmarks = []
148         for bookmark in self.__bookmark_list:
149             if category in bookmark.categories:
150                 bookmarks.append(bookmark)
151         return bookmarks
152
153     def get_non_category_bookmark(self):
154         bookmarks = []
155         for bookmark in self.__bookmark_list:
156             have_category = False
157             for category in bookmark.categories:
158                 if category:
159                     have_category = True
160                     break
161             if not have_category:
162                 bookmarks.append(bookmark)
163         return bookmarks
164
165     def add_new_bookmark(self, bookmark):
166         self.__bookmark_list.append(bookmark)
167         self.save()
168
169     def delete_bookmark(self, bookmark):
170         try:
171             index = self.__bookmark_list.index(bookmark)
172         except ValueError:
173             pass
174         else:
175             del self.__bookmark_list[index]
176             self.save()
177
178     def add_bookmark_with_edit(self, name="New Bookmark", uri=None,
179                                categories=None):
180
181         def on_edit_complete(new_bookmark):
182             self.add_new_bookmark(new_bookmark)
183             self.new_bookmark_added()
184
185         if not categories:
186             categories = []
187         bookmark = Bookmark(name=name, uri=uri, categories=categories)
188         BookmarkEditWindow(on_edit_complete, bookmark)
189
190     def new_bookmark_added(self):
191         if self.__view:
192             self.__view.update_categories()