clBookmarks.py

00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 # ----------------------------------------------------------------------------
00005 # pyjama - python jamendo audioplayer
00006 # Copyright (c) 2009 Daniel Nögel
00007 #
00008 # This program is free software: you can redistribute it and/or modify
00009 # it under the terms of the GNU General Public License as published by
00010 # the Free Software Foundation, either version 3 of the License, or
00011 # (at your option) any later version.
00012 #
00013 # This program is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 # GNU General Public License for more details.
00017 # You should have received a copy of the GNU General Public License
00018 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 # ----------------------------------------------------------------------------
00020 
00021 import gtk
00022 import pickle
00023 import base64
00024 
00025 import hashlib
00026 import os
00027 
00028 from modules import functions
00029 from modules import clEntry
00030 
00031 import clBookmarkDialog
00032 
00033 def serialize(object):
00034     ret = pickle.dumps(object, 0)
00035     return base64.b64encode(ret)
00036 
00037 def unserialize(string):
00038     ret = base64.b64decode(string)
00039     return pickle.loads(ret)
00040     
00041 
00042 class main():
00043     def __init__(self, pyjama):
00044         self.bookmarks = []
00045         self.pyjama = pyjama
00046         self.bookmarks_dir = os.path.join(self.pyjama.home, "bookmarks")
00047 
00048         # bookmark name reserved for the home page
00049         self.home_bookmark_name = "__!!home!!__"
00050 
00051         # create bookmarks directory in ~/.pyjama
00052         home = self.pyjama.home
00053         if os.path.exists(os.path.join(home,  "bookmarks")) == False:
00054             try:
00055                 os.makedirs(os.path.join(home, "bookmarks"))
00056                 print "Created %s" % os.path.join(home, "bookmarks")
00057             except:
00058                 self.pyjama.Events.raise_event("error", _("Could not create folder '%s' - most probably\npyjama does not have the right privilegs to create that folder" % os.path.join(home, "bookmarks")))
00059                 return
00060 
00061         # hide from configtool
00062         bl = self.pyjama.settings.get_value("CONFIGTOOL", "blacklist", "PATHs URLs")
00063         if not "Bookmarks" in bl:
00064             bl += " Bookmarks"
00065             self.pyjama.settings.set_value("CONFIGTOOL", "blacklist", bl)
00066 
00067         self.pyjama.Events.connect_event("alldone", self.ev_alldone)
00068 
00069 #        gladefile = os.path.join(functions.install_dir(), "plugins" , "bookmarks", "bookmarks.glade")
00070 #        try:
00071 #            self.wTree = gtk.glade.XML(gladefile)  
00072 #            self.dialog = self.wTree.get_widget("dialog1")
00073 #            self.ok = self.wTree.get_widget("button1")
00074 #            self.ok.connect("clicked", self.ev_ok_clicked)
00075 #        except Exception, inst:
00076 #            self.pyjama.Events.raise_event("error", inst, "Error loading glade file %s" % gladefile)
00077 #            self.dialog = None
00078 
00079     ######################################################################
00080     #                                                                    #
00081     #                           Functions                                #
00082     #                                                                    #
00083     ######################################################################
00084 
00085     def edit_bookmarks(self, widget):
00086 #        self.pyjama.Events.raise_event("error", "Editing Bookmarks wasn't implemented, yet")
00087 #        return
00088         # load dialog
00089         dialog = clBookmarkDialog.Dialog(self, self.bookmarks)
00090         response = dialog.run()
00091         if response == 1:
00092             bookmarks =  dialog.bookmarks
00093             deleted = dialog.deleted
00094             menu = self.pyjama.window.menubar
00095             self.bookmarks = []
00096             for bookmark in deleted:
00097                 name = bookmark['name']
00098                 content = bookmark['hash']
00099 
00100                 # delete all entries
00101                 mnu = menu.get_submenu(base64.b64encode(name))
00102                 mnu.destroy()
00103                 
00104                 sql = "DELETE FROM settings WHERE section='bookmarks' and option = '%s'" % name
00105                 playlists = self.pyjama.settingsdb.query(sql)            
00106             for bookmark in bookmarks:
00107                 name = bookmark['name']
00108                 content = bookmark['hash']
00109 
00110                 # delete all entries
00111                 mnu = menu.get_submenu(base64.b64encode(name))
00112                 mnu.destroy()
00113                 
00114                 sql = "DELETE FROM settings WHERE section='bookmarks' and option = '%s'" % name
00115                 playlists = self.pyjama.settingsdb.query(sql)
00116                 
00117             for bookmark in bookmarks:
00118                 name = bookmark['name']
00119                 content = bookmark['hash']
00120 
00121 
00122                 # Append to bookmarks and write to settingsdb
00123                 self.pyjama.settingsdb.set_value("bookmarks", name, content)
00124 
00125                 # Append menu entry
00126                 menu = self.pyjama.window.menubar
00127                 entry = menu.append_entry(menu.get_rootmenu("Bookmarks"), name, base64.b64encode(name))
00128                 entry.connect("activate", self.ev_load_bookmark, name)
00129 
00130                 self.bookmarks.append({'name':name, 'hash':content})
00131             #~ for del_bookmark in deleted:
00132                 #~ self.pyjama.settings.remove_option("Bookmarks", del_bookmark)
00133         dialog.destroy()
00134 
00135 
00136     def add_bookmark(self, widget, name=None):
00137 
00138         data = self.pyjama.historyCurrent
00139         content = serialize(data)
00140 
00141         if name is None:
00142             # ask for name
00143             result = clEntry.input_box(title=_('Bookmark'),
00144                 message=_('Please enter a Name for this bookmark'),
00145                 default_text=_("Bookmark"))
00146             if result is None:
00147                 return
00148             name = str(result)
00149         else:
00150             name = self.home_bookmark_name
00151         
00152         # Save bookmark
00153         self.pyjama.settingsdb.set_value("bookmarks", name, content)
00154 
00155         if name is not None:
00156             # Append menu entry
00157             menu = self.pyjama.window.menubar
00158             entry = menu.append_entry(menu.get_rootmenu("Bookmarks"), name, base64.b64encode(name))
00159             entry.connect("activate", self.ev_load_bookmark, name)
00160             entry.show()
00161             self.bookmarks.append({'name':name, 'hash':content})
00162 
00163     def read_bookmark_folder(self):
00164         if self.pyjama.settings.config.has_section("Bookmarks"):
00165             for md5_hash, name in self.pyjama.settings.config.items("Bookmarks"):
00166                 if name != self.home_bookmark_name and md5_hash != self.home_bookmark_name:
00167                     #~ # Append menu entry
00168                     #~ menu = self.pyjama.window.menubar
00169                     #~ entry = menu.append_entry(menu.get_rootmenu("Bookmarks"), name, md5_hash)
00170                     #~ entry.connect("activate", self.ev_load_bookmark, md5_hash)
00171                     
00172                     self.pyjama.settingsdb.set_value("bookmarks", name, self.bookmarkfromhash(md5_hash))
00173                     print ("Moved bookmark '%s' from config-file to database" % name)
00174                     self.pyjama.settings.remove_option("Bookmarks", md5_hash)
00175                 else:
00176                     self.pyjama.settingsdb.set_value("bookmarks", self.home_bookmark_name, self.bookmarkfromhash(self.home_bookmark_name))
00177                     print ("Moved startpage from config-file to database")
00178                     self.pyjama.settings.remove_option("Bookmarks", md5_hash)
00179 
00180         sql = "SELECT option, value FROM settings WHERE section='bookmarks'"
00181         bookmarks = self.pyjama.settingsdb.query(sql)
00182         if  bookmarks:
00183             for name, value in bookmarks:
00184                 if name != self.home_bookmark_name:
00185                     # Append menu entry
00186                     menu = self.pyjama.window.menubar
00187                     entry = menu.append_entry(menu.get_rootmenu("Bookmarks"), name, base64.b64encode(name))
00188                     entry.connect("activate", self.ev_load_bookmark, name)
00189                     self.bookmarks.append({'name':name, 'hash':value})
00190                 else:
00191                     self.pyjama.set_home_fkt(self.home)
00192         
00193     def bookmarkfromhash(self, hash):
00194         try: 
00195             fh = open(os.path.join(self.bookmarks_dir, hash), "r")
00196         except:
00197             self.pyjama.Events.raise_event("error", _("Could not read bookmark file"))
00198             return
00199         content = fh.read()
00200         fh.close()
00201         
00202         content = pickle.loads(content)
00203         return serialize(content)
00204 
00205     ######################################################################
00206     #                                                                    #
00207     #                                Events                              #
00208     #                                                                    #
00209     ######################################################################
00210 
00211     def ev_ok_clicked(self, widget):
00212         self.dialog.destroy()
00213 
00214     def ev_alldone(self):
00215         # Create Bookmarks menu
00216         menu = self.pyjama.window.menubar
00217         bookmarks = menu.insert_root_before("Extras", _("Bookmarks"), "Bookmarks")
00218 
00219         translation_array = [_("Add Bookmark"), _("Edit Bookmarks"), _("Set this page as homepage")]
00220 
00221         sub = ["Add Bookmark", "Edit Bookmarks", "Set this page as homepage", "---"]  
00222         self.bookmark_menu = menu.create_submenu(rootmenu=bookmarks, submenu=sub)
00223 
00224         add_bookmark = menu.get_submenu("Add Bookmark")
00225         menu.set_item_image(add_bookmark, gtk.STOCK_ADD )
00226         add_bookmark.connect("activate", self.add_bookmark)
00227 
00228         edit_bookmarks = menu.get_submenu("Edit Bookmarks")
00229         menu.set_item_image(edit_bookmarks, gtk.STOCK_EDIT )
00230         edit_bookmarks.connect("activate", self.edit_bookmarks)
00231 
00232         set_homepage = menu.get_submenu("Set this page as homepage")
00233         menu.set_item_image(set_homepage, gtk.STOCK_HOME )
00234         set_homepage.connect("activate", self.cb_set_homepage)
00235 
00236         # Create accel group
00237         self.accel_group = gtk.AccelGroup()
00238         add_bookmark.add_accelerator("activate", self.accel_group, ord("d"), gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE)
00239         self.pyjama.window.add_accel_group(self.accel_group)
00240 
00241         self.read_bookmark_folder()
00242 
00243     def ev_load_bookmark(self, widget, name):
00244         #~ # Read file
00245         #~ try: 
00246             #~ fh = open(os.path.join(self.bookmarks_dir, md5_hash), "r")
00247         #~ except:
00248             #~ self.pyjama.Events.raise_event("error", _("Could not read bookmark file"))
00249             #~ return
00250         #~ content = fh.read()
00251         #~ fh.close()
00252 
00253         # unserialise and show
00254         #~ data = pickle.loads(content)
00255         bookmark = self.pyjama.settingsdb.get_value("bookmarks", name)
00256         if bookmark:
00257             data = unserialize(bookmark)
00258             self.pyjama.layouts.show_layout(data['layout'], data['data1'], data['data2'], data['data3'], data['data4'], who_called="ev_load_bookmark")
00259 
00260     def cb_set_homepage(self, widget):
00261         self.pyjama.set_home_fkt(self.home)
00262         self.add_bookmark(None, self.home_bookmark_name)
00263 
00264     def home(self):
00265         self.pyjama.jamendo.last_query_hack()
00266         self.ev_load_bookmark(None, self.home_bookmark_name)

Generated on Thu Jun 4 19:08:24 2009 for Pyjama by  doxygen 1.5.8