clAlbumBrowser.py

00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 # ----------------------------------------------------------------------------
00005 # pyjama - python jamendo audioplayer
00006 # Copyright (c) 2008 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 ######################################################################
00022 #                                                                    #
00023 #                     Some default Layouts                           #
00024 #                                                                    #
00025 ######################################################################    
00026 #
00027 # Top Layout - main album browser
00028 #
00029 
00030 import gtk
00031 from time import strftime, gmtime, time, sleep
00032 from math import floor
00033 from modules import clWidgets, clThreadedDownload, clEntry
00034 
00035 class AlbumBrowser(gtk.Layout):
00036 
00037     def __init__(self, pyjama):
00038         self.pyjama = pyjama
00039         
00040         gtk.Layout.__init__(self)
00041         self.set_size(700,400)                
00042         
00043         # Some default values        
00044         self.results_per_page = 10
00045         self.ordering = "ratingweek"
00046         self.tag = "all"
00047         self.cur_page = 1     
00048 
00049         
00050         self.albuminfos={}    
00051         
00052         self.pyjama.Events.connect_event("scrolled_window_resized", self.ev_scrolled_window_resized)
00053 
00054         # might be obsolet
00055         self.pyjama.window.setcolor(self)
00056 
00057         
00058 
00059     def draw(self, num=None, mode="ratingweek", page=1, tag="all"):
00060         # Checking if next / prev page are available
00061         self.check_next_possible(page, num)
00062         self.check_prev_possible(page)
00063         # Got to be fixed: Setting CBs in History- Mode
00064         # causes "changed" event, which destroys the
00065         # current history- session
00066         # Switched off HistoryForward- Deletion,
00067         # as this seems to be the smaller issue
00068         self.pyjama.layouts.toolbars['top'].cbOrder.set_item(mode)
00069         show_tag = tag
00070         if tag == "all": show_tag = _("--all--")
00071         if not tag in self.pyjama.layouts.toolbars['top'].cbTags.modelist and tag != "all": 
00072             self.pyjama.layouts.toolbars['top'].cbTags.entry.set_text(tag.replace("+", " "))
00073         else:
00074             self.pyjama.layouts.toolbars['top'].cbTags.set_item(show_tag)            
00075         self.pyjama.layouts.toolbars['top'].cbResultsPerPage.set_item(num)
00076         # Setting the label
00077         if tag == "all":
00078 #            markup = self.pyjama.window.markuplbCaption.replace("TEXT", _("Show top %i albums in '%s'") % (int(num), mode))
00079             txt = _("Showing top %i albums in '%s'") % (int(num), mode)
00080             self.pyjama.layouts.toolbars['top'].lCurPage.set_text(_("Page %i of %i") % (page, (self.pyjama.db.albums/num)))
00081         else:
00082 #            markup = self.pyjama.window.markuplbCaption.replace("TEXT", _("Show %i albums tagged '%s' ordered by '%s'") % (int(num), tag.replace("+", " "), mode))
00083             txt = _("Showing %i albums tagged '%s' ordered by '%s'") % (int(num), tag.replace("+", " "), mode)
00084             self.pyjama.layouts.toolbars['top'].lCurPage.set_text(_("Page %i") % page)
00085 
00086         self.cur_page = page
00087 #        self.pyjama.window.lbCaption.set_markup(markup)
00088         self.pyjama.window.LayoutInfo.set_text(txt)
00089         self.pyjama.window.LayoutInfo.set_image("folder_06.png")
00090 
00091         if num==None:num=self.results_per_page
00092         for albuminfo in self.albuminfos:
00093             self.albuminfos[albuminfo].destroy()
00094         
00095         self.albuminfos = {}
00096         
00097         top = self.pyjama.jamendo.top(num, mode, page, tag)
00098         if top == None:
00099             clWidgets.tofast(self.pyjama.window)
00100             return None
00101         elif top == -1:
00102             return None
00103         ## Again:
00104         ## 2 loops in order to
00105         ## first burst download
00106         ## and then create AlbumInfo
00107         counter = 0
00108         threads = {}
00109 #        hidden = []
00110 #        shown = []
00111         for album in top:
00112 #            if "+" in tag:
00113 #                count = tag.count("+")+1
00114 #                if top.count(album) != count:
00115 #                    hidden.append(album)
00116 #                    continue
00117             if self.pyjama.debug:
00118                 print "(%i/%i)" % (counter + 1, len(top))
00119             # if more than 10 threads...
00120             if clThreadedDownload.threadLimiter(threads):
00121                 print ("Waiting for threads (%i seconds)") % self.pyjama.settings.get_value("PERFORMANCE", "THREAD_WAIT")
00122                 sleep(self.pyjama.settings.get_value("PERFORMANCE", "THREAD_WAIT"))
00123 #           album['album_image']
00124             threads[counter] = clThreadedDownload.Download(self.pyjama, ("album", album['album_id']), counter)
00125             threads[counter].start()
00126             counter += 1
00127         counter = 0
00128         for album in top:
00129 #            if not album in hidden and not album in shown:
00130 #                shown.append(album)
00131             self.albuminfos[counter] = clWidgets.AlbumInfo(self.pyjama, album)
00132             self.albuminfos[counter].show()
00133             self.put(self.albuminfos[counter], counter, counter)
00134             counter += 1
00135 
00136         self.arrange_topoftheweek()
00137         self.show()
00138 
00139         
00140     def arrange_topoftheweek(self):
00141         if self.albuminfos == {}: return None
00142 
00143         width = self.pyjama.window.scrolledwindow_width
00144         hspace = 20
00145         vspacer = 20
00146         
00147         imgwidth = 150
00148         imgheight = 160
00149         
00150         starty = -10
00151         
00152         y = 0
00153         x = 0
00154         for counter in self.albuminfos:
00155             if ((x+1) * imgwidth) + hspace*(x+1) >= width:
00156                 y += 1
00157                 x = 0
00158             self.move(self.albuminfos[counter], (imgwidth * x) + hspace*(x+1), (vspacer*(y+1) + (y * imgheight))+starty)
00159             x += 1
00160         height = (y+1) * (imgheight + vspacer) +starty
00161         self.set_size(width-20,height+vspacer)
00162 
00163     # Possible Modi:
00164     #
00165     # rating, ratingweek, ratingmonth
00166     # date
00167     # downloaded, listened, starred, playlisted
00168     #
00169 
00170     def check_next_possible(self, page=None, rpp=None):
00171         if page == None: page = self.cur_page
00172         if rpp == None: rpp = self.results_per_page
00173         if page >= floor(self.pyjama.db.albums / rpp) - 2:
00174             self.pyjama.layouts.toolbars['top'].sbNextPage.set_sensitive(False)
00175         else:
00176             self.pyjama.layouts.toolbars['top'].sbNextPage.set_sensitive(True)
00177 
00178     def check_prev_possible(self, page=None):
00179         if page == None: page = self.cur_page
00180         if page == 1:
00181             self.pyjama.layouts.toolbars['top'].sbPrevPage.set_sensitive(False)
00182         else:
00183             self.pyjama.layouts.toolbars['top'].sbPrevPage.set_sensitive(True)
00184         
00185     def ev_scrolled_window_resized(self):
00186         #
00187         # Rearrange layout
00188         #
00189         self.arrange_topoftheweek()
00190     #
00191     # Actually this toolbar is just a hbox...
00192     #
00193     class ToolBar(gtk.HBox):
00194         def __init__(self, pyjama):
00195             gtk.HBox.__init__(self)
00196             self.pyjama = pyjama
00197             self.layout = self.pyjama.layouts.layouts['top']
00198             
00199             
00200             #
00201             # Toolbar
00202             #
00203             #self.ToolBar = gtk.VBox()
00204             #self.pack_end(self.ToolBar, False, True)
00205 
00206             #self.hbTop = gtk.HBox()
00207             #self.ToolBar.pack_end(self.hbTop, False, True,0)
00208             
00209             # ComboBoxes for TOP- View
00210             self.cbOrder = clWidgets.OrderCombo()
00211             self.cbOrder.show()
00212             self.cbOrder.connect("changed", self.cbOrder_changed)
00213             self.pack_start(self.cbOrder, False, True, 0)
00214             # Combo for Tags
00215             self.cbTags = clWidgets.TagsCombo(self.pyjama)
00216     #        self.cbTags.connect("key_press_event", self.cbTags_key_press_event)
00217     #        self.cbTags.set_events(gtk.gdk.KEY_PRESS_MASK)
00218             self.cbTags.connect("changed", self.cbTags_changed)
00219             self.cbTags.entry.connect("activate", self.cbTagsEntry_activate)
00220             self.pack_start(self.cbTags, False, True, 0)
00221             # RESULTS PER PAGE
00222             self.cbResultsPerPage = clWidgets.ResultsPerPageCombo()
00223             self.cbResultsPerPage.connect("changed", self.cbResultsPerPage_changed)
00224             self.pack_start(self.cbResultsPerPage, False, True, 0)
00225 
00226             # Page- Buttons:
00227             self.sbNextPage = clWidgets.StockButton(gtk.STOCK_GO_FORWARD)
00228             self.sbNextPage.set_tooltip_text(_("Next page"))
00229             self.sbNextPage.connect("clicked", self.on_sbNextPage_clicked)
00230             self.pack_end(self.sbNextPage, False, True, 0)
00231 
00232             self.lCurPage = gtk.Label("")
00233             self.pack_end(self.lCurPage, False, True, 0)
00234 
00235             self.sbPrevPage = clWidgets.StockButton(gtk.STOCK_GO_BACK)
00236             self.sbPrevPage.set_tooltip_text(_("Previous page"))
00237             self.sbPrevPage.connect("clicked", self.on_sbPrevPage_clicked)
00238             self.sbPrevPage.set_sensitive(False)
00239             self.pack_end(self.sbPrevPage, False, True, 0)
00240 
00241             self.sbJumpToPage = clWidgets.StockButton(gtk.STOCK_JUMP_TO)
00242             self.sbJumpToPage.set_tooltip_text(_("Jump to page..."))
00243             self.sbJumpToPage.connect("clicked", self.on_sbJumpToPage_clicked)
00244             self.pack_end(self.sbJumpToPage, False, True, 0)
00245 
00246 
00247             #~ from modules.clWidgets import PulsingBar
00248             #~ self.pyjama.x = PulsingBar()
00249             #~ self.pack_end(self.pyjama.x)
00250             #~ self.pyjama.x.show()
00251 
00252             self.show_all()
00253             
00254         #
00255         # Events
00256         #            
00257         def cbOrder_changed(self, widget):
00258             self.layout.ordering = self.pyjama.window.get_active_text(self.cbOrder)
00259             if widget is not None:
00260                 if widget.auto_setting_item: return
00261 
00262             self.pyjama.layouts.show_layout("top", self.layout.results_per_page, self.layout.ordering, 1, self.layout.tag)
00263 
00264         def cbResultsPerPage_changed(self, widget=None):
00265             self.layout.results_per_page = int(self.pyjama.window.get_active_text(self.cbResultsPerPage))
00266             if widget is not None:
00267                 if widget.auto_setting_item: return
00268 
00269 
00270             self.pyjama.layouts.show_layout("top", self.layout.results_per_page, self.layout.ordering, 1, self.layout.tag, who_called = "cbResultsPerPage_changed")
00271             #print self.results_per_page
00272 
00273         def cbTags_key_press_event(self, ev1, ev2=None):
00274             print "KEYPRESS"
00275 
00276         def cbTagsEntry_activate(self, widget):
00277             self.cbTags_changed(None, force=True)
00278 
00279         def cbTags_changed(self, widget, force=False):
00280             tag_entry = self.cbTags.entry.get_text()
00281             tag_box = self.pyjama.window.get_active_text(self.cbTags)
00282             if tag_entry != tag_box and force == False: return
00283             tag_box = tag_entry
00284             if tag_box == _("--all--"): tag_box = "all"
00285             self.layout.tag = tag_box
00286             if widget is not None:
00287                 if widget.auto_setting_item: return
00288 
00289 
00290 #            if tag == _("--custom--"):
00291 #                # Show a custom tag
00292 #                # ask for name
00293 #                result = clEntry.input_box(title=_('Custom Tag'),
00294 #                    message=_('Please enter a tag you want to browse'),
00295 #                    default_text=_("ambient"))
00296 #                if result is None:
00297 #                    self.cbTags.set_item(_("--all--"))
00298 #                    return
00299 #                tag = str(result)
00300 
00301             self.pyjama.layouts.show_layout("top", self.layout.results_per_page, self.layout.ordering, 1, self.layout.tag.replace(" ", "+"), who_called = "cbTags_changed")        
00302             
00303         def on_sbJumpToPage_clicked(self, ev):
00304             result = clEntry.input_box(title=_('Jump to page'),
00305                 message=_('Enter Page'),
00306                 default_text="%i" % (self.layout.cur_page+10))
00307             if result is None:
00308                 return None
00309             elif not result.isdigit():
00310                 dia = clWidget.MyDialog(_('invalid expression'),
00311                                   self.get_toplevel(),
00312                                   gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, 
00313                                     (gtk.STOCK_OK, gtk.RESPONSE_ACCEPT),        gtk.STOCK_DIALOG_WARNING, _('\'%s\' is not a valid page') % str(result))
00314                 dia.run()
00315                 dia.destroy()
00316                 return None
00317             else:
00318                 page = int(result)
00319             self.pyjama.layouts.show_layout("top", self.layout.results_per_page, self.layout.ordering, page, self.layout.tag, who_called = "on_sbjumptopage_clicked")
00320             self.layout.check_next_possible()
00321             self.layout.check_prev_possible()
00322                 
00323         def on_sbNextPage_clicked(self, ev):
00324             self.pyjama.layouts.show_layout("top", self.layout.results_per_page, self.layout.ordering, self.layout.cur_page+1, self.layout.tag, who_called = "on_sbnextpage_clicked")
00325             self.layout.check_next_possible()
00326 
00327         def on_sbPrevPage_clicked(self, ev):
00328             self.pyjama.layouts.show_layout("top", self.layout.results_per_page, self.layout.ordering, self.layout.cur_page-1, self.layout.tag, who_called = "on_sbprevpage_clicked")
00329             self.layout.check_prev_possible()

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